76 $from = static::getCurrentDateTimeFunction();
79 return 'DATE_ADD('.$from.
', INTERVAL '.$seconds.
' SECOND)';
89 $from = static::getCurrentDateTimeFunction();
92 return 'DATE_ADD('.$from.
', INTERVAL '.$days.
' DAY)';
100 return 'DATE('.$value.
')';
108 static $search = array(
122 static $replace = array(
137 $format = str_replace($search, $replace, $format);
139 if (!str_contains($format,
'%H'))
141 $format = str_replace(
"H",
"%h", $format);
144 if (!str_contains($format,
'%M'))
146 $format = str_replace(
"M",
"%b", $format);
155 return "DATE_FORMAT(".$field.
", '".$format.
"')";
164 $str = implode(
", ", func_get_args());
167 $str =
"CONCAT(".$str.
")";
177 return "IFNULL(".$expression.
", ".$result.
")";
185 return "LENGTH(".$field.
")";
193 return "'".$value.
"'";
211 return array($this,
"convertFromDbDateTime");
213 elseif($field instanceof ORM\Fields\
DateField)
215 return array($this,
"convertFromDbDate");
219 return parent::getConverter($field);
228 if($value !==
null && $value !=
'0000-00-00 00:00:00')
241 if($value !==
null && $value !=
'0000-00-00')
254 return 'CAST('.$fieldName.
' AS char)';
272 switch ($field->getSize())
285 $defaultPrecision = 18;
288 $precision = $field->getPrecision() > 0 ? $field->getPrecision() : $defaultPrecision;
289 $scale = $field->getScale() > 0 ? $field->getScale() : $defaultScale;
291 if ($scale >= $precision)
293 $precision = $defaultPrecision;
294 $scale = $defaultScale;
297 return "decimal($precision, $scale)";
299 elseif ($field instanceof ORM\Fields\
FloatField)
307 elseif ($field instanceof ORM\Fields\
DateField)
311 elseif ($field instanceof ORM\Fields\
TextField)
313 return $field->isLong() ?
'longtext' :
'text';
317 $values = $field->getValues();
319 if (preg_match(
'/^[0-9]+$/', $values[0]) && preg_match(
'/^[0-9]+$/', $values[1]))
325 return 'varchar('.max(mb_strlen($values[0]), mb_strlen($values[1])).
')';
328 elseif ($field instanceof ORM\Fields\
EnumField)
330 return 'varchar('.max(array_map(
'strlen', $field->getValues())).
')';
335 $defaultLength =
false;
338 if ($validator instanceof ORM\Fields\Validators\LengthValidator)
340 if ($defaultLength ===
false || $defaultLength > $validator->getMax())
342 $defaultLength = $validator->getMax();
346 return 'varchar('.($defaultLength > 0? $defaultLength: 255).
')';
355 $offset = intval($offset);
356 $limit = intval($limit);
358 if ($offset > 0 && $limit <= 0)
359 throw new \Bitrix\Main\ArgumentException(
"Limit must be set if offset is set");
363 $sql .=
"\nLIMIT ".$offset.
", ".$limit.
"\n";
372 public function prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
378 $insert && $insert[0] !=
"" && $insert[1] !=
""
379 && $update && $update[1] !=
""
383 INSERT INTO ".$this->quote($tableName).
" (".$insert[0].
")
384 VALUES (".$insert[1].
")
385 ON DUPLICATE KEY UPDATE ".$update[0].
"
401 public function forSql($value, $maxLength = 0)
404 $value = mb_substr($value, 0, $maxLength);
406 $con = $this->connection->getResource();
408 return $con->real_escape_string($value);
418 case MYSQLI_TYPE_TINY:
419 case MYSQLI_TYPE_SHORT:
420 return (
new ORM\Fields\
IntegerField($name))->configureSize(2);
421 case MYSQLI_TYPE_LONG:
422 case MYSQLI_TYPE_INT24:
423 case MYSQLI_TYPE_CHAR:
424 return (
new ORM\Fields\
IntegerField($name))->configureSize(4);
425 case MYSQLI_TYPE_LONGLONG:
426 return ((
new ORM\Fields\
IntegerField($name)))->configureSize(8);
427 case MYSQLI_TYPE_DECIMAL:
428 case MYSQLI_TYPE_NEWDECIMAL:
429 case MYSQLI_TYPE_FLOAT:
430 case MYSQLI_TYPE_DOUBLE:
432 case MYSQLI_TYPE_DATETIME:
433 case MYSQLI_TYPE_TIMESTAMP:
435 case MYSQLI_TYPE_DATE:
436 case MYSQLI_TYPE_NEWDATE:
460 return 'INSERT IGNORE INTO ' . $tableName . $fields . $sql;
468 return $field .
' regexp ' . $regexp;
476 return 'VALUES(' . $this->
quote($identifier) .
')';
481 return "MATCH (" . $field .
") AGAINST (" . $value .
" IN BOOLEAN MODE)";
488 foreach ($values as $i => $searchTerm)
490 $values[$i] = $searchTerm .
'*';
493 return '+' . implode(
' +', $values);
500 foreach ($values as $i => $searchTerm)
502 $values[$i] = $searchTerm .
'*';
505 return implode(
' ', $values);
515 $maxBodySize = 1024*1024;
518 foreach ($insertRows as $insertFields)
521 if (!$head && $insert && $insert[0])
523 $head =
'REPLACE INTO ' . $this->
quote($tableName) .
' (' . $insert[0] .
') VALUES ';
525 if ($insert && $insert[1])
527 $values =
'(' . $insert[1] .
')';
528 $bodySize += mb_strlen($values) + 4;
530 if ($bodySize > $maxBodySize)
532 $result[] = $head.implode(
', ', $body);
540 $result[] = $head.implode(
', ', $body);
549 public function prepareMergeSelect($tableName, array $primaryFields, array $selectFields, $select, $updateFields)
553 $tableFields = $this->connection->getTableFields($tableName);
555 $tableFields = array_change_key_case($tableFields, CASE_UPPER);
556 $updateFields = array_change_key_case($updateFields, CASE_UPPER);
557 foreach ($updateFields as $columnName => $value)
559 if (isset($tableFields[$columnName]))
561 $update[] = $this->
quote($columnName) .
' = '. $this->
convertToDb($value, $tableFields[$columnName]);
565 trigger_error(
"Column `{$columnName}` is not found in the `{$tableName}` table", E_USER_WARNING);
569 $sql =
'INSERT INTO ' . $this->
quote($tableName) .
' (' . implode(
',', array_map([$this,
'quote'], $selectFields)) .
') ';
571 $sql .=
' ON DUPLICATE KEY UPDATE ' . implode(
',', $update);
582 foreach ($order as $columnName => $sort)
584 $orderColumns[] = $this->
quote($columnName) .
' ' . $sort;
586 $sqlOrder = $orderColumns ?
' ORDER BY ' . implode(
', ', $orderColumns) :
'';
587 return 'DELETE FROM ' . $this->
quote($tableName) .
' WHERE ' . $where . $sqlOrder .
' LIMIT ' . intval($limit);
592 return 'set @' . $variableName .
' = 0';
597 return '@' . $variableName .
':=' .
'@' . $variableName .
' + 1';
605 $dml =
"UPDATE " . $tableName .
' AS ' . $tableAlias .
",\n";
606 $dml .= $from .
"\n";
609 foreach ($fields as $fieldName => $fieldValue)
611 $set .= ($set ?
',' :
'') . $tableAlias .
'.' . $fieldName .
' = ' .$fieldValue .
"\n";
613 $dml .=
'SET ' . $set;
614 $dml .=
'WHERE ' . $where .
"\n";
619 protected function getOrderByField(
string $field, array $values, callable $callback,
bool $quote =
true): string
621 $field = $quote ? $this->
quote($field) : $field;
622 $values = implode(
',', array_map($callback, $values));
624 return "FIELD({$field}, {$values})";
softCastTextToChar($fieldName)
getOrderByField(string $field, array $values, callable $callback, bool $quote=true)
prepareMergeMultiple($tableName, array $primaryFields, array $insertRows)
getMatchFunction($field, $value)
getTopSql($sql, $limit, $offset=0)
getIsNullFunction($expression, $result)
getDateToCharFunction($fieldName)
formatDate($format, $field=null)
getRegexpOperator($field, $regexp)
getDatetimeToDateFunction($value)
prepareDeleteLimit($tableName, array $primaryFields, $where, array $order, $limit)
addDaysToDateTime($days, $from=null)
getFieldByColumnType($name, $type, array $parameters=null)
getColumnTypeByField(ScalarField $field)
initRowNumber($variableName)
getConverter(ScalarField $field)
prepareCorrelatedUpdate($tableName, $tableAlias, $fields, $from, $where)
getMatchAndExpression($values, $prefixSearch=false)
getLengthFunction($field)
getMatchOrExpression($values, $prefixSearch=false)
forSql($value, $maxLength=0)
getCurrentDateTimeFunction()
convertFromDbDateTime($value)
addSecondsToDateTime($seconds, $from=null)
convertFromDbDate($value)
prepareMergeSelect($tableName, array $primaryFields, array $selectFields, $select, $updateFields)
getCharToDateFunction($value)
getInsertIgnore($tableName, $fields, $sql)
prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
getRowNumber($variableName)
prepareUpdate($tableName, array $fields)
prepareInsert($tableName, array $fields, $returnAsArray=false)
convertToDb($value, ORM\Fields\IReadable $field=null)