0 follower

类 yii\db\conditions\InConditionBuilder

继承yii\db\conditions\InConditionBuilder
实现yii\db\ExpressionBuilderInterface
使用特性yii\db\ExpressionBuilderTrait
子类yii\db\mssql\conditions\InConditionBuilder, yii\db\oci\conditions\InConditionBuilder, yii\db\sqlite\conditions\InConditionBuilder
可用版本2.0.14
源代码 https://github.com/yiisoft/yii2/blob/master/framework/db/conditions/InConditionBuilder.php

类 InConditionBuilder 构建 yii\db\conditions\InCondition 对象

受保护属性

隐藏继承属性

属性 类型 描述 定义于

公共方法

隐藏继承方法

方法 描述 定义于
__construct() ExpressionBuilderTrait 构造函数。 yii\db\ExpressionBuilderTrait
build() 此方法从 $expression 中构建原始 SQL,不会额外转义或引用。 yii\db\conditions\InConditionBuilder

方法详情

隐藏继承方法

__construct() 公共方法

定义于: yii\db\ExpressionBuilderTrait::__construct()

ExpressionBuilderTrait 构造函数。

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

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

            
build() 公共方法

此方法从 $expression 中构建原始 SQL,不会额外转义或引用。

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

要构建的表达式。

$params array

绑定参数。

返回 string

原始 SQL,不会额外转义或引用。

                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() 受保护方法

构建 IN 条件的 SQL。

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

SQL

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

            
buildSubqueryInCondition() 受保护方法

构建 IN 条件的 SQL。

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

SQL

                protected function buildSubqueryInCondition($operator, $columns, $values, &$params)
{
    $sql = $this->queryBuilder->buildExpression($values, $params);
    if (is_array($columns)) {
        foreach ($columns as $i => $col) {
            if ($col instanceof Expression) {
                $col = $col->expression;
            }
            if (strpos($col, '(') === false) {
                $columns[$i] = $this->queryBuilder->db->quoteColumnName($col);
            }
        }
        return '(' . implode(', ', $columns) . ") $operator $sql";
    }
    if ($columns instanceof Expression) {
        $columns = $columns->expression;
    }
    if (strpos($columns, '(') === false) {
        $columns = $this->queryBuilder->db->quoteColumnName($columns);
    }
    return "$columns $operator $sql";
}

            
buildValues() 受保护方法

构建 $values 用于 yii\db\conditions\InCondition

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

绑定参数

返回 array

已准备好的 SQL 占位符

                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() 受保护方法 (可用版本 2.0.31)

根据操作符构建列的 is null/is not null 条件

protected string getNullCondition ( $operator, $column )
$operator string
$column string
返回 string

Is null 或 is not null 条件

                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() 受保护方法 (自版本 2.0.31 起可用)

protected array getRawValuesFromTraversableObject ( Traversable $traversableObject )
$traversableObject 可遍历对象
返回 array

原始值

                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;
}