|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql\Condition; |
|
6
|
|
|
|
|
7
|
|
|
use Traversable; |
|
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
11
|
|
|
use Yiisoft\Db\Query\Conditions\InConditionBuilder as AbstractInConditionBuilder; |
|
12
|
|
|
use Yiisoft\Db\Query\Query; |
|
13
|
|
|
|
|
14
|
|
|
use function implode; |
|
15
|
|
|
use function is_array; |
|
16
|
|
|
use function strpos; |
|
17
|
|
|
|
|
18
|
|
|
final class InConditionBuilder extends AbstractInConditionBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Builds SQL for IN condition. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $operator |
|
24
|
|
|
* @param array|string $columns |
|
25
|
|
|
* @param Query $values |
|
26
|
|
|
* @param array $params |
|
27
|
|
|
* |
|
28
|
|
|
* @return string SQL |
|
29
|
|
|
* |
|
30
|
|
|
* @throws NotSupportedException |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function buildSubqueryInCondition(string $operator, $columns, Query $values, array &$params = []): string |
|
33
|
|
|
{ |
|
34
|
|
|
if (is_array($columns)) { |
|
35
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return parent::buildSubqueryInCondition($operator, $columns, $values, $params); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Builds SQL for IN condition. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $operator |
|
45
|
|
|
* @param array|Traversable $columns |
|
46
|
|
|
* @param array $values |
|
47
|
|
|
* @param array $params |
|
48
|
|
|
* |
|
49
|
|
|
* @return string SQL |
|
50
|
|
|
* |
|
51
|
|
|
* @throws NotSupportedException |
|
52
|
|
|
* @throws Exception |
|
53
|
|
|
* @throws InvalidConfigException |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function buildCompositeInCondition(?string $operator, $columns, $values, array &$params = []): string |
|
56
|
|
|
{ |
|
57
|
|
|
$quotedColumns = []; |
|
58
|
|
|
foreach ($columns as $i => $column) { |
|
59
|
|
|
$quotedColumns[$i] = strpos($column, '(') === false |
|
60
|
|
|
? $this->queryBuilder->getDb()->quoteColumnName($column) : $column; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$vss = []; |
|
64
|
|
|
foreach ($values as $value) { |
|
65
|
|
|
$vs = []; |
|
66
|
|
|
foreach ($columns as $i => $column) { |
|
67
|
|
|
if (isset($value[$column])) { |
|
68
|
|
|
$phName = $this->queryBuilder->bindParam($value[$column], $params); |
|
69
|
|
|
$vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName; |
|
70
|
|
|
} else { |
|
71
|
|
|
$vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
$vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')'; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|