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.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\ORM\Query;
23 * This class is used to generate DQL expressions via a set of PHP static functions
26 * @link www.doctrine-project.org
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
37 * Creates a conjunction of the given boolean expressions.
42 * // (u.type = ?1) AND (u.role = ?2)
43 * $expr->andX($expr->eq('u.type', ':1'), $expr->eq('u.role', ':2'));
45 * @param \Doctrine\ORM\Query\Expr\Comparison |
46 * \Doctrine\ORM\Query\Expr\Func |
47 * \Doctrine\ORM\Query\Expr\Orx
48 * $x Optional clause. Defaults = null, but requires at least one defined when converting to string.
51 public function andX($x = null)
53 return new Expr\Andx(func_get_args());
57 * Creates a disjunction of the given boolean expressions.
62 * // (u.type = ?1) OR (u.role = ?2)
63 * $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2'));
65 * @param mixed $x Optional clause. Defaults = null, but requires
66 * at least one defined when converting to string.
69 public function orX($x = null)
71 return new Expr\Orx(func_get_args());
75 * Creates an ASCending order expression.
78 * @return Expr\OrderBy
80 public function asc($expr)
82 return new Expr\OrderBy($expr, 'ASC');
86 * Creates a DESCending order expression.
89 * @return Expr\OrderBy
91 public function desc($expr)
93 return new Expr\OrderBy($expr, 'DESC');
97 * Creates an equality comparison expression with the given arguments.
99 * First argument is considered the left expression and the second is the right expression.
100 * When converted to string, it will generated a <left expr> = <right expr>. Example:
104 * $expr->eq('u.id', '?1');
106 * @param mixed $x Left expression
107 * @param mixed $y Right expression
108 * @return Expr\Comparison
110 public function eq($x, $y)
112 return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
116 * Creates an instance of Expr\Comparison, with the given arguments.
117 * First argument is considered the left expression and the second is the right expression.
118 * When converted to string, it will generated a <left expr> <> <right expr>. Example:
122 * $q->where($q->expr()->neq('u.id', '?1'));
124 * @param mixed $x Left expression
125 * @param mixed $y Right expression
126 * @return Expr\Comparison
128 public function neq($x, $y)
130 return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
134 * Creates an instance of Expr\Comparison, with the given arguments.
135 * First argument is considered the left expression and the second is the right expression.
136 * When converted to string, it will generated a <left expr> < <right expr>. Example:
140 * $q->where($q->expr()->lt('u.id', '?1'));
142 * @param mixed $x Left expression
143 * @param mixed $y Right expression
144 * @return Expr\Comparison
146 public function lt($x, $y)
148 return new Expr\Comparison($x, Expr\Comparison::LT, $y);
152 * Creates an instance of Expr\Comparison, with the given arguments.
153 * First argument is considered the left expression and the second is the right expression.
154 * When converted to string, it will generated a <left expr> <= <right expr>. Example:
158 * $q->where($q->expr()->lte('u.id', '?1'));
160 * @param mixed $x Left expression
161 * @param mixed $y Right expression
162 * @return Expr\Comparison
164 public function lte($x, $y)
166 return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
170 * Creates an instance of Expr\Comparison, with the given arguments.
171 * First argument is considered the left expression and the second is the right expression.
172 * When converted to string, it will generated a <left expr> > <right expr>. Example:
176 * $q->where($q->expr()->gt('u.id', '?1'));
178 * @param mixed $x Left expression
179 * @param mixed $y Right expression
180 * @return Expr\Comparison
182 public function gt($x, $y)
184 return new Expr\Comparison($x, Expr\Comparison::GT, $y);
188 * Creates an instance of Expr\Comparison, with the given arguments.
189 * First argument is considered the left expression and the second is the right expression.
190 * When converted to string, it will generated a <left expr> >= <right expr>. Example:
194 * $q->where($q->expr()->gte('u.id', '?1'));
196 * @param mixed $x Left expression
197 * @param mixed $y Right expression
198 * @return Expr\Comparison
200 public function gte($x, $y)
202 return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
206 * Creates an instance of AVG() function, with the given argument.
208 * @param mixed $x Argument to be used in AVG() function.
211 public function avg($x)
213 return new Expr\Func('AVG', array($x));
217 * Creates an instance of MAX() function, with the given argument.
219 * @param mixed $x Argument to be used in MAX() function.
222 public function max($x)
224 return new Expr\Func('MAX', array($x));
228 * Creates an instance of MIN() function, with the given argument.
230 * @param mixed $x Argument to be used in MIN() function.
233 public function min($x)
235 return new Expr\Func('MIN', array($x));
239 * Creates an instance of COUNT() function, with the given argument.
241 * @param mixed $x Argument to be used in COUNT() function.
244 public function count($x)
246 return new Expr\Func('COUNT', array($x));
250 * Creates an instance of COUNT(DISTINCT) function, with the given argument.
252 * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
255 public function countDistinct($x)
257 return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
261 * Creates an instance of EXISTS() function, with the given DQL Subquery.
263 * @param mixed $subquery DQL Subquery to be used in EXISTS() function.
266 public function exists($subquery)
268 return new Expr\Func('EXISTS', array($subquery));
272 * Creates an instance of ALL() function, with the given DQL Subquery.
274 * @param mixed $subquery DQL Subquery to be used in ALL() function.
277 public function all($subquery)
279 return new Expr\Func('ALL', array($subquery));
283 * Creates a SOME() function expression with the given DQL subquery.
285 * @param mixed $subquery DQL Subquery to be used in SOME() function.
288 public function some($subquery)
290 return new Expr\Func('SOME', array($subquery));
294 * Creates an ANY() function expression with the given DQL subquery.
296 * @param mixed $subquery DQL Subquery to be used in ANY() function.
299 public function any($subquery)
301 return new Expr\Func('ANY', array($subquery));
305 * Creates a negation expression of the given restriction.
307 * @param mixed $restriction Restriction to be used in NOT() function.
310 public function not($restriction)
312 return new Expr\Func('NOT', array($restriction));
316 * Creates an ABS() function expression with the given argument.
318 * @param mixed $x Argument to be used in ABS() function.
321 public function abs($x)
323 return new Expr\Func('ABS', array($x));
327 * Creates a product mathematical expression with the given arguments.
329 * First argument is considered the left expression and the second is the right expression.
330 * When converted to string, it will generated a <left expr> * <right expr>. Example:
333 * // u.salary * u.percentAnualSalaryIncrease
334 * $q->expr()->prod('u.salary', 'u.percentAnualSalaryIncrease')
336 * @param mixed $x Left expression
337 * @param mixed $y Right expression
340 public function prod($x, $y)
342 return new Expr\Math($x, '*', $y);
346 * Creates a difference mathematical expression with the given arguments.
347 * First argument is considered the left expression and the second is the right expression.
348 * When converted to string, it will generated a <left expr> - <right expr>. Example:
351 * // u.monthlySubscriptionCount - 1
352 * $q->expr()->diff('u.monthlySubscriptionCount', '1')
354 * @param mixed $x Left expression
355 * @param mixed $y Right expression
358 public function diff($x, $y)
360 return new Expr\Math($x, '-', $y);
364 * Creates a sum mathematical expression with the given arguments.
365 * First argument is considered the left expression and the second is the right expression.
366 * When converted to string, it will generated a <left expr> + <right expr>. Example:
369 * // u.numChildren + 1
370 * $q->expr()->diff('u.numChildren', '1')
372 * @param mixed $x Left expression
373 * @param mixed $y Right expression
376 public function sum($x, $y)
378 return new Expr\Math($x, '+', $y);
382 * Creates a quotient mathematical expression with the given arguments.
383 * First argument is considered the left expression and the second is the right expression.
384 * When converted to string, it will generated a <left expr> / <right expr>. Example:
387 * // u.total / u.period
388 * $expr->quot('u.total', 'u.period')
390 * @param mixed $x Left expression
391 * @param mixed $y Right expression
394 public function quot($x, $y)
396 return new Expr\Math($x, '/', $y);
400 * Creates a SQRT() function expression with the given argument.
402 * @param mixed $x Argument to be used in SQRT() function.
405 public function sqrt($x)
407 return new Expr\Func('SQRT', array($x));
411 * Creates an IN() expression with the given arguments.
413 * @param string $x Field in string format to be restricted by IN() function
414 * @param mixed $y Argument to be used in IN() function.
417 public function in($x, $y)
420 foreach ($y as &$literal) {
421 if ( ! ($literal instanceof Expr\Literal)) {
422 $literal = $this->_quoteLiteral($literal);
426 return new Expr\Func($x . ' IN', (array) $y);
430 * Creates a NOT IN() expression with the given arguments.
432 * @param string $x Field in string format to be restricted by NOT IN() function
433 * @param mixed $y Argument to be used in NOT IN() function.
436 public function notIn($x, $y)
439 foreach ($y as &$literal) {
440 if ( ! ($literal instanceof Expr\Literal)) {
441 $literal = $this->_quoteLiteral($literal);
445 return new Expr\Func($x . ' NOT IN', (array) $y);
449 * Creates an IS NULL expression with the given arguments.
451 * @param string $x Field in string format to be restricted by IS NULL
454 public function isNull($x)
456 return $x . ' IS NULL';
460 * Creates an IS NOT NULL expression with the given arguments.
462 * @param string $x Field in string format to be restricted by IS NOT NULL
465 public function isNotNull($x)
467 return $x . ' IS NOT NULL';
471 * Creates a LIKE() comparison expression with the given arguments.
473 * @param string $x Field in string format to be inspected by LIKE() comparison.
474 * @param mixed $y Argument to be used in LIKE() comparison.
475 * @return Expr\Comparison
477 public function like($x, $y)
479 return new Expr\Comparison($x, 'LIKE', $y);
483 * Creates a CONCAT() function expression with the given arguments.
485 * @param mixed $x First argument to be used in CONCAT() function.
486 * @param mixed $x Second argument to be used in CONCAT() function.
489 public function concat($x, $y)
491 return new Expr\Func('CONCAT', array($x, $y));
495 * Creates a SUBSTRING() function expression with the given arguments.
497 * @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function.
498 * @param integer $from Initial offset to start cropping string. May accept negative values.
499 * @param integer $len Length of crop. May accept negative values.
502 public function substring($x, $from, $len = null)
504 $args = array($x, $from);
508 return new Expr\Func('SUBSTRING', $args);
512 * Creates a LOWER() function expression with the given argument.
514 * @param mixed $x Argument to be used in LOWER() function.
515 * @return Expr\Func A LOWER function expression.
517 public function lower($x)
519 return new Expr\Func('LOWER', array($x));
523 * Creates an UPPER() function expression with the given argument.
525 * @param mixed $x Argument to be used in UPPER() function.
526 * @return Expr\Func An UPPER function expression.
528 public function upper($x)
530 return new Expr\Func('UPPER', array($x));
534 * Creates a LENGTH() function expression with the given argument.
536 * @param mixed $x Argument to be used as argument of LENGTH() function.
537 * @return Expr\Func A LENGTH function expression.
539 public function length($x)
541 return new Expr\Func('LENGTH', array($x));
545 * Creates a literal expression of the given argument.
547 * @param mixed $literal Argument to be converted to literal.
548 * @return Expr\Literal
550 public function literal($literal)
552 return new Expr\Literal($this->_quoteLiteral($literal));
556 * Quotes a literal value, if necessary, according to the DQL syntax.
558 * @param mixed $literal The literal value.
561 private function _quoteLiteral($literal)
563 if (is_numeric($literal) && !is_string($literal)) {
564 return (string) $literal;
565 } else if (is_bool($literal)) {
566 return $literal ? "true" : "false";
568 return "'" . str_replace("'", "''", $literal) . "'";
573 * Creates an instance of BETWEEN() function, with the given argument.
575 * @param mixed $val Valued to be inspected by range values.
576 * @param integer $x Starting range value to be used in BETWEEN() function.
577 * @param integer $y End point value to be used in BETWEEN() function.
578 * @return Expr\Func A BETWEEN expression.
580 public function between($val, $x, $y)
582 return $val . ' BETWEEN ' . $x . ' AND ' . $y;
586 * Creates an instance of TRIM() function, with the given argument.
588 * @param mixed $x Argument to be used as argument of TRIM() function.
589 * @return Expr\Func a TRIM expression.
591 public function trim($x)
593 return new Expr\Func('TRIM', $x);