Test Failed
Pull Request — master (#216)
by Wilmer
11:56 queued 07:48
created

InConditionBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
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 64
ccs 24
cts 24
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
 * InConditionBuilder builds conditions for {@see `\Yiisoft\Db\QueryBuilder\Condition\InCondition`} IN operator for
20
 * MSSQL Server.
21
 */
22
final class InConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder
23 37
{
24
    /**
25 37
     * Builds SQL for IN condition.
26
     *
27
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
28
     */
29
    protected function buildSubqueryInCondition(
30
        string $operator,
31
        iterable|string|Iterator $columns,
32
        ExpressionInterface $values,
33 3
        array &$params = []
34
    ): string {
35
        if (is_array($columns)) {
36
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
37
        }
38
39 3
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
40 1
    }
41
42
    /**
43 2
     * Builds SQL for IN condition.
44
     *
45
     * @throws Exception
46
     * @throws InvalidArgumentException
47
     * @throws InvalidConfigException
48
     * @throws NotSupportedException
49 7
     */
50
    protected function buildCompositeInCondition(
51
        string|null $operator,
52
        iterable $columns,
53
        iterable|Iterator $values,
54
        array &$params = []
55 7
    ): string {
56
        $quotedColumns = [];
57
58 7
        /** @psalm-var string[] $columns */
59 7
        foreach ($columns as $i => $column) {
60 1
            if ($column instanceof ExpressionInterface) {
61 1
                $quotedColumns[$i] = $columns[$i] = $this->queryBuilder->buildExpression($column);
0 ignored issues
show
Bug introduced by
The property queryBuilder is declared private in Yiisoft\Db\QueryBuilder\...lder\InConditionBuilder and cannot be accessed from this context.
Loading history...
62
                continue;
63
            }
64 6
65 6
            $quotedColumns[$i] = !str_contains($column, '(')
66
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
67
        }
68 7
69
        $vss = [];
70
71 7
        /** @psalm-var string[][] $values */
72 6
        foreach ($values as $value) {
73 6
            $vs = [];
74 6
            foreach ($columns as $i => $column) {
75 6
                if (isset($value[$column])) {
76 6
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
77
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
78 2
                } else {
79
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
80
                }
81 6
            }
82
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
83
        }
84 7
85
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
86
    }
87
}
88