Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mysqlcommonsqlhelper.php
1<?php
2namespace Bitrix\Main\DB;
3
8
9abstract class MysqlCommonSqlHelper extends SqlHelper
10{
16 public function getLeftQuote()
17 {
18 return '`';
19 }
20
26 public function getRightQuote()
27 {
28 return '`';
29 }
30
36 public function getAliasLength()
37 {
38 return 256;
39 }
40
46 public function getQueryDelimiter()
47 {
48 return ';';
49 }
50
57 {
58 return "NOW()";
59 }
60
66 public function getCurrentDateFunction()
67 {
68 return "CURDATE()";
69 }
70
83 public function addSecondsToDateTime($seconds, $from = null)
84 {
85 if ($from === null)
86 {
87 $from = static::getCurrentDateTimeFunction();
88 }
89
90 return 'DATE_ADD('.$from.', INTERVAL '.$seconds.' SECOND)';
91 }
92
102 public function getDatetimeToDateFunction($value)
103 {
104 return 'DATE('.$value.')';
105 }
106
132 public function formatDate($format, $field = null)
133 {
134 static $search = array(
135 "YYYY",
136 "MMMM",
137 "MM",
138 "MI",
139 "DD",
140 "HH",
141 "GG",
142 "G",
143 "SS",
144 "TT",
145 "T"
146 );
147 static $replace = array(
148 "%Y",
149 "%M",
150 "%m",
151 "%i",
152 "%d",
153 "%H",
154 "%h",
155 "%l",
156 "%s",
157 "%p",
158 "%p"
159 );
160
161 $format = str_replace($search, $replace, $format);
162
163 if (mb_strpos($format, '%H') === false)
164 {
165 $format = str_replace("H", "%h", $format);
166 }
167
168 if (mb_strpos($format, '%M') === false)
169 {
170 $format = str_replace("M", "%b", $format);
171 }
172
173 if($field === null)
174 {
175 return $format;
176 }
177 else
178 {
179 return "DATE_FORMAT(".$field.", '".$format."')";
180 }
181 }
182
192 public function getConcatFunction()
193 {
194 $str = "";
195 $ar = func_get_args();
196 if (is_array($ar))
197 $str .= implode(", ", $ar);
198 if ($str <> '')
199 $str = "CONCAT(".$str.")";
200 return $str;
201 }
202
214 public function getIsNullFunction($expression, $result)
215 {
216 return "IFNULL(".$expression.", ".$result.")";
217 }
218
228 public function getLengthFunction($field)
229 {
230 return "LENGTH(".$field.")";
231 }
232
244 public function getCharToDateFunction($value)
245 {
246 return "'".$value."'";
247 }
248
261 public function getDateToCharFunction($fieldName)
262 {
263 return $fieldName;
264 }
265
274 public function getConverter(ScalarField $field)
275 {
276 if($field instanceof ORM\Fields\DatetimeField)
277 {
278 return array($this, "convertFromDbDateTime");
279 }
280 elseif($field instanceof ORM\Fields\DateField)
281 {
282 return array($this, "convertFromDbDate");
283 }
284 else
285 {
286 return parent::getConverter($field);
287 }
288 }
289
301 public function convertDatetimeField($value)
302 {
303 return $this->convertFromDbDateTime($value);
304 }
305
312 public function convertFromDbDateTime($value)
313 {
314 if($value !== null && $value != '0000-00-00 00:00:00')
315 {
316 return new Type\DateTime($value, "Y-m-d H:i:s");
317 }
318
319 return null;
320 }
321
333 public function convertDateField($value)
334 {
335 return $this->convertFromDbDate($value);
336 }
337
344 public function convertFromDbDate($value)
345 {
346 if($value !== null && $value != '0000-00-00')
347 {
348 return new Type\Date($value, "Y-m-d");
349 }
350
351 return null;
352 }
353
361 public function castToChar($fieldName)
362 {
363 return 'CAST('.$fieldName.' AS char)';
364 }
365
373 public function softCastTextToChar($fieldName)
374 {
375 return $fieldName;
376 }
377
385 public function getColumnTypeByField(ScalarField $field)
386 {
387 if ($field instanceof ORM\Fields\IntegerField)
388 {
389 return 'int';
390 }
391 elseif ($field instanceof ORM\Fields\DecimalField)
392 {
393 $defaultPrecision = 18;
394 $defaultScale = 2;
395
396 $precision = $field->getPrecision() > 0 ? $field->getPrecision() : $defaultPrecision;
397 $scale = $field->getScale() > 0 ? $field->getScale() : $defaultScale;
398
399 if ($scale >= $precision)
400 {
401 $precision = $defaultPrecision;
402 $scale = $defaultScale;
403 }
404
405 return "decimal($precision, $scale)";
406 }
407 elseif ($field instanceof ORM\Fields\FloatField)
408 {
409 return 'double';
410 }
411 elseif ($field instanceof ORM\Fields\DatetimeField)
412 {
413 return 'datetime';
414 }
415 elseif ($field instanceof ORM\Fields\DateField)
416 {
417 return 'date';
418 }
419 elseif ($field instanceof ORM\Fields\TextField)
420 {
421 return 'text';
422 }
423 elseif ($field instanceof ORM\Fields\BooleanField)
424 {
425 $values = $field->getValues();
426
427 if (preg_match('/^[0-9]+$/', $values[0]) && preg_match('/^[0-9]+$/', $values[1]))
428 {
429 return 'int';
430 }
431 else
432 {
433 return 'varchar('.max(mb_strlen($values[0]), mb_strlen($values[1])).')';
434 }
435 }
436 elseif ($field instanceof ORM\Fields\EnumField)
437 {
438 return 'varchar('.max(array_map('strlen', $field->getValues())).')';
439 }
440 else
441 {
442 // string by default
443 $defaultLength = false;
444 foreach ($field->getValidators() as $validator)
445 {
446 if ($validator instanceof ORM\Fields\Validators\LengthValidator)
447 {
448 if ($defaultLength === false || $defaultLength > $validator->getMax())
449 {
450 $defaultLength = $validator->getMax();
451 }
452 }
453 }
454 return 'varchar('.($defaultLength > 0? $defaultLength: 255).')';
455 }
456 }
457
470 public function getTopSql($sql, $limit, $offset = 0)
471 {
472 $offset = intval($offset);
473 $limit = intval($limit);
474
475 if ($offset > 0 && $limit <= 0)
476 throw new \Bitrix\Main\ArgumentException("Limit must be set if offset is set");
477
478 if ($limit > 0)
479 {
480 $sql .= "\nLIMIT ".$offset.", ".$limit."\n";
481 }
482
483 return $sql;
484 }
485
496 public function prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
497 {
498 $insert = $this->prepareInsert($tableName, $insertFields);
499 $update = $this->prepareUpdate($tableName, $updateFields);
500
501 if (
502 $insert && $insert[0] != "" && $insert[1] != ""
503 && $update && $update[1] != ""
504 )
505 {
506 $sql = "
507 INSERT INTO ".$this->quote($tableName)." (".$insert[0].")
508 VALUES (".$insert[1].")
509 ON DUPLICATE KEY UPDATE ".$update[0]."
510 ";
511 }
512 else
513 {
514 $sql = "";
515 }
516
517 return array(
518 $sql
519 );
520 }
521
525 public function getConditionalAssignment(string $field, string $value): string
526 {
527 $field = $this->quote($field);
528 $value = $this->convertToDbString($value);
529 $hash = $this->convertToDbString(sha1($value));
530
531 return "IF(SHA1({$field}) = {$hash}, {$field}, {$value})";
532 }
533}
getConditionalAssignment(string $field, string $value)
prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
convertToDbString($value, $length=null)
prepareUpdate($tableName, array $fields)
prepareInsert($tableName, array $fields, $returnAsArray=false)