0 follower

Class yii\db\oci\conditions\InConditionBuilder

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

Protected Properties

Hide inherited properties

Property Type Description Defined By

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\oci\conditions\InConditionBuilder

Protected Methods

Hide inherited methods

Method Description Defined By
buildCompositeInCondition() Builds SQL for IN condition. yii\db\conditions\InConditionBuilder
buildSubqueryInCondition() Builds SQL for IN condition. yii\db\conditions\InConditionBuilder
buildValues() Builds $values to be used in yii\db\conditions\InCondition yii\db\conditions\InConditionBuilder
getNullCondition() Builds is null/is not null condition for column based on operator yii\db\conditions\InConditionBuilder
getRawValuesFromTraversableObject() yii\db\conditions\InConditionBuilder
splitCondition() Oracle DBMS does not support more than 1000 parameters in IN condition. yii\db\oci\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

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 = [])
{
    $splitCondition = $this->splitCondition($expression, $params);
    if ($splitCondition !== null) {
        return $splitCondition;
    }
    return parent::build($expression, $params);
}

            
buildCompositeInCondition() protected method
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)
{
    $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() protected method

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

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

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

            
splitCondition() protected method

Oracle DBMS does not support more than 1000 parameters in IN condition.

This method splits long IN condition into series of smaller ones.

protected string|null splitCondition ( yii\db\conditions\InCondition $condition, &$params )
$condition yii\db\ExpressionInterface|yii\db\conditions\InCondition

The expression to be built.

$params array

The binding parameters.

return string|null

Null when split is not required. Otherwise - built SQL condition.

                protected function splitCondition(InCondition $condition, &$params)
{
    $operator = $condition->getOperator();
    $values = $condition->getValues();
    $column = $condition->getColumn();
    if ($values instanceof \Traversable) {
        $values = iterator_to_array($values);
    }
    if (!is_array($values)) {
        return null;
    }
    $maxParameters = 1000;
    $count = count($values);
    if ($count <= $maxParameters) {
        return null;
    }
    $slices = [];
    for ($i = 0; $i < $count; $i += $maxParameters) {
        $slices[] = $this->queryBuilder->createConditionFromArray([$operator, $column, array_slice($values, $i, $maxParameters)]);
    }
    array_unshift($slices, ($operator === 'IN') ? 'OR' : 'AND');
    return $this->queryBuilder->buildCondition($slices, $params);
}