|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Query\Expression; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use function func_get_arg; |
|
7
|
|
|
use function func_get_args; |
|
8
|
|
|
use function func_num_args; |
|
9
|
|
|
use function implode; |
|
10
|
|
|
use function sprintf; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* ExpressionBuilder class is responsible to dynamically create SQL query parts. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ExpressionBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
public const EQ = '='; |
|
18
|
|
|
public const NEQ = '<>'; |
|
19
|
|
|
public const LT = '<'; |
|
20
|
|
|
public const LTE = '<='; |
|
21
|
|
|
public const GT = '>'; |
|
22
|
|
|
public const GTE = '>='; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The DBAL Connection. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Connection |
|
28
|
|
|
*/ |
|
29
|
|
|
private $connection; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes a new <tt>ExpressionBuilder</tt>. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Connection $connection The DBAL Connection. |
|
35
|
|
|
*/ |
|
36
|
5138 |
|
public function __construct(Connection $connection) |
|
37
|
|
|
{ |
|
38
|
5138 |
|
$this->connection = $connection; |
|
39
|
5138 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a conjunction of the given expressions. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|CompositeExpression $expression |
|
45
|
|
|
* @param string|CompositeExpression ...$expressions |
|
46
|
|
|
*/ |
|
47
|
2514 |
|
public function and($expression, ...$expressions) : CompositeExpression |
|
48
|
|
|
{ |
|
49
|
2514 |
|
return CompositeExpression::and($expression, ...$expressions); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates a disjunction of the given expressions. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string|CompositeExpression $expression |
|
56
|
|
|
* @param string|CompositeExpression ...$expressions |
|
57
|
|
|
*/ |
|
58
|
2283 |
|
public function or($expression, ...$expressions) : CompositeExpression |
|
59
|
|
|
{ |
|
60
|
2283 |
|
return CompositeExpression::or($expression, ...$expressions); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @deprecated Use `and()` instead. |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $x Optional clause. Defaults = null, but requires |
|
67
|
|
|
* at least one defined when converting to string. |
|
68
|
|
|
* |
|
69
|
|
|
* @return CompositeExpression |
|
70
|
|
|
*/ |
|
71
|
2398 |
|
public function andX($x = null) |
|
72
|
|
|
{ |
|
73
|
2398 |
|
return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @deprecated Use `or()` instead. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $x Optional clause. Defaults = null, but requires |
|
80
|
|
|
* at least one defined when converting to string. |
|
81
|
|
|
* |
|
82
|
|
|
* @return CompositeExpression |
|
83
|
|
|
*/ |
|
84
|
2168 |
|
public function orX($x = null) |
|
85
|
|
|
{ |
|
86
|
2168 |
|
return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Creates a comparison expression. |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $x The left expression. |
|
93
|
|
|
* @param string $operator One of the ExpressionBuilder::* constants. |
|
94
|
|
|
* @param mixed $y The right expression. |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
2075 |
|
public function comparison($x, $operator, $y) |
|
99
|
|
|
{ |
|
100
|
2075 |
|
return $x . ' ' . $operator . ' ' . $y; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Creates an equality comparison expression with the given arguments. |
|
105
|
|
|
* |
|
106
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
107
|
|
|
* When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
108
|
|
|
* |
|
109
|
|
|
* [php] |
|
110
|
|
|
* // u.id = ? |
|
111
|
|
|
* $expr->eq('u.id', '?'); |
|
112
|
|
|
* |
|
113
|
|
|
* @param mixed $x The left expression. |
|
114
|
|
|
* @param mixed $y The right expression. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
2033 |
|
public function eq($x, $y) |
|
119
|
|
|
{ |
|
120
|
2033 |
|
return $this->comparison($x, self::EQ, $y); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Creates a non equality comparison expression with the given arguments. |
|
125
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
126
|
|
|
* When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
127
|
|
|
* |
|
128
|
|
|
* [php] |
|
129
|
|
|
* // u.id <> 1 |
|
130
|
|
|
* $q->where($q->expr()->neq('u.id', '1')); |
|
131
|
|
|
* |
|
132
|
|
|
* @param mixed $x The left expression. |
|
133
|
|
|
* @param mixed $y The right expression. |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function neq($x, $y) |
|
138
|
|
|
{ |
|
139
|
1 |
|
return $this->comparison($x, self::NEQ, $y); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Creates a lower-than comparison expression with the given arguments. |
|
144
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
145
|
|
|
* When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
146
|
|
|
* |
|
147
|
|
|
* [php] |
|
148
|
|
|
* // u.id < ? |
|
149
|
|
|
* $q->where($q->expr()->lt('u.id', '?')); |
|
150
|
|
|
* |
|
151
|
|
|
* @param mixed $x The left expression. |
|
152
|
|
|
* @param mixed $y The right expression. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function lt($x, $y) |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->comparison($x, self::LT, $y); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Creates a lower-than-equal comparison expression with the given arguments. |
|
163
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
164
|
|
|
* When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
165
|
|
|
* |
|
166
|
|
|
* [php] |
|
167
|
|
|
* // u.id <= ? |
|
168
|
|
|
* $q->where($q->expr()->lte('u.id', '?')); |
|
169
|
|
|
* |
|
170
|
|
|
* @param mixed $x The left expression. |
|
171
|
|
|
* @param mixed $y The right expression. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function lte($x, $y) |
|
176
|
|
|
{ |
|
177
|
1 |
|
return $this->comparison($x, self::LTE, $y); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Creates a greater-than comparison expression with the given arguments. |
|
182
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
183
|
|
|
* When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
184
|
|
|
* |
|
185
|
|
|
* [php] |
|
186
|
|
|
* // u.id > ? |
|
187
|
|
|
* $q->where($q->expr()->gt('u.id', '?')); |
|
188
|
|
|
* |
|
189
|
|
|
* @param mixed $x The left expression. |
|
190
|
|
|
* @param mixed $y The right expression. |
|
191
|
|
|
* |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
1 |
|
public function gt($x, $y) |
|
195
|
|
|
{ |
|
196
|
1 |
|
return $this->comparison($x, self::GT, $y); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Creates a greater-than-equal comparison expression with the given arguments. |
|
201
|
|
|
* First argument is considered the left expression and the second is the right expression. |
|
202
|
|
|
* When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
203
|
|
|
* |
|
204
|
|
|
* [php] |
|
205
|
|
|
* // u.id >= ? |
|
206
|
|
|
* $q->where($q->expr()->gte('u.id', '?')); |
|
207
|
|
|
* |
|
208
|
|
|
* @param mixed $x The left expression. |
|
209
|
|
|
* @param mixed $y The right expression. |
|
210
|
|
|
* |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function gte($x, $y) |
|
214
|
|
|
{ |
|
215
|
1 |
|
return $this->comparison($x, self::GTE, $y); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Creates an IS NULL expression with the given arguments. |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $x The field in string format to be restricted by IS NULL. |
|
222
|
|
|
* |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
2002 |
|
public function isNull($x) |
|
226
|
|
|
{ |
|
227
|
2002 |
|
return $x . ' IS NULL'; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Creates an IS NOT NULL expression with the given arguments. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $x The field in string format to be restricted by IS NOT NULL. |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function isNotNull($x) |
|
238
|
|
|
{ |
|
239
|
1 |
|
return $x . ' IS NOT NULL'; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Creates a LIKE() comparison expression with the given arguments. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $x Field in string format to be inspected by LIKE() comparison. |
|
246
|
|
|
* @param mixed $y Argument to be used in LIKE() comparison. |
|
247
|
|
|
* |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
1957 |
|
public function like($x, $y/*, ?string $escapeChar = null */) |
|
251
|
|
|
{ |
|
252
|
1957 |
|
return $this->comparison($x, 'LIKE', $y) . |
|
253
|
1957 |
|
(func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : ''); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Creates a NOT LIKE() comparison expression with the given arguments. |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $x Field in string format to be inspected by NOT LIKE() comparison. |
|
260
|
|
|
* @param mixed $y Argument to be used in NOT LIKE() comparison. |
|
261
|
|
|
* |
|
262
|
|
|
* @return string |
|
263
|
|
|
*/ |
|
264
|
1911 |
|
public function notLike($x, $y/*, ?string $escapeChar = null */) |
|
265
|
|
|
{ |
|
266
|
1911 |
|
return $this->comparison($x, 'NOT LIKE', $y) . |
|
267
|
1911 |
|
(func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : ''); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Creates a IN () comparison expression with the given arguments. |
|
272
|
|
|
* |
|
273
|
|
|
* @param string $x The field in string format to be inspected by IN() comparison. |
|
274
|
|
|
* @param string|string[] $y The placeholder or the array of values to be used by IN() comparison. |
|
275
|
|
|
* |
|
276
|
|
|
* @return string |
|
277
|
|
|
*/ |
|
278
|
1980 |
|
public function in($x, $y) |
|
279
|
|
|
{ |
|
280
|
1980 |
|
return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')'); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Creates a NOT IN () comparison expression with the given arguments. |
|
285
|
|
|
* |
|
286
|
|
|
* @param string $x The field in string format to be inspected by NOT IN() comparison. |
|
287
|
|
|
* @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison. |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
2 |
|
public function notIn($x, $y) |
|
292
|
|
|
{ |
|
293
|
2 |
|
return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')'); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Quotes a given input parameter. |
|
298
|
|
|
* |
|
299
|
|
|
* @param mixed $input The parameter to be quoted. |
|
300
|
|
|
* @param int|null $type The type of the parameter. |
|
301
|
|
|
* |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
public function literal($input, $type = null) |
|
305
|
|
|
{ |
|
306
|
|
|
return $this->connection->quote($input, $type); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|