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