Class yii\db\mssql\conditions\InConditionBuilder

Inheritanceyii\db\mssql\conditions\InConditionBuilder » yii\db\conditions\InConditionBuilder
Implementsyii\db\ExpressionBuilderInterface
Uses Traitsyii\db\ExpressionBuilderTrait
Available since version2.0.14
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/mssql/conditions/InConditionBuilder.php

Protected Properties

Hide inherited properties

Property Type Description Defined By
$queryBuilder yii\db\QueryBuilder yii\db\ExpressionBuilderTrait

Public Methods

Hide inherited methods

Method Description Defined By
__construct() ExpressionBuilderTrait constructor. yii\db\ExpressionBuilderTrait
build() Method builds the raw SQL from the $expression that will not be additionally escaped or quoted. yii\db\conditions\InConditionBuilder

Method Details

Hide inherited methods

__construct() public method

Defined in: yii\db\ExpressionBuilderTrait::__construct()

ExpressionBuilderTrait constructor.

public void __construct ( yii\db\QueryBuilder $queryBuilder )
$queryBuilder yii\db\QueryBuilder

                public function __construct(QueryBuilder $queryBuilder)
{
    $this->queryBuilder = $queryBuilder;
}

            
build() public method

Defined in: yii\db\conditions\InConditionBuilder::build()

Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.

public string build ( yii\db\ExpressionInterface $expression, array &$params = [] )
$expression yii\db\ExpressionInterface|yii\db\conditions\InCondition

The expression to be built.

$params array

The binding parameters.

return string

The raw SQL that will not be additionally escaped or quoted.

                public function build(ExpressionInterface $expression, array &$params = [])
{
    $operator = strtoupper($expression->getOperator());
    $column = $expression->getColumn();
    $values = $expression->getValues();
    if ($column === []) {
        // no columns to test against
        return $operator === 'IN' ? '0=1' : '';
    }
    if ($values instanceof Query) {
        return $this->buildSubqueryInCondition($operator, $column, $values, $params);
    }
    if (!is_array($values) && !$values instanceof \Traversable) {
        // ensure values is an array
        $values = (array) $values;
    }
    if (is_array($column)) {
        if (count($column) > 1) {
            return $this->buildCompositeInCondition($operator, $column, $values, $params);
        }
        $column = reset($column);
    }
    if ($column instanceof \Traversable) {
        if (iterator_count($column) > 1) {
            return $this->buildCompositeInCondition($operator, $column, $values, $params);
        }
        $column->rewind();
        $column = $column->current();
    }
    if ($column instanceof Expression) {
        $column = $column->expression;
    }
    if (is_array($values)) {
        $rawValues = $values;
    } elseif ($values instanceof \Traversable) {
        $rawValues = $this->getRawValuesFromTraversableObject($values);
    }
    $nullCondition = null;
    $nullConditionOperator = null;
    if (isset($rawValues) && in_array(null, $rawValues, true)) {
        $nullCondition = $this->getNullCondition($operator, $column);
        $nullConditionOperator = $operator === 'IN' ? 'OR' : 'AND';
    }
    $sqlValues = $this->buildValues($expression, $values, $params);
    if (empty($sqlValues)) {
        if ($nullCondition === null) {
            return $operator === 'IN' ? '0=1' : '';
        }
        return $nullCondition;
    }
    if (strpos($column, '(') === false) {
        $column = $this->queryBuilder->db->quoteColumnName($column);
    }
    if (count($sqlValues) > 1) {
        $sql = "$column $operator (" . implode(', ', $sqlValues) . ')';
    } else {
        $operator = $operator === 'IN' ? '=' : '<>';
        $sql = $column . $operator . reset($sqlValues);
    }
    return $nullCondition !== null && $nullConditionOperator !== null
        ? sprintf('%s %s %s', $sql, $nullConditionOperator, $nullCondition)
        : $sql;
}

            
buildCompositeInCondition() protected method

Builds SQL for IN condition.

protected string buildCompositeInCondition ( $operator, $columns, $values, &$params )
$operator string
$columns array|Traversable
$values array
$params array
return string

SQL

                protected function buildCompositeInCondition($operator, $columns, $values, &$params)
{
    $quotedColumns = [];
    foreach ($columns as $i => $column) {
        if ($column instanceof Expression) {
            $column = $column->expression;
        }
        $quotedColumns[$i] = strpos($column, '(') === false ? $this->queryBuilder->db->quoteColumnName($column) : $column;
    }
    $vss = [];
    foreach ($values as $value) {
        $vs = [];
        foreach ($columns as $i => $column) {
            if ($column instanceof Expression) {
                $column = $column->expression;
            }
            if (isset($value[$column])) {
                $phName = $this->queryBuilder->bindParam($value[$column], $params);
                $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
            } else {
                $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
            }
        }
        $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
    }
    return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
}

            
buildSubqueryInCondition() protected method

Builds SQL for IN condition.

protected string buildSubqueryInCondition ( $operator, $columns, $values, &$params )
$operator string
$columns array|string
$values yii\db\Query
$params array
return string

SQL

throws yii\base\NotSupportedException

if $columns is an array

                protected function buildSubqueryInCondition($operator, $columns, $values, &$params)
{
    if (is_array($columns)) {
        throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
    }
    return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
}

            
buildValues() protected method
protected array buildValues ( yii\db\conditions\ConditionInterface $condition, $values, &$params )
$condition yii\db\conditions\ConditionInterface|yii\db\conditions\InCondition
$values array
$params array

The binding parameters

return array

Of prepared for SQL placeholders

                protected function buildValues(ConditionInterface $condition, $values, &$params)
{
    $sqlValues = [];
    $column = $condition->getColumn();
    if (is_array($column)) {
        $column = reset($column);
    }
    if ($column instanceof \Traversable) {
        $column->rewind();
        $column = $column->current();
    }
    if ($column instanceof Expression) {
        $column = $column->expression;
    }
    foreach ($values as $i => $value) {
        if (is_array($value) || $value instanceof \ArrayAccess) {
            $value = isset($value[$column]) ? $value[$column] : null;
        }
        if ($value === null) {
            continue;
        } elseif ($value instanceof ExpressionInterface) {
            $sqlValues[$i] = $this->queryBuilder->buildExpression($value, $params);
        } else {
            $sqlValues[$i] = $this->queryBuilder->bindParam($value, $params);
        }
    }
    return $sqlValues;
}

            
getNullCondition() protected method (available since version 2.0.31)

Defined in: yii\db\conditions\InConditionBuilder::getNullCondition()

Builds is null/is not null condition for column based on operator

protected string getNullCondition ( $operator, $column )
$operator string
$column string
return string

Is null or is not null condition

                protected function getNullCondition($operator, $column) {
    $column = $this->queryBuilder->db->quoteColumnName($column);
    if ($operator === 'IN') {
        return sprintf('%s IS NULL', $column);
    }
    return sprintf('%s IS NOT NULL', $column);
}

            
getRawValuesFromTraversableObject() protected method (available since version 2.0.31)
protected array getRawValuesFromTraversableObject ( Traversable $traversableObject )
$traversableObject Traversable
return array

Raw values

                protected function getRawValuesFromTraversableObject(\Traversable $traversableObject)
{
    $rawValues = [];
    foreach ($traversableObject as $value) {
        if (is_array($value)) {
            $values = array_values($value);
            $rawValues = array_merge($rawValues, $values);
        } else {
            $rawValues[] = $value;
        }
    }
    return $rawValues;
}