Completed
Push — master ( 360009...4a8242 )
by Beniamin
04:49
created

ExprBuilder::gt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\ConjunctionExpression;
16
use Phuria\QueryBuilder\Expression\ExpressionInterface;
17
use Phuria\QueryBuilder\Expression\FunctionCallContext;
18
use Phuria\QueryBuilder\Expression\FunctionExpression;
19
use Phuria\QueryBuilder\Expression\InExpression;
20
use Phuria\QueryBuilder\Expression\OrderExpression;
21
use Phuria\QueryBuilder\Expression\UsingExpression;
22
23
/**
24
 * @author Beniamin Jonatan Šimko <[email protected]>
25
 */
26
class ExprBuilder implements ExpressionInterface
27
{
28
    use ExprBuilder\ComparisionTrait;
29
30
    /**
31
     * @var ExpressionInterface $wrappedExpression
32
     */
33
    private $wrappedExpression;
34
35
    /**
36
     * ExprBuilder constructor.
37
     */
38 29
    public function __construct()
39 3
    {
40 29
        $this->wrappedExpression = ExprNormalizer::normalizeExpression(func_get_args());
41 29
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 2
    public function getWrappedExpression()
47
    {
48 2
        return $this->wrappedExpression;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 28
    public function compile()
55
    {
56 28
        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 OrderExpression($this->wrappedExpression, OrderExpression::ORDER_DESC));
103
    }
104
105
    /**
106
     * @return ExprBuilder
107
     */
108 1
    public function asc()
109
    {
110 1
        return new self(new OrderExpression($this->wrappedExpression, OrderExpression::ORDER_ASC));
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 3
    public function conjunction($connector, $expression)
117
    {
118 3
        $expression = ExprNormalizer::normalizeExpression($expression);
119
120 3
        return new self(new ConjunctionExpression($this, $connector, $expression));
121
    }
122
123
    /**
124
     * @param string                   $functionName
125
     * @param FunctionCallContext|null $context
126
     *
127
     * @return ExprBuilder
128
     */
129 11
    public function func($functionName, FunctionCallContext $context = null)
130
    {
131 11
        return new self(new FunctionExpression($functionName, $this->wrappedExpression, $context));
132
    }
133
134
    ##############################
135
    ### ARITHMETIC EXPRESSIONS ###
136
    ##############################
137
138
    /**
139
     * @param mixed $expression
140
     *
141
     * @return ExprBuilder
142
     */
143 1
    public function add($expression)
144
    {
145 1
        $expression = ExprNormalizer::normalizeExpression($expression);
146
147 1
        return $this->conjunction(ConjunctionExpression::SYMBOL_ADD, $expression);
148
    }
149
150
    /**
151
     * @param mixed $expression
152
     *
153
     * @return ExprBuilder
154
     */
155
    public function div($expression)
156
    {
157
        $expression = ExprNormalizer::normalizeExpression($expression);
158
159
        return $this->conjunction(ConjunctionExpression::SYMBOL_DIV, $expression);
160
    }
161
162
    /**
163
     * @param mixed $expression
164
     *
165
     * @return ExprBuilder
166
     */
167
    public function divide($expression)
168
    {
169
        $expression = ExprNormalizer::normalizeExpression($expression);
170
171
        return $this->conjunction(ConjunctionExpression::SYMBOL_DIVIDE, $expression);
172
    }
173
174
    /**
175
     * @param mixed $expression
176
     *
177
     * @return ExprBuilder
178
     */
179
    public function modulo($expression)
180
    {
181
        $expression = ExprNormalizer::normalizeExpression($expression);
182
183
        return $this->conjunction(ConjunctionExpression::SYMBOL_MODULO, $expression);
184
    }
185
186
    /**
187
     * @param mixed $expression
188
     *
189
     * @return ExprBuilder
190
     */
191
    public function multiply($expression)
192
    {
193
        $expression = ExprNormalizer::normalizeExpression($expression);
194
195
        return $this->conjunction(ConjunctionExpression::SYMBOL_MULTIPLY, $expression);
196
    }
197
198
    /**
199
     * @param mixed $expression
200
     *
201
     * @return ExprBuilder
202
     */
203
    public function subtract($expression)
204
    {
205
        $expression = ExprNormalizer::normalizeExpression($expression);
206
207
        return $this->conjunction(ConjunctionExpression::SYMBOL_SUBTRACT, $expression);
208
    }
209
210
    #################
211
    ### FUNCTIONS ###
212
    #################
213
214
    /**
215
     * @return ExprBuilder
216
     */
217 1
    public function asci()
218
    {
219 1
        return $this->func(FunctionExpression::FUNC_ASCI);
220
    }
221
222
    /**
223
     * @return ExprBuilder
224
     */
225
    public function bin()
226
    {
227
        return $this->func(FunctionExpression::FUNC_BIN);
228
    }
229
230
    /**
231
     * @return ExprBuilder
232
     */
233
    public function bitLength()
234
    {
235
        return $this->func(FunctionExpression::FUNC_BIT_LENGTH);
236
    }
237
238
    /**
239
     * @param mixed $using
240
     *
241
     * @return ExprBuilder
242
     */
243 1
    public function char($using = null)
244
    {
245 1
        $context = null;
246
247 1
        if ($using) {
248 1
            $using = ExprNormalizer::normalizeExpression($using);
249 1
            $using = new UsingExpression($using);
250 1
            $context = new FunctionCallContext(['callHints' => [$using]]);
251 1
        }
252
253 1
        return $this->func(FunctionExpression::FUNC_CHAR, $context);
254
    }
255
256
    /**
257
     * @return ExprBuilder
258
     */
259 1
    public function coalesce()
260
    {
261 1
        return $this->func(FunctionExpression::FUNC_COALESCE);
262
    }
263
264
    /**
265
     * @return ExprBuilder
266
     */
267
    public function concat()
268
    {
269
        return $this->func(FunctionExpression::FUNC_CONCAT);
270
    }
271
272
    /**
273
     * @return ExprBuilder
274
     */
275
    public function concatWs()
276
    {
277
        return $this->func(FunctionExpression::FUNC_CONCAT_WS);
278
    }
279
280
    /**
281
     * @return ExprBuilder
282
     */
283
    public function elt()
284
    {
285
        return $this->func(FunctionExpression::FUNC_ELT);
286
    }
287
288
    /**
289
     * @return ExprBuilder
290
     */
291 1
    public function exportSet()
292
    {
293 1
        return $this->func(FunctionExpression::FUNC_EXPORT_SET);
294
    }
295
296
    /**
297
     * @return ExprBuilder
298
     */
299 1
    public function field()
300
    {
301 1
        return $this->func(FunctionExpression::FUNC_FIELD);
302
    }
303
304
    /**
305
     * @param mixed $expression
306
     *
307
     * @return ExprBuilder
308
     */
309 1
    public function ifNull($expression)
310
    {
311 1
        return new self(new FunctionExpression(
312 1
            FunctionExpression::FUNC_IFNULL,
313 1
            ExprNormalizer::normalizeExpression([$this->wrappedExpression, $expression])
314 1
        ));
315
    }
316
317
    /**
318
     * @return ExprBuilder
319
     */
320 2
    public function max()
321
    {
322 2
        return $this->func(FunctionExpression::FUNC_MAX);
323
    }
324
325
    /**
326
     * @return ExprBuilder
327
     */
328 4
    public function sum()
329
    {
330 4
        return $this->func(FunctionExpression::FUNC_SUM);
331
    }
332
333
    /**
334
     * @return ExprBuilder
335
     */
336 1
    public function year()
337
    {
338 1
        return $this->func(FunctionExpression::FUNC_YEAR);
339
    }
340
}