1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sql_util.php
См. документацию.
1<?php
2/*
3 * SQL Helper
4 */
6{
7 public static function GetCount($tableName, $tableAlias, &$arFields, &$arFilter)
8 {
9 $tableName = strval($tableName);
10 if($tableName === '')
11 {
12 return false;
13 }
14
15 global $DB;
16
17 $sql = "SELECT COUNT(*) AS QTY FROM {$tableName}";
18
19 if(is_array($arFilter) && !empty($arFilter))
20 {
21 if(!is_array($arFields))
22 {
23 return false;
24 }
25
26 $arJoins = array();
27 $condition = self::PrepareWhere($arFields, $arFilter, $arJoins);
28 if($condition !== '')
29 {
30 $tableAlias = strval($tableAlias);
31 if($tableAlias !== '')
32 {
33 $sql .= " AS {$tableAlias}";
34 }
35
36 $sql .= " WHERE {$condition}";
37 }
38 }
39
40 $dbResult = $DB->Query($sql);
41 $arResult = $dbResult ? $dbResult->Fetch() : null;
42 return $arResult !== null && isset($arResult['QTY']) ? intval($arResult['QTY']) : 0;
43 }
44
45 public static function GetFilterOperation($key)
46 {
47 $strNegative = "N";
48 if (str_starts_with($key, "!"))
49 {
50 $key = substr($key, 1);
51 $strNegative = "Y";
52 }
53
54 $strOrNull = "N";
55 if (str_starts_with($key, "+"))
56 {
57 $key = substr($key, 1);
58 $strOrNull = "Y";
59 }
60
61 if (str_starts_with($key, ">="))
62 {
63 $key = substr($key, 2);
64 $strOperation = ">=";
65 }
66 elseif (str_starts_with($key, ">"))
67 {
68 $key = substr($key, 1);
69 $strOperation = ">";
70 }
71 elseif (str_starts_with($key, "<="))
72 {
73 $key = substr($key, 2);
74 $strOperation = "<=";
75 }
76 elseif (str_starts_with($key, "<"))
77 {
78 $key = substr($key, 1);
79 $strOperation = "<";
80 }
81 elseif (str_starts_with($key, "@"))
82 {
83 $key = substr($key, 1);
84 $strOperation = "IN";
85 }
86 elseif (str_starts_with($key, "=%"))
87 {
88 $key = substr($key, 2);
89 $strOperation = "RLIKE";
90 }
91 elseif (str_starts_with($key, "%="))
92 {
93 $key = substr($key, 2);
94 $strOperation = "LLIKE";
95 }
96 elseif (str_starts_with($key, "%"))
97 {
98 $key = substr($key, 1);
99 $strOperation = "LIKE";
100 }
101 elseif (str_starts_with($key, "?"))
102 {
103 $key = substr($key, 1);
104 $strOperation = "QUERY";
105 }
106 elseif (str_starts_with($key, "*="))
107 {
108 $key = substr($key, 2);
109 $strOperation = "FTI";
110 }
111 elseif (str_starts_with($key, "*%"))
112 {
113 $key = substr($key, 2);
114 $strOperation = "FTL";
115 }
116 elseif (str_starts_with($key, "*"))
117 {
118 $key = substr($key, 1);
119 $strOperation = "FT";
120 }
121 elseif (str_starts_with($key, "="))
122 {
123 $key = substr($key, 1);
124 $strOperation = "=";
125 }
126 else
127 {
128 $strOperation = "=";
129 }
130
131 return array("FIELD" => $key, "NEGATIVE" => $strNegative, "OPERATION" => $strOperation, "OR_NULL" => $strOrNull);
132 }
133
134 private static function AddToSelect($fieldKey, $arField, &$strSqlSelect)
135 {
136 global $DB;
137
138 if ($strSqlSelect <> '')
139 $strSqlSelect .= ", ";
140
141 if ($arField["TYPE"] == "datetime")
142 {
143 $strSqlSelect .= $DB->DateToCharFunction($arField["FIELD"], "FULL")." as ".$fieldKey;
144 }
145 elseif ($arField["TYPE"] == "date")
146 {
147 $strSqlSelect .= $DB->DateToCharFunction($arField["FIELD"], "SHORT")." as ".$fieldKey;
148 }
149 else
150 {
151 $strSqlSelect .= $arField["FIELD"]." as ".$fieldKey;
152 }
153 }
154
155 private static function AddToFrom($arField, &$arJoined, &$strSqlFrom)
156 {
157 if (isset($arField["FROM"])
158 && $arField["FROM"] <> ''
159 && !in_array($arField["FROM"], $arJoined))
160 {
161 if ($strSqlFrom <> '')
162 $strSqlFrom .= " ";
163 $strSqlFrom .= $arField["FROM"];
164 $arJoined[] = $arField["FROM"];
165 }
166 }
167
168 private static function PrepareDefaultFields($arFields, &$arJoined, &$strSqlSelect, &$strSqlFrom)
169 {
170 $arFieldsKeys = array_keys($arFields);
171 $qty = count($arFieldsKeys);
172 for ($i = 0; $i < $qty; $i++)
173 {
174 if (isset($arFields[$arFieldsKeys[$i]]["WHERE_ONLY"])
175 && $arFields[$arFieldsKeys[$i]]["WHERE_ONLY"] == "Y")
176 {
177 continue;
178 }
179
180 if (isset($arFields[$arFieldsKeys[$i]]["DEFAULT"])
181 && $arFields[$arFieldsKeys[$i]]["DEFAULT"] == "N")
182 {
183 continue;
184 }
185
186 self::AddToSelect($arFieldsKeys[$i], $arFields[$arFieldsKeys[$i]], $strSqlSelect);
187 self::AddToFrom($arFields[$arFieldsKeys[$i]], $arJoined, $strSqlFrom);
188 }
189 }
190
191 public static function PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields, $arOptions = array())
192 {
193 global $DB;
194
195 if(!is_array($arOptions))
196 {
197 $arOptions = array();
198 }
199
200 $strSqlSelect = '';
201 $strSqlFrom = '';
202 $strSqlFromWhere = '';
203 $strSqlGroupBy = '';
204
205 $arGroupByFunct = array("COUNT", "AVG", "MIN", "MAX", "SUM");
206
207 $arAlreadyJoined = array();
208
209 // GROUP BY -->
210 if (is_array($arGroupBy) && !empty($arGroupBy))
211 {
212 $arSelectFields = $arGroupBy;
213 foreach ($arGroupBy as $key => $val)
214 {
215 $val = mb_strtoupper($val);
216 $key = mb_strtoupper($key);
217 if (array_key_exists($val, $arFields) && !in_array($key, $arGroupByFunct))
218 {
219 if ($strSqlGroupBy <> '')
220 $strSqlGroupBy .= ", ";
221 $strSqlGroupBy .= $arFields[$val]["FIELD"];
222
223 if (isset($arFields[$val]["FROM"])
224 && $arFields[$val]["FROM"] <> ''
225 && !in_array($arFields[$val]["FROM"], $arAlreadyJoined))
226 {
227 if ($strSqlFrom <> '')
228 $strSqlFrom .= " ";
229 $strSqlFrom .= $arFields[$val]["FROM"];
230 $arAlreadyJoined[] = $arFields[$val]["FROM"];
231 }
232 }
233 }
234 }
235 // <-- GROUP BY
236
237 // SELECT -->
238 $arFieldsKeys = array_keys($arFields);
239
240 if (is_array($arGroupBy) && empty($arGroupBy))
241 {
242 $strSqlSelect = "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT ";
243 }
244 else
245 {
246 if (isset($arSelectFields) && is_string($arSelectFields) && $arSelectFields <> '' && array_key_exists($arSelectFields, $arFields))
247 $arSelectFields = array($arSelectFields);
248
249 if (empty($arSelectFields) || !is_array($arSelectFields))
250 {
251 self::PrepareDefaultFields($arFields, $arAlreadyJoined, $strSqlSelect, $strSqlFrom);
252 }
253 else
254 {
255 foreach ($arSelectFields as $key => $val)
256 {
257 if($val === '*')
258 {
259 self::PrepareDefaultFields($arFields, $arAlreadyJoined, $strSqlSelect, $strSqlFrom);
260 }
261
262 $val = mb_strtoupper($val);
263 $key = mb_strtoupper($key);
264
265 if (!array_key_exists($val, $arFields))
266 {
267 continue;
268 }
269
270 if (in_array($key, $arGroupByFunct))
271 {
272 if ($strSqlSelect <> '')
273 $strSqlSelect .= ", ";
274
275 $strSqlSelect .= $key."(".$arFields[$val]["FIELD"].") as ".$val;
276 }
277 else
278 {
279 self::AddToSelect($val, $arFields[$val], $strSqlSelect);
280 }
281 self::AddToFrom($arFields[$val], $arAlreadyJoined, $strSqlFrom);
282 }
283 }
284
285 if($strSqlGroupBy === '')
286 {
287 $strSqlSelect = "%%_DISTINCT_%% ".$strSqlSelect;
288 }
289 elseif(!isset($arOptions['ENABLE_GROUPING_COUNT']) || $arOptions['ENABLE_GROUPING_COUNT'] === true)
290 {
291 if ($strSqlSelect <> '')
292 $strSqlSelect .= ", ";
293 $strSqlSelect .= "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT";
294 }
295 }
296 // <-- SELECT
297
298 // WHERE -->
299 $arJoins = array();
300 $strSqlWhere = self::PrepareWhere($arFields, $arFilter, $arJoins);
301
302 foreach($arJoins as $join)
303 {
304 if($join === '')
305 {
306 continue;
307 }
308
309 if ($strSqlFromWhere !== '')
310 {
311 $strSqlFromWhere .= ' ';
312 }
313 $strSqlFromWhere .= $join;
314
315 if(!in_array($join, $arAlreadyJoined))
316 {
317 if ($strSqlFrom !== '')
318 {
319 $strSqlFrom .= ' ';
320 }
321
322 $strSqlFrom .= $join;
323 $arAlreadyJoined[] = $join;
324 }
325 }
326 // <-- WHERE
327
328 // ORDER BY -->
329 $arSqlOrder = array();
330 $dbType = $DB->type;
331 $nullsLast = isset($arOptions['NULLS_LAST']) ? (bool)$arOptions['NULLS_LAST'] : false;
332 foreach ($arOrder as $by => $order)
333 {
334 $by = mb_strtoupper($by);
335 $order = mb_strtoupper($order);
336
337 if ($order != "ASC")
338 $order = "DESC";
339
340 if (array_key_exists($by, $arFields))
341 {
342 if(!$nullsLast)
343 {
344 if($dbType !== "ORACLE")
345 {
346 $arSqlOrder[] = $arFields[$by]["FIELD"]." ".$order;
347 }
348 else
349 {
350 if($order === 'ASC')
351 $arSqlOrder[] = $arFields[$by]["FIELD"]." ".$order." NULLS FIRST";
352 else
353 $arSqlOrder[] = $arFields[$by]["FIELD"]." ".$order." NULLS LAST";
354 }
355 }
356 else
357 {
358 if($dbType === "MYSQL")
359 {
360 //Use MySql feature for sort in 'NULLS_LAST' mode
361 if($order === 'ASC')
362 $arSqlOrder[] = "-".$arFields[$by]["FIELD"]." DESC";
363 else
364 $arSqlOrder[] = $arFields[$by]["FIELD"]." ".$order;
365 }
366 }
367
368 if (isset($arFields[$by]["FROM"])
369 && $arFields[$by]["FROM"] <> ''
370 && !in_array($arFields[$by]["FROM"], $arAlreadyJoined))
371 {
372 if ($strSqlFrom <> '')
373 $strSqlFrom .= " ";
374 $strSqlFrom .= $arFields[$by]["FROM"];
375 $arAlreadyJoined[] = $arFields[$by]["FROM"];
376 }
377 }
378 }
379
380 $strSqlOrderBy = '';
381 DelDuplicateSort($arSqlOrder);
382 $sqlOrderQty = count($arSqlOrder);
383 for ($i = 0; $i < $sqlOrderQty; $i++)
384 {
385 if ($strSqlOrderBy <> '')
386 $strSqlOrderBy .= ", ";
387
388 $strSqlOrderBy .= $arSqlOrder[$i];
389 }
390 // <-- ORDER BY
391
392 return array(
393 "SELECT" => $strSqlSelect,
394 "FROM" => $strSqlFrom,
395 "WHERE" => $strSqlWhere,
396 "FROM_WHERE" => $strSqlFromWhere,
397 "GROUPBY" => $strSqlGroupBy,
398 "ORDERBY" => $strSqlOrderBy
399 );
400 }
401
402 public static function PrepareWhere(&$arFields, &$arFilter, &$arJoins)
403 {
404 global $DB;
405 $arSqlSearch = Array();
406
407 if (!is_array($arFilter))
408 $arFilter = Array();
409
410 foreach ($arFilter as $filterKey => $vals)
411 {
412 if (!is_array($vals))
413 $vals = array($vals);
414
415 if(str_starts_with($filterKey, '__INNER_FILTER'))
416 {
417 $innerFilterSql = self::PrepareWhere($arFields, $vals, $arJoins);
418 if(is_string($innerFilterSql) && $innerFilterSql !== '')
419 {
420 $arSqlSearch[] = '('.$innerFilterSql.')';
421 }
422 continue;
423 }
424
425 $key_res = self::GetFilterOperation($filterKey);
426 $key = $key_res["FIELD"];
427 $strNegative = $key_res["NEGATIVE"];
428 $strOperation = $key_res["OPERATION"];
429 $strOrNull = $key_res["OR_NULL"];
430
431 if (array_key_exists($key, $arFields))
432 {
433 $arSqlSearch_tmp = array();
434
435 if (!empty($vals))
436 {
437 if ($strOperation == "IN")
438 {
439 if (isset($arFields[$key]["WHERE"]))
440 {
441 $arSqlSearch_tmp1 = call_user_func_array(
442 $arFields[$key]["WHERE"],
443 array($vals, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
444 );
445 if ($arSqlSearch_tmp1 !== false)
446 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
447 }
448 else
449 {
450 if ($arFields[$key]["TYPE"] == "int")
451 {
452 array_walk(
453 $vals,
454 function (&$item) {
455 $item = (int)$item;
456 }
457 );
458 $vals = array_unique($vals);
459 $val = implode(",", $vals);
460
461 if (empty($vals))
462 $arSqlSearch_tmp[] = "(1 = 2)";
463 else
464 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " NOT " : "")."(".$arFields[$key]["FIELD"]." IN (".$val."))";
465 }
466 elseif ($arFields[$key]["TYPE"] == "double")
467 {
468 array_walk(
469 $vals,
470 function (&$item) {
471 $item = (float)$item;
472 }
473 );
474 $vals = array_unique($vals);
475 $val = implode(",", $vals);
476
477 if (empty($vals))
478 $arSqlSearch_tmp[] = "(1 = 2)";
479 else
480 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." (".$val."))";
481 }
482 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
483 {
484 array_walk(
485 $vals,
486 function (&$item) {
487 $item = "'".$GLOBALS["DB"]->ForSql($item)."'";
488 }
489 );
490 $vals = array_unique($vals);
491 $val = implode(",", $vals);
492
493 if (empty($vals))
494 $arSqlSearch_tmp[] = "(1 = 2)";
495 else
496 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." (".$val."))";
497 }
498 elseif ($arFields[$key]["TYPE"] == "datetime")
499 {
500 array_walk(
501 $vals,
502 function (&$item) {
503 $item = $GLOBALS["DB"]->CharToDateFunction($item, "FULL");
504 }
505 );
506 $vals = array_unique($vals);
507 $val = implode(",", $vals);
508
509 if (empty($vals))
510 $arSqlSearch_tmp[] = "1 = 2";
511 else
512 $arSqlSearch_tmp[] = ($strNegative=="Y"?" NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." (".$val."))";
513 }
514 elseif ($arFields[$key]["TYPE"] == "date")
515 {
516 array_walk(
517 $vals,
518 function (&$item) {
519 $item = $GLOBALS["DB"]->CharToDateFunction($item, "SHORT");
520 }
521 );
522 $vals = array_unique($vals);
523 $val = implode(",", $vals);
524
525 if (empty($vals))
526 $arSqlSearch_tmp[] = "1 = 2";
527 else
528 $arSqlSearch_tmp[] = ($strNegative=="Y"?" NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." (".$val."))";
529 }
530 }
531 }
532 else
533 {
534 foreach ($vals as $val)
535 {
536 if (isset($arFields[$key]["WHERE"]))
537 {
538 $arSqlSearch_tmp1 = call_user_func_array(
539 $arFields[$key]["WHERE"],
540 array($val, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
541 );
542 if ($arSqlSearch_tmp1 !== false)
543 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
544 }
545 else
546 {
547 $fieldType = $arFields[$key]["TYPE"];
548 $fieldName = $arFields[$key]["FIELD"];
549 if ($strOperation === "QUERY" && $fieldType !== "string" && $fieldType !== "char")
550 {
551 // Ignore QUERY operation for not character types - QUERY is supported only for character types.
552 $strOperation = '=';
553 }
554
555 if (($strOperation === "LIKE" || $strOperation === "RLIKE" || $strOperation === "LLIKE")
556 && ($fieldType === "int" || $fieldType === "double"))
557 {
558 // Ignore LIKE operation for numeric types.
559 $strOperation = '=';
560 }
561
562 if ($fieldType === "int")
563 {
564 if ((intval($val) === 0) && (str_contains($strOperation, "=")))
565 $arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
566 else
567 {
568 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".intval($val)." )";
569 }
570 }
571 elseif ($fieldType === "double")
572 {
573 $val = str_replace(",", ".", $val);
574
575 if ((doubleval($val) == 0) && (str_contains($strOperation, "=")))
576 $arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
577 else
578 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".DoubleVal($val)." )";
579 }
580 elseif ($fieldType === "string" || $fieldType === "char")
581 {
582 if ($strOperation === "QUERY")
583 {
584 $arSqlSearch_tmp[] = GetFilterQuery($fieldName, $val, "Y");
585 }
586 else
587 {
588 if (($val == '') && (str_contains($strOperation, "=")))
589 {
590 $arSqlSearch_tmp[] = "(".$fieldName." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$DB->Length($fieldName)." <= 0) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$fieldName." ".$strOperation." '".$DB->ForSql($val)."' )";
591 }
592 else
593 {
594 if($strOperation === "LIKE")
595 {
596 if($val == '')
597 $arSqlSearch_tmp[] = "({$fieldName} IS NULL OR {$fieldName} = '')";
598 else
599 $arSqlSearch_tmp[] = $fieldName." LIKE '%".self::ForLike($val)."%' ESCAPE '!'";
600
601 }
602 elseif($strOperation === "RLIKE" || $strOperation === "LLIKE")
603 {
604 if($val == '')
605 $arSqlSearch_tmp[] = "({$fieldName} IS NULL OR {$fieldName} = '')";
606 else
607 $arSqlSearch_tmp[] = $fieldName." LIKE '".$DB->ForSql($val)."'";
608 }
609 elseif($strOperation === "FT" || $strOperation === "FTI" || $strOperation === "FTL")
610 {
611 $queryWhere = new CSQLWhere();
612 $queryWhere->SetFields(
613 array(
614 $key => array(
615 'FIELD_NAME' => $fieldName,
616 'FIELD_TYPE' => 'string',
617 'JOIN' => false
618 )
619 )
620 );
621
622 $query = $queryWhere->GetQuery(array($filterKey => $val));
623 if($query !== '')
624 {
625 $arSqlSearch_tmp[] = $query;
626 }
627 }
628 else
629 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$fieldName." IS NULL OR NOT " : "")."(".$fieldName." ".$strOperation." '".$DB->ForSql($val)."' )";
630 }
631 }
632 }
633 elseif ($fieldType === "datetime")
634 {
635 if(!in_array($strOperation, array('=', '<', '>', '<=', '>='), true))
636 {
637 $strOperation = '=';
638 }
639
640 if ($val == '')
641 $arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
642 elseif (mb_strtoupper($val) === "NOW")
643 $arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->GetNowFunction().")";
644 else
645 $arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "FULL").")";
646 }
647 elseif ($fieldType === "date")
648 {
649 if(!in_array($strOperation, array('=', '<', '>', '<=', '>='), true))
650 {
651 $strOperation = '=';
652 }
653
654 if ($val == '')
655 $arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
656 else
657 $arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "SHORT").")";
658 }
659 }
660 }
661 }
662 }
663
664 if (isset($arFields[$key]["FROM"])
665 && $arFields[$key]["FROM"] <> ''
666 && !in_array($arFields[$key]["FROM"], $arJoins))
667 {
668 $arJoins[] = $arFields[$key]["FROM"];
669 }
670
671 $strSqlSearch_tmp = "";
672 $sqlSearchQty = count($arSqlSearch_tmp);
673 for ($j = 0; $j < $sqlSearchQty; $j++)
674 {
675 if ($j > 0)
676 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
677 $strSqlSearch_tmp .= self::AddBrackets($arSqlSearch_tmp[$j]);
678 }
679 if ($strOrNull == "Y")
680 {
681 if ($strSqlSearch_tmp <> '')
682 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
683 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." IS ".($strNegative=="Y" ? "NOT " : "")."NULL)";
684
685 if ($arFields[$key]["TYPE"] == "int" || $arFields[$key]["TYPE"] == "double")
686 {
687 if ($strSqlSearch_tmp <> '')
688 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
689 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." 0)";
690 }
691 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
692 {
693 if ($strSqlSearch_tmp <> '')
694 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
695 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." '')";
696 }
697 }
698
699 if ($strSqlSearch_tmp != "")
700 {
701 $arSqlSearch[] = $strSqlSearch_tmp;
702 }
703 }
704 }
705
706 $logic = 'AND';
707 if(isset($arFilter['LOGIC']) && $arFilter['LOGIC'] !== '')
708 {
709 $logic = strtoupper($arFilter['LOGIC']);
710 if($logic !== 'AND' && $logic !== 'OR')
711 {
712 $logic = 'AND';
713 }
714 }
715
716 $strSqlWhere = '';
717 $logic = " $logic ";
718 foreach ($arSqlSearch as $searchItem)
719 {
720 if($searchItem === '')
721 {
722 continue;
723 }
724
725 if ($strSqlWhere !== '')
726 $strSqlWhere .= $logic;
727
728 $strSqlWhere .= "($searchItem)";
729 }
730
731 return $strSqlWhere;
732 }
733
734 private static function AddBrackets($str)
735 {
736 return preg_match('/^\‍(.*\‍)$/s', $str) > 0 ? $str : "($str)";
737 }
738
739 public static function GetRowCount($arSql, $tableName, $tableAlias = '')
740 {
741 global $DB;
742
743 $tableName = strval($tableName);
744 $tableAlias = strval($tableAlias);
745
746 $query = 'SELECT COUNT(\'x\') as CNT FROM '.$tableName;
747
748 if($tableAlias !== '')
749 {
750 $query .= ' '.$tableAlias;
751 }
752
753 if (isset($arSql['FROM'][0]))
754 {
755 $query .= ' '.$arSql['FROM'];
756 }
757
758 if (isset($arSql['WHERE'][0]))
759 {
760 $query .= ' WHERE '.$arSql['WHERE'];
761 }
762
763 if (isset($arSql['GROUPBY'][0]))
764 {
765 $query .= ' GROUP BY '.$arSql['GROUPBY'];
766 }
767
768 $rs = $DB->Query($query);
769 //MYSQL, MSSQL, ORACLE
770 $result = 0;
771 while($ary = $rs->Fetch())
772 {
773 $result += intval($ary['CNT']);
774 }
775
776 return $result;
777 }
778
779 public static function PrepareSelectTop(&$sql, $top)
780 {
781 $sql .= ' LIMIT '.$top;
782 }
783
784 private static function ForLike($str)
785 {
786 global $DB;
787 static $search = array( "!", "_", "%");
788 static $replace = array("!!", "!_", "!%");
789 return str_replace($search, $replace, $DB->ForSQL($str));
790 }
791}
$arResult
Определения generate_coupon.php:16
Определения sqlwhere.php:1359
Определения sql_util.php:6
static PrepareWhere(&$arFields, &$arFilter, &$arJoins)
Определения sql_util.php:402
static GetFilterOperation($key)
Определения sql_util.php:45
static GetCount($tableName, $tableAlias, &$arFields, &$arFilter)
Определения sql_util.php:7
static PrepareSelectTop(&$sql, $top)
Определения sql_util.php:779
static PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields, $arOptions=array())
Определения sql_util.php:191
static GetRowCount($arSql, $tableName, $tableAlias='')
Определения sql_util.php:739
$str
Определения commerceml2.php:63
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
GetFilterQuery($field, $val, $procent="Y", $ex_sep=array(), $clob="N", $div_fields="Y", $clob_upper="N")
Определения filter_tools.php:383
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$dbType
Определения autoload.php:6
global $DB
Определения cron_frame.php:29
$arOptions
Определения structure.php:223
DelDuplicateSort(&$arSort)
Определения tools.php:2055
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$val
Определения options.php:1793
$rs
Определения action.php:82
$GLOBALS['_____370096793']
Определения update_client.php:1
$dbResult
Определения updtr957.php:3
$arFilter
Определения user_search.php:106