298 elseif ($field instanceof ORM\Fields\
FloatField)
306 elseif ($field instanceof ORM\Fields\
DateField)
310 elseif ($field instanceof ORM\Fields\
TextField)
316 $values = $field->getValues();
318 if (preg_match(
'/^[0-9]+$/', $values[0]) && preg_match(
'/^[0-9]+$/', $values[1]))
324 return 'varchar('.max(mb_strlen($values[0]), mb_strlen($values[1])).
')';
327 elseif ($field instanceof ORM\Fields\
EnumField)
329 return 'varchar('.max(array_map(
'strlen', $field->getValues())).
')';
334 $defaultLength =
false;
337 if ($validator instanceof ORM\Fields\Validators\LengthValidator)
339 if ($defaultLength ===
false || $defaultLength > $validator->getMax())
341 $defaultLength = $validator->getMax();
345 return 'varchar('.($defaultLength > 0? $defaultLength: 255).
')';
411 $offset = intval($offset);
412 $limit = intval($limit);
414 if ($offset > 0 && $limit <= 0)
421 $sql = preg_replace(
"/^\\s*SELECT/i",
"SELECT TOP ".$limit, $sql);
428 preg_match_all(
"#\\sorder\\s+by\\s#i", $sql, $matches, PREG_OFFSET_CAPTURE);
429 if (isset($matches[0]) && is_array($matches[0]) && !empty($matches[0]))
431 $idx = $matches[0][count($matches[0]) - 1][1];
432 $s = mb_substr($sql, $idx);
433 if (substr_count($s,
'(') === substr_count($s,
')'))
436 $sqlTmp = mb_substr($sql, 0, $idx);
442 $orderBy =
"ORDER BY (SELECT 1)";
447 $sqlTmp = preg_replace(
449 "SELECT ROW_NUMBER() OVER (".$orderBy.
") AS ROW_NUMBER_ALIAS,",
454 "WITH ROW_NUMBER_QUERY_ALIAS AS (".$sqlTmp.
") ".
456 "FROM ROW_NUMBER_QUERY_ALIAS ".
457 "WHERE ROW_NUMBER_ALIAS BETWEEN ".($offset + 1).
" AND ".($offset + $limit);
466 public function prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
470 $updateColumns = array();
471 $sourceSelectValues = array();
472 $sourceSelectColumns = array();
473 $targetConnectColumns = array();
474 $tableFields = $this->connection->getTableFields($tableName);
475 foreach($tableFields as $columnName => $tableField)
477 $quotedName = $this->
quote($columnName);
478 if (in_array($columnName, $primaryFields))
480 $sourceSelectValues[] = $this->
convertToDb($insertFields[$columnName], $tableField);
481 $sourceSelectColumns[] = $quotedName;
482 if($insertFields[$columnName] ===
null)
485 $targetConnectColumns[] =
"(source.".$quotedName.
" IS NULL AND target.".$quotedName.
" IS NULL)";
489 $targetConnectColumns[] =
"(source.".$quotedName.
" = target.".$quotedName.
")";
493 if (isset($updateFields[$columnName]) || array_key_exists($columnName, $updateFields))
495 $updateColumns[] =
"target.".$quotedName.
' = '.$this->
convertToDb($updateFields[$columnName], $tableField);
500 $insert && $insert[0] !=
"" && $insert[1] !=
""
502 && $sourceSelectValues && $sourceSelectColumns && $targetConnectColumns
506 MERGE INTO ".$this->quote($tableName).
" AS target USING (
507 SELECT ".implode(
", ", $sourceSelectValues).
"
509 ".implode(
", ", $sourceSelectColumns).
"
513 ".implode(
" AND ", $targetConnectColumns).
"
516 UPDATE SET ".implode(
", ", $updateColumns).
"
517 WHEN NOT MATCHED THEN
518 INSERT (".$insert[0].
")
519 VALUES (".$insert[1].
")