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