2009-07-09 08:18:58 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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 LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
|
|
|
|
/**
|
2009-07-10 11:38:42 +04:00
|
|
|
* This class is responsible for building DQL query strings via an object oriented
|
|
|
|
* PHP interface.
|
2009-07-09 08:18:58 +04:00
|
|
|
*
|
|
|
|
* TODO: I don't like the API of using the Expr::*() syntax inside of the QueryBuilder
|
|
|
|
* methods. What can we do to allow them to do it more fluently with the QueryBuilder.
|
|
|
|
*
|
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-07-10 11:38:42 +04:00
|
|
|
* @link http://www.doctrine-project.org
|
2009-07-09 08:18:58 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
class QueryBuilder
|
|
|
|
{
|
|
|
|
const SELECT = 0;
|
|
|
|
const DELETE = 1;
|
|
|
|
const UPDATE = 2;
|
|
|
|
|
|
|
|
const STATE_DIRTY = 0;
|
|
|
|
const STATE_CLEAN = 1;
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
|
|
|
* @var EntityManager $em Instance of an EntityManager to use for query
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_em;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $dqlParts The array of DQL parts collected
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_dqlParts = array(
|
2009-07-09 08:18:58 +04:00
|
|
|
'select' => array(),
|
|
|
|
'from' => array(),
|
|
|
|
'where' => array(),
|
|
|
|
'groupBy' => array(),
|
|
|
|
'having' => array(),
|
2009-07-10 21:53:48 +04:00
|
|
|
'orderBy' => array()
|
2009-07-09 08:18:58 +04:00
|
|
|
);
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer $type The type of query this is. Can be select, update or delete
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_type = self::SELECT;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer $state The state of the query object. Can be dirty or clean.
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_state = self::STATE_CLEAN;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $dql The complete DQL string for this query
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_dql;
|
2009-07-09 08:18:58 +04:00
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
/**
|
|
|
|
* @var array $params Parameters of this query.
|
|
|
|
*/
|
2009-07-10 21:53:48 +04:00
|
|
|
private $_params = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer The first result to return (the "offset").
|
|
|
|
*/
|
|
|
|
private $_firstResult = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer The maximum number of results to return (the "limit").
|
|
|
|
*/
|
|
|
|
private $_maxResults = null;
|
2009-07-10 01:56:34 +04:00
|
|
|
|
2009-07-09 08:18:58 +04:00
|
|
|
public function __construct(EntityManager $entityManager)
|
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
$this->_em = $entityManager;
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function create(EntityManager $entityManager)
|
|
|
|
{
|
|
|
|
return new self($entityManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return $this->_type;
|
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
|
|
|
|
2009-07-09 08:18:58 +04:00
|
|
|
public function getState()
|
|
|
|
{
|
|
|
|
return $this->_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDql()
|
|
|
|
{
|
|
|
|
if ($this->_dql !== null && self::STATE_CLEAN) {
|
|
|
|
return $this->_dql;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dql = '';
|
|
|
|
|
|
|
|
switch ($this->_type) {
|
|
|
|
case self::DELETE:
|
|
|
|
$dql = $this->_getDqlForDelete();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::UPDATE:
|
|
|
|
$dql = $this->_getDqlForUpdate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::SELECT:
|
|
|
|
default:
|
|
|
|
$dql = $this->_getDqlForSelect();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_dql = $dql;
|
|
|
|
|
|
|
|
return $dql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQuery()
|
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
$q = new Query($this->_em);
|
2009-07-09 08:18:58 +04:00
|
|
|
$q->setDql($this->getDql());
|
2009-07-10 01:56:34 +04:00
|
|
|
$q->setParameters($this->getParameters());
|
2009-07-10 21:53:48 +04:00
|
|
|
$q->setFirstResult($this->getFirstResult());
|
|
|
|
$q->setMaxResults($this->getMaxResults());
|
2009-07-09 08:18:58 +04:00
|
|
|
|
|
|
|
return $q;
|
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
public function execute($params = array(), $hydrationMode = null)
|
|
|
|
{
|
|
|
|
return $this->getQuery()->execute($params, $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a query parameter.
|
|
|
|
*
|
|
|
|
* @param string|integer $key The parameter position or name.
|
|
|
|
* @param mixed $value The parameter value.
|
|
|
|
*/
|
|
|
|
public function setParameter($key, $value)
|
|
|
|
{
|
|
|
|
$this->_params[$key] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a collection of query parameters.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function setParameters(array $params)
|
|
|
|
{
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$this->setParameter($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all defined parameters
|
|
|
|
*
|
|
|
|
* @return array Defined parameters
|
|
|
|
*/
|
|
|
|
public function getParameters($params = array())
|
|
|
|
{
|
|
|
|
if ($params) {
|
|
|
|
return array_merge($this->_params, $params);
|
|
|
|
}
|
|
|
|
return $this->_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a query parameter.
|
|
|
|
*
|
|
|
|
* @param mixed $key The key (index or name) of the bound parameter.
|
|
|
|
* @return mixed The value of the bound parameter.
|
|
|
|
*/
|
|
|
|
public function getParameter($key)
|
|
|
|
{
|
|
|
|
return isset($this->_params[$key]) ? $this->_params[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a single DQL query part to the array of parts
|
|
|
|
*
|
|
|
|
* @param string $dqlPartName
|
|
|
|
* @param string $dqlPart
|
|
|
|
* @param string $append
|
|
|
|
* @return QueryBuilder $this
|
|
|
|
*/
|
|
|
|
public function add($dqlPartName, $dqlPart, $append = false)
|
|
|
|
{
|
|
|
|
if ($append) {
|
|
|
|
$this->_dqlParts[$dqlPartName][] = $dqlPart;
|
|
|
|
} else {
|
|
|
|
$this->_dqlParts[$dqlPartName] = array($dqlPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_state = self::STATE_DIRTY;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
public function select($select = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
$selects = func_get_args();
|
2009-07-09 08:18:58 +04:00
|
|
|
$this->_type = self::SELECT;
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
if (empty($selects)) {
|
2009-07-09 08:18:58 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-07-10 18:02:06 +04:00
|
|
|
$select = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'select'), $selects);
|
|
|
|
return $this->add('select', $select, true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($delete = null, $alias = null)
|
|
|
|
{
|
|
|
|
$this->_type = self::DELETE;
|
|
|
|
|
|
|
|
if ( ! $delete) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-07-10 18:02:06 +04:00
|
|
|
return $this->add('from', $delete . ' ' . $alias);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function update($update = null, $alias = null)
|
|
|
|
{
|
|
|
|
$this->_type = self::UPDATE;
|
|
|
|
|
|
|
|
if ( ! $update) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-07-10 18:02:06 +04:00
|
|
|
return $this->add('from', $update . ' ' . $alias);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
public function set($key, $value)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
return $this->add('set', Expr::eq($key, $value), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function from($from, $alias)
|
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
return $this->add('from', $from . ' ' . $alias, true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
public function innerJoin($parentAlias, $join, $alias, $condition = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
$join = 'INNER JOIN ' . $parentAlias . '.' . $join . ' '
|
|
|
|
. $alias . (isset($condition) ? ' ' . $condition : null);
|
|
|
|
|
|
|
|
return $this->add('from', $join, true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
public function leftJoin($parentAlias, $join, $alias, $condition = null)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
$join = 'LEFT JOIN ' . $parentAlias . '.' . $join . ' '
|
|
|
|
. $alias . (isset($condition) ? ' ' . $condition : null);
|
|
|
|
|
|
|
|
return $this->add('from', $join, true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function where($where)
|
|
|
|
{
|
2009-07-10 18:02:06 +04:00
|
|
|
$where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'andx'), func_get_args());
|
2009-07-10 01:56:34 +04:00
|
|
|
return $this->add('where', $where, false);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 18:02:06 +04:00
|
|
|
public function andWhere($where)
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'AND', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'andx'), func_get_args());
|
|
|
|
return $this->add('where', $where, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function orWhere($where)
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'OR', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$where = call_user_func_array(array('Doctrine\ORM\Query\Expr', 'orx'), func_get_args());
|
|
|
|
return $this->add('where', $where, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function andWhereIn($expr, $params)
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'AND', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('where', Expr::in($expr, $params), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function orWhereIn($expr, $params = array(), $not = false)
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'OR', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('where', Expr::in($expr, $params), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function andWhereNotIn($expr, $params = array())
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'AND', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('where', Expr::notIn($expr, $params), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function orWhereNotIn($expr, $params = array())
|
|
|
|
{
|
|
|
|
if (count($this->_getDqlQueryPart('where')) > 0) {
|
|
|
|
$this->add('where', 'OR', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->add('where', Expr::notIn($expr, $params), true);
|
|
|
|
}
|
|
|
|
|
2009-07-09 08:18:58 +04:00
|
|
|
public function groupBy($groupBy)
|
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('groupBy', Expr::groupBy($groupBy), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addGroupBy($groupBy)
|
|
|
|
{
|
|
|
|
return $this->add('groupBy', Expr::groupBy($groupBy), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function having($having)
|
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('having', Expr::having($having), false);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function andHaving($having)
|
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
if (count($this->_getDqlQueryPart('having')) > 0) {
|
|
|
|
$this->add('having', 'AND', true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('having', Expr::having($having), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function orHaving($having)
|
|
|
|
{
|
2009-07-10 01:56:34 +04:00
|
|
|
if (count($this->_getDqlQueryPart('having')) > 0) {
|
|
|
|
$this->add('having', 'OR', true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('having', Expr::having($having), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function orderBy($sort, $order)
|
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('orderBy', Expr::orderBy($sort, $order), false);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addOrderBy($sort, $order)
|
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->add('orderBy', Expr::orderBy($sort, $order), true);
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
/**
|
|
|
|
* Sets the position of the first result to retrieve (the "offset").
|
|
|
|
*
|
|
|
|
* @param integer $firstResult The first result to return.
|
|
|
|
* @return Query This query object.
|
|
|
|
*/
|
|
|
|
public function setFirstResult($firstResult)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
$this->_firstResult = $firstResult;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the position of the first result the query object was set to retrieve (the "offset").
|
|
|
|
* Returns NULL if {@link setFirstResult} was not applied to this query.
|
|
|
|
*
|
|
|
|
* @return integer The position of the first result.
|
|
|
|
*/
|
|
|
|
public function getFirstResult()
|
|
|
|
{
|
|
|
|
return $this->_firstResult;
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
/**
|
|
|
|
* Sets the maximum number of results to retrieve (the "limit").
|
|
|
|
*
|
|
|
|
* @param integer $maxResults
|
|
|
|
* @return Query This query object.
|
|
|
|
*/
|
|
|
|
public function setMaxResults($maxResults)
|
|
|
|
{
|
|
|
|
$this->_maxResults = $maxResults;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum number of results the query object was set to retrieve (the "limit").
|
|
|
|
* Returns NULL if {@link setMaxResults} was not applied to this query.
|
|
|
|
*
|
|
|
|
* @return integer Maximum number of results.
|
|
|
|
*/
|
|
|
|
public function getMaxResults()
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
2009-07-10 21:53:48 +04:00
|
|
|
return $this->_maxResults;
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the DQL query string for DELETE queries
|
|
|
|
*
|
|
|
|
* BNF:
|
|
|
|
*
|
|
|
|
* DeleteStatement = DeleteClause [WhereClause] [OrderByClause] [LimitClause] [OffsetClause]
|
|
|
|
* DeleteClause = "DELETE" "FROM" RangeVariableDeclaration
|
|
|
|
* WhereClause = "WHERE" ConditionalExpression
|
|
|
|
* OrderByClause = "ORDER" "BY" OrderByItem {"," OrderByItem}
|
|
|
|
* LimitClause = "LIMIT" integer
|
|
|
|
* OffsetClause = "OFFSET" integer
|
|
|
|
*
|
|
|
|
* @return string $dql
|
|
|
|
*/
|
|
|
|
private function _getDqlForDelete()
|
|
|
|
{
|
|
|
|
return 'DELETE'
|
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the DQL query string for UPDATE queries
|
|
|
|
*
|
|
|
|
* BNF:
|
|
|
|
*
|
|
|
|
* UpdateStatement = UpdateClause [WhereClause] [OrderByClause] [LimitClause] [OffsetClause]
|
|
|
|
* UpdateClause = "UPDATE" RangeVariableDeclaration "SET" UpdateItem {"," UpdateItem}
|
|
|
|
* WhereClause = "WHERE" ConditionalExpression
|
|
|
|
* OrderByClause = "ORDER" "BY" OrderByItem {"," OrderByItem}
|
|
|
|
* LimitClause = "LIMIT" integer
|
|
|
|
* OffsetClause = "OFFSET" integer
|
|
|
|
*
|
|
|
|
* @return string $dql
|
|
|
|
*/
|
|
|
|
private function _getDqlForUpdate()
|
|
|
|
{
|
|
|
|
return 'UPDATE'
|
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' ', 'separator' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('set', array('pre' => ' SET ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the DQL query string for SELECT queries
|
|
|
|
*
|
|
|
|
* BNF:
|
|
|
|
*
|
|
|
|
* SelectStatement = [SelectClause] FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause] [LimitClause] [OffsetClause]
|
|
|
|
* SelectClause = "SELECT" ["ALL" | "DISTINCT"] SelectExpression {"," SelectExpression}
|
|
|
|
* FromClause = "FROM" IdentificationVariableDeclaration {"," IdentificationVariableDeclaration}
|
|
|
|
* WhereClause = "WHERE" ConditionalExpression
|
|
|
|
* GroupByClause = "GROUP" "BY" GroupByItem {"," GroupByItem}
|
|
|
|
* HavingClause = "HAVING" ConditionalExpression
|
|
|
|
* OrderByClause = "ORDER" "BY" OrderByItem {"," OrderByItem}
|
|
|
|
* LimitClause = "LIMIT" integer
|
|
|
|
* OffsetClause = "OFFSET" integer
|
|
|
|
*
|
|
|
|
* @return string $dql
|
|
|
|
*/
|
|
|
|
private function _getDqlForSelect()
|
|
|
|
{
|
|
|
|
return 'SELECT'
|
|
|
|
. $this->_getReducedDqlQueryPart('select', array('pre' => ' ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDqlQueryPart('from', array('pre' => ' FROM ', 'separator' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('where', array('pre' => ' WHERE ', 'separator' => ' '))
|
|
|
|
. $this->_getReducedDqlQueryPart('groupBy', array('pre' => ' GROUP BY ', 'separator' => ', '))
|
|
|
|
. $this->_getReducedDqlQueryPart('having', array('pre' => ' HAVING ', 'separator' => ' '))
|
2009-07-10 21:53:48 +04:00
|
|
|
. $this->_getReducedDqlQueryPart('orderBy', array('pre' => ' ORDER BY ', 'separator' => ', '));
|
2009-07-09 08:18:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _getReducedDqlQueryPart($queryPartName, $options = array())
|
|
|
|
{
|
|
|
|
if (empty($this->_dqlParts[$queryPartName])) {
|
|
|
|
return (isset($options['empty']) ? $options['empty'] : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = (isset($options['pre']) ? $options['pre'] : '');
|
2009-07-10 01:56:34 +04:00
|
|
|
$str .= implode($options['separator'], $this->_getDqlQueryPart($queryPartName));
|
2009-07-09 08:18:58 +04:00
|
|
|
$str .= (isset($options['post']) ? $options['post'] : '');
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2009-07-10 01:56:34 +04:00
|
|
|
private function _getDqlQueryPart($queryPartName)
|
2009-07-09 08:18:58 +04:00
|
|
|
{
|
|
|
|
return $this->_dqlParts[$queryPartName];
|
|
|
|
}
|
|
|
|
}
|