1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Phuria SQL Builder package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\QueryBuilder; |
13
|
|
|
|
14
|
|
|
use Phuria\QueryBuilder\Expression\AliasExpression; |
15
|
|
|
use Phuria\QueryBuilder\Expression\Comparison as Comparison; |
16
|
|
|
use Phuria\QueryBuilder\Expression\ConjunctionExpression; |
17
|
|
|
use Phuria\QueryBuilder\Expression\ExpressionInterface; |
18
|
|
|
use Phuria\QueryBuilder\Expression\FunctionCallContext; |
19
|
|
|
use Phuria\QueryBuilder\Expression\FunctionExpression; |
20
|
|
|
use Phuria\QueryBuilder\Expression\InExpression; |
21
|
|
|
use Phuria\QueryBuilder\Expression\OrderExpression; |
22
|
|
|
use Phuria\QueryBuilder\Expression\UsingExpression; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ExprBuilder implements ExpressionInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ExpressionInterface $wrappedExpression |
31
|
|
|
*/ |
32
|
|
|
private $wrappedExpression; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ExprBuilder constructor. |
36
|
|
|
*/ |
37
|
29 |
|
public function __construct() |
38
|
|
|
{ |
39
|
29 |
|
$this->wrappedExpression = ExprNormalizer::normalizeExpression(func_get_args()); |
40
|
29 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ExpressionInterface |
44
|
|
|
*/ |
45
|
|
|
public function getWrappedExpression() |
46
|
|
|
{ |
47
|
|
|
return $this->wrappedExpression; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
28 |
|
public function compile() |
54
|
|
|
{ |
55
|
28 |
|
return $this->wrappedExpression->compile(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param mixed $alias |
60
|
|
|
* |
61
|
|
|
* @return ExprBuilder |
62
|
|
|
*/ |
63
|
3 |
|
public function alias($alias) |
64
|
|
|
{ |
65
|
3 |
|
$alias = ExprNormalizer::normalizeExpression($alias); |
66
|
|
|
|
67
|
3 |
|
return new self(new AliasExpression($this->wrappedExpression, $alias)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ExprBuilder |
72
|
|
|
*/ |
73
|
|
|
public function sumNullable() |
74
|
|
|
{ |
75
|
|
|
return $this->ifNull('0')->sum(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return ExprBuilder |
80
|
|
|
*/ |
81
|
1 |
|
public function in() |
82
|
|
|
{ |
83
|
1 |
|
$arguments = ExprNormalizer::normalizeExpression(func_get_args()); |
84
|
|
|
|
85
|
1 |
|
return new self(new InExpression($this->wrappedExpression, $arguments)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return ExprBuilder |
90
|
|
|
*/ |
91
|
|
|
public function using() |
92
|
|
|
{ |
93
|
|
|
return new self(new UsingExpression($this->wrappedExpression)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ExprBuilder |
98
|
|
|
*/ |
99
|
1 |
|
public function desc() |
100
|
|
|
{ |
101
|
1 |
|
return new self(new OrderExpression($this->wrappedExpression, OrderExpression::ORDER_DESC)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ExprBuilder |
106
|
|
|
*/ |
107
|
1 |
|
public function asc() |
108
|
|
|
{ |
109
|
1 |
|
return new self(new OrderExpression($this->wrappedExpression, OrderExpression::ORDER_ASC)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $connector |
114
|
|
|
* @param mixed $expression |
115
|
|
|
* |
116
|
|
|
* @return ExprBuilder |
117
|
|
|
*/ |
118
|
3 |
|
public function conjunction($connector, $expression) |
119
|
|
|
{ |
120
|
3 |
|
$expression = ExprNormalizer::normalizeExpression($expression); |
121
|
|
|
|
122
|
3 |
|
return new self(new ConjunctionExpression($this, $connector, $expression)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $functionName |
127
|
|
|
* @param FunctionCallContext|null $context |
128
|
|
|
* |
129
|
|
|
* @return ExprBuilder |
130
|
|
|
*/ |
131
|
11 |
|
public function func($functionName, FunctionCallContext $context = null) |
132
|
|
|
{ |
133
|
11 |
|
return new self(new FunctionExpression($functionName, $this->wrappedExpression, $context)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
############################### |
137
|
|
|
### COMPARISION EXPRESSIONS ### |
138
|
|
|
############################### |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param mixed $from |
142
|
|
|
* @param mixed $to |
143
|
|
|
* |
144
|
|
|
* @return ExprBuilder |
145
|
|
|
*/ |
146
|
1 |
|
public function between($from, $to) |
147
|
|
|
{ |
148
|
1 |
|
$from = ExprNormalizer::normalizeExpression($from); |
149
|
1 |
|
$to = ExprNormalizer::normalizeExpression($to); |
150
|
|
|
|
151
|
1 |
|
return new self(new Comparison\Between($this->wrappedExpression, $from, $to)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $expression |
156
|
|
|
* |
157
|
|
|
* @return ExprBuilder |
158
|
|
|
*/ |
159
|
1 |
|
public function eq($expression) |
160
|
|
|
{ |
161
|
1 |
|
$expression = ExprNormalizer::normalizeExpression($expression); |
162
|
|
|
|
163
|
1 |
|
return $this->conjunction(ConjunctionExpression::SYMBOL_EQ, $expression); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed $expression |
168
|
|
|
* |
169
|
|
|
* @return ExprBuilder |
170
|
|
|
*/ |
171
|
1 |
|
public function gt($expression) |
172
|
|
|
{ |
173
|
1 |
|
$expression = ExprNormalizer::normalizeExpression($expression); |
174
|
|
|
|
175
|
1 |
|
return $this->conjunction(ConjunctionExpression::SYMBOL_GT, $expression); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param mixed $expression |
180
|
|
|
* |
181
|
|
|
* @return ExprBuilder |
182
|
|
|
*/ |
183
|
|
|
public function gte($expression) |
184
|
|
|
{ |
185
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
186
|
|
|
|
187
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_GTE, $expression); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return ExprBuilder |
192
|
|
|
*/ |
193
|
|
|
public function isNull() |
194
|
|
|
{ |
195
|
|
|
return new self(new Comparison\IsNull($this->wrappedExpression)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param mixed $expression |
200
|
|
|
* |
201
|
|
|
* @return ExprBuilder |
202
|
|
|
*/ |
203
|
|
|
public function lt($expression) |
204
|
|
|
{ |
205
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
206
|
|
|
|
207
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_LT, $expression); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param mixed $expression |
212
|
|
|
* |
213
|
|
|
* @return ExprBuilder |
214
|
|
|
*/ |
215
|
|
|
public function lte($expression) |
216
|
|
|
{ |
217
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
218
|
|
|
|
219
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_LTE, $expression); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param mixed $expression |
224
|
|
|
* |
225
|
|
|
* @return ExprBuilder |
226
|
|
|
*/ |
227
|
|
|
public function neq($expression) |
228
|
|
|
{ |
229
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
230
|
|
|
|
231
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_NEQ, $expression); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param mixed $from |
236
|
|
|
* @param mixed $to |
237
|
|
|
* |
238
|
|
|
* @return ExprBuilder |
239
|
|
|
*/ |
240
|
1 |
|
public function notBetween($from, $to) |
241
|
|
|
{ |
242
|
1 |
|
$from = ExprNormalizer::normalizeExpression($from); |
243
|
1 |
|
$to = ExprNormalizer::normalizeExpression($to); |
244
|
|
|
|
245
|
1 |
|
return new self(new Comparison\NotBetween($this->wrappedExpression, $from, $to)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param mixed $expression |
250
|
|
|
* |
251
|
|
|
* @return ExprBuilder |
252
|
|
|
*/ |
253
|
|
|
public function nullEq($expression) |
254
|
|
|
{ |
255
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
256
|
|
|
|
257
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_NULL_EQ, $expression); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
############################## |
261
|
|
|
### ARITHMETIC EXPRESSIONS ### |
262
|
|
|
############################## |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param mixed $expression |
266
|
|
|
* |
267
|
|
|
* @return ExprBuilder |
268
|
|
|
*/ |
269
|
1 |
|
public function add($expression) |
270
|
|
|
{ |
271
|
1 |
|
$expression = ExprNormalizer::normalizeExpression($expression); |
272
|
|
|
|
273
|
1 |
|
return $this->conjunction(ConjunctionExpression::SYMBOL_ADD, $expression); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param mixed $expression |
278
|
|
|
* |
279
|
|
|
* @return ExprBuilder |
280
|
|
|
*/ |
281
|
|
|
public function div($expression) |
282
|
|
|
{ |
283
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
284
|
|
|
|
285
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_DIV, $expression); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param mixed $expression |
290
|
|
|
* |
291
|
|
|
* @return ExprBuilder |
292
|
|
|
*/ |
293
|
|
|
public function divide($expression) |
294
|
|
|
{ |
295
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
296
|
|
|
|
297
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_DIVIDE, $expression); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param mixed $expression |
302
|
|
|
* |
303
|
|
|
* @return ExprBuilder |
304
|
|
|
*/ |
305
|
|
|
public function modulo($expression) |
306
|
|
|
{ |
307
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
308
|
|
|
|
309
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_MODULO, $expression); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param mixed $expression |
314
|
|
|
* |
315
|
|
|
* @return ExprBuilder |
316
|
|
|
*/ |
317
|
|
|
public function multiply($expression) |
318
|
|
|
{ |
319
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
320
|
|
|
|
321
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_MULTIPLY, $expression); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param mixed $expression |
326
|
|
|
* |
327
|
|
|
* @return ExprBuilder |
328
|
|
|
*/ |
329
|
|
|
public function subtract($expression) |
330
|
|
|
{ |
331
|
|
|
$expression = ExprNormalizer::normalizeExpression($expression); |
332
|
|
|
|
333
|
|
|
return $this->conjunction(ConjunctionExpression::SYMBOL_SUBTRACT, $expression); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
################# |
337
|
|
|
### FUNCTIONS ### |
338
|
|
|
################# |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return ExprBuilder |
342
|
|
|
*/ |
343
|
1 |
|
public function asci() |
344
|
|
|
{ |
345
|
1 |
|
return $this->func(FunctionExpression::FUNC_ASCI); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @return ExprBuilder |
350
|
|
|
*/ |
351
|
|
|
public function bin() |
352
|
|
|
{ |
353
|
|
|
return $this->func(FunctionExpression::FUNC_BIN); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return ExprBuilder |
358
|
|
|
*/ |
359
|
|
|
public function bitLength() |
360
|
|
|
{ |
361
|
|
|
return $this->func(FunctionExpression::FUNC_BIT_LENGTH); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param mixed $using |
366
|
|
|
* |
367
|
|
|
* @return ExprBuilder |
368
|
|
|
*/ |
369
|
1 |
|
public function char($using = null) |
370
|
|
|
{ |
371
|
1 |
|
$context = null; |
372
|
|
|
|
373
|
1 |
|
if ($using) { |
374
|
1 |
|
$using = ExprNormalizer::normalizeExpression($using); |
375
|
1 |
|
$using = new UsingExpression($using); |
376
|
1 |
|
$context = new FunctionCallContext(['callHints' => [$using]]); |
377
|
1 |
|
} |
378
|
|
|
|
379
|
1 |
|
return $this->func(FunctionExpression::FUNC_CHAR, $context); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @return ExprBuilder |
384
|
|
|
*/ |
385
|
1 |
|
public function coalesce() |
386
|
|
|
{ |
387
|
1 |
|
return $this->func(FunctionExpression::FUNC_COALESCE); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return ExprBuilder |
392
|
|
|
*/ |
393
|
|
|
public function concat() |
394
|
|
|
{ |
395
|
|
|
return $this->func(FunctionExpression::FUNC_CONCAT); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return ExprBuilder |
400
|
|
|
*/ |
401
|
|
|
public function concatWs() |
402
|
|
|
{ |
403
|
|
|
return $this->func(FunctionExpression::FUNC_CONCAT_WS); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return ExprBuilder |
408
|
|
|
*/ |
409
|
|
|
public function elt() |
410
|
|
|
{ |
411
|
|
|
return $this->func(FunctionExpression::FUNC_ELT); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return ExprBuilder |
416
|
|
|
*/ |
417
|
1 |
|
public function exportSet() |
418
|
|
|
{ |
419
|
1 |
|
return $this->func(FunctionExpression::FUNC_EXPORT_SET); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @return ExprBuilder |
424
|
|
|
*/ |
425
|
1 |
|
public function field() |
426
|
|
|
{ |
427
|
1 |
|
return $this->func(FunctionExpression::FUNC_FIELD); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @param mixed $expression |
432
|
|
|
* |
433
|
|
|
* @return ExprBuilder |
434
|
|
|
*/ |
435
|
1 |
|
public function ifNull($expression) |
436
|
|
|
{ |
437
|
1 |
|
return new self(new FunctionExpression( |
438
|
1 |
|
FunctionExpression::FUNC_IFNULL, |
439
|
1 |
|
ExprNormalizer::normalizeExpression([$this->wrappedExpression, $expression]) |
440
|
1 |
|
)); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @return ExprBuilder |
445
|
|
|
*/ |
446
|
2 |
|
public function max() |
447
|
|
|
{ |
448
|
2 |
|
return $this->func(FunctionExpression::FUNC_MAX); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @return ExprBuilder |
453
|
|
|
*/ |
454
|
4 |
|
public function sum() |
455
|
|
|
{ |
456
|
4 |
|
return $this->func(FunctionExpression::FUNC_SUM); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @return ExprBuilder |
461
|
|
|
*/ |
462
|
1 |
|
public function year() |
463
|
|
|
{ |
464
|
1 |
|
return $this->func(FunctionExpression::FUNC_YEAR); |
465
|
|
|
} |
466
|
|
|
} |