|
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 buildCondition($condition, MySqlQueryBuilderState $state) |
|
29
|
|
|
{ |
|
30
|
12 |
|
static $map = null; |
|
31
|
|
|
|
|
32
|
12 |
|
if ($map === null) { |
|
33
|
|
|
$map = [ |
|
34
|
2 |
|
'all' => fn($condition, $state) => $this->buildAssociativeCondition(' AND ', $condition, $state), |
|
35
|
3 |
|
'any' => fn($condition, $state) => $this->buildAssociativeCondition(' OR ', $condition, $state), |
|
36
|
3 |
|
'and' => fn($condition, $state) => $this->buildConjuctionCondition(' AND ', $condition, $state), |
|
37
|
3 |
|
'or' => fn($condition, $state) => $this->buildConjuctionCondition(' OR ', $condition, $state), |
|
38
|
6 |
|
'column' => fn($condition, $state) => $this->buildColumnCondition($condition), |
|
|
|
|
|
|
39
|
6 |
|
'value' => fn($condition, $state) => $this->buildValueCondition($condition, $state), |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
12 |
|
if ($condition instanceof StructuredQuery) { |
|
44
|
1 |
|
return $this->buildSubQuery($condition, $state); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
if (is_array($condition)) { |
|
48
|
11 |
|
if (empty($map[$condition[0]])) { |
|
49
|
1 |
|
throw new \Exception('Unknown condition: ' . var_export($condition[0], true)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
10 |
|
return $map[$condition[0]]($condition, $state); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
throw new \Exception('Condition must be an array.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
protected function buildAssociativeCondition($glue, $condition, MySqlQueryBuilderState $state) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$result = []; |
|
61
|
3 |
|
foreach ($condition[1] as $key => $value) { |
|
62
|
3 |
|
$result[] = $this->quoteName($key) . ' = ' . $state->getParamsBuilder()->wrapValue($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
return implode($glue, $result); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
protected function buildConjuctionCondition(string $glue, $condition, MySqlQueryBuilderState $state) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$result = []; |
|
71
|
|
|
|
|
72
|
2 |
|
$max = count($condition); |
|
73
|
2 |
|
for ($i = 1; $i < $max; $i++) { |
|
74
|
2 |
|
$result[] = '(' . $this->buildCondition($condition[$i], $state) . ')'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return implode($glue, $result); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
protected function buildColumnCondition($condition) |
|
81
|
|
|
{ |
|
82
|
4 |
|
$column = $condition[1] ?? null; |
|
83
|
|
|
|
|
84
|
4 |
|
if (empty($column)) { |
|
85
|
1 |
|
throw new \Exception('Column name must be set.'); |
|
86
|
3 |
|
} else if (!is_string($column)) { |
|
87
|
1 |
|
throw new \Exception('Column name must be a string.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $this->quoteName($column); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
protected function buildValueCondition($condition, MySqlQueryBuilderState $state) |
|
94
|
|
|
{ |
|
95
|
4 |
|
return $state->getParamsBuilder()->wrapValue($condition[1] ?? null, $condition[2] ?? null); |
|
96
|
|
|
} |
|
97
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.