1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle\Builder; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\Exception; |
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
11
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
12
|
|
|
use Yiisoft\Db\QueryBuilder\Condition\Interface\InConditionInterface; |
13
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
14
|
|
|
|
15
|
|
|
use function array_slice; |
16
|
|
|
use function array_unshift; |
17
|
|
|
use function count; |
18
|
|
|
use function is_array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Build an object of {@see \Yiisoft\Db\QueryBuilder\Condition\InCondition} into SQL expressions for Oracle Server. |
22
|
|
|
*/ |
23
|
|
|
final class InConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The Method builds the raw SQL from the $expression that won't be additionally escaped or quoted. |
27
|
|
|
* |
28
|
|
|
* @param ExpressionInterface $expression The expression to be built. |
29
|
|
|
* @param array $params The binding parameters. |
30
|
|
|
* |
31
|
|
|
* @throws Exception |
32
|
|
|
* @throws InvalidArgumentException |
33
|
|
|
* @throws InvalidConfigException |
34
|
|
|
* @throws NotSupportedException |
35
|
|
|
* |
36
|
|
|
* @return string The raw SQL that won't be additionally escaped or quoted. |
37
|
|
|
* |
38
|
|
|
* @psalm-param InConditionInterface $expression |
39
|
|
|
*/ |
40
|
37 |
|
public function build(ExpressionInterface $expression, array &$params = []): string |
41
|
|
|
{ |
42
|
37 |
|
$splitCondition = $this->splitCondition($expression, $params); |
43
|
|
|
|
44
|
37 |
|
return $splitCondition ?? parent::build($expression, $params); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Oracle DBMS doesn't support more than 1000 parameters in `IN` condition. |
49
|
|
|
* |
50
|
|
|
* This method splits long `IN` condition into series of smaller ones. |
51
|
|
|
* |
52
|
|
|
* @param array $params The binding parameters. |
53
|
|
|
* |
54
|
|
|
* @throws Exception |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
* @throws InvalidConfigException |
57
|
|
|
* @throws NotSupportedException |
58
|
|
|
* |
59
|
|
|
* @return string|null `null` when split isn't required. Otherwise - built SQL condition. |
60
|
|
|
*/ |
61
|
37 |
|
protected function splitCondition(InConditionInterface $condition, array &$params): string|null |
62
|
|
|
{ |
63
|
37 |
|
$operator = $condition->getOperator(); |
64
|
37 |
|
$values = $condition->getValues(); |
65
|
37 |
|
$column = $condition->getColumn(); |
66
|
|
|
|
67
|
37 |
|
if (!is_array($values)) { |
68
|
20 |
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
17 |
|
$maxParameters = 1000; |
72
|
17 |
|
$count = count($values); |
73
|
|
|
|
74
|
17 |
|
if ($count <= $maxParameters) { |
75
|
17 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$slices = []; |
79
|
|
|
|
80
|
|
|
for ($i = 0; $i < $count; $i += $maxParameters) { |
81
|
|
|
$slices[] = $this->queryBuilder->createConditionFromArray( |
|
|
|
|
82
|
|
|
[$operator, $column, array_slice($values, $i, $maxParameters)] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
array_unshift($slices, ($operator === 'IN') ? 'OR' : 'AND'); |
87
|
|
|
|
88
|
|
|
return $this->queryBuilder->buildCondition($slices, $params); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|