1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Drivers\MySql\Builder\Builders\Traits; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState; |
22
|
|
|
|
23
|
|
|
trait ConditionTrait |
24
|
|
|
{ |
25
|
|
|
use SubQueryTrait; |
26
|
|
|
use QuoteNameTrait; |
27
|
|
|
|
28
|
12 |
|
protected function resolveCondition($condition, MySqlQueryBuilderState $state) |
29
|
|
|
{ |
30
|
12 |
|
if ($condition instanceof StructuredQuery) { |
31
|
1 |
|
return $this->buildSubQuery($condition, $state); |
32
|
|
|
} |
33
|
|
|
|
34
|
12 |
|
if (is_array($condition)) { |
35
|
11 |
|
switch($condition[0]) { |
36
|
11 |
|
case 'all': |
37
|
2 |
|
return $this->buildAssociativeCondition(' AND ',$condition, $state); |
38
|
9 |
|
case 'any': |
39
|
1 |
|
return $this->buildAssociativeCondition(' OR ',$condition, $state); |
40
|
8 |
|
case 'and': |
41
|
1 |
|
return $this->buildConjuctionCondition(' AND ', $condition, $state); |
42
|
8 |
|
case 'or': |
43
|
1 |
|
return $this->buildConjuctionCondition(' OR ', $condition, $state); |
44
|
8 |
|
case 'column': |
45
|
4 |
|
return $this->buildColumnCondition($condition); |
46
|
5 |
|
case 'value': |
47
|
4 |
|
return $this->buildValueCondition($condition, $state); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
throw new \Exception('Unknown condition: ' . var_export($condition[0], true)); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
throw new \Exception('Condition must be an array.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
protected function buildConjuctionCondition(string $glue, $condition, MySqlQueryBuilderState $state) |
57
|
|
|
{ |
58
|
2 |
|
$result = []; |
59
|
|
|
|
60
|
2 |
|
$max = count($condition); |
61
|
2 |
|
for($i = 1; $i < $max; $i++) { |
62
|
2 |
|
$result[] = '(' . $this->resolveCondition($condition[$i], $state) . ')'; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return implode($glue, $result); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
protected function buildAssociativeCondition($glue, $condition, MySqlQueryBuilderState $state) |
69
|
|
|
{ |
70
|
3 |
|
$result = []; |
71
|
3 |
|
foreach ($condition[1] as $key => $value) { |
72
|
3 |
|
$result[] = $this->quoteName($key) . ' = ' . $state->getParamsBuilder()->wrapValue($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return implode($glue, $result); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
protected function buildColumnCondition($condition) |
79
|
|
|
{ |
80
|
4 |
|
$column = $condition[1] ?? null; |
81
|
|
|
|
82
|
4 |
|
if (empty($column)) { |
83
|
1 |
|
throw new \Exception('Column name must be set.'); |
84
|
3 |
|
} else if (!is_string($column)) { |
85
|
1 |
|
throw new \Exception('Column name must be a string.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return $this->quoteName($column); |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
protected function buildValueCondition($condition, MySqlQueryBuilderState $state) |
92
|
|
|
{ |
93
|
4 |
|
return $state->getParamsBuilder()->wrapValue($condition[1] ?? null, $condition[2] ?? null); |
94
|
|
|
} |
95
|
|
|
} |