|
1 <?php |
|
2 /* |
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14 * |
|
15 * This software consists of voluntary contributions made by many individuals |
|
16 * and is licensed under the LGPL. For more information, see |
|
17 * <http://www.doctrine-project.org>. |
|
18 */ |
|
19 |
|
20 namespace Doctrine\ORM\Query; |
|
21 |
|
22 /** |
|
23 * This class is used to generate DQL expressions via a set of PHP static functions |
|
24 * |
|
25 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
26 * @link www.doctrine-project.org |
|
27 * @since 2.0 |
|
28 * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
|
29 * @author Jonathan Wage <jonwage@gmail.com> |
|
30 * @author Roman Borschel <roman@code-factory.org> |
|
31 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
32 * @todo Rename: ExpressionBuilder |
|
33 */ |
|
34 class Expr |
|
35 { |
|
36 /** |
|
37 * Creates a conjunction of the given boolean expressions. |
|
38 * |
|
39 * Example: |
|
40 * |
|
41 * [php] |
|
42 * // (u.type = ?1) AND (u.role = ?2) |
|
43 * $expr->andX('u.type = ?1', 'u.role = ?2')); |
|
44 * |
|
45 * @param mixed $x Optional clause. Defaults = null, but requires |
|
46 * at least one defined when converting to string. |
|
47 * @return Expr\Andx |
|
48 */ |
|
49 public function andX($x = null) |
|
50 { |
|
51 return new Expr\Andx(func_get_args()); |
|
52 } |
|
53 |
|
54 /** |
|
55 * Creates a disjunction of the given boolean expressions. |
|
56 * |
|
57 * Example: |
|
58 * |
|
59 * [php] |
|
60 * // (u.type = ?1) OR (u.role = ?2) |
|
61 * $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2')); |
|
62 * |
|
63 * @param mixed $x Optional clause. Defaults = null, but requires |
|
64 * at least one defined when converting to string. |
|
65 * @return Expr\Orx |
|
66 */ |
|
67 public function orX($x = null) |
|
68 { |
|
69 return new Expr\Orx(func_get_args()); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Creates an ASCending order expression. |
|
74 * |
|
75 * @param $sort |
|
76 * @return OrderBy |
|
77 */ |
|
78 public function asc($expr) |
|
79 { |
|
80 return new Expr\OrderBy($expr, 'ASC'); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Creates a DESCending order expression. |
|
85 * |
|
86 * @param $sort |
|
87 * @return OrderBy |
|
88 */ |
|
89 public function desc($expr) |
|
90 { |
|
91 return new Expr\OrderBy($expr, 'DESC'); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Creates an equality comparison expression with the given arguments. |
|
96 * |
|
97 * First argument is considered the left expression and the second is the right expression. |
|
98 * When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
99 * |
|
100 * [php] |
|
101 * // u.id = ?1 |
|
102 * $expr->eq('u.id', '?1'); |
|
103 * |
|
104 * @param mixed $x Left expression |
|
105 * @param mixed $y Right expression |
|
106 * @return Expr\Comparison |
|
107 */ |
|
108 public function eq($x, $y) |
|
109 { |
|
110 return new Expr\Comparison($x, Expr\Comparison::EQ, $y); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Creates an instance of Expr\Comparison, with the given arguments. |
|
115 * First argument is considered the left expression and the second is the right expression. |
|
116 * When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
117 * |
|
118 * [php] |
|
119 * // u.id <> ?1 |
|
120 * $q->where($q->expr()->neq('u.id', '?1')); |
|
121 * |
|
122 * @param mixed $x Left expression |
|
123 * @param mixed $y Right expression |
|
124 * @return Expr\Comparison |
|
125 */ |
|
126 public function neq($x, $y) |
|
127 { |
|
128 return new Expr\Comparison($x, Expr\Comparison::NEQ, $y); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Creates an instance of Expr\Comparison, with the given arguments. |
|
133 * First argument is considered the left expression and the second is the right expression. |
|
134 * When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
135 * |
|
136 * [php] |
|
137 * // u.id < ?1 |
|
138 * $q->where($q->expr()->lt('u.id', '?1')); |
|
139 * |
|
140 * @param mixed $x Left expression |
|
141 * @param mixed $y Right expression |
|
142 * @return Expr\Comparison |
|
143 */ |
|
144 public function lt($x, $y) |
|
145 { |
|
146 return new Expr\Comparison($x, Expr\Comparison::LT, $y); |
|
147 } |
|
148 |
|
149 /** |
|
150 * Creates an instance of Expr\Comparison, with the given arguments. |
|
151 * First argument is considered the left expression and the second is the right expression. |
|
152 * When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
153 * |
|
154 * [php] |
|
155 * // u.id <= ?1 |
|
156 * $q->where($q->expr()->lte('u.id', '?1')); |
|
157 * |
|
158 * @param mixed $x Left expression |
|
159 * @param mixed $y Right expression |
|
160 * @return Expr\Comparison |
|
161 */ |
|
162 public function lte($x, $y) |
|
163 { |
|
164 return new Expr\Comparison($x, Expr\Comparison::LTE, $y); |
|
165 } |
|
166 |
|
167 /** |
|
168 * Creates an instance of Expr\Comparison, with the given arguments. |
|
169 * First argument is considered the left expression and the second is the right expression. |
|
170 * When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
171 * |
|
172 * [php] |
|
173 * // u.id > ?1 |
|
174 * $q->where($q->expr()->gt('u.id', '?1')); |
|
175 * |
|
176 * @param mixed $x Left expression |
|
177 * @param mixed $y Right expression |
|
178 * @return Expr\Comparison |
|
179 */ |
|
180 public function gt($x, $y) |
|
181 { |
|
182 return new Expr\Comparison($x, Expr\Comparison::GT, $y); |
|
183 } |
|
184 |
|
185 /** |
|
186 * Creates an instance of Expr\Comparison, with the given arguments. |
|
187 * First argument is considered the left expression and the second is the right expression. |
|
188 * When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
189 * |
|
190 * [php] |
|
191 * // u.id >= ?1 |
|
192 * $q->where($q->expr()->gte('u.id', '?1')); |
|
193 * |
|
194 * @param mixed $x Left expression |
|
195 * @param mixed $y Right expression |
|
196 * @return Expr\Comparison |
|
197 */ |
|
198 public function gte($x, $y) |
|
199 { |
|
200 return new Expr\Comparison($x, Expr\Comparison::GTE, $y); |
|
201 } |
|
202 |
|
203 /** |
|
204 * Creates an instance of AVG() function, with the given argument. |
|
205 * |
|
206 * @param mixed $x Argument to be used in AVG() function. |
|
207 * @return Expr\Func |
|
208 */ |
|
209 public function avg($x) |
|
210 { |
|
211 return new Expr\Func('AVG', array($x)); |
|
212 } |
|
213 |
|
214 /** |
|
215 * Creates an instance of MAX() function, with the given argument. |
|
216 * |
|
217 * @param mixed $x Argument to be used in MAX() function. |
|
218 * @return Expr\Func |
|
219 */ |
|
220 public function max($x) |
|
221 { |
|
222 return new Expr\Func('MAX', array($x)); |
|
223 } |
|
224 |
|
225 /** |
|
226 * Creates an instance of MIN() function, with the given argument. |
|
227 * |
|
228 * @param mixed $x Argument to be used in MIN() function. |
|
229 * @return Expr\Func |
|
230 */ |
|
231 public function min($x) |
|
232 { |
|
233 return new Expr\Func('MIN', array($x)); |
|
234 } |
|
235 |
|
236 /** |
|
237 * Creates an instance of COUNT() function, with the given argument. |
|
238 * |
|
239 * @param mixed $x Argument to be used in COUNT() function. |
|
240 * @return Expr\Func |
|
241 */ |
|
242 public function count($x) |
|
243 { |
|
244 return new Expr\Func('COUNT', array($x)); |
|
245 } |
|
246 |
|
247 /** |
|
248 * Creates an instance of COUNT(DISTINCT) function, with the given argument. |
|
249 * |
|
250 * @param mixed $x Argument to be used in COUNT(DISTINCT) function. |
|
251 * @return string |
|
252 */ |
|
253 public function countDistinct($x) |
|
254 { |
|
255 return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')'; |
|
256 } |
|
257 |
|
258 /** |
|
259 * Creates an instance of EXISTS() function, with the given DQL Subquery. |
|
260 * |
|
261 * @param mixed $subquery DQL Subquery to be used in EXISTS() function. |
|
262 * @return Expr\Func |
|
263 */ |
|
264 public function exists($subquery) |
|
265 { |
|
266 return new Expr\Func('EXISTS', array($subquery)); |
|
267 } |
|
268 |
|
269 /** |
|
270 * Creates an instance of ALL() function, with the given DQL Subquery. |
|
271 * |
|
272 * @param mixed $subquery DQL Subquery to be used in ALL() function. |
|
273 * @return Expr\Func |
|
274 */ |
|
275 public function all($subquery) |
|
276 { |
|
277 return new Expr\Func('ALL', array($subquery)); |
|
278 } |
|
279 |
|
280 /** |
|
281 * Creates a SOME() function expression with the given DQL subquery. |
|
282 * |
|
283 * @param mixed $subquery DQL Subquery to be used in SOME() function. |
|
284 * @return Expr\Func |
|
285 */ |
|
286 public function some($subquery) |
|
287 { |
|
288 return new Expr\Func('SOME', array($subquery)); |
|
289 } |
|
290 |
|
291 /** |
|
292 * Creates an ANY() function expression with the given DQL subquery. |
|
293 * |
|
294 * @param mixed $subquery DQL Subquery to be used in ANY() function. |
|
295 * @return Expr\Func |
|
296 */ |
|
297 public function any($subquery) |
|
298 { |
|
299 return new Expr\Func('ANY', array($subquery)); |
|
300 } |
|
301 |
|
302 /** |
|
303 * Creates a negation expression of the given restriction. |
|
304 * |
|
305 * @param mixed $restriction Restriction to be used in NOT() function. |
|
306 * @return Expr\Func |
|
307 */ |
|
308 public function not($restriction) |
|
309 { |
|
310 return new Expr\Func('NOT', array($restriction)); |
|
311 } |
|
312 |
|
313 /** |
|
314 * Creates an ABS() function expression with the given argument. |
|
315 * |
|
316 * @param mixed $x Argument to be used in ABS() function. |
|
317 * @return Expr\Func |
|
318 */ |
|
319 public function abs($x) |
|
320 { |
|
321 return new Expr\Func('ABS', array($x)); |
|
322 } |
|
323 |
|
324 /** |
|
325 * Creates a product mathematical expression with the given arguments. |
|
326 * |
|
327 * First argument is considered the left expression and the second is the right expression. |
|
328 * When converted to string, it will generated a <left expr> * <right expr>. Example: |
|
329 * |
|
330 * [php] |
|
331 * // u.salary * u.percentAnualSalaryIncrease |
|
332 * $q->expr()->prod('u.salary', 'u.percentAnualSalaryIncrease') |
|
333 * |
|
334 * @param mixed $x Left expression |
|
335 * @param mixed $y Right expression |
|
336 * @return Expr\Math |
|
337 */ |
|
338 public function prod($x, $y) |
|
339 { |
|
340 return new Expr\Math($x, '*', $y); |
|
341 } |
|
342 |
|
343 /** |
|
344 * Creates a difference mathematical expression with the given arguments. |
|
345 * First argument is considered the left expression and the second is the right expression. |
|
346 * When converted to string, it will generated a <left expr> - <right expr>. Example: |
|
347 * |
|
348 * [php] |
|
349 * // u.monthlySubscriptionCount - 1 |
|
350 * $q->expr()->diff('u.monthlySubscriptionCount', '1') |
|
351 * |
|
352 * @param mixed $x Left expression |
|
353 * @param mixed $y Right expression |
|
354 * @return Expr\Math |
|
355 */ |
|
356 public function diff($x, $y) |
|
357 { |
|
358 return new Expr\Math($x, '-', $y); |
|
359 } |
|
360 |
|
361 /** |
|
362 * Creates a sum mathematical expression with the given arguments. |
|
363 * First argument is considered the left expression and the second is the right expression. |
|
364 * When converted to string, it will generated a <left expr> + <right expr>. Example: |
|
365 * |
|
366 * [php] |
|
367 * // u.numChildren + 1 |
|
368 * $q->expr()->diff('u.numChildren', '1') |
|
369 * |
|
370 * @param mixed $x Left expression |
|
371 * @param mixed $y Right expression |
|
372 * @return Expr\Math |
|
373 */ |
|
374 public function sum($x, $y) |
|
375 { |
|
376 return new Expr\Math($x, '+', $y); |
|
377 } |
|
378 |
|
379 /** |
|
380 * Creates a quotient mathematical expression with the given arguments. |
|
381 * First argument is considered the left expression and the second is the right expression. |
|
382 * When converted to string, it will generated a <left expr> / <right expr>. Example: |
|
383 * |
|
384 * [php] |
|
385 * // u.total / u.period |
|
386 * $expr->quot('u.total', 'u.period') |
|
387 * |
|
388 * @param mixed $x Left expression |
|
389 * @param mixed $y Right expression |
|
390 * @return Expr\Math |
|
391 */ |
|
392 public function quot($x, $y) |
|
393 { |
|
394 return new Expr\Math($x, '/', $y); |
|
395 } |
|
396 |
|
397 /** |
|
398 * Creates a SQRT() function expression with the given argument. |
|
399 * |
|
400 * @param mixed $x Argument to be used in SQRT() function. |
|
401 * @return Expr\Func |
|
402 */ |
|
403 public function sqrt($x) |
|
404 { |
|
405 return new Expr\Func('SQRT', array($x)); |
|
406 } |
|
407 |
|
408 /** |
|
409 * Creates an IN() expression with the given arguments. |
|
410 * |
|
411 * @param string $x Field in string format to be restricted by IN() function |
|
412 * @param mixed $y Argument to be used in IN() function. |
|
413 * @return Expr\Func |
|
414 */ |
|
415 public function in($x, $y) |
|
416 { |
|
417 if (is_array($y)) { |
|
418 foreach ($y as &$literal) { |
|
419 if ( ! ($literal instanceof Expr\Literal)) { |
|
420 $literal = $this->_quoteLiteral($literal); |
|
421 } |
|
422 } |
|
423 } |
|
424 return new Expr\Func($x . ' IN', (array) $y); |
|
425 } |
|
426 |
|
427 /** |
|
428 * Creates a NOT IN() expression with the given arguments. |
|
429 * |
|
430 * @param string $x Field in string format to be restricted by NOT IN() function |
|
431 * @param mixed $y Argument to be used in NOT IN() function. |
|
432 * @return Expr\Func |
|
433 */ |
|
434 public function notIn($x, $y) |
|
435 { |
|
436 if (is_array($y)) { |
|
437 foreach ($y as &$literal) { |
|
438 if ( ! ($literal instanceof Expr\Literal)) { |
|
439 $literal = $this->_quoteLiteral($literal); |
|
440 } |
|
441 } |
|
442 } |
|
443 return new Expr\Func($x . ' NOT IN', (array) $y); |
|
444 } |
|
445 |
|
446 /** |
|
447 * Creates an IS NULL expression with the given arguments. |
|
448 * |
|
449 * @param string $x Field in string format to be restricted by IS NULL |
|
450 * @return string |
|
451 */ |
|
452 public function isNull($x) |
|
453 { |
|
454 return $x . ' IS NULL'; |
|
455 } |
|
456 |
|
457 /** |
|
458 * Creates an IS NOT NULL expression with the given arguments. |
|
459 * |
|
460 * @param string $x Field in string format to be restricted by IS NOT NULL |
|
461 * @return string |
|
462 */ |
|
463 public function isNotNull($x) |
|
464 { |
|
465 return $x . ' IS NOT NULL'; |
|
466 } |
|
467 |
|
468 /** |
|
469 * Creates a LIKE() comparison expression with the given arguments. |
|
470 * |
|
471 * @param string $x Field in string format to be inspected by LIKE() comparison. |
|
472 * @param mixed $y Argument to be used in LIKE() comparison. |
|
473 * @return Expr\Comparison |
|
474 */ |
|
475 public function like($x, $y) |
|
476 { |
|
477 return new Expr\Comparison($x, 'LIKE', $y); |
|
478 } |
|
479 |
|
480 /** |
|
481 * Creates a CONCAT() function expression with the given arguments. |
|
482 * |
|
483 * @param mixed $x First argument to be used in CONCAT() function. |
|
484 * @param mixed $x Second argument to be used in CONCAT() function. |
|
485 * @return Expr\Func |
|
486 */ |
|
487 public function concat($x, $y) |
|
488 { |
|
489 return new Expr\Func('CONCAT', array($x, $y)); |
|
490 } |
|
491 |
|
492 /** |
|
493 * Creates a SUBSTRING() function expression with the given arguments. |
|
494 * |
|
495 * @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function. |
|
496 * @param integer $from Initial offset to start cropping string. May accept negative values. |
|
497 * @param integer $len Length of crop. May accept negative values. |
|
498 * @return Expr\Func |
|
499 */ |
|
500 public function substring($x, $from, $len = null) |
|
501 { |
|
502 $args = array($x, $from); |
|
503 if (null !== $len) { |
|
504 $args[] = $len; |
|
505 } |
|
506 return new Expr\Func('SUBSTRING', $args); |
|
507 } |
|
508 |
|
509 /** |
|
510 * Creates a LOWER() function expression with the given argument. |
|
511 * |
|
512 * @param mixed $x Argument to be used in LOWER() function. |
|
513 * @return Expr\Func A LOWER function expression. |
|
514 */ |
|
515 public function lower($x) |
|
516 { |
|
517 return new Expr\Func('LOWER', array($x)); |
|
518 } |
|
519 |
|
520 /** |
|
521 * Creates an UPPER() function expression with the given argument. |
|
522 * |
|
523 * @param mixed $x Argument to be used in UPPER() function. |
|
524 * @return Expr\Func An UPPER function expression. |
|
525 */ |
|
526 public function upper($x) |
|
527 { |
|
528 return new Expr\Func('UPPER', array($x)); |
|
529 } |
|
530 |
|
531 /** |
|
532 * Creates a LENGTH() function expression with the given argument. |
|
533 * |
|
534 * @param mixed $x Argument to be used as argument of LENGTH() function. |
|
535 * @return Expr\Func A LENGTH function expression. |
|
536 */ |
|
537 public function length($x) |
|
538 { |
|
539 return new Expr\Func('LENGTH', array($x)); |
|
540 } |
|
541 |
|
542 /** |
|
543 * Creates a literal expression of the given argument. |
|
544 * |
|
545 * @param mixed $literal Argument to be converted to literal. |
|
546 * @return Expr\Literal |
|
547 */ |
|
548 public function literal($literal) |
|
549 { |
|
550 return new Expr\Literal($this->_quoteLiteral($literal)); |
|
551 } |
|
552 |
|
553 /** |
|
554 * Quotes a literal value, if necessary, according to the DQL syntax. |
|
555 * |
|
556 * @param mixed $literal The literal value. |
|
557 * @return string |
|
558 */ |
|
559 private function _quoteLiteral($literal) |
|
560 { |
|
561 if (is_numeric($literal) && !is_string($literal)) { |
|
562 return (string) $literal; |
|
563 } else { |
|
564 return "'" . str_replace("'", "''", $literal) . "'"; |
|
565 } |
|
566 } |
|
567 |
|
568 /** |
|
569 * Creates an instance of BETWEEN() function, with the given argument. |
|
570 * |
|
571 * @param mixed $val Valued to be inspected by range values. |
|
572 * @param integer $x Starting range value to be used in BETWEEN() function. |
|
573 * @param integer $y End point value to be used in BETWEEN() function. |
|
574 * @return Expr\Func A BETWEEN expression. |
|
575 */ |
|
576 public function between($val, $x, $y) |
|
577 { |
|
578 return $val . ' BETWEEN ' . $x . ' AND ' . $y; |
|
579 } |
|
580 |
|
581 /** |
|
582 * Creates an instance of TRIM() function, with the given argument. |
|
583 * |
|
584 * @param mixed $x Argument to be used as argument of TRIM() function. |
|
585 * @return Expr\Func a TRIM expression. |
|
586 */ |
|
587 public function trim($x) |
|
588 { |
|
589 return new Expr\Func('TRIM', $x); |
|
590 } |
|
591 } |