1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
oraclesqlhelper.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\DB;
4
5use Bitrix\Main\Type;
6use Bitrix\Main\ORM;
7use Bitrix\Main\ORM\Fields\ScalarField;
8
10{
14 public function getLeftQuote()
15 {
16 return '"';
17 }
18
22 public function getRightQuote()
23 {
24 return '"';
25 }
26
30 public function getAliasLength()
31 {
32 return 30;
33 }
34
38 public function quote($identifier)
39 {
40 return parent::quote(mb_strtoupper($identifier));
41 }
42
46 public function getQueryDelimiter()
47 {
48 return "(?<!\\*)/(?!\\*)";
49 }
50
54 function forSql($value, $maxLength = 0)
55 {
56 if ($maxLength <= 0 || $maxLength > 2000)
57 $maxLength = 2000;
58
59 $value = mb_substr($value, 0, $maxLength);
60
61 // From http://w3.org/International/questions/qa-forms-utf-8.html
62 // This one can crash php with segmentation fault on large input data (over 20K)
63 // https://bugs.php.net/bug.php?id=60423
64 if (preg_match_all('%(
65 [\x00-\x7E] # ASCII
66 |[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
67 |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
68 |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
69 |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
70 |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
71 |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
72 |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
73 )+%x', $value, $match))
74 $value = implode(' ', $match[0]);
75 else
76 return ''; //There is no valid utf at all
77
78 return str_replace("'", "''", $value);
79 }
80
85 {
86 return "SYSDATE";
87 }
88
92 public function getCurrentDateFunction()
93 {
94 return "TRUNC(SYSDATE)";
95 }
96
100 public function addSecondsToDateTime($seconds, $from = null)
101 {
102 if ($from === null)
103 {
104 $from = static::getCurrentDateTimeFunction();
105 }
106
107 return '('.$from.'+'.$seconds.'/86400)';
108 }
109
113 public function getDatetimeToDateFunction($value)
114 {
115 return 'TRUNC('.$value.')';
116 }
117
121 public function formatDate($format, $field = null)
122 {
123 $format = str_replace("HH", "HH24", $format);
124 $format = str_replace("GG", "HH24", $format);
125
126 if (!str_contains($format, 'HH24'))
127 {
128 $format = str_replace("H", "HH", $format);
129 }
130
131 $format = str_replace("G", "HH", $format);
132
133 $format = str_replace("MI", "II", $format);
134
135 if (str_contains($format, 'MMMM'))
136 {
137 $format = str_replace("MMMM", "MONTH", $format);
138 }
139 elseif (!str_contains($format, 'MM'))
140 {
141 $format = str_replace("M", "MON", $format);
142 }
143
144 $format = str_replace("II", "MI", $format);
145
146 $format = str_replace("TT", "AM", $format);
147 $format = str_replace("T", "AM", $format);
148
149 if ($field === null)
150 {
151 return $format;
152 }
153 else
154 {
155 return "TO_CHAR(".$field.", '".$format."')";
156 }
157 }
158
162 public function getConcatFunction()
163 {
164 return implode(" || ", func_get_args());
165 }
166
170 public function getIsNullFunction($expression, $result)
171 {
172 return "NVL(".$expression.", ".$result.")";
173 }
174
178 public function getLengthFunction($field)
179 {
180 return "LENGTH(".$field.")";
181 }
182
186 public function getCharToDateFunction($value)
187 {
188 return "TO_DATE('".$value."', 'YYYY-MM-DD HH24:MI:SS')";
189 }
190
194 public function getDateToCharFunction($fieldName)
195 {
196 return "TO_CHAR(".$fieldName.", 'YYYY-MM-DD HH24:MI:SS')";
197 }
198
202 protected function prepareBinds(array $tableFields, array $fields)
203 {
204 $binds = array();
205
206 foreach ($tableFields as $columnName => $tableField)
207 {
208 if (isset($fields[$columnName]) && !($fields[$columnName] instanceof SqlExpression))
209 {
210 if ($tableField instanceof ORM\Fields\TextField && $fields[$columnName] <> '')
211 {
212 $binds[$columnName] = $fields[$columnName];
213 }
214 }
215 }
216
217 return $binds;
218 }
219
223 public function getConverter(ScalarField $field)
224 {
225 if ($field instanceof ORM\Fields\DatetimeField)
226 {
227 return array($this, "convertFromDbDateTime");
228 }
229 elseif ($field instanceof ORM\Fields\TextField)
230 {
231 return array($this, "convertFromDbText");
232 }
233 elseif ($field instanceof ORM\Fields\StringField)
234 {
235 return array($this, "convertFromDbString");
236 }
237 else
238 {
239 return parent::getConverter($field);
240 }
241 }
242
246 public function convertFromDbDateTime($value)
247 {
248 if ($value !== null)
249 {
250 if (mb_strlen($value) == 19)
251 {
252 //preferable format: NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
253 $value = new Type\DateTime($value, "Y-m-d H:i:s");
254 }
255 else
256 {
257 //default Oracle date format: 03-MAR-14
258 $value = new Type\DateTime($value." 00:00:00", "d-M-y H:i:s");
259 }
260 }
261
262 return $value;
263 }
264
268 public function convertFromDbText($value)
269 {
270 if (is_object($value))
271 {
273 $value = $value->load();
274 }
275
276 return $value;
277 }
278
282 public function convertToDbText($value)
283 {
284 return empty($value) ? "NULL" : "EMPTY_CLOB()";
285 }
286
290 public function convertFromDbString($value, $length = null)
291 {
292 if ($value !== null)
293 {
294 if ((mb_strlen($value) == 19) && preg_match("#^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$#", $value))
295 {
296 return new Type\DateTime($value, "Y-m-d H:i:s");
297 }
298 }
299
300 return parent::convertFromDbString($value, $length);
301 }
302
306 public function castToChar($fieldName)
307 {
308 return 'TO_CHAR('.$fieldName.')';
309 }
310
314 public function softCastTextToChar($fieldName)
315 {
316 return 'dbms_lob.substr('.$fieldName.', 4000, 1)';
317 }
318
322 public function getColumnTypeByField(ScalarField $field)
323 {
324 if ($field instanceof ORM\Fields\IntegerField)
325 {
326 return 'number(18)';
327 }
328 elseif ($field instanceof ORM\Fields\FloatField)
329 {
330 $scale = $field->getScale();
331 return 'number'.($scale !== null? "(*,".$scale.")": "");
332 }
333 elseif ($field instanceof ORM\Fields\DatetimeField)
334 {
335 return 'date';
336 }
337 elseif ($field instanceof ORM\Fields\DateField)
338 {
339 return 'date';
340 }
341 elseif ($field instanceof ORM\Fields\TextField)
342 {
343 return 'clob';
344 }
345 elseif ($field instanceof ORM\Fields\BooleanField)
346 {
347 $values = $field->getValues();
348
349 if (preg_match('/^[0-9]+$/', $values[0]) && preg_match('/^[0-9]+$/', $values[1]))
350 {
351 return 'number(1)';
352 }
353 else
354 {
355 $falseLen = mb_strlen($values[0]);
356 $trueLen = mb_strlen($values[1]);
357 if ($falseLen === 1 && $trueLen === 1)
358 {
359 return 'char(1 char)';
360 }
361 return 'varchar2(' . max($falseLen, $trueLen) . ' char)';
362 }
363 }
364 elseif ($field instanceof ORM\Fields\EnumField)
365 {
366 return 'varchar2('.max(array_map('mb_strlen', $field->getValues())).' char)';
367 }
368 else
369 {
370 // string by default
371 $defaultLength = false;
372 foreach ($field->getValidators() as $validator)
373 {
374 if ($validator instanceof ORM\Fields\Validators\LengthValidator)
375 {
376 if ($defaultLength === false || $defaultLength > $validator->getMax())
377 {
378 $defaultLength = $validator->getMax();
379 }
380 }
381 }
382 return 'varchar2('.($defaultLength > 0? $defaultLength: 255).' char)';
383 }
384 }
385
389 public function getFieldByColumnType($name, $type, array $parameters = null)
390 {
391 switch ($type)
392 {
393 case "DATE":
395
396 case "NCLOB":
397 case "CLOB":
398 case "BLOB":
399 return new ORM\Fields\TextField($name);
400
401 case "FLOAT":
402 case "BINARY_FLOAT":
403 case "BINARY_DOUBLE":
404 return new ORM\Fields\FloatField($name);
405
406 case "NUMBER":
407 if ($parameters["precision"] == 0 && $parameters["scale"] == -127)
408 {
409 //NUMBER
410 return new ORM\Fields\FloatField($name);
411 }
412 if (intval($parameters["scale"]) <= 0)
413 {
414 //NUMBER(18)
415 //NUMBER(18,-2)
416 return new ORM\Fields\IntegerField($name);
417 }
418 //NUMBER(*,2)
419 return new ORM\Fields\FloatField($name, array("scale" => $parameters["scale"]));
420 }
421 //LONG
422 //VARCHAR2(size [BYTE | CHAR])
423 //NVARCHAR2(size)
424 //TIMESTAMP [(fractional_seconds_precision)]
425 //TIMESTAMP [(fractional_seconds)] WITH TIME ZONE
426 //TIMESTAMP [(fractional_seconds)] WITH LOCAL TIME ZONE
427 //INTERVAL YEAR [(year_precision)] TO MONTH
428 //INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds)]
429 //RAW(size)
430 //LONG RAW
431 //ROWID
432 //UROWID [(size)]
433 //CHAR [(size [BYTE | CHAR])]
434 //NCHAR[(size)]
435 //BFILE
436 return new ORM\Fields\StringField($name, array("size" => $parameters["size"]));
437 }
438
442 public function getTopSql($sql, $limit, $offset = 0)
443 {
444 $offset = intval($offset);
445 $limit = intval($limit);
446
447 if ($offset > 0 && $limit <= 0)
448 throw new \Bitrix\Main\ArgumentException("Limit must be set if offset is set");
449
450 if ($limit > 0)
451 {
452 //The first row selected has a ROWNUM of 1, the second has 2, and so on
453 if ($offset <= 0)
454 {
455 $sql =
456 "SELECT * ".
457 "FROM (".$sql.") ".
458 "WHERE ROWNUM <= ".$limit;
459 }
460 else
461 {
462 $sql =
463 "SELECT * ".
464 "FROM (".
465 " SELECT rownum_query_alias.*, ROWNUM rownum_alias ".
466 " FROM (".$sql.") rownum_query_alias ".
467 " WHERE ROWNUM <= ".($offset + $limit)." ".
468 ") ".
469 "WHERE rownum_alias >= ".($offset + 1);
470 }
471 }
472 return $sql;
473 }
474
478 public function getAscendingOrder()
479 {
480 return 'ASC NULLS FIRST';
481 }
482
486 public function getDescendingOrder()
487 {
488 return 'DESC NULLS LAST';
489 }
490
494 public function prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
495 {
496 $insert = $this->prepareInsert($tableName, $insertFields);
497
498 $updateColumns = array();
499 $sourceSelectColumns = array();
500 $targetConnectColumns = array();
501 $tableFields = $this->connection->getTableFields($tableName);
502 foreach($tableFields as $columnName => $tableField)
503 {
504 $quotedName = $this->quote($columnName);
505 if (in_array($columnName, $primaryFields))
506 {
507 $sourceSelectColumns[] = $this->convertToDb($insertFields[$columnName], $tableField)." AS ".$quotedName;
508 if($insertFields[$columnName] === null)
509 {
510 //can't just compare NULLs
511 $targetConnectColumns[] = "(source.".$quotedName." IS NULL AND target.".$quotedName." IS NULL)";
512 }
513 else
514 {
515 $targetConnectColumns[] = "(source.".$quotedName." = target.".$quotedName.")";
516 }
517 }
518
519 if (isset($updateFields[$columnName]) || array_key_exists($columnName, $updateFields))
520 {
521 $updateColumns[] = "target.".$quotedName.' = '.$this->convertToDb($updateFields[$columnName], $tableField);
522 }
523 }
524
525 if (
526 $insert && $insert[0] != "" && $insert[1] != ""
527 && $updateColumns
528 && $sourceSelectColumns && $targetConnectColumns
529 )
530 {
531 $sql = "
532 MERGE INTO ".$this->quote($tableName)." target USING (
533 SELECT ".implode(", ", $sourceSelectColumns)." FROM dual
534 )
535 source ON
536 (
537 ".implode(" AND ", $targetConnectColumns)."
538 )
539 WHEN MATCHED THEN
540 UPDATE SET ".implode(", ", $updateColumns)."
541 WHEN NOT MATCHED THEN
542 INSERT (".$insert[0].")
543 VALUES (".$insert[1].")
544 ";
545 }
546 else
547 {
548 $sql = "";
549 }
550
551 return array(
552 $sql
553 );
554 }
555}
$type
Определения options.php:106
softCastTextToChar($fieldName)
Определения oraclesqlhelper.php:314
getTopSql($sql, $limit, $offset=0)
Определения oraclesqlhelper.php:442
getIsNullFunction($expression, $result)
Определения oraclesqlhelper.php:170
getDateToCharFunction($fieldName)
Определения oraclesqlhelper.php:194
formatDate($format, $field=null)
Определения oraclesqlhelper.php:121
getDatetimeToDateFunction($value)
Определения oraclesqlhelper.php:113
convertFromDbString($value, $length=null)
Определения oraclesqlhelper.php:290
castToChar($fieldName)
Определения oraclesqlhelper.php:306
getFieldByColumnType($name, $type, array $parameters=null)
Определения oraclesqlhelper.php:389
getColumnTypeByField(ScalarField $field)
Определения oraclesqlhelper.php:322
convertToDbText($value)
Определения oraclesqlhelper.php:282
getConverter(ScalarField $field)
Определения oraclesqlhelper.php:223
getLengthFunction($field)
Определения oraclesqlhelper.php:178
forSql($value, $maxLength=0)
Определения oraclesqlhelper.php:54
convertFromDbDateTime($value)
Определения oraclesqlhelper.php:246
addSecondsToDateTime($seconds, $from=null)
Определения oraclesqlhelper.php:100
getCharToDateFunction($value)
Определения oraclesqlhelper.php:186
quote($identifier)
Определения oraclesqlhelper.php:38
prepareBinds(array $tableFields, array $fields)
Определения oraclesqlhelper.php:202
prepareMerge($tableName, array $primaryFields, array $insertFields, array $updateFields)
Определения oraclesqlhelper.php:494
convertFromDbText($value)
Определения sqlhelper.php:619
prepareInsert($tableName, array $fields, $returnAsArray=false)
Определения sqlhelper.php:342
convertToDb($value, ORM\Fields\IReadable $field=null)
Определения sqlhelper.php:461
getValidators()
Определения field.php:236
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$fields
Определения yandex_run.php:501