Passed
Pull Request — master (#14)
by Wilmer
11:43
created

InConditionBuilder::buildSubqueryInCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\Condition;
6
7
use Yiisoft\Db\Exception\NotSupportedException;
8
use Yiisoft\Db\Query\Query;
9
use Yiisoft\Db\Query\Conditions\InConditionBuilder as AbstractInConditionBuilder;
10
11
final class InConditionBuilder extends AbstractInConditionBuilder
12
{
13
    protected function buildSubqueryInCondition(string $operator, $columns, Query $values, array &$params = []): string
14
    {
15
        if (is_array($columns)) {
16
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
17
        }
18
19
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
20
    }
21
22
    protected function buildCompositeInCondition(?string $operator, $columns, $values, array &$params = []): string
23
    {
24
        $quotedColumns = [];
25
        foreach ($columns as $i => $column) {
26
            $quotedColumns[$i] = strpos($column, '(') === false
27
                ? $this->queryBuilder->getDb()->quoteColumnName($column) : $column;
28
        }
29
30
        $vss = [];
31
        foreach ($values as $value) {
32
            $vs = [];
33
            foreach ($columns as $i => $column) {
34
                if (isset($value[$column])) {
35
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
36
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
37
                } else {
38
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
39
                }
40
            }
41
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
42
        }
43
44
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
45
    }
46
}
47