59 $this->
logic = static::LOGIC_AND;
77 if (!in_array(strtolower(
$logic), [static::LOGIC_AND, static::LOGIC_OR],
true))
96 $this->isNegative = (bool) $negative;
128 if (count($filter) == 1 && $filter[0] instanceof
ConditionTree)
130 $this->conditions[] = $filter[0];
135 if (count($filter) == 1 && $filter[0] instanceof
Condition)
137 $this->conditions[] = $filter[0];
142 if (count($filter) == 1 && is_array($filter[0]))
144 foreach ($filter[0] as $condition)
147 call_user_func_array(array($this,
'where'), $condition);
153 if (count($filter) == 3)
156 list($column, $operator, $value) = $filter;
158 elseif (count($filter) == 2)
161 list($column, $value) = $filter;
164 elseif (count($filter) == 1)
167 $column = $filter[0];
178 if (!isset($operators[$operator]))
184 $this->conditions[] =
new Condition($column, $operator, $value);
199 $subFilter =
new static();
200 call_user_func_array(array($subFilter,
'where'), $filter);
202 $this->conditions[] = $subFilter->negative();
217 if (count($filter) == 3)
219 list($column, $operator, $value) = $filter;
221 elseif (count($filter) == 2)
223 list($column, $value) = $filter;
235 $this->
where($column, $operator, $value);
249 $this->conditions[] =
new Condition($column,
'=',
null);
263 $this->conditions[] =
new Condition($column,
'<>',
null);
280 $this->conditions[] =
new Condition($column,
'in', $values);
297 $subFilter =
new static();
298 $this->conditions[] = $subFilter->whereIn($column, $values)->negative();
314 $this->conditions[] =
new Condition($column,
'between', array($valueMin, $valueMax));
331 $subFilter =
new static();
332 $this->conditions[] = $subFilter->whereBetween($column, $valueMin, $valueMax)->negative();
347 $this->conditions[] =
new Condition($column,
'like', $value);
363 $subFilter =
new static();
364 $this->conditions[] = $subFilter->whereLike($column, $value)->negative();
378 $this->conditions[] =
new Condition(
null,
'exists', $query);
396 $subFilter =
new static();
397 $this->conditions[] = $subFilter->whereExists($query)->negative();
416 $this->conditions[] =
new Condition($column,
'match', $value);
432 $subFilter =
new static();
433 $this->conditions[] = $subFilter->whereMatch($column, $value)->negative();
453 $tmpName =
'TMP_'.$randomSequence->randString(10);
457 ->configureValueType(BooleanField::class);
460 $this->
where($tmpField,
'expr',
true);
474 public function getSql(
$chains)
482 foreach ($this->conditions as $condition)
487 $subFilter = $condition;
488 $sql = $subFilter->getSql(
$chains);
490 if (count($subFilter->getConditions()) > 1)
498 $columnSqlDefinition =
null;
502 if ($condition->getColumn() !==
null)
504 $chain =
$chains[$condition->getDefinition()];
505 $columnSqlDefinition = $chain->getSqlDefinition();
508 $columnField = $chain->getLastElement()->getValue();
512 if (in_array($condition->getOperator(), array(
'in',
'between'),
true) && is_array($condition->getValue()))
515 $finalValue = $this->
convertValues($condition->getValue(), $columnField);
519 $finalValue = $this->convertValue($condition->getValue(), $columnField);
524 $operator = $operators[$condition->getOperator()];
527 $sql = call_user_func(
528 array(
'Bitrix\Main\ORM\Query\Filter\Operator', $operator),
529 $columnSqlDefinition, $finalValue
541 if (!empty($finalSql))
544 $sql = join(
" ".strtoupper($this->
logic).
" ", $finalSql);
547 if ($this->isNegative)
549 $sql = count($finalSql) > 1 ?
"NOT ({$sql})" :
"NOT {$sql}";
578 $this->conditions[] = $condition;
592 return !empty($this->conditions);
605 foreach ($this->conditions as $k => $condition)
607 if ($condition === $currentCondition)
609 $this->conditions[$k] = $newCondition;
626 foreach ($this->conditions as $k => $_condition)
628 if ($condition == $_condition)
630 unset($this->conditions[$k]);
643 $this->conditions = [];
656 protected function convertValue($value,
IReadable $field =
null)
661 return $value->compile();
665 if ($value instanceof
Query)
667 return $value->getQuery();
673 return $value->getSql($this->chains);
679 return new Expressions\NullExpression;
682 if ($value instanceof Expressions\ColumnExpression)
685 $valueChain = $this->chains[$value->getDefinition()];
686 return $valueChain->getSqlDefinition();
689 return $field->convertValueToDb($value);
705 foreach ($values as $k => $value)
707 $values[$k] = $this->convertValue($value, $field);
715 $newConditions = array();
717 foreach ($this->conditions as $condition)
719 $newConditions[] = clone $condition;
722 $this->conditions = $newConditions;
730 if (!isset(static::$randomSequence))
735 return static::$randomSequence;
750 if (isset($filter[
'logic']))
752 $conditionTree->logic($filter[
'logic']);
753 unset($filter[
'logic']);
756 if (isset($filter[
'negative']))
758 $conditionTree->negative($filter[
'negative']);
759 unset($filter[
'negative']);
762 foreach ($filter as $condition)
764 if (isset($condition[0]) && is_array($condition[0]))
766 $conditionTree->where(static::createFromArray($condition));
771 $valueKey = key(array_reverse($condition,
true));
772 $valueElement = $condition[$valueKey];
774 if (is_array($valueElement))
776 if (isset($valueElement[
'value']))
778 $value = $valueElement[
'value'];
780 elseif (isset($valueElement[
'column']))
787 foreach ($valueElement as $k => $singleValue)
789 if (is_array($singleValue))
791 if (isset($singleValue[
'value']))
793 $valueElement[$k] = $singleValue[
'value'];
795 elseif (isset($singleValue[
'column']))
802 $value = $valueElement;
805 $condition[$valueKey] = $value;
808 $conditionTree->where(...$condition);
812 return $conditionTree;
whereNotIn($column, $values)
whereNotMatch($column, $value)
whereExpr($expr, $arguments)
whereNotBetween($column, $valueMin, $valueMax)
whereLike($column, $value)
static createFromArray($filter)
static RandomSequence $randomSequence
whereNotLike($column, $value)
whereBetween($column, $valueMin, $valueMax)
whereIn($column, $values)
whereMatch($column, $value)
replaceCondition($currentCondition, $newCondition)
removeCondition($condition)
static getRandomSequence()
convertValues($values, IReadable $field=null)