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