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