Completed
Push — master ( c817f5...bf255d )
by Beniamin
03:22
created

ExprBuilder::using()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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