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\ArithmeticTrait; |
29
|
|
|
use ExprBuilder\ComparisionTrait; |
30
|
|
|
use ExprBuilder\DateTimeTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ExpressionInterface $wrappedExpression |
34
|
|
|
*/ |
35
|
|
|
private $wrappedExpression; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ExprBuilder constructor. |
39
|
|
|
*/ |
40
|
29 |
|
public function __construct() |
41
|
|
|
{ |
42
|
29 |
|
$this->wrappedExpression = ExprNormalizer::normalizeExpression(func_get_args()); |
43
|
29 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
2 |
|
public function getWrappedExpression() |
49
|
|
|
{ |
50
|
2 |
|
return $this->wrappedExpression; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
28 |
|
public function compile() |
57
|
|
|
{ |
58
|
28 |
|
return $this->wrappedExpression->compile(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $alias |
63
|
|
|
* |
64
|
|
|
* @return ExprBuilder |
65
|
|
|
*/ |
66
|
3 |
|
public function alias($alias) |
67
|
|
|
{ |
68
|
3 |
|
$alias = ExprNormalizer::normalizeExpression($alias); |
69
|
|
|
|
70
|
3 |
|
return new self(new AliasExpression($this->wrappedExpression, $alias)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ExprBuilder |
75
|
|
|
*/ |
76
|
|
|
public function sumNullable() |
77
|
|
|
{ |
78
|
|
|
return $this->ifNull('0')->sum(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ExprBuilder |
83
|
|
|
*/ |
84
|
1 |
|
public function in() |
85
|
|
|
{ |
86
|
1 |
|
$arguments = ExprNormalizer::normalizeExpression(func_get_args()); |
87
|
|
|
|
88
|
1 |
|
return new self(new InExpression($this->wrappedExpression, $arguments)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return ExprBuilder |
93
|
|
|
*/ |
94
|
|
|
public function using() |
95
|
|
|
{ |
96
|
|
|
return new self(new UsingExpression($this->wrappedExpression)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ExprBuilder |
101
|
|
|
*/ |
102
|
1 |
|
public function desc() |
103
|
|
|
{ |
104
|
1 |
|
return new self(new OrderExpression($this->wrappedExpression, OrderExpression::ORDER_DESC)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return ExprBuilder |
109
|
|
|
*/ |
110
|
1 |
|
public function asc() |
111
|
|
|
{ |
112
|
1 |
|
return new self(new OrderExpression($this->wrappedExpression, OrderExpression::ORDER_ASC)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
3 |
|
public function conjunction($connector, $expression) |
119
|
|
|
{ |
120
|
3 |
|
$expression = ExprNormalizer::normalizeExpression($expression); |
121
|
|
|
|
122
|
3 |
|
return new self(new ConjunctionExpression($this, $connector, $expression)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $functionName |
127
|
|
|
* @param FunctionCallContext|null $context |
128
|
|
|
* |
129
|
|
|
* @return ExprBuilder |
130
|
|
|
*/ |
131
|
11 |
|
public function func($functionName, FunctionCallContext $context = null) |
132
|
|
|
{ |
133
|
11 |
|
return new self(new FunctionExpression($functionName, $this->wrappedExpression, $context)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
################# |
137
|
|
|
### FUNCTIONS ### |
138
|
|
|
################# |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return ExprBuilder |
142
|
|
|
*/ |
143
|
1 |
|
public function asci() |
144
|
|
|
{ |
145
|
1 |
|
return $this->func(FunctionExpression::FUNC_ASCI); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return ExprBuilder |
150
|
|
|
*/ |
151
|
|
|
public function bin() |
152
|
|
|
{ |
153
|
|
|
return $this->func(FunctionExpression::FUNC_BIN); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return ExprBuilder |
158
|
|
|
*/ |
159
|
|
|
public function bitLength() |
160
|
|
|
{ |
161
|
|
|
return $this->func(FunctionExpression::FUNC_BIT_LENGTH); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed $using |
166
|
|
|
* |
167
|
|
|
* @return ExprBuilder |
168
|
|
|
*/ |
169
|
1 |
|
public function char($using = null) |
170
|
|
|
{ |
171
|
1 |
|
$context = null; |
172
|
|
|
|
173
|
1 |
|
if ($using) { |
174
|
1 |
|
$using = ExprNormalizer::normalizeExpression($using); |
175
|
1 |
|
$using = new UsingExpression($using); |
176
|
1 |
|
$context = new FunctionCallContext(['callHints' => [$using]]); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
return $this->func(FunctionExpression::FUNC_CHAR, $context); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return ExprBuilder |
184
|
|
|
*/ |
185
|
1 |
|
public function coalesce() |
186
|
|
|
{ |
187
|
1 |
|
return $this->func(FunctionExpression::FUNC_COALESCE); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return ExprBuilder |
192
|
|
|
*/ |
193
|
|
|
public function concat() |
194
|
|
|
{ |
195
|
|
|
return $this->func(FunctionExpression::FUNC_CONCAT); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return ExprBuilder |
200
|
|
|
*/ |
201
|
|
|
public function concatWs() |
202
|
|
|
{ |
203
|
|
|
return $this->func(FunctionExpression::FUNC_CONCAT_WS); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return ExprBuilder |
208
|
|
|
*/ |
209
|
|
|
public function elt() |
210
|
|
|
{ |
211
|
|
|
return $this->func(FunctionExpression::FUNC_ELT); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return ExprBuilder |
216
|
|
|
*/ |
217
|
1 |
|
public function exportSet() |
218
|
|
|
{ |
219
|
1 |
|
return $this->func(FunctionExpression::FUNC_EXPORT_SET); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return ExprBuilder |
224
|
|
|
*/ |
225
|
1 |
|
public function field() |
226
|
|
|
{ |
227
|
1 |
|
return $this->func(FunctionExpression::FUNC_FIELD); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param mixed $expression |
232
|
|
|
* |
233
|
|
|
* @return ExprBuilder |
234
|
|
|
*/ |
235
|
1 |
|
public function ifNull($expression) |
236
|
|
|
{ |
237
|
1 |
|
return new self(new FunctionExpression( |
238
|
1 |
|
FunctionExpression::FUNC_IFNULL, |
239
|
1 |
|
ExprNormalizer::normalizeExpression([$this->wrappedExpression, $expression]) |
240
|
1 |
|
)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return ExprBuilder |
245
|
|
|
*/ |
246
|
2 |
|
public function max() |
247
|
|
|
{ |
248
|
2 |
|
return $this->func(FunctionExpression::FUNC_MAX); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return ExprBuilder |
253
|
|
|
*/ |
254
|
4 |
|
public function sum() |
255
|
|
|
{ |
256
|
4 |
|
return $this->func(FunctionExpression::FUNC_SUM); |
257
|
|
|
} |
258
|
|
|
} |