123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- <?php
- /*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\ORM\Query;
- use Traversable;
- use function func_get_args;
- use function implode;
- use function is_bool;
- use function is_iterable;
- use function is_numeric;
- use function is_string;
- use function iterator_to_array;
- use function str_replace;
- /**
- * This class is used to generate DQL expressions via a set of PHP static functions.
- *
- * @link www.doctrine-project.org
- *
- * @todo Rename: ExpressionBuilder
- */
- class Expr
- {
- /**
- * Creates a conjunction of the given boolean expressions.
- *
- * Example:
- *
- * [php]
- * // (u.type = ?1) AND (u.role = ?2)
- * $expr->andX($expr->eq('u.type', ':1'), $expr->eq('u.role', ':2'));
- *
- * @param Expr\Comparison|Expr\Func|Expr\Andx|Expr\Orx|string $x Optional clause. Defaults to null,
- * but requires at least one defined
- * when converting to string.
- *
- * @return Expr\Andx
- */
- public function andX($x = null)
- {
- return new Expr\Andx(func_get_args());
- }
- /**
- * Creates a disjunction of the given boolean expressions.
- *
- * Example:
- *
- * [php]
- * // (u.type = ?1) OR (u.role = ?2)
- * $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2'));
- *
- * @param Expr\Comparison|Expr\Func|Expr\Andx|Expr\Orx|string $x Optional clause. Defaults to null,
- * but requires at least one defined
- * when converting to string.
- *
- * @return Expr\Orx
- */
- public function orX($x = null)
- {
- return new Expr\Orx(func_get_args());
- }
- /**
- * Creates an ASCending order expression.
- *
- * @param mixed $expr
- *
- * @return Expr\OrderBy
- */
- public function asc($expr)
- {
- return new Expr\OrderBy($expr, 'ASC');
- }
- /**
- * Creates a DESCending order expression.
- *
- * @param mixed $expr
- *
- * @return Expr\OrderBy
- */
- public function desc($expr)
- {
- return new Expr\OrderBy($expr, 'DESC');
- }
- /**
- * Creates an equality comparison expression with the given arguments.
- *
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> = <right expr>. Example:
- *
- * [php]
- * // u.id = ?1
- * $expr->eq('u.id', '?1');
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function eq($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
- }
- /**
- * Creates an instance of Expr\Comparison, with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> <> <right expr>. Example:
- *
- * [php]
- * // u.id <> ?1
- * $q->where($q->expr()->neq('u.id', '?1'));
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function neq($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
- }
- /**
- * Creates an instance of Expr\Comparison, with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> < <right expr>. Example:
- *
- * [php]
- * // u.id < ?1
- * $q->where($q->expr()->lt('u.id', '?1'));
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function lt($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::LT, $y);
- }
- /**
- * Creates an instance of Expr\Comparison, with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> <= <right expr>. Example:
- *
- * [php]
- * // u.id <= ?1
- * $q->where($q->expr()->lte('u.id', '?1'));
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function lte($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
- }
- /**
- * Creates an instance of Expr\Comparison, with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> > <right expr>. Example:
- *
- * [php]
- * // u.id > ?1
- * $q->where($q->expr()->gt('u.id', '?1'));
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function gt($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::GT, $y);
- }
- /**
- * Creates an instance of Expr\Comparison, with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> >= <right expr>. Example:
- *
- * [php]
- * // u.id >= ?1
- * $q->where($q->expr()->gte('u.id', '?1'));
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Comparison
- */
- public function gte($x, $y)
- {
- return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
- }
- /**
- * Creates an instance of AVG() function, with the given argument.
- *
- * @param mixed $x Argument to be used in AVG() function.
- *
- * @return Expr\Func
- */
- public function avg($x)
- {
- return new Expr\Func('AVG', [$x]);
- }
- /**
- * Creates an instance of MAX() function, with the given argument.
- *
- * @param mixed $x Argument to be used in MAX() function.
- *
- * @return Expr\Func
- */
- public function max($x)
- {
- return new Expr\Func('MAX', [$x]);
- }
- /**
- * Creates an instance of MIN() function, with the given argument.
- *
- * @param mixed $x Argument to be used in MIN() function.
- *
- * @return Expr\Func
- */
- public function min($x)
- {
- return new Expr\Func('MIN', [$x]);
- }
- /**
- * Creates an instance of COUNT() function, with the given argument.
- *
- * @param mixed $x Argument to be used in COUNT() function.
- *
- * @return Expr\Func
- */
- public function count($x)
- {
- return new Expr\Func('COUNT', [$x]);
- }
- /**
- * Creates an instance of COUNT(DISTINCT) function, with the given argument.
- *
- * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
- *
- * @return string
- */
- public function countDistinct($x)
- {
- return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
- }
- /**
- * Creates an instance of EXISTS() function, with the given DQL Subquery.
- *
- * @param mixed $subquery DQL Subquery to be used in EXISTS() function.
- *
- * @return Expr\Func
- */
- public function exists($subquery)
- {
- return new Expr\Func('EXISTS', [$subquery]);
- }
- /**
- * Creates an instance of ALL() function, with the given DQL Subquery.
- *
- * @param mixed $subquery DQL Subquery to be used in ALL() function.
- *
- * @return Expr\Func
- */
- public function all($subquery)
- {
- return new Expr\Func('ALL', [$subquery]);
- }
- /**
- * Creates a SOME() function expression with the given DQL subquery.
- *
- * @param mixed $subquery DQL Subquery to be used in SOME() function.
- *
- * @return Expr\Func
- */
- public function some($subquery)
- {
- return new Expr\Func('SOME', [$subquery]);
- }
- /**
- * Creates an ANY() function expression with the given DQL subquery.
- *
- * @param mixed $subquery DQL Subquery to be used in ANY() function.
- *
- * @return Expr\Func
- */
- public function any($subquery)
- {
- return new Expr\Func('ANY', [$subquery]);
- }
- /**
- * Creates a negation expression of the given restriction.
- *
- * @param mixed $restriction Restriction to be used in NOT() function.
- *
- * @return Expr\Func
- */
- public function not($restriction)
- {
- return new Expr\Func('NOT', [$restriction]);
- }
- /**
- * Creates an ABS() function expression with the given argument.
- *
- * @param mixed $x Argument to be used in ABS() function.
- *
- * @return Expr\Func
- */
- public function abs($x)
- {
- return new Expr\Func('ABS', [$x]);
- }
- /**
- * Creates a product mathematical expression with the given arguments.
- *
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> * <right expr>. Example:
- *
- * [php]
- * // u.salary * u.percentAnnualSalaryIncrease
- * $q->expr()->prod('u.salary', 'u.percentAnnualSalaryIncrease')
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Math
- */
- public function prod($x, $y)
- {
- return new Expr\Math($x, '*', $y);
- }
- /**
- * Creates a difference mathematical expression with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> - <right expr>. Example:
- *
- * [php]
- * // u.monthlySubscriptionCount - 1
- * $q->expr()->diff('u.monthlySubscriptionCount', '1')
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Math
- */
- public function diff($x, $y)
- {
- return new Expr\Math($x, '-', $y);
- }
- /**
- * Creates a sum mathematical expression with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> + <right expr>. Example:
- *
- * [php]
- * // u.numChildren + 1
- * $q->expr()->sum('u.numChildren', '1')
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Math
- */
- public function sum($x, $y)
- {
- return new Expr\Math($x, '+', $y);
- }
- /**
- * Creates a quotient mathematical expression with the given arguments.
- * First argument is considered the left expression and the second is the right expression.
- * When converted to string, it will generated a <left expr> / <right expr>. Example:
- *
- * [php]
- * // u.total / u.period
- * $expr->quot('u.total', 'u.period')
- *
- * @param mixed $x Left expression.
- * @param mixed $y Right expression.
- *
- * @return Expr\Math
- */
- public function quot($x, $y)
- {
- return new Expr\Math($x, '/', $y);
- }
- /**
- * Creates a SQRT() function expression with the given argument.
- *
- * @param mixed $x Argument to be used in SQRT() function.
- *
- * @return Expr\Func
- */
- public function sqrt($x)
- {
- return new Expr\Func('SQRT', [$x]);
- }
- /**
- * Creates an IN() expression with the given arguments.
- *
- * @param string $x Field in string format to be restricted by IN() function.
- * @param mixed $y Argument to be used in IN() function.
- *
- * @return Expr\Func
- */
- public function in($x, $y)
- {
- if (is_iterable($y)) {
- if ($y instanceof Traversable) {
- $y = iterator_to_array($y);
- }
- foreach ($y as &$literal) {
- if (! ($literal instanceof Expr\Literal)) {
- $literal = $this->quoteLiteral($literal);
- }
- }
- }
- return new Expr\Func($x . ' IN', (array) $y);
- }
- /**
- * Creates a NOT IN() expression with the given arguments.
- *
- * @param string $x Field in string format to be restricted by NOT IN() function.
- * @param mixed $y Argument to be used in NOT IN() function.
- *
- * @return Expr\Func
- */
- public function notIn($x, $y)
- {
- if (is_iterable($y)) {
- if ($y instanceof Traversable) {
- $y = iterator_to_array($y);
- }
- foreach ($y as &$literal) {
- if (! ($literal instanceof Expr\Literal)) {
- $literal = $this->quoteLiteral($literal);
- }
- }
- }
- return new Expr\Func($x . ' NOT IN', (array) $y);
- }
- /**
- * Creates an IS NULL expression with the given arguments.
- *
- * @param string $x Field in string format to be restricted by IS NULL.
- *
- * @return string
- */
- public function isNull($x)
- {
- return $x . ' IS NULL';
- }
- /**
- * Creates an IS NOT NULL expression with the given arguments.
- *
- * @param string $x Field in string format to be restricted by IS NOT NULL.
- *
- * @return string
- */
- public function isNotNull($x)
- {
- return $x . ' IS NOT NULL';
- }
- /**
- * Creates a LIKE() comparison expression with the given arguments.
- *
- * @param string $x Field in string format to be inspected by LIKE() comparison.
- * @param mixed $y Argument to be used in LIKE() comparison.
- *
- * @return Expr\Comparison
- */
- public function like($x, $y)
- {
- return new Expr\Comparison($x, 'LIKE', $y);
- }
- /**
- * Creates a NOT LIKE() comparison expression with the given arguments.
- *
- * @param string $x Field in string format to be inspected by LIKE() comparison.
- * @param mixed $y Argument to be used in LIKE() comparison.
- *
- * @return Expr\Comparison
- */
- public function notLike($x, $y)
- {
- return new Expr\Comparison($x, 'NOT LIKE', $y);
- }
- /**
- * Creates a CONCAT() function expression with the given arguments.
- *
- * @param mixed $x First argument to be used in CONCAT() function.
- * @param mixed $y,... Other arguments to be used in CONCAT() function.
- *
- * @return Expr\Func
- */
- public function concat($x, $y)
- {
- return new Expr\Func('CONCAT', func_get_args());
- }
- /**
- * Creates a SUBSTRING() function expression with the given arguments.
- *
- * @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function.
- * @param int $from Initial offset to start cropping string. May accept negative values.
- * @param int|null $len Length of crop. May accept negative values.
- *
- * @return Expr\Func
- */
- public function substring($x, $from, $len = null)
- {
- $args = [$x, $from];
- if ($len !== null) {
- $args[] = $len;
- }
- return new Expr\Func('SUBSTRING', $args);
- }
- /**
- * Creates a LOWER() function expression with the given argument.
- *
- * @param mixed $x Argument to be used in LOWER() function.
- *
- * @return Expr\Func A LOWER function expression.
- */
- public function lower($x)
- {
- return new Expr\Func('LOWER', [$x]);
- }
- /**
- * Creates an UPPER() function expression with the given argument.
- *
- * @param mixed $x Argument to be used in UPPER() function.
- *
- * @return Expr\Func An UPPER function expression.
- */
- public function upper($x)
- {
- return new Expr\Func('UPPER', [$x]);
- }
- /**
- * Creates a LENGTH() function expression with the given argument.
- *
- * @param mixed $x Argument to be used as argument of LENGTH() function.
- *
- * @return Expr\Func A LENGTH function expression.
- */
- public function length($x)
- {
- return new Expr\Func('LENGTH', [$x]);
- }
- /**
- * Creates a literal expression of the given argument.
- *
- * @param mixed $literal Argument to be converted to literal.
- *
- * @return Expr\Literal
- */
- public function literal($literal)
- {
- return new Expr\Literal($this->quoteLiteral($literal));
- }
- /**
- * Quotes a literal value, if necessary, according to the DQL syntax.
- *
- * @param mixed $literal The literal value.
- */
- private function quoteLiteral($literal): string
- {
- if (is_numeric($literal) && ! is_string($literal)) {
- return (string) $literal;
- } elseif (is_bool($literal)) {
- return $literal ? 'true' : 'false';
- }
- return "'" . str_replace("'", "''", $literal) . "'";
- }
- /**
- * Creates an instance of BETWEEN() function, with the given argument.
- *
- * @param mixed $val Valued to be inspected by range values.
- * @param int|string $x Starting range value to be used in BETWEEN() function.
- * @param int|string $y End point value to be used in BETWEEN() function.
- *
- * @return string A BETWEEN expression.
- */
- public function between($val, $x, $y)
- {
- return $val . ' BETWEEN ' . $x . ' AND ' . $y;
- }
- /**
- * Creates an instance of TRIM() function, with the given argument.
- *
- * @param mixed $x Argument to be used as argument of TRIM() function.
- *
- * @return Expr\Func a TRIM expression.
- */
- public function trim($x)
- {
- return new Expr\Func('TRIM', $x);
- }
- /**
- * Creates an instance of MEMBER OF function, with the given arguments.
- *
- * @param string $x Value to be checked
- * @param string $y Value to be checked against
- *
- * @return Expr\Comparison
- */
- public function isMemberOf($x, $y)
- {
- return new Expr\Comparison($x, 'MEMBER OF', $y);
- }
- /**
- * Creates an instance of INSTANCE OF function, with the given arguments.
- *
- * @param string $x Value to be checked
- * @param string $y Value to be checked against
- *
- * @return Expr\Comparison
- */
- public function isInstanceOf($x, $y)
- {
- return new Expr\Comparison($x, 'INSTANCE OF', $y);
- }
- }
|