Completed
Push — master ( 14d1d8...f3cdf4 )
by Beniamin
03:22
created

ExprBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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