Passed
Pull Request — master (#185)
by Wilmer
23:37 queued 16:57
created

InConditionBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 71
ccs 15
cts 22
cp 0.6818
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 5 1
A __construct() 0 3 1
A splitCondition() 0 28 5
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
 * Builds conditions for {@see `\Yiisoft\Db\QueryBuilder\Condition\InCondition`} IN operator for Oracle Server.
22
 */
23
final class InConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder
24
{
25 37
    public function __construct(private QueryBuilderInterface $queryBuilder)
26
    {
27 37
        parent::__construct($queryBuilder);
28
    }
29
30
    /**
31
     * Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.
32
     *
33
     * @param ExpressionInterface $expression The expression to be built.
34
     * @param array $params The binding parameters.
35
     *
36
     * @throws Exception
37
     * @throws InvalidArgumentException
38
     * @throws InvalidConfigException
39
     * @throws NotSupportedException
40
     *
41
     * @return string The raw SQL that will not be additionally escaped or quoted.
42
     *
43
     * @psalm-param InConditionInterface $expression
44
     */
45 37
    public function build(ExpressionInterface $expression, array &$params = []): string
46
    {
47 37
        $splitCondition = $this->splitCondition($expression, $params);
48
49 37
        return $splitCondition ?? parent::build($expression, $params);
50
    }
51
52
    /**
53
     * Oracle DBMS does not support more than 1000 parameters in `IN` condition.
54
     *
55
     * This method splits long `IN` condition into series of smaller ones.
56
     *
57
     * @param array $params The binding parameters.
58
     *
59
     * @throws Exception
60
     * @throws InvalidArgumentException
61
     * @throws InvalidConfigException
62
     * @throws NotSupportedException
63
     *
64
     * @return string|null `null` when split is not required. Otherwise - built SQL condition.
65
     */
66 37
    protected function splitCondition(InConditionInterface $condition, array &$params): string|null
67
    {
68 37
        $operator = $condition->getOperator();
69 37
        $values = $condition->getValues();
70 37
        $column = $condition->getColumn();
71
72 37
        if (!is_array($values)) {
73 20
            return null;
74
        }
75
76 17
        $maxParameters = 1000;
77 17
        $count = count($values);
78
79 17
        if ($count <= $maxParameters) {
80 17
            return null;
81
        }
82
83
        $slices = [];
84
85
        for ($i = 0; $i < $count; $i += $maxParameters) {
86
            $slices[] = $this->queryBuilder->createConditionFromArray(
87
                [$operator, $column, array_slice($values, $i, $maxParameters)]
88
            );
89
        }
90
91
        array_unshift($slices, ($operator === 'IN') ? 'OR' : 'AND');
92
93
        return $this->queryBuilder->buildCondition($slices, $params);
94
    }
95
}
96