-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWhereParser.php
267 lines (232 loc) · 8.25 KB
/
WhereParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace sitkoru\cache\ar;
use yii\base\InvalidParamException;
use yii\db\Exception;
use yii\db\Expression;
use yii\db\Query;
/**
* Class WhereParser
* @package sitkoru\cache\ar
*/
class WhereParser
{
private $data = [];
protected $conditionParsers = [
'NOT' => 'parseNotCondition',
'AND' => 'parseAndCondition',
'OR' => 'parseAndCondition',
'BETWEEN' => 'parseBetweenCondition',
'NOT BETWEEN' => 'parseBetweenCondition',
'IN' => 'parseInCondition',
'NOT IN' => 'parseInCondition',
'LIKE' => 'parseLikeCondition',
'NOT LIKE' => 'parseLikeCondition',
'OR LIKE' => 'parseLikeCondition',
'OR NOT LIKE' => 'parseLikeCondition',
'EXISTS' => 'parseExistsCondition',
'NOT EXISTS' => 'parseExistsCondition'
];
public function parse($condition, $params)
{
$this->data = [];
$this->parseCondition($condition, $params);
return $this->data;
}
private function parseCondition($condition, $params)
{
if (is_array($condition) && array_key_exists(0, $condition)
) { // operator format: operator, operand 1, operand 2, ...
$operator = strtoupper($condition[0]);
$method = 'parseSimpleCondition';
if (array_key_exists($operator, $this->conditionParsers)) {
$method = $this->conditionParsers[$operator];
}
array_shift($condition);
$this->$method($operator, $condition, $params);
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
$this->parseHashCondition($condition, $params);
}
}
/**
* Creates a condition based on column-value pairs.
* @param array $condition the condition specification.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function parseHashCondition($condition, &$params)
{
$parts = [];
foreach ($condition as $column => $value) {
if (is_array($value) || $value instanceof Query) {
// IN condition
$parts[] = $this->parseInCondition('IN', [$column, $value], $params);
} else {
$this->data[] = [$column, '=', $value];
}
}
return true;
}
/**
* Connects two or more SQL expressions with the `AND` or `OR` operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the SQL expressions to connect.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function parseAndCondition($operator, $operands, &$params)
{
foreach ($operands as $operand) {
if (is_array($operand)) {
$this->parseCondition($operand, $params);
}
}
return true;
}
/**
* Inverts an SQL expressions with `NOT` operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the SQL expressions to connect.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function parseNotCondition($operator, $operands, &$params)
{
//TODO: implement
return true;
}
/**
* Creates an SQL expressions with the `BETWEEN` operator.
* @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
* @param array $operands the first operand is the column name. The second and third operands
* describe the interval that column value should be in.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function parseBetweenCondition($operator, $operands, &$params)
{
//TODO: implement
return true;
}
/**
* @param $operator
* @param $operands
* @param $params
* @return string
* @throws Exception
*/
public function parseInCondition($operator, $operands, &$params)
{
if (!isset($operands[0], $operands[1])) {
throw new Exception("Operator '$operator' requires two operands.");
}
[$column, $values] = $operands;
if ($values === [] || $column === []) {
return $operator === 'IN' ? '0=1' : '';
}
if ($values instanceof Query) {
return $this->parseSubqueryInCondition($operator, $column, $values, $params);
}
$values = (array)$values;
if (\is_array($column) && \count($column) > 1) {
return $this->parseCompositeInCondition($operator, $column, $values, $params);
}
if (\is_array($column)) {
$column = reset($column);
}
foreach ($values as $i => $value) {
if (\is_array($value)) {
$value = $value[$column] ?? null;
}
if ($value === null) {
$values[$i] = 'NULL';
} elseif ($value instanceof Expression) {
$values[$i] = $value->expression;
}
}
$this->data[] = [$column, $operator, $values];
return true;
}
/**
* Parse SQL for IN condition
*
* @param string $operator
* @param array $columns
* @param Query $values
* @param array $params
* @return string SQL
*/
protected function parseSubqueryInCondition($operator, $columns, $values, &$params)
{
//TODO: implement
return;
}
/**
* Builds SQL for IN condition
*
* @param string $operator
* @param array $columns
* @param array $values
* @param array $params
* @return string SQL
*/
protected function parseCompositeInCondition($operator, $columns, $values, &$params)
{
foreach ($columns as $i => $column) {
if (array_key_exists($i, $values)) {
$this->data[] = [$column, $operator, $values[$i]];
}
}
return true;
}
/**
* @param string $operator
* @param array $operands
* @param array $params
* @return string
* @throws InvalidParamException
*/
public function parseLikeCondition($operator, $operands, &$params)
{
//TODO: Implement
return;
}
/**
* Creates an SQL expressions with the `EXISTS` operator.
* @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`)
* @param array $operands contains only one element which is a [[Query]] object representing the sub-query.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if the operand is not a [[Query]] object.
*/
public function parseExistsCondition($operator, $operands, &$params)
{
//TODO: Implement
return;
}
/**
* Creates an SQL expressions like `"column" operator value`.
* @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
* @param array $operands contains two column names.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function parseSimpleCondition($operator, $operands, &$params)
{
if (count($operands) !== 2) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
if ($value === null) {
$this->data[] = [$column, $operator, null];
return true;
} elseif ($value instanceof Expression) {
return true;
} else {
$this->data[] = [$column, $operator, $value];
return true;
}
}
}