yii\sphinx\QueryBuilder::buildInCondition PHP Method

buildInCondition() public method

Creates an SQL expressions with the IN operator.
public buildInCondition ( IndexSchema[] $indexes, string $operator, array $operands, array &$params ) : string
$indexes IndexSchema[] list of indexes, which affected by query
$operator string the operator to use (e.g. `IN` or `NOT IN`)
$operands array the first operand is the column name. If it is an array a composite IN condition will be generated. The second operand is an array of values that column value should be among. If it is an empty array the generated expression will be a `false` value if operator is `IN` and empty if operator is `NOT IN`.
$params array the binding parameters to be populated
return string the generated SQL expression
    public function buildInCondition($indexes, $operator, $operands, &$params)
    {
        if (!isset($operands[0], $operands[1])) {
            throw new Exception("Operator '{$operator}' requires two operands.");
        }
        list($column, $values) = $operands;
        if ($values === []) {
            if ($operator === 'IN') {
                if (empty($column)) {
                    throw new Exception("Operator '{$operator}' requires column being specified.");
                }
                $column = $this->db->quoteColumnName($column);
                return "({$column} = 0 AND {$column} = 1)";
            }
            return '';
        }
        if ($column === []) {
            return '';
        }
        if ($values instanceof Query) {
            // sub-query
            list($sql, $params) = $this->build($values, $params);
            $column = (array) $column;
            if (is_array($column)) {
                foreach ($column as $i => $col) {
                    if (strpos($col, '(') === false) {
                        $column[$i] = $this->db->quoteColumnName($col);
                    }
                }
                return '(' . implode(', ', $column) . ") {$operator} ({$sql})";
            } else {
                if (strpos($column, '(') === false) {
                    $column = $this->db->quoteColumnName($column);
                }
                return "{$column} {$operator} ({$sql})";
            }
        }
        $values = (array) $values;
        if (count($column) > 1) {
            return $this->buildCompositeInCondition($indexes, $operator, $column, $values, $params);
        }
        if (is_array($column)) {
            $column = reset($column);
        }
        foreach ($values as $i => $value) {
            if (is_array($value)) {
                $value = isset($value[$column]) ? $value[$column] : null;
            }
            $values[$i] = $this->composeColumnValue($indexes, $column, $value, $params);
        }
        if (strpos($column, '(') === false) {
            $column = $this->db->quoteColumnName($column);
        }
        if (count($values) > 1) {
            return "{$column} {$operator} (" . implode(', ', $values) . ')';
        } else {
            $operator = $operator === 'IN' ? '=' : '<>';
            return $column . $operator . reset($values);
        }
    }