InConditionBuilder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 67
ccs 22
cts 22
cp 1
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
B buildCompositeInCondition() 0 36 11
A buildSubqueryInCondition() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\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 MSSQL 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 MSSQL.');
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 7
    protected function buildCompositeInCondition(
53
        string|null $operator,
54
        iterable $columns,
55
        iterable|Iterator $values,
56
        array &$params = []
57
    ): string {
58 7
        $quotedColumns = [];
59
60
        /** @psalm-var string[] $columns */
61 7
        foreach ($columns as $i => $column) {
62 7
            if ($column instanceof ExpressionInterface) {
63 1
                $quotedColumns[$i] = $columns[$i] = $this->queryBuilder->buildExpression($column);
64 1
                continue;
65
            }
66
67 6
            $quotedColumns[$i] = !str_contains($column, '(')
68 6
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
69
        }
70
71 7
        $vss = [];
72
73
        /** @psalm-var string[][] $values */
74 7
        foreach ($values as $value) {
75 6
            $vs = [];
76 6
            foreach ($columns as $i => $column) {
77 6
                if (isset($value[$column])) {
78 6
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
79 6
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
80
                } else {
81 2
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
82
                }
83
            }
84 6
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
85
        }
86
87 7
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
88
    }
89
}
90