Passed
Pull Request — master (#185)
by Wilmer
14:30 queued 07:28
created

InConditionBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 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\Builder\InConditionBuilder as AbstractInConditionBuilder;
13
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
14
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
15
16
use function array_slice;
17
use function array_unshift;
18
use function count;
19
use function is_array;
20
21
/**
22
 * InConditionBuilder builds conditions for {@see `\Yiisoft\Db\QueryBuilder\Condition\InCondition`} IN operator for
23
 * Oracle Server.
24
 */
25
final class InConditionBuilder extends AbstractInConditionBuilder
26
{
27 37
    public function __construct(private QueryBuilderInterface $queryBuilder)
28
    {
29 37
        parent::__construct($queryBuilder);
30
    }
31
32
    /**
33
     * Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.
34
     *
35
     * @param ExpressionInterface $expression The expression to be built.
36
     * @param array $params The binding parameters.
37
     *
38
     * @throws Exception
39
     * @throws InvalidArgumentException
40
     * @throws InvalidConfigException
41
     * @throws NotSupportedException
42
     *
43
     * @return string The raw SQL that will not be additionally escaped or quoted.
44
     */
45 37
    public function build(ExpressionInterface $expression, array &$params = []): string
46
    {
47
        /** @var Incondition $expression */
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(InCondition $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