2009-07-09 08:18:58 +04:00
|
|
|
<?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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-07-09 08:18:58 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Query;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is used to generate DQL expressions via a set of PHP static functions
|
|
|
|
*
|
2012-05-26 16:37:00 +04:00
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2010-07-01 23:42:38 +04:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2010-02-09 20:13:49 +03:00
|
|
|
* @todo Rename: ExpressionBuilder
|
2009-07-09 08:18:58 +04:00
|
|
|
*/
|
|
|
|
class Expr
|
|
|
|
{
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a conjunction of the given boolean expressions.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* Example:
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* // (u.type = ?1) AND (u.role = ?2)
|
2011-10-21 12:55:54 +04:00
|
|
|
* $expr->andX($expr->eq('u.type', ':1'), $expr->eq('u.role', ':2'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
2011-12-12 01:46:24 +04:00
|
|
|
* @param \Doctrine\ORM\Query\Expr\Comparison |
|
|
|
|
* \Doctrine\ORM\Query\Expr\Func |
|
|
|
|
* \Doctrine\ORM\Query\Expr\Orx
|
2011-10-21 12:55:54 +04:00
|
|
|
* $x Optional clause. Defaults = null, but requires at least one defined when converting to string.
|
2009-09-06 00:05:39 +04:00
|
|
|
* @return Expr\Andx
|
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function andX($x = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Andx(func_get_args());
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a disjunction of the given boolean expressions.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* Example:
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* // (u.type = ?1) OR (u.role = ?2)
|
2010-02-09 20:13:49 +03:00
|
|
|
* $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Optional clause. Defaults = null, but requires
|
|
|
|
* at least one defined when converting to string.
|
|
|
|
* @return Expr\Orx
|
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function orX($x = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Orx(func_get_args());
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
2010-05-25 22:27:06 +04:00
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an ASCending order expression.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param $sort
|
2011-12-23 20:05:08 +04:00
|
|
|
* @return Expr\OrderBy
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function asc($expr)
|
2009-08-15 00:46:43 +04:00
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
return new Expr\OrderBy($expr, 'ASC');
|
2009-08-15 00:46:43 +04:00
|
|
|
}
|
2010-05-25 22:27:06 +04:00
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a DESCending order expression.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param $sort
|
2011-12-23 20:05:08 +04:00
|
|
|
* @return Expr\OrderBy
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function desc($expr)
|
2009-08-15 02:50:36 +04:00
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
return new Expr\OrderBy($expr, 'DESC');
|
2009-08-15 02:50:36 +04:00
|
|
|
}
|
2009-07-09 08:18:58 +04:00
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an equality comparison expression with the given arguments.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* 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
|
2010-02-09 20:13:49 +03:00
|
|
|
* $expr->eq('u.id', '?1');
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function eq($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->where($q->expr()->neq('u.id', '?1'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function neq($x, $y)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->where($q->expr()->lt('u.id', '?1'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function lt($x, $y)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::LT, $y);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->where($q->expr()->lte('u.id', '?1'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function lte($x, $y)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->where($q->expr()->gt('u.id', '?1'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function gt($x, $y)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::GT, $y);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->where($q->expr()->gte('u.id', '?1'));
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Comparison
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function gte($x, $y)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-20 06:59:42 +04:00
|
|
|
return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of AVG() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in AVG() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function avg($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('AVG', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of MAX() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in MAX() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function max($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('MAX', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of MIN() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in MIN() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function min($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('MIN', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of COUNT() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in COUNT() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function count($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('COUNT', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of COUNT(DISTINCT) function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in COUNT(DISTINCT) function.
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function countDistinct($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function exists($subquery)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('EXISTS', array($subquery));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function all($subquery)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('ALL', array($subquery));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a SOME() function expression with the given DQL subquery.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $subquery DQL Subquery to be used in SOME() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function some($subquery)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('SOME', array($subquery));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an ANY() function expression with the given DQL subquery.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $subquery DQL Subquery to be used in ANY() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function any($subquery)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('ANY', array($subquery));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a negation expression of the given restriction.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $restriction Restriction to be used in NOT() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function not($restriction)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('NOT', array($restriction));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an ABS() function expression with the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in ABS() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function abs($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('ABS', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates a product mathematical expression with the given arguments.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* 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.percentAnualSalaryIncrease
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->expr()->prod('u.salary', 'u.percentAnualSalaryIncrease')
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Math
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function prod($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Math($x, '*', $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->expr()->diff('u.monthlySubscriptionCount', '1')
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Math
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function diff($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Math($x, '-', $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-09 02:19:03 +04:00
|
|
|
* $q->expr()->diff('u.numChildren', '1')
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Math
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function sum($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Math($x, '+', $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* 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]
|
2010-02-09 20:13:49 +03:00
|
|
|
* // u.total / u.period
|
|
|
|
* $expr->quot('u.total', 'u.period')
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Left expression
|
|
|
|
* @param mixed $y Right expression
|
|
|
|
* @return Expr\Math
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function quot($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Math($x, '/', $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a SQRT() function expression with the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in SQRT() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function sqrt($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('SQRT', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an IN() expression with the given arguments.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function in($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-02-09 20:13:49 +03:00
|
|
|
if (is_array($y)) {
|
|
|
|
foreach ($y as &$literal) {
|
|
|
|
if ( ! ($literal instanceof Expr\Literal)) {
|
|
|
|
$literal = $this->_quoteLiteral($literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func($x . ' IN', (array) $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a NOT IN() expression with the given arguments.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function notIn($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-07-01 23:42:38 +04:00
|
|
|
if (is_array($y)) {
|
|
|
|
foreach ($y as &$literal) {
|
|
|
|
if ( ! ($literal instanceof Expr\Literal)) {
|
|
|
|
$literal = $this->_quoteLiteral($literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func($x . ' NOT IN', (array) $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2011-02-20 07:53:55 +03:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a LIKE() comparison expression with the given arguments.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function like($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-08-15 00:46:43 +04:00
|
|
|
return new Expr\Comparison($x, 'LIKE', $y);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a CONCAT() function expression with the given arguments.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x First argument to be used in CONCAT() function.
|
|
|
|
* @param mixed $x Second argument to be used in CONCAT() function.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function concat($x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('CONCAT', array($x, $y));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-03-01 06:55:02 +03:00
|
|
|
* Creates a SUBSTRING() function expression with the given arguments.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
2010-03-01 06:55:02 +03:00
|
|
|
* @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function.
|
2009-09-06 00:05:39 +04:00
|
|
|
* @param integer $from Initial offset to start cropping string. May accept negative values.
|
|
|
|
* @param integer $len Length of crop. May accept negative values.
|
|
|
|
* @return Expr\Func
|
|
|
|
*/
|
2010-05-26 00:10:59 +04:00
|
|
|
public function substring($x, $from, $len = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-05-26 00:10:59 +04:00
|
|
|
$args = array($x, $from);
|
|
|
|
if (null !== $len) {
|
|
|
|
$args[] = $len;
|
|
|
|
}
|
|
|
|
return new Expr\Func('SUBSTRING', $args);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a LOWER() function expression with the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used in LOWER() function.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Expr\Func A LOWER function expression.
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function lower($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('LOWER', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates an UPPER() function expression with the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param mixed $x Argument to be used in UPPER() function.
|
|
|
|
* @return Expr\Func An UPPER function expression.
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function upper($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('UPPER', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a LENGTH() function expression with the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used as argument of LENGTH() function.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Expr\Func A LENGTH function expression.
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function length($x)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return new Expr\Func('LENGTH', array($x));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
2010-02-09 20:13:49 +03:00
|
|
|
* Creates a literal expression of the given argument.
|
2009-09-06 00:05:39 +04:00
|
|
|
*
|
|
|
|
* @param mixed $literal Argument to be converted to literal.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Expr\Literal
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function literal($literal)
|
2010-02-09 20:13:49 +03:00
|
|
|
{
|
|
|
|
return new Expr\Literal($this->_quoteLiteral($literal));
|
|
|
|
}
|
2010-05-25 22:27:06 +04:00
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
/**
|
|
|
|
* Quotes a literal value, if necessary, according to the DQL syntax.
|
2010-05-25 22:27:06 +04:00
|
|
|
*
|
2010-02-09 20:13:49 +03:00
|
|
|
* @param mixed $literal The literal value.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _quoteLiteral($literal)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-05-25 22:27:06 +04:00
|
|
|
if (is_numeric($literal) && !is_string($literal)) {
|
2009-07-10 18:02:06 +04:00
|
|
|
return (string) $literal;
|
2012-03-14 23:49:25 +04:00
|
|
|
} else if (is_bool($literal)) {
|
|
|
|
return $literal ? "true" : "false";
|
2009-07-10 18:02:06 +04:00
|
|
|
} else {
|
2010-02-09 20:13:49 +03:00
|
|
|
return "'" . str_replace("'", "''", $literal) . "'";
|
2009-07-10 18:02:06 +04:00
|
|
|
}
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of BETWEEN() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $val Valued to be inspected by range values.
|
|
|
|
* @param integer $x Starting range value to be used in BETWEEN() function.
|
|
|
|
* @param integer $y End point value to be used in BETWEEN() function.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Expr\Func A BETWEEN expression.
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function between($val, $x, $y)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2010-03-28 14:30:43 +04:00
|
|
|
return $val . ' BETWEEN ' . $x . ' AND ' . $y;
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
|
|
|
|
2009-09-06 00:05:39 +04:00
|
|
|
/**
|
|
|
|
* Creates an instance of TRIM() function, with the given argument.
|
|
|
|
*
|
|
|
|
* @param mixed $x Argument to be used as argument of TRIM() function.
|
2010-02-09 20:13:49 +03:00
|
|
|
* @return Expr\Func a TRIM expression.
|
2009-09-06 00:05:39 +04:00
|
|
|
*/
|
2009-09-09 02:19:03 +04:00
|
|
|
public function trim($x)
|
2009-07-10 01:56:34 +04:00
|
|
|
{
|
2009-08-15 00:46:43 +04:00
|
|
|
return new Expr\Func('TRIM', $x);
|
2009-07-10 01:56:34 +04:00
|
|
|
}
|
2010-05-25 22:27:06 +04:00
|
|
|
}
|