65 public function quote($identifier)
67 if (empty($this->idCache[$identifier]))
73 if (str_contains($quotedIdentifier,
'.'))
82 return $this->idCache[$identifier];
100 abstract public function forSql($value, $maxLength = 0);
203 $sql =
'SUBSTR('.$str.
', '.$from;
205 if (!is_null($length))
206 $sql .=
', '.$length;
305 abstract public function getTopSql($sql, $limit, $offset = 0);
317 public function prepareInsert($tableName, array $fields, $returnAsArray =
false)
322 $tableFields = $this->connection->getTableFields($tableName);
325 $tableFields = array_change_key_case($tableFields, CASE_UPPER);
326 $fields = array_change_key_case($fields, CASE_UPPER);
328 foreach ($fields as $columnName => $value)
330 if (isset($tableFields[$columnName]))
332 $columns[] = $this->
quote($columnName);
333 $values[] = $this->
convertToDb($value, $tableFields[$columnName]);
337 trigger_error(
"Column `{$columnName}` is not found in the `{$tableName}` table", E_USER_WARNING);
344 $returnAsArray ? $columns : implode(
", ", $columns),
345 $returnAsArray ? $values : implode(
", ", $values),
362 $tableFields = $this->connection->getTableFields($tableName);
365 $tableFields = array_change_key_case($tableFields, CASE_UPPER);
366 $fields = array_change_key_case($fields, CASE_UPPER);
368 foreach ($fields as $columnName => $value)
370 if (isset($tableFields[$columnName]))
372 $update[] = $this->
quote($columnName).
' = '.$this->
convertToDb($value, $tableFields[$columnName]);
376 trigger_error(
"Column `{$columnName}` is not found in the `{$tableName}` table", E_USER_WARNING);
383 implode(
", ", $update),
398 abstract public function prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields);
424 $tableField = $this->connection->getTableField($tableName, $columnName);
426 return $this->
quote($columnName).
' = '.$this->
convertToDb($value, $tableField);
446 return $value->compile();
449 if (is_a($field,
'\Bitrix\Main\ORM\Fields\StringField'))
451 $size = $field->getSize();
454 $value = mb_substr($value, 0, $size);
458 if($field instanceof ORM\Fields\
IReadable)
460 $result = $field->convertValueToDb($value);
482 return $field->convertValueFromDb($value);
494 return intval($value);
504 return intval($value);
517 $value = doubleval($value);
518 if(!is_finite($value))
523 return $scale !==
null ?
"'".round($value, $scale).
"'" :
"'".$value.
"'";
534 $value = doubleval($value);
536 return $scale !==
null ? round($value, $scale) : $value;
549 return "'".$this->forSql($value, $length).
"'";
562 $value = mb_substr($value, 0, $length);
565 return strval($value);
643 $value = clone($value);
644 $value->setDefaultTimeZone();
758 $field = $this->
quote($field);
762 return 'case when ' . $this->
getSha1Function($field) .
' = ' . $hash .
' then ' . $field .
' else ' . $value .
' end';
801 return 'sha1(' . $field .
')';
884 public function prepareMergeSelect($tableName, array $primaryFields, array $selectFields, $select, $updateFields)
950 public function prepareMergeValues(
string $tableName, array $primaryFields, array $insertRows, array $updateFields = []): string
952 $insertColumns = array_keys($insertRows[array_key_first($insertRows)] ?? []);
953 $insertValuesStrings = [];
954 foreach ($insertRows as $row)
957 $insertValuesStrings[] = $rowValues;
960 if (empty($updateFields))
962 $notPrimaryFields = array_diff($insertColumns, $primaryFields);
963 if (empty($notPrimaryFields))
965 trigger_error(
"Only primary fields to update, use getInsertIgnore() or specify fields", E_USER_WARNING);
967 $updateFields = $notPrimaryFields;
970 $compatibleUpdateFields = [];
972 foreach ($updateFields as $key => $value)
974 if (is_numeric($key) && is_string($value))
976 $compatibleUpdateFields[$value] =
new SqlExpression(
'?v', $value);
980 $compatibleUpdateFields[$key] = $value;
984 $insertValueString =
'values (' . implode(
'),(', $insertValuesStrings) .
')';
986 return $this->
prepareMergeSelect($tableName, $primaryFields, $insertColumns, $insertValueString, $compatibleUpdateFields);
998 return $this->
getOrderByField($field, $values, [$this,
'convertToDbString'], $quote);
1010 return $this->
getOrderByField($field, $values, [$this,
'convertFromDbInteger'], $quote);
1021 protected function getOrderByField(
string $field, array $values, callable $callback,
bool $quote =
true): string
1023 return $quote ? $this->
quote($field) : $field;
softCastTextToChar($fieldName)
getOrderByField(string $field, array $values, callable $callback, bool $quote=true)
prepareMergeMultiple($tableName, array $primaryFields, array $insertRows)
getMatchFunction($field, $value)
getOrderByStringField(string $field, array $values, bool $quote=true)
__construct(Connection $connection)
getTopSql($sql, $limit, $offset=0)
convertToDbString($value, $length=null)
getConverter(ORM\Fields\ScalarField $field)
getIsNullFunction($expression, $result)
getDateToCharFunction($fieldName)
convertFromDbInteger($value)
formatDate($format, $field=null)
getRegexpOperator($field, $regexp)
getDatetimeToDateFunction($value)
getConditionalAssignment(string $field, string $value)
prepareDeleteLimit($tableName, array $primaryFields, $where, array $order, $limit)
convertToDbInteger($value)
convertDatetimeField($value)
prepareUpdate($tableName, array $fields)
convertToDbFloat($value, $scale=null)
getColumnTypeByField(ORM\Fields\ScalarField $field)
convertFromDbString($value, $length=null)
addDaysToDateTime($days, $from=null)
convertFromDbText($value)
getFieldByColumnType($name, $type, array $parameters=null)
convertFromDb($value, ORM\Fields\IReadable $field)
prepareAssignment($tableName, $columnName, $value)
initRowNumber($variableName)
convertToDbDateTime($value)
prepareInsert($tableName, array $fields, $returnAsArray=false)
convertFromDbFloat($value, $scale=null)
getSubstrFunction($str, $from, $length=null)
prepareCorrelatedUpdate($tableName, $tableAlias, $fields, $from, $where)
getMatchAndExpression($values, $prefixSearch=false)
convertToDb($value, ORM\Fields\IReadable $field=null)
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)
prepareMergeValues(string $tableName, array $primaryFields, array $insertRows, array $updateFields=[])
prepareBinds(array $tableFields, array $fields)
getInsertIgnore($tableName, $fields, $sql)
prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
getOrderByIntField(string $field, array $values, bool $quote=true)
getRowNumber($variableName)