类 yii\db\oci\conditions\InConditionBuilder
公共方法
方法 | 描述 | 定义于 |
---|---|---|
__construct() | ExpressionBuilderTrait 构造函数。 | yii\db\ExpressionBuilderTrait |
build() | 该方法根据 $expression 构建原始 SQL,该 SQL 不会被额外转义或引用。 | yii\db\oci\conditions\InConditionBuilder |
受保护方法
方法 | 描述 | 定义于 |
---|---|---|
buildCompositeInCondition() | 构建 IN 条件的 SQL。 | yii\db\conditions\InConditionBuilder |
buildSubqueryInCondition() | 构建 IN 条件的 SQL。 | yii\db\conditions\InConditionBuilder |
buildValues() | 构建将在 yii\db\conditions\InCondition 中使用的 $values | yii\db\conditions\InConditionBuilder |
getNullCondition() | 根据操作符构建列的 is null/is not null 条件 | yii\db\conditions\InConditionBuilder |
getRawValuesFromTraversableObject() | yii\db\conditions\InConditionBuilder | |
splitCondition() | Oracle DBMS 不支持在 IN 条件中使用超过 1000 个参数。 |
yii\db\oci\conditions\InConditionBuilder |
方法详情
定义于: yii\db\ExpressionBuilderTrait::__construct()
ExpressionBuilderTrait 构造函数。
public void __construct ( yii\db\QueryBuilder $queryBuilder ) | ||
$queryBuilder | yii\db\QueryBuilder |
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
该方法根据 $expression 构建原始 SQL,该 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 = [])
{
$splitCondition = $this->splitCondition($expression, $params);
if ($splitCondition !== null) {
return $splitCondition;
}
return parent::build($expression, $params);
}
定义于: yii\db\conditions\InConditionBuilder::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) . ')';
}
定义于: yii\db\conditions\InConditionBuilder::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";
}
定义于: yii\db\conditions\InConditionBuilder::buildValues()
构建将在 yii\db\conditions\InCondition 中使用的 $values
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;
}
定义于: yii\db\conditions\InConditionBuilder::getNullCondition()
根据操作符构建列的 is null/is not null 条件
受保护的 字符串 getNullCondition ( $operator, $column ) | ||
$operator | string | |
$column | string | |
返回值 | string |
空值或非空值条件 |
---|
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 ( 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;
}
Oracle DBMS 不支持在 IN
条件中使用超过 1000 个参数。
此方法将长的 IN
条件拆分为一系列较小的条件。
受保护的 字符串|空 splitCondition ( yii\db\conditions\InCondition $condition, &$params ) | ||
$condition | yii\db\ExpressionInterface|yii\db\conditions\InCondition |
要构建的表达式。 |
$params | array |
绑定参数。 |
返回值 | 字符串|空 |
当不需要拆分时为 Null。否则 - 生成的 SQL 条件。 |
---|
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);
}
为了发表评论,请注册 或 登录。