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); |
|
|
|
|
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
|
|
|
|