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