Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Oracle SQL queries with IN condition and more than 1000 params #14065

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Yii Framework 2 Change Log
- Enh #13981: `yii\caching\Cache::getOrSet()` now supports both `Closure` and `callable` (silverfire)
- Enh #13911: Significantly enhanced MSSQL schema reading performance (paulzi, WebdevMerlion)
- Enh #14059: Removed unused AR instantiating for calling of static methods (ElisDN)
- Bug #10305: Oracle SQL queries with `IN` condition and more than 1000 parameters are working now (silverfire)

2.0.11.2 February 08, 2017
--------------------------
Expand Down
52 changes: 52 additions & 0 deletions framework/db/oci/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,56 @@ public function buildLikeCondition($operator, $operands, &$params)
}
return parent::buildLikeCondition($operator, $operands, $params);
}

public function buildInCondition($operator, $operands, &$params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add @inheritdoc.

{
$splitCondition = $this->splitInCondition($operator, $operands, $params);
if ($splitCondition !== null) {
return $splitCondition;
}

return parent::buildInCondition($operator, $operands, $params);
}

/**
* Oracle DBMS does not support more than 1000 parameters in `IN` condition.
* This method splits long `IN` condition into series of smaller ones.
*
* @param string $operator
* @param array $operands
* @param array $params
* @return null|string null when split is not required. Otherwise - built SQL condition.
* @throws Exception
* @since 2.0.12
*/
protected function splitInCondition($operator, $operands, &$params)
{
if (!isset($operands[0], $operands[1])) {
throw new Exception("Operator '$operator' requires two operands.");
}

list($column, $values) = $operands;

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

$condition = [($operator === 'IN') ? 'OR' : 'AND'];
for ($i = 0; $i < $count; $i += $maxParameters) {
$condition[] = [$operator, $column, array_slice($values, $i, $maxParameters)];
}

return $this->buildCondition(['AND', $condition], $params);
}

}
2 changes: 0 additions & 2 deletions tests/framework/db/mssql/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace yiiunit\framework\db\mssql;

use yii\db\Expression;
use yii\db\mssql\Schema;
use yii\db\Query;

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/framework/db/oci/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yiiunit\framework\db\oci;

use yii\db\oci\Schema;
use yiiunit\data\base\TraversableObject;

/**
* @group db
Expand Down Expand Up @@ -86,4 +87,52 @@ public function likeConditionProvider()
$this->likeParameterReplacements[$encodedBackslash] = '\\';
return parent::likeConditionProvider();
}

public function conditionProvider()
{
return array_merge(parent::conditionProvider(), [
[
['in', 'id', range(0, 2500)],

' ('
. '([[id]] IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 0, 999)) . '))'
. ' OR ([[id]] IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 1000, 1999)) . '))'
. ' OR ([[id]] IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 2000, 2500)) .'))'
. ')',

array_flip($this->generateSprintfSeries(':qp%d', 0, 2500))
],
[
['not in', 'id', range(0, 2500)],

'('
. '([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 0, 999)) . '))'
. ' AND ([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 1000, 1999)) . '))'
. ' AND ([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 2000, 2500)) .'))'
. ')',

array_flip($this->generateSprintfSeries(':qp%d', 0, 2500))
],
[
['not in', 'id', new TraversableObject(range(0, 2500))],

'('
. '([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 0, 999)) . '))'
. ' AND ([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 1000, 1999)) . '))'
. ' AND ([[id]] NOT IN (' . implode(', ', $this->generateSprintfSeries(':qp%d', 2000, 2500)) .'))'
. ')',

array_flip($this->generateSprintfSeries(':qp%d', 0, 2500))
],
]);
}

protected function generateSprintfSeries ($pattern, $from, $to) {
$items = [];
for ($i = $from; $i <= $to; $i++) {
$items[] = sprintf($pattern, $i);
}

return $items;
}
}