271 if (preg_match(
"#^\\s*insert\\s+into\\s+(.+?)(\\(|)\\s*(\\s*select.*)\\s*\\2\\s*(\$|ON\\s+DUPLICATE\\s+KEY\\s+UPDATE)#is",
$sql, $match))
275 elseif (preg_match(
'#^\\s*DELETE\\s+#i',
$sql))
277 $result = preg_replace(
'#^\\s*(DELETE.*?FROM)#is',
'select * from',
$sql);
279 elseif (preg_match(
'#^\\s*SELECT\\s+#i',
$sql))
293 return trim(preg_replace(
"/[ \t\n\r]+/",
' ',
$str),
" \t\n\r");
298 $this->sql = preg_replace(
'/([()=])/',
" \\1 ",
$sql);
302 if (preg_match(
'/^(select) /i', $this->sql, $match))
304 $this->type = mb_strtolower($match[1]);
308 $this->type =
'unknown';
311 if ($this->type ===
'select')
321 if (!$this->from->parse($this->sql))
326 $tables_regex =
'(?:' . implode(
'|', $this->from->getTableAliases()) .
')';
328 if (preg_match(
'/ where (.+?)($| group | having | order )/i', $this->sql, $match))
330 $this->where->parse($match[1]);
343 $this->subqueries = [];
345 $ar = preg_split(
'/(\\(\\s*select|\\(|\\))/is', $this->sql, -1, PREG_SPLIT_DELIM_CAPTURE);
361 if (preg_match(
'/^\\(\\s*select/is',
$str))
363 $this->subqueries[] = mb_substr(
$str, 1);
375 $this->subqueries[
count($this->subqueries) - 1] .=
$str;
380 $this->sql = implode(
'',
$ar);
384 public function cmp($table, $alias)
386 if ($table === $alias)
390 elseif ($table ===
'`' . $alias .
'`')
400 public function table_joins($table_alias)
403 $suggest_table =
null;
405 foreach ($this->from->tables as $table)
407 if ($this->
cmp($table->alias, $table_alias))
409 $suggest_table = $table;
412 if (!isset($suggest_table))
421 foreach ($this->from->joins as $join)
423 if ($this->
cmp($join->left_table, $table_alias) && $join->right_table !==
'')
425 if (!isset($arTableJoins[$join->right_table]))
427 $arTableJoins[$join->right_table] = [];
429 $arTableJoins[$join->right_table][] = $join->left_column;
431 elseif ($this->
cmp($join->right_table, $table_alias) && $join->left_table !==
'')
433 if (!isset($arTableJoins[$join->left_table]))
435 $arTableJoins[$join->left_table] = [];
437 $arTableJoins[$join->left_table][] = $join->right_column;
441 foreach ($this->where->joins as $join)
443 if ($this->
cmp($join->left_table, $table_alias) && $join->right_table !==
'')
445 if (!isset($arTableJoins[$join->right_table]))
447 $arTableJoins[$join->right_table] = [];
449 $arTableJoins[$join->right_table][] = $join->left_column;
451 elseif ($this->
cmp($join->right_table, $table_alias) && $join->left_table !==
'')
453 if (!isset($arTableJoins[$join->left_table]))
455 $arTableJoins[$join->left_table] = [];
457 $arTableJoins[$join->left_table][] = $join->right_column;
461 foreach ($this->from->joins as $join)
463 if ($this->
cmp($join->left_table, $table_alias) && $join->right_table ===
'')
465 foreach ($arTableJoins as
$i => $arColumns)
467 $arTableJoins[
$i][] = $join->left_column;
470 elseif ($this->
cmp($join->right_table, $table_alias) && $join->left_table ===
'')
472 foreach ($arTableJoins as
$i => $arColumns)
474 $arTableJoins[
$i][] = $join->right_column;
479 foreach ($this->where->joins as $join)
481 if ($this->
cmp($join->left_table, $table_alias) && $join->right_table ===
'')
483 foreach ($arTableJoins as
$i => $arColumns)
485 $arTableJoins[
$i][] = $join->left_column;
488 elseif ($this->
cmp($join->right_table, $table_alias) && $join->left_table ===
'')
490 foreach ($arTableJoins as
$i => $arColumns)
492 $arTableJoins[
$i][] = $join->right_column;
497 if (empty($arTableJoins[
'WHERE']))
499 unset($arTableJoins[
'WHERE']);
502 return $arTableJoins;
505 public function suggest_index($table_alias)
509 $suggest_table =
null;
511 foreach ($this->from->tables as $table)
513 if ($this->
cmp(
$table->alias, $table_alias))
518 if (!isset($suggest_table))
523 $arTableJoins = $this->table_joins($table_alias);
527 if (!empty($arTableJoins))
529 if (!
$DB->TableExists($suggest_table->name))
534 $table =
new CPerfomanceTable;
535 $table->Init($suggest_table->name);
536 $arIndexes =
$table->GetIndexes();
537 foreach ($arIndexes as $index_name => $arColumns)
539 $arIndexes[$index_name] = implode(
',', $arColumns);
543 foreach ($arTableJoins as $arColumns)
550 foreach ($arCombosToTest as $arComboColumns)
552 if (!empty($arComboColumns))
554 $index2test = implode(
',', $arComboColumns);
556 foreach ($arIndexes as $index_name => $index_columns)
558 if (mb_substr($index_columns, 0, mb_strlen($index2test)) === $index2test)
562 ||
count(explode(
',', $index_found)) <
count(explode(
',', $index2test))
565 $index_found = $index2test;
575 $arSuggest[] = $suggest_table->alias .
':' . $suggest_table->name .
':' . implode(
',', $arColumns);
580 if (!empty($arSuggest))
593 foreach ($array as $element)
595 foreach ($results as $combination)
597 $results[] = array_merge([$element], $combination);
605 $arColumns = array_unique($arColumns);
606 while (mb_strlen(implode(
',', $arColumns)) > 250)
611 array_pop($arColumns);
618 if ($table_alias ===
false)
620 return !empty($this->where->joins);
623 foreach ($this->where->joins as $join)
625 if ($this->
cmp($join->left_table, $table_alias))
629 elseif ($this->
cmp($join->right_table, $table_alias))
638 public function find_value($table_name, $column_name)
642 foreach ($this->from->tables as $table)
644 if ($table->name === $table_name)
646 $table_alias = $table->alias;
648 foreach ($this->where->joins as $join)
651 $join->left_table === $table_alias
652 && $join->left_column === $column_name
653 && $join->right_const !==
''
656 return $join->right_const;
659 $join->right_table === $table_alias
660 && $join->right_column === $column_name
661 && $join->left_const !==
''
664 return $join->left_const;
668 foreach ($this->from->joins as $join)
671 $join->left_table === $table_alias
672 && $join->left_column === $column_name
673 && $join->right_const !==
''
676 return $join->right_const;
679 $join->right_table === $table_alias
680 && $join->right_column === $column_name
681 && $join->left_const !==
''
684 return $join->left_const;
693 public function find_join($table_name, $column_name)
696 $suggest_table =
null;
698 foreach ($this->from->tables as $table)
700 if (
$table->name === $table_name)
706 if (!isset($suggest_table))
710 $table_alias = $suggest_table->alias;
712 foreach ($this->where->joins as $join)
715 $join->left_table === $table_alias
716 && $join->left_column === $column_name
717 && $join->right_table !==
''
720 return $join->right_table .
'.' . $join->right_column;
723 $join->right_table === $table_alias
724 && $join->right_column === $column_name
725 && $join->left_table !==
''
728 return $join->left_table .
'.' . $join->left_column;
732 foreach ($this->from->joins as $join)
735 $join->left_table === $table_alias
736 && $join->left_column === $column_name
737 && $join->right_table !==
''
740 return $join->right_table .
'.' . $join->right_column;
743 $join->right_table === $table_alias
744 && $join->right_column === $column_name
745 && $join->left_table !==
''
748 return $join->left_table .
'.' . $join->left_column;
757 return preg_replace(
'/(
758 "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # match double quoted string
760 \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # match single quoted string
762 (?s:\\/\\*.*?\\*\\/) # multi line comments
764 \\/\\/.*?\\n # single line comments
766 (?<![A-Za-z_])([0-9]+\\.[0-9]+|[0-9]+)(?![A-Za-z_]) # an number
768 (?i:\\sIN\\s*\\(\\s*[0-9.]+(?:\\s*,\\s*[0-9.])*\\s*\\)) # in (1, 2, 3)
770 (?i:\\sIN\\s*\\(\\s*[\'].+?[\'](?:\\s*,\\s*[\'].+?[\'])*\\s*\\)) # in (\'a\', \'b\', \'c\')
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)