InConditionBuilder::buildSubqueryInCondition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\Builder;
6
7
use Iterator;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
use Yiisoft\Db\Expression\ExpressionInterface;
13
14
use function implode;
15
use function is_array;
16
use function str_contains;
17
18
/**
19
 * Build an object of {@see \Yiisoft\Db\QueryBuilder\Condition\InCondition} into SQL expressions for SQLite Server.
20
 */
21
final class InConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder
22
{
23
    /**
24
     * Builds SQL for IN condition.
25
     *
26
     * @throws Exception
27
     * @throws InvalidArgumentException
28
     * @throws InvalidConfigException
29
     * @throws NotSupportedException
30
     */
31 3
    protected function buildSubqueryInCondition(
32
        string $operator,
33
        iterable|string|Iterator $columns,
34
        ExpressionInterface $values,
35
        array &$params = []
36
    ): string {
37 3
        if (is_array($columns)) {
38 1
            throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
39
        }
40
41 2
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
42
    }
43
44
    /**
45
     * Builds SQL for IN condition.
46
     *
47
     * @throws Exception
48
     * @throws InvalidArgumentException
49
     * @throws InvalidConfigException
50
     * @throws NotSupportedException
51
     */
52 9
    protected function buildCompositeInCondition(
53
        string|null $operator,
54
        iterable $columns,
55
        iterable|Iterator $values,
56
        array &$params = []
57
    ): string {
58 9
        $quotedColumns = [];
59
60
        /** @psalm-var string[] $columns */
61 9
        foreach ($columns as $i => $column) {
62 9
            if ($column instanceof ExpressionInterface) {
63 1
                $quotedColumns[$i] = $this->queryBuilder->buildExpression($column);
64 1
                continue;
65
            }
66 8
            $quotedColumns[$i] = !str_contains($column, '(')
67 8
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
68
        }
69
70 9
        $vss = [];
71
72
        /** @psalm-var string[][] $values */
73 9
        foreach ($values as $value) {
74 9
            $vs = [];
75 9
            foreach ($columns as $i => $column) {
76 9
                if ($column instanceof ExpressionInterface) {
77 1
                    $column = $this->queryBuilder->buildExpression($column);
78
                }
79 9
                if (isset($value[$column])) {
80 9
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
81 9
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
82
                } else {
83 3
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
84
                }
85
            }
86 9
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
87
        }
88
89 9
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
90
    }
91
}
92