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\Sql; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Sql\Query\Delete; |
22
|
|
|
use ArekX\PQL\Sql\Query\Insert; |
23
|
|
|
use ArekX\PQL\Sql\Query\Raw; |
24
|
|
|
use ArekX\PQL\Sql\Query\Select; |
25
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConditionTrait; |
26
|
|
|
use ArekX\PQL\Sql\Query\Union; |
27
|
|
|
use ArekX\PQL\Sql\Query\Update; |
28
|
|
|
use ArekX\PQL\Sql\Statement\Call; |
29
|
|
|
use ArekX\PQL\Sql\Statement\CaseWhen; |
30
|
|
|
use ArekX\PQL\Sql\Statement\Method; |
31
|
|
|
|
32
|
|
|
if (!function_exists('\ArekX\PQL\Sql\select')) { |
33
|
|
|
/** |
34
|
|
|
* Create a SELECT query. |
35
|
|
|
* |
36
|
|
|
* @param array|StructuredQuery|string|null $columns Columns to be selected |
37
|
|
|
* @return Select |
38
|
|
|
* @see Select |
39
|
|
|
*/ |
40
|
|
|
function select(StructuredQuery|array|string|null $columns = null): Select |
41
|
|
|
{ |
42
|
21 |
|
return Select::create()->columns($columns); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!function_exists('\ArekX\PQL\Sql\insert')) { |
47
|
|
|
/** |
48
|
|
|
* Create a INSERT query |
49
|
|
|
* |
50
|
|
|
* @param string|StructuredQuery $into Where to insert data to. |
51
|
|
|
* @param array|null $data Associative array of data to be inserted. |
52
|
|
|
* @return Insert |
53
|
|
|
*/ |
54
|
|
|
function insert(StructuredQuery|string $into, array $data = null): Insert |
55
|
|
|
{ |
56
|
19 |
|
$query = Insert::create()->into($into); |
57
|
|
|
|
58
|
19 |
|
if ($data !== null) { |
59
|
19 |
|
$query->data($data); |
60
|
|
|
} |
61
|
|
|
|
62
|
19 |
|
return $query; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!function_exists('\ArekX\PQL\Sql\delete')) { |
67
|
|
|
/** |
68
|
|
|
* Creates a DELETE query |
69
|
|
|
* |
70
|
|
|
* @param string|StructuredQuery $from From where to delete |
71
|
|
|
* @param array|StructuredQuery|null $condition Filter for deletion |
72
|
|
|
* @return Delete |
73
|
|
|
*/ |
74
|
|
|
function delete(StructuredQuery|string $from, StructuredQuery|array $condition = null): Delete |
75
|
|
|
{ |
76
|
1 |
|
return Delete::create()->from($from)->where($condition); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!function_exists('\ArekX\PQL\Sql\update')) { |
81
|
|
|
/** |
82
|
|
|
* Creates an UPDATE query |
83
|
|
|
* |
84
|
|
|
* @param string|StructuredQuery $to Destination which will be updated |
85
|
|
|
* @param array $data Data to be set. |
86
|
|
|
* @param array|StructuredQuery|null $condition Filter for deletion |
87
|
|
|
* @return Update |
88
|
|
|
*/ |
89
|
|
|
function update(StructuredQuery|string $to, array $data, StructuredQuery|array $condition = null): Update |
90
|
|
|
{ |
91
|
1 |
|
return Update::create()->to($to)->set($data)->where($condition); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!function_exists('\ArekX\PQL\Sql\union')) { |
96
|
|
|
/** |
97
|
|
|
* Creates a UNION query |
98
|
|
|
* |
99
|
|
|
* @param StructuredQuery ...$queries Queries to union |
100
|
|
|
* @return Union |
101
|
|
|
*/ |
102
|
|
|
function union(StructuredQuery ...$queries): Union |
103
|
|
|
{ |
104
|
1 |
|
$unionQuery = Union::create(); |
105
|
|
|
|
106
|
1 |
|
if (empty($queries)) { |
107
|
1 |
|
return $unionQuery; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$unionQuery->from($queries[0]); |
111
|
|
|
|
112
|
1 |
|
$max = count($queries); |
113
|
1 |
|
for ($i = 1; $i < $max; $i++) { |
114
|
1 |
|
$unionQuery->unionWith($queries[$i]); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $unionQuery; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!function_exists('\ArekX\PQL\Sql\raw')) { |
122
|
|
|
/** |
123
|
|
|
* Creates a raw query |
124
|
|
|
* |
125
|
|
|
* @param mixed $query Query to be used |
126
|
|
|
* @param array|null $params Params for the query |
127
|
|
|
* @return Raw |
128
|
|
|
*/ |
129
|
|
|
function raw(mixed $query, array $params = null): Raw |
130
|
|
|
{ |
131
|
8 |
|
return Raw::from($query, $params); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!function_exists('\ArekX\PQL\Sql\call')) { |
136
|
|
|
/** |
137
|
|
|
* Creates a CALL statement |
138
|
|
|
* |
139
|
|
|
* @param string $name Name of the procedure to call. |
140
|
|
|
* @param mixed ...$params Params to be passed. |
141
|
|
|
* @return Call |
142
|
|
|
*/ |
143
|
|
|
function call(string $name, ...$params): Call |
144
|
|
|
{ |
145
|
1 |
|
return Call::as($name, ...$params); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (!function_exists('\ArekX\PQL\Sql\method')) { |
150
|
|
|
/** |
151
|
|
|
* Creates a method statement |
152
|
|
|
* |
153
|
|
|
* @param string $name Name of the procedure to call. |
154
|
|
|
* @param mixed ...$params Params to be passed. |
155
|
|
|
* @return Method |
156
|
|
|
*/ |
157
|
|
|
function method(string $name, ...$params): Method |
158
|
|
|
{ |
159
|
1 |
|
return Method::as($name, ...$params); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!function_exists('\ArekX\PQL\Sql\caseWhen')) { |
164
|
|
|
/** |
165
|
|
|
* Creates a CASE statement |
166
|
|
|
* |
167
|
|
|
* @param mixed|null $of Of value to be used in start of CASE |
168
|
|
|
* @param mixed ...$whenThen List of [$when, $then] statements. |
169
|
|
|
* @return CaseWhen |
170
|
|
|
*/ |
171
|
|
|
function caseWhen(mixed $of = null, ...$whenThen): CaseWhen |
172
|
|
|
{ |
173
|
1 |
|
return CaseWhen::create() |
174
|
1 |
|
->of($of) |
175
|
1 |
|
->when($whenThen); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (!function_exists('\ArekX\PQL\Sql\value')) { |
180
|
|
|
/** |
181
|
|
|
* Creates a value operation |
182
|
|
|
* |
183
|
|
|
* This function is used as a helper for ['value', value]. |
184
|
|
|
* |
185
|
|
|
* Values added in this way are properly parametrized |
186
|
|
|
* and escaped meaning that they this can be used |
187
|
|
|
* for user input. |
188
|
|
|
* |
189
|
|
|
* @param mixed $value Value to be wrapped |
190
|
|
|
* @return array |
191
|
|
|
* @see ConditionTrait::where() |
192
|
|
|
*/ |
193
|
|
|
function value(mixed $value): array |
194
|
|
|
{ |
195
|
2 |
|
return ['value', $value]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (!function_exists('\ArekX\PQL\Sql\column')) { |
200
|
|
|
/** |
201
|
|
|
* Creates a column operation |
202
|
|
|
* |
203
|
|
|
* This function is used as a helper for ['column', name]. |
204
|
|
|
* |
205
|
|
|
* **SQL Injection Warning**: This operation is not safe for user input. |
206
|
|
|
* |
207
|
|
|
* @param mixed $column Column to be wrapped |
208
|
|
|
* @return array |
209
|
|
|
* @see ConditionTrait::where() |
210
|
|
|
*/ |
211
|
|
|
function column(mixed $column): array |
212
|
|
|
{ |
213
|
1 |
|
return ['column', $column]; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (!function_exists('\ArekX\PQL\Sql\any')) { |
218
|
|
|
/** |
219
|
|
|
* Creates an any operation |
220
|
|
|
* |
221
|
|
|
* This function is used as a helper for ['any', associativeArray]. |
222
|
|
|
* |
223
|
|
|
* **SQL Injection Warning**: Keys in the associative arrays |
224
|
|
|
* are not safe for user input, but values are properly escaped |
225
|
|
|
* and therefore are safe. |
226
|
|
|
* |
227
|
|
|
* @param array $values Associative array values |
228
|
|
|
* @return array |
229
|
|
|
* @see ConditionTrait::where() |
230
|
|
|
*/ |
231
|
|
|
function any(array $values): array |
232
|
|
|
{ |
233
|
5 |
|
return ['any', $values]; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!function_exists('\ArekX\PQL\Sql\all')) { |
238
|
|
|
/** |
239
|
|
|
* Creates an all operation |
240
|
|
|
* |
241
|
|
|
* This function is used as a helper for ['all', associativeArray]. |
242
|
|
|
* |
243
|
|
|
* **SQL Injection Warning**: Keys in the associative arrays |
244
|
|
|
* are not safe for user input, but values are properly escaped |
245
|
|
|
* and therefore are safe. |
246
|
|
|
* |
247
|
|
|
* @param array $values Associative array values |
248
|
|
|
* @return array |
249
|
|
|
* @see ConditionTrait::where() |
250
|
|
|
*/ |
251
|
|
|
function all(array $values): array |
252
|
|
|
{ |
253
|
4 |
|
return ['all', $values]; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!function_exists('\ArekX\PQL\Sql\not')) { |
258
|
|
|
/** |
259
|
|
|
* Creates a not operation |
260
|
|
|
* |
261
|
|
|
* This function is used as a helper for ['not', expression]. |
262
|
|
|
* |
263
|
|
|
* @param array $expression Expression to be negated |
264
|
|
|
* @return array |
265
|
|
|
* @see ConditionTrait::where() |
266
|
|
|
*/ |
267
|
|
|
function not(array $expression): array |
268
|
|
|
{ |
269
|
1 |
|
return ['not', $expression]; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (!function_exists('\ArekX\PQL\Sql\exists')) { |
274
|
|
|
/** |
275
|
|
|
* Creates a EXISTS operation |
276
|
|
|
* |
277
|
|
|
* This function is used as a helper for ['exists', expression]. |
278
|
|
|
* |
279
|
|
|
* @param array $expression Expression to be negated |
280
|
|
|
* @return array |
281
|
|
|
* @see ConditionTrait::where() |
282
|
|
|
*/ |
283
|
|
|
function exists(array $expression): array |
284
|
|
|
{ |
285
|
1 |
|
return ['exists', $expression]; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if (!function_exists('\ArekX\PQL\Sql\andWith')) { |
290
|
|
|
/** |
291
|
|
|
* Creates an and operation |
292
|
|
|
* |
293
|
|
|
* This function is used as a helper for ['and', ...expressions]. |
294
|
|
|
* |
295
|
|
|
* @param array $expressions Expression to be places in AND |
296
|
|
|
* @return array |
297
|
|
|
* @see ConditionTrait::where() |
298
|
|
|
*/ |
299
|
|
|
function andWith(array ...$expressions): array |
300
|
|
|
{ |
301
|
1 |
|
return ['and', ...$expressions]; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (!function_exists('\ArekX\PQL\Sql\orWith')) { |
306
|
|
|
/** |
307
|
|
|
* Creates an or operation |
308
|
|
|
* |
309
|
|
|
* This function is used as a helper for ['or', ...expressions]. |
310
|
|
|
* |
311
|
|
|
* @param array $expressions Expression to be places in AND |
312
|
|
|
* @return array |
313
|
|
|
* @see ConditionTrait::where() |
314
|
|
|
*/ |
315
|
|
|
function orWith(array ...$expressions): array |
316
|
|
|
{ |
317
|
1 |
|
return ['or', ...$expressions]; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if (!function_exists('\ArekX\PQL\Sql\compare')) { |
322
|
|
|
/** |
323
|
|
|
* Creates a comparison operation |
324
|
|
|
* |
325
|
|
|
* This function is used as a helper for multiple comparison |
326
|
|
|
* operations. |
327
|
|
|
* |
328
|
|
|
* This helper allows an easier way of adding comparisons. |
329
|
|
|
* |
330
|
|
|
* @param array $a Operand A |
331
|
|
|
* @param string $b Operator: =, <>, !=, >, <, >=, <= |
332
|
|
|
* @param array $c Operand B |
333
|
|
|
* @return array |
334
|
|
|
* @see ConditionTrait::where() |
335
|
|
|
*/ |
336
|
|
|
function compare(array $a, string $b, array $c): array |
337
|
|
|
{ |
338
|
1 |
|
return [$b, $a, $c]; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (!function_exists('\ArekX\PQL\Sql\equal')) { |
343
|
|
|
/** |
344
|
|
|
* Creates an equality operation |
345
|
|
|
* |
346
|
|
|
* This function is used as a helper for multiple comparison |
347
|
|
|
* operations. |
348
|
|
|
* |
349
|
|
|
* This helper allows an easier way of adding comparisons. |
350
|
|
|
* |
351
|
|
|
* @param array $a Operand A |
352
|
|
|
* @param array $b Operand B |
353
|
|
|
* @return array |
354
|
|
|
* @see ConditionTrait::where() |
355
|
|
|
*/ |
356
|
|
|
function equal(array $a, array $b): array |
357
|
|
|
{ |
358
|
2 |
|
return ['=', $a, $b]; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if (!function_exists('\ArekX\PQL\Sql\between')) { |
363
|
|
|
/** |
364
|
|
|
* Creates a BETWEEN operation |
365
|
|
|
* |
366
|
|
|
* This is a helper function for ['between', expression, fromExpression, toExpression] |
367
|
|
|
* |
368
|
|
|
* @param array $of Of value to be used in between. |
369
|
|
|
* @param array $from From value to be used in between. |
370
|
|
|
* @param array $to To value to be used in between. |
371
|
|
|
* @return array |
372
|
|
|
* @see ConditionTrait::where() |
373
|
|
|
*/ |
374
|
|
|
function between(array $of, array $from, array $to): array |
375
|
|
|
{ |
376
|
1 |
|
return ['between', $of, $from, $to]; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if (!function_exists('\ArekX\PQL\Sql\search')) { |
381
|
|
|
/** |
382
|
|
|
* Creates a LIKE operation |
383
|
|
|
* |
384
|
|
|
* This is a helper function for ['like', expression, '%' . $value . '%'] |
385
|
|
|
* |
386
|
|
|
* @param array $what What to search on |
387
|
|
|
* @param string $value Value to be searched. This value will be properly escaped. |
388
|
|
|
* @return array |
389
|
|
|
* @see ConditionTrait::where() |
390
|
|
|
*/ |
391
|
|
|
function search(array $what, string $value): array |
392
|
|
|
{ |
393
|
1 |
|
return ['like', $what, ['value', '%' . $value . '%']]; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
if (!function_exists('\ArekX\PQL\Sql\asFilters')) { |
398
|
|
|
/** |
399
|
|
|
* Processes passed values and applies them as |
400
|
|
|
* filters if they need to be added. |
401
|
|
|
* |
402
|
|
|
* Use case for this function in table grid systems |
403
|
|
|
* or places with an advanced search including multiple |
404
|
|
|
* value. |
405
|
|
|
* |
406
|
|
|
* Filters are to be passed in the following format: |
407
|
|
|
* ```php |
408
|
|
|
* [ |
409
|
|
|
* [$value1, condition], |
410
|
|
|
* [$value2, condition], |
411
|
|
|
* ] |
412
|
|
|
* ``` |
413
|
|
|
* |
414
|
|
|
* If $value1 and $value2 are not '' or null then those conditions |
415
|
|
|
* will be used. Condition is any valid condition expression used in |
416
|
|
|
* ConditionTrait::where() function. |
417
|
|
|
* |
418
|
|
|
* If value is a closure function, it will be called and if the result is |
419
|
|
|
* true then the condition on the right will be used. |
420
|
|
|
* |
421
|
|
|
* @param array $filters |
422
|
|
|
* @param string $op |
423
|
|
|
* @return array |
424
|
|
|
* @see ConditionTrait::where() |
425
|
|
|
*/ |
426
|
|
|
function asFilters(array $filters, string $op = 'and'): array |
427
|
|
|
{ |
428
|
1 |
|
$results = [$op]; |
429
|
|
|
|
430
|
1 |
|
foreach ($filters as [$check, $condition]) { |
431
|
1 |
|
if ($check === null || $check === '') { |
432
|
1 |
|
continue; |
433
|
|
|
} |
434
|
|
|
|
435
|
1 |
|
if ($check instanceof \Closure && !$check()) { |
436
|
1 |
|
continue; |
437
|
|
|
} |
438
|
|
|
|
439
|
1 |
|
$results[] = $condition; |
440
|
|
|
} |
441
|
|
|
|
442
|
1 |
|
return $results; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|