2007-04-13 22:03:44 +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.phpdoctrine.com>.
|
|
|
|
*/
|
2007-05-19 22:06:42 +04:00
|
|
|
Doctrine::autoload('Doctrine_Query_Abstract');
|
2007-04-13 22:03:44 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Query
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2007-05-19 22:06:42 +04:00
|
|
|
class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
2007-05-16 23:20:55 +04:00
|
|
|
{
|
2007-06-19 14:43:19 +04:00
|
|
|
const STATE_CLEAN = 1;
|
|
|
|
|
|
|
|
const STATE_DIRTY = 2;
|
|
|
|
|
|
|
|
const STATE_DIRECT = 3;
|
|
|
|
|
|
|
|
const STATE_LOCKED = 4;
|
|
|
|
|
|
|
|
|
2007-05-19 22:29:29 +04:00
|
|
|
protected $subqueryAliases = array();
|
2007-04-13 22:03:44 +04:00
|
|
|
/**
|
|
|
|
* @param boolean $needsSubquery
|
|
|
|
*/
|
2007-05-19 22:29:29 +04:00
|
|
|
protected $needsSubquery = false;
|
2007-04-13 22:03:44 +04:00
|
|
|
/**
|
|
|
|
* @param boolean $isSubquery whether or not this query object is a subquery of another
|
|
|
|
* query object
|
|
|
|
*/
|
2007-05-18 03:13:58 +04:00
|
|
|
protected $isSubquery;
|
2007-05-24 18:19:44 +04:00
|
|
|
|
|
|
|
protected $isLimitSubqueryUsed = false;
|
2007-05-26 23:50:40 +04:00
|
|
|
/**
|
|
|
|
* @var array $_neededTableAliases an array containing the needed table aliases
|
|
|
|
*/
|
|
|
|
protected $_neededTables = array();
|
2007-04-13 22:03:44 +04:00
|
|
|
/**
|
|
|
|
* @var array $pendingFields
|
|
|
|
*/
|
2007-05-18 03:13:58 +04:00
|
|
|
protected $pendingFields = array();
|
2007-04-14 20:28:09 +04:00
|
|
|
/**
|
|
|
|
* @var array $pendingSubqueries SELECT part subqueries, these are called pending subqueries since
|
|
|
|
* they cannot be parsed directly (some queries might be correlated)
|
|
|
|
*/
|
2007-05-18 03:13:58 +04:00
|
|
|
protected $pendingSubqueries = array();
|
2007-05-19 22:29:29 +04:00
|
|
|
/**
|
2007-05-26 23:50:40 +04:00
|
|
|
* @var array $_parsers an array of parser objects, each DQL query part has its own parser
|
2007-05-16 23:20:55 +04:00
|
|
|
*/
|
2007-05-19 22:29:29 +04:00
|
|
|
protected $_parsers = array();
|
2007-05-18 03:13:58 +04:00
|
|
|
/**
|
|
|
|
* @var array $_enumParams an array containing the keys of the parameters that should be enumerated
|
|
|
|
*/
|
|
|
|
protected $_enumParams = array();
|
2007-05-27 15:38:16 +04:00
|
|
|
|
2007-05-19 22:29:29 +04:00
|
|
|
/**
|
|
|
|
* @var array $_dqlParts an array containing all DQL query parts
|
|
|
|
*/
|
|
|
|
protected $_dqlParts = array(
|
|
|
|
'select' => array(),
|
|
|
|
'forUpdate' => false,
|
|
|
|
'from' => array(),
|
|
|
|
'set' => array(),
|
|
|
|
'join' => array(),
|
|
|
|
'where' => array(),
|
|
|
|
'groupby' => array(),
|
|
|
|
'having' => array(),
|
|
|
|
'orderby' => array(),
|
2007-05-24 20:13:50 +04:00
|
|
|
'limit' => array(),
|
|
|
|
'offset' => array(),
|
2007-05-19 22:29:29 +04:00
|
|
|
);
|
2007-06-11 23:27:16 +04:00
|
|
|
/**
|
|
|
|
* @var array $_pendingJoinConditions an array containing pending joins
|
|
|
|
*/
|
|
|
|
protected $_pendingJoinConditions = array();
|
2007-06-19 14:43:19 +04:00
|
|
|
|
|
|
|
protected $_state = Doctrine_Query::STATE_CLEAN;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create
|
|
|
|
* returns a new Doctrine_Query object
|
|
|
|
*
|
2007-05-24 20:13:50 +04:00
|
|
|
* @param Doctrine_Connection $conn optional connection parameter
|
2007-04-13 22:03:44 +04:00
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-05-24 20:13:50 +04:00
|
|
|
public static function create($conn = null)
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-05-24 20:13:50 +04:00
|
|
|
return new Doctrine_Query($conn);
|
|
|
|
}
|
2007-06-19 02:30:15 +04:00
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$this->_enumParams = array();
|
|
|
|
$this->_pendingJoinConditions = array();
|
|
|
|
$this->pendingSubqueries = array();
|
|
|
|
$this->pendingFields = array();
|
|
|
|
$this->_neededTables = array();
|
|
|
|
$this->subqueryAliases = array();
|
|
|
|
$this->needsSubquery = false;
|
|
|
|
$this->isLimitSubqueryUsed = false;
|
|
|
|
}
|
2007-05-24 20:13:50 +04:00
|
|
|
/**
|
|
|
|
* setOption
|
|
|
|
*
|
|
|
|
* @param string $name option name
|
|
|
|
* @param string $value option value
|
|
|
|
* @return Doctrine_Query this object
|
|
|
|
*/
|
|
|
|
public function setOption($name, $value)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_options[$name])) {
|
|
|
|
throw new Doctrine_Query_Exception('Unknown option ' . $name);
|
|
|
|
}
|
|
|
|
$this->_options[$name] = $value;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-06-11 23:27:16 +04:00
|
|
|
/**
|
|
|
|
* addPendingJoinCondition
|
|
|
|
*
|
|
|
|
* @param string $componentAlias component alias
|
|
|
|
* @param string $joinCondition dql join condition
|
|
|
|
* @return Doctrine_Query this object
|
|
|
|
*/
|
|
|
|
public function addPendingJoinCondition($componentAlias, $joinCondition)
|
|
|
|
{
|
|
|
|
$this->_pendingJoins[$componentAlias] = $joinCondition;
|
|
|
|
}
|
2007-05-18 03:13:58 +04:00
|
|
|
/**
|
|
|
|
* addEnumParam
|
|
|
|
* sets input parameter as an enumerated parameter
|
|
|
|
*
|
|
|
|
* @param string $key the key of the input parameter
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function addEnumParam($key, $table = null, $column = null)
|
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
$array = (isset($table) || isset($column)) ? array($table, $column) : array();
|
2007-05-18 03:13:58 +04:00
|
|
|
|
2007-06-19 02:30:15 +04:00
|
|
|
if ($key === '?') {
|
|
|
|
$this->_enumParams[] = $array;
|
2007-05-18 03:13:58 +04:00
|
|
|
} else {
|
|
|
|
$this->_enumParams[$key] = $array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getEnumParams
|
|
|
|
* get all enumerated parameters
|
|
|
|
*
|
|
|
|
* @return array all enumerated parameters
|
|
|
|
*/
|
|
|
|
public function getEnumParams()
|
|
|
|
{
|
|
|
|
return $this->_enumParams;
|
|
|
|
}
|
2007-05-24 18:19:44 +04:00
|
|
|
/**
|
|
|
|
* limitSubqueryUsed
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isLimitSubqueryUsed()
|
|
|
|
{
|
|
|
|
return $this->isLimitSubqueryUsed;
|
|
|
|
}
|
2007-05-18 03:13:58 +04:00
|
|
|
/**
|
|
|
|
* convertEnums
|
|
|
|
* convert enum parameters to their integer equivalents
|
|
|
|
*
|
|
|
|
* @return array converted parameter array
|
|
|
|
*/
|
|
|
|
public function convertEnums($params)
|
|
|
|
{
|
|
|
|
foreach ($this->_enumParams as $key => $values) {
|
|
|
|
if (isset($params[$key])) {
|
|
|
|
if ( ! empty($values)) {
|
|
|
|
$params[$key] = $values[0]->enumIndex($values[1], $params[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
/**
|
|
|
|
* isSubquery
|
|
|
|
* if $bool parameter is set this method sets the value of
|
|
|
|
* Doctrine_Query::$isSubquery. If this value is set to true
|
|
|
|
* the query object will not load the primary key fields of the selected
|
|
|
|
* components.
|
|
|
|
*
|
|
|
|
* If null is given as the first parameter this method retrieves the current
|
|
|
|
* value of Doctrine_Query::$isSubquery.
|
|
|
|
*
|
|
|
|
* @param boolean $bool whether or not this query acts as a subquery
|
|
|
|
* @return Doctrine_Query|bool
|
|
|
|
*/
|
|
|
|
public function isSubquery($bool = null)
|
|
|
|
{
|
|
|
|
if ($bool === null) {
|
|
|
|
return $this->isSubquery;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isSubquery = (bool) $bool;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getAggregateAlias
|
|
|
|
*
|
2007-05-26 23:50:40 +04:00
|
|
|
* @param string $dqlAlias the dql alias of an aggregate value
|
2007-04-13 22:03:44 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAggregateAlias($dqlAlias)
|
|
|
|
{
|
2007-06-01 14:17:50 +04:00
|
|
|
if (isset($this->aggregateMap[$dqlAlias])) {
|
2007-04-13 22:03:44 +04:00
|
|
|
return $this->aggregateMap[$dqlAlias];
|
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
if ( ! empty($this->pendingAggregates)) {
|
|
|
|
$this->processPendingAggregates();
|
|
|
|
|
|
|
|
return $this->getAggregateAlias($dqlAlias);
|
|
|
|
}
|
|
|
|
throw new Doctrine_Query_Exception('Unknown aggregate alias ' . $dqlAlias);
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* getParser
|
|
|
|
* parser lazy-loader
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Query_Exception if unknown parser name given
|
|
|
|
* @return Doctrine_Query_Part
|
|
|
|
*/
|
|
|
|
public function getParser($name)
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! isset($this->_parsers[$name])) {
|
|
|
|
$class = 'Doctrine_Query_' . ucwords(strtolower($name));
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
Doctrine::autoload($class);
|
|
|
|
|
|
|
|
if ( ! class_exists($class)) {
|
|
|
|
throw new Doctrine_Query_Exception('Unknown parser ' . $name);
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$this->_parsers[$name] = new $class($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_parsers[$name];
|
|
|
|
}
|
2007-05-19 22:06:42 +04:00
|
|
|
/**
|
|
|
|
* parseQueryPart
|
2007-05-19 22:29:29 +04:00
|
|
|
* parses given DQL query part
|
2007-05-19 22:06:42 +04:00
|
|
|
*
|
|
|
|
* @param string $queryPartName the name of the query part
|
|
|
|
* @param string $queryPart query part to be parsed
|
|
|
|
* @param boolean $append whether or not to append the query part to its stack
|
|
|
|
* if false is given, this method will overwrite
|
|
|
|
* the given query part stack with $queryPart
|
|
|
|
* @return Doctrine_Query this object
|
|
|
|
*/
|
|
|
|
public function parseQueryPart($queryPartName, $queryPart, $append = false)
|
2007-06-19 14:43:19 +04:00
|
|
|
{
|
|
|
|
if ($this->_state === self::STATE_LOCKED) {
|
|
|
|
throw new Doctrine_Query_Exception('This query object is locked. No query parts can be manipulated.');
|
|
|
|
}
|
|
|
|
|
2007-05-31 21:45:07 +04:00
|
|
|
|
2007-06-19 02:30:15 +04:00
|
|
|
// sanity check
|
|
|
|
if ($queryPart === '' || $queryPart === null) {
|
2007-05-24 23:47:28 +04:00
|
|
|
throw new Doctrine_Query_Exception('Empty ' . $queryPartName . ' part given.');
|
2007-06-19 02:30:15 +04:00
|
|
|
}
|
2007-05-24 23:47:28 +04:00
|
|
|
|
2007-05-26 23:50:40 +04:00
|
|
|
// add query part to the dql part array
|
2007-06-19 02:30:15 +04:00
|
|
|
if ($append) {
|
|
|
|
$this->_dqlParts[$queryPartName][] = $queryPart;
|
|
|
|
} else {
|
2007-05-24 20:13:50 +04:00
|
|
|
$this->_dqlParts[$queryPartName] = array($queryPart);
|
2007-06-19 02:30:15 +04:00
|
|
|
}
|
|
|
|
|
2007-06-19 14:43:19 +04:00
|
|
|
if ($this->_state === self::STATE_DIRECT) {
|
|
|
|
$parser = $this->getParser($queryPartName);
|
|
|
|
|
|
|
|
$sql = $parser->parse($queryPart);
|
|
|
|
|
|
|
|
if (isset($sql)) {
|
|
|
|
if ($append) {
|
|
|
|
$this->addQueryPart($queryPartName, $sql);
|
|
|
|
} else {
|
|
|
|
$this->setQueryPart($queryPartName, $sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-19 02:30:15 +04:00
|
|
|
|
2007-05-24 20:13:50 +04:00
|
|
|
return $this;
|
2007-05-19 22:06:42 +04:00
|
|
|
}
|
2007-05-19 22:29:29 +04:00
|
|
|
/**
|
|
|
|
* getDql
|
|
|
|
* returns the DQL query associated with this object
|
|
|
|
*
|
|
|
|
* the query is built from $_dqlParts
|
|
|
|
*
|
|
|
|
* @return string the DQL query
|
|
|
|
*/
|
|
|
|
public function getDql()
|
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
$q = '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['select']))? 'SELECT ' . implode(', ', $this->_dqlParts['select']) : '';
|
2007-05-24 20:13:50 +04:00
|
|
|
$q .= ( ! empty($this->_dqlParts['from']))? ' FROM ' . implode(' ', $this->_dqlParts['from']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_dqlParts['where']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_dqlParts['groupby']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_dqlParts['having']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_dqlParts['orderby']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['limit']))? ' LIMIT ' . implode(' ', $this->_dqlParts['limit']) : '';
|
|
|
|
$q .= ( ! empty($this->_dqlParts['offset']))? ' OFFSET ' . implode(' ', $this->_dqlParts['offset']) : '';
|
2007-05-19 22:29:29 +04:00
|
|
|
|
|
|
|
return $q;
|
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* processPendingFields
|
|
|
|
* the fields in SELECT clause cannot be parsed until the components
|
|
|
|
* in FROM clause are parsed, hence this method is called everytime a
|
|
|
|
* specific component is being parsed.
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Query_Exception if unknown component alias has been given
|
|
|
|
* @param string $componentAlias the alias of the component
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function processPendingFields($componentAlias)
|
|
|
|
{
|
|
|
|
$tableAlias = $this->getTableAlias($componentAlias);
|
|
|
|
$table = $this->_aliasMap[$componentAlias]['table'];
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
if (isset($this->pendingFields[$componentAlias])) {
|
|
|
|
$fields = $this->pendingFields[$componentAlias];
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
// check for wildcards
|
2007-04-14 20:28:09 +04:00
|
|
|
if (in_array('*', $fields)) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$fields = $table->getColumnNames();
|
|
|
|
} else {
|
|
|
|
// only auto-add the primary key fields if this query object is not
|
|
|
|
// a subquery of another query object
|
|
|
|
if ( ! $this->isSubquery) {
|
|
|
|
$fields = array_unique(array_merge($table->getPrimaryKeys(), $fields));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($fields as $name) {
|
|
|
|
$name = $table->getColumnName($name);
|
|
|
|
|
2007-04-14 20:28:09 +04:00
|
|
|
$this->parts['select'][] = $tableAlias . '.' .$name . ' AS ' . $tableAlias . '__' . $name;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->neededTables[] = $tableAlias;
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* parseSelect
|
|
|
|
* parses the query select part and
|
|
|
|
* adds selected fields to pendingFields array
|
|
|
|
*
|
|
|
|
* @param string $dql
|
|
|
|
*/
|
|
|
|
public function parseSelect($dql)
|
|
|
|
{
|
2007-05-17 01:28:33 +04:00
|
|
|
$refs = Doctrine_Tokenizer::bracketExplode($dql, ',');
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-09 00:17:17 +04:00
|
|
|
$pos = strpos(trim($refs[0]), ' ');
|
|
|
|
$first = substr($refs[0], 0, $pos);
|
|
|
|
|
|
|
|
if ($first === 'DISTINCT') {
|
|
|
|
$this->parts['distinct'] = true;
|
|
|
|
|
|
|
|
$refs[0] = substr($refs[0], ++$pos);
|
|
|
|
}
|
|
|
|
|
2007-04-14 20:28:09 +04:00
|
|
|
foreach ($refs as $reference) {
|
2007-06-01 14:17:50 +04:00
|
|
|
$reference = trim($reference);
|
2007-04-14 20:28:09 +04:00
|
|
|
if (strpos($reference, '(') !== false) {
|
|
|
|
if (substr($reference, 0, 1) === '(') {
|
|
|
|
// subselect found in SELECT part
|
|
|
|
$this->parseSubselect($reference);
|
|
|
|
} else {
|
2007-05-26 23:50:40 +04:00
|
|
|
$this->parseAggregateFunction($reference);
|
2007-04-14 20:28:09 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
} else {
|
|
|
|
|
2007-06-09 00:17:17 +04:00
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
$e = explode('.', $reference);
|
|
|
|
if (count($e) > 2) {
|
|
|
|
$this->pendingFields[] = $reference;
|
|
|
|
} else {
|
|
|
|
$this->pendingFields[$e[0]][] = $e[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-14 20:28:09 +04:00
|
|
|
/**
|
|
|
|
* parseSubselect
|
|
|
|
*
|
|
|
|
* parses the subquery found in DQL SELECT part and adds the
|
|
|
|
* parsed form into $pendingSubqueries stack
|
|
|
|
*
|
|
|
|
* @param string $reference
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function parseSubselect($reference)
|
|
|
|
{
|
2007-05-17 01:28:33 +04:00
|
|
|
$e = Doctrine_Tokenizer::bracketExplode($reference, ' ');
|
2007-04-14 20:28:09 +04:00
|
|
|
$alias = $e[1];
|
|
|
|
|
|
|
|
if (count($e) > 2) {
|
|
|
|
if (strtoupper($e[1]) !== 'AS') {
|
|
|
|
throw new Doctrine_Query_Exception('Syntax error near: ' . $reference);
|
|
|
|
}
|
|
|
|
$alias = $e[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
$subquery = substr($e[0], 1, -1);
|
|
|
|
|
|
|
|
$this->pendingSubqueries[] = array($subquery, $alias);
|
|
|
|
}
|
2007-05-26 23:50:40 +04:00
|
|
|
/**
|
|
|
|
* parseAggregateFunction
|
|
|
|
* parses an aggregate function and returns the parsed form
|
|
|
|
*
|
|
|
|
* @see Doctrine_Expression
|
2007-06-01 14:17:50 +04:00
|
|
|
* @param string $expr DQL aggregate function
|
2007-05-26 23:50:40 +04:00
|
|
|
* @throws Doctrine_Query_Exception if unknown aggregate function given
|
|
|
|
* @return array parsed form of given function
|
|
|
|
*/
|
2007-06-01 14:17:50 +04:00
|
|
|
public function parseAggregateFunction($expr, $nestedCall = false)
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-06-01 14:17:50 +04:00
|
|
|
$e = Doctrine_Tokenizer::bracketExplode($expr, ' ');
|
2007-04-13 22:03:44 +04:00
|
|
|
$func = $e[0];
|
|
|
|
|
|
|
|
$pos = strpos($func, '(');
|
2007-06-01 14:17:50 +04:00
|
|
|
if ($pos === false) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the name of the function
|
|
|
|
$name = substr($func, 0, $pos);
|
|
|
|
$argStr = substr($func, ($pos + 1), -1);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
$args = array();
|
|
|
|
// parse args
|
|
|
|
foreach (Doctrine_Tokenizer::bracketExplode($argStr, ',') as $expr) {
|
|
|
|
$args[] = $this->parseAggregateFunction($expr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert DQL function to its RDBMS specific equivalent
|
2007-04-16 21:59:45 +04:00
|
|
|
try {
|
2007-06-01 14:17:50 +04:00
|
|
|
$expr = call_user_func_array(array($this->_conn->expression, $name), $args);
|
2007-04-16 21:59:45 +04:00
|
|
|
} catch(Doctrine_Expression_Exception $e) {
|
|
|
|
throw new Doctrine_Query_Exception('Unknown function ' . $func . '.');
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
|
|
|
|
if ( ! $nestedCall) {
|
|
|
|
// try to find all component references
|
|
|
|
preg_match_all("/[a-z0-9_]+\.[a-z0-9_]+[\.[a-z0-9]+]*/i", $argStr, $m);
|
|
|
|
|
|
|
|
if (isset($e[1])) {
|
|
|
|
if (strtoupper($e[1]) === 'AS') {
|
|
|
|
if ( ! isset($e[2])) {
|
|
|
|
throw new Doctrine_Query_Exception('Missing aggregate function alias.');
|
|
|
|
}
|
|
|
|
$alias = $e[2];
|
|
|
|
} else {
|
|
|
|
$alias = $e[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$alias = substr($expr, 0, strpos($expr, '('));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pendingAggregates[] = array($expr, $m[0], $alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expr;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-26 23:50:40 +04:00
|
|
|
/**
|
|
|
|
* processPendingSubqueries
|
|
|
|
* processes pending subqueries
|
|
|
|
*
|
|
|
|
* subqueries can only be processed when the query is fully constructed
|
|
|
|
* since some subqueries may be correlated
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-04-14 20:28:09 +04:00
|
|
|
public function processPendingSubqueries()
|
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
foreach ($this->pendingSubqueries as $value) {
|
2007-04-14 20:28:09 +04:00
|
|
|
list($dql, $alias) = $value;
|
|
|
|
|
|
|
|
$sql = $this->createSubquery()->parseQuery($dql, false)->getQuery();
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
reset($this->_aliasMap);
|
|
|
|
$componentAlias = key($this->_aliasMap);
|
|
|
|
$tableAlias = $this->getTableAlias($componentAlias);
|
2007-04-14 20:28:09 +04:00
|
|
|
|
|
|
|
$sqlAlias = $tableAlias . '__' . count($this->aggregateMap);
|
2007-05-19 21:49:16 +04:00
|
|
|
|
2007-04-14 20:28:09 +04:00
|
|
|
$this->parts['select'][] = '(' . $sql . ') AS ' . $sqlAlias;
|
2007-05-19 21:49:16 +04:00
|
|
|
|
2007-04-14 20:28:09 +04:00
|
|
|
$this->aggregateMap[$alias] = $sqlAlias;
|
2007-05-26 23:50:40 +04:00
|
|
|
$this->_aliasMap[$componentAlias]['agg'][] = $alias;
|
2007-04-14 20:28:09 +04:00
|
|
|
}
|
2007-05-19 21:49:16 +04:00
|
|
|
$this->pendingSubqueries = array();
|
2007-04-14 20:28:09 +04:00
|
|
|
}
|
2007-05-26 23:50:40 +04:00
|
|
|
/**
|
|
|
|
* processPendingAggregates
|
|
|
|
* processes pending aggregate values for given component alias
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-01 14:17:50 +04:00
|
|
|
public function processPendingAggregates()
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
// iterate trhough all aggregates
|
2007-06-01 14:17:50 +04:00
|
|
|
foreach ($this->pendingAggregates as $aggregate) {
|
|
|
|
list ($expression, $components, $alias) = $aggregate;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
$tableAliases = array();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
// iterate through the component references within the aggregate function
|
|
|
|
if ( ! empty ($components)) {
|
|
|
|
foreach ($components as $component) {
|
|
|
|
$e = explode('.', $component);
|
|
|
|
|
|
|
|
$field = array_pop($e);
|
|
|
|
$componentAlias = implode('.', $e);
|
|
|
|
|
|
|
|
// check the existence of the component alias
|
|
|
|
if ( ! isset($this->_aliasMap[$componentAlias])) {
|
|
|
|
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
|
|
|
|
$table = $this->_aliasMap[$componentAlias]['table'];
|
|
|
|
|
|
|
|
$field = $table->getColumnName($field);
|
|
|
|
|
|
|
|
// check column existence
|
|
|
|
if ( ! $table->hasColumn($field)) {
|
|
|
|
throw new Doctrine_Query_Exception('Unknown column ' . $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tableAlias = $this->getTableAlias($componentAlias);
|
|
|
|
|
|
|
|
$tableAliases[$tableAlias] = true;
|
|
|
|
|
|
|
|
// build sql expression
|
|
|
|
$expression = str_replace($component, $tableAlias . '.' . $field, $expression);
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
|
|
|
|
if (count($tableAliases) !== 1) {
|
|
|
|
$componentAlias = reset($this->tableAliases);
|
|
|
|
$tableAlias = key($this->tableAliases);
|
|
|
|
}
|
|
|
|
|
2007-05-26 23:50:40 +04:00
|
|
|
$index = count($this->aggregateMap);
|
|
|
|
$sqlAlias = $tableAlias . '__' . $index;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
$this->parts['select'][] = $expression . ' AS ' . $sqlAlias;
|
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->aggregateMap[$alias] = $sqlAlias;
|
2007-06-01 14:17:50 +04:00
|
|
|
|
2007-05-26 23:50:40 +04:00
|
|
|
$this->_aliasMap[$componentAlias]['agg'][$index] = $alias;
|
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->neededTables[] = $tableAlias;
|
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
// reset the state
|
|
|
|
$this->pendingAggregates = array();
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* getQueryBase
|
|
|
|
* returns the base of the generated sql query
|
|
|
|
* On mysql driver special strategy has to be used for DELETE statements
|
2007-04-13 22:03:44 +04:00
|
|
|
*
|
2007-05-16 23:20:55 +04:00
|
|
|
* @return string the base of the generated sql query
|
2007-04-13 22:03:44 +04:00
|
|
|
*/
|
2007-05-16 23:20:55 +04:00
|
|
|
public function getQueryBase()
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-05-16 23:20:55 +04:00
|
|
|
switch ($this->type) {
|
|
|
|
case self::DELETE:
|
|
|
|
$q = 'DELETE FROM ';
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case self::UPDATE:
|
|
|
|
$q = 'UPDATE ';
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case self::SELECT:
|
2007-05-22 19:57:17 +04:00
|
|
|
$distinct = ($this->parts['distinct']) ? 'DISTINCT ' : '';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$q = 'SELECT ' . $distinct . implode(', ', $this->parts['select']) . ' FROM ';
|
|
|
|
break;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
return $q;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* buildFromPart
|
2007-05-26 23:50:40 +04:00
|
|
|
* builds the from part of the query and returns it
|
2007-04-13 22:03:44 +04:00
|
|
|
*
|
2007-05-26 23:50:40 +04:00
|
|
|
* @return string the query sql from part
|
2007-04-13 22:03:44 +04:00
|
|
|
*/
|
2007-05-16 23:20:55 +04:00
|
|
|
public function buildFromPart()
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
$q = '';
|
2007-05-16 23:20:55 +04:00
|
|
|
foreach ($this->parts['from'] as $k => $part) {
|
|
|
|
if ($k === 0) {
|
|
|
|
$q .= $part;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// preserve LEFT JOINs only if needed
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if (substr($part, 0, 9) === 'LEFT JOIN') {
|
|
|
|
$e = explode(' ', $part);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$aliases = array_merge($this->subqueryAliases,
|
|
|
|
array_keys($this->neededTables));
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if( ! in_array($e[3], $aliases) &&
|
|
|
|
! in_array($e[2], $aliases) &&
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
! empty($this->pendingFields)) {
|
|
|
|
continue;
|
2007-04-20 14:11:49 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-11 23:27:16 +04:00
|
|
|
if (isset($this->_pendingJoinConditions[$k])) {
|
2007-05-16 23:20:55 +04:00
|
|
|
$parser = new Doctrine_Query_JoinCondition($this);
|
2007-06-11 23:27:16 +04:00
|
|
|
$part .= ' AND ' . $parser->parse($this->_pendingJoinConditions[$k]);
|
|
|
|
|
|
|
|
unset($this->_pendingJoinConditions[$k]);
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$q .= ' ' . $part;
|
2007-06-19 02:30:15 +04:00
|
|
|
|
2007-06-11 23:27:16 +04:00
|
|
|
$this->parts['from'][$k] = $part;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
return $q;
|
2007-06-19 14:43:19 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* preQuery
|
|
|
|
*
|
|
|
|
* Empty template method to provide Query subclasses with the possibility
|
|
|
|
* to hook into the query building procedure, doing any custom / specialized
|
|
|
|
* query building procedures that are neccessary.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function preQuery()
|
|
|
|
{
|
|
|
|
|
2007-06-19 22:53:46 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* postQuery
|
|
|
|
*
|
|
|
|
* Empty template method to provide Query subclasses with the possibility
|
|
|
|
* to hook into the query building procedure, doing any custom / specialized
|
|
|
|
* post query procedures (for example logging) that are neccessary.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function postQuery()
|
|
|
|
{
|
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* builds the sql query from the given parameters and applies things such as
|
|
|
|
* column aggregation inheritance and limit subqueries if needed
|
|
|
|
*
|
|
|
|
* @param array $params an array of prepared statement params (needed only in mysql driver
|
|
|
|
* when limit subquery algorithm is used)
|
|
|
|
* @return string the built sql query
|
|
|
|
*/
|
|
|
|
public function getQuery($params = array())
|
|
|
|
{
|
2007-06-19 14:43:19 +04:00
|
|
|
$parts = $this->_dqlParts;
|
2007-06-19 02:30:15 +04:00
|
|
|
|
2007-06-19 14:43:19 +04:00
|
|
|
// reset the state
|
2007-06-19 02:30:15 +04:00
|
|
|
$this->_aliasMap = array();
|
|
|
|
$this->pendingAggregates = array();
|
|
|
|
$this->aggregateMap = array();
|
|
|
|
|
2007-06-19 14:43:19 +04:00
|
|
|
$this->reset();
|
2007-05-27 22:52:33 +04:00
|
|
|
|
2007-06-19 14:43:19 +04:00
|
|
|
// parse the DQL parts
|
2007-06-19 02:30:15 +04:00
|
|
|
foreach ($this->_dqlParts as $queryPartName => $queryParts) {
|
|
|
|
$this->parts[$queryPartName] = array();
|
|
|
|
if (is_array($queryParts) && ! empty($queryParts)) {
|
|
|
|
|
|
|
|
foreach ($queryParts as $queryPart) {
|
|
|
|
$parser = $this->getParser($queryPartName);
|
|
|
|
|
|
|
|
|
|
|
|
$sql = $parser->parse($queryPart);
|
|
|
|
|
|
|
|
if (isset($sql)) {
|
|
|
|
if ($queryPartName == 'limit' ||
|
|
|
|
$queryPartName == 'offset') {
|
|
|
|
|
|
|
|
$this->setQueryPart($queryPartName, $sql);
|
|
|
|
} else {
|
|
|
|
$this->addQueryPart($queryPartName, $sql);
|
|
|
|
}
|
2007-05-24 20:13:50 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-19 02:30:15 +04:00
|
|
|
}
|
2007-05-24 20:13:50 +04:00
|
|
|
}
|
2007-06-19 14:43:19 +04:00
|
|
|
$this->_state = self::STATE_DIRECT;
|
|
|
|
|
|
|
|
// invoke the preQuery hook
|
|
|
|
$this->preQuery();
|
|
|
|
$this->_state = self::STATE_CLEAN;
|
|
|
|
|
|
|
|
$this->_dqlParts = $parts;
|
2007-06-19 02:30:15 +04:00
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
if (empty($this->parts['from'])) {
|
2007-04-13 22:03:44 +04:00
|
|
|
return false;
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
$needsSubQuery = false;
|
|
|
|
$subquery = '';
|
2007-05-16 23:20:55 +04:00
|
|
|
$map = reset($this->_aliasMap);
|
|
|
|
$table = $map['table'];
|
|
|
|
$rootAlias = key($this->_aliasMap);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
|
2007-05-24 18:19:44 +04:00
|
|
|
$this->isLimitSubqueryUsed = true;
|
2007-04-13 22:03:44 +04:00
|
|
|
$needsSubQuery = true;
|
|
|
|
}
|
|
|
|
|
2007-04-14 20:28:09 +04:00
|
|
|
// process all pending SELECT part subqueries
|
|
|
|
$this->processPendingSubqueries();
|
2007-06-01 14:17:50 +04:00
|
|
|
$this->processPendingAggregates();
|
2007-04-14 20:28:09 +04:00
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
// build the basic query
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$q = $this->getQueryBase();
|
|
|
|
$q .= $this->buildFromPart();
|
|
|
|
|
|
|
|
if ( ! empty($this->parts['set'])) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$q .= ' SET ' . implode(', ', $this->parts['set']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-17 21:01:35 +04:00
|
|
|
$string = $this->applyInheritance();
|
|
|
|
|
|
|
|
// apply inheritance to WHERE part
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! empty($string)) {
|
|
|
|
$this->parts['where'][] = '(' . $string . ')';
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
$modifyLimit = true;
|
2007-06-19 02:30:15 +04:00
|
|
|
if ( ! empty($this->parts['limit']) || ! empty($this->parts['offset'])) {
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($needsSubQuery) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$subquery = $this->getLimitSubquery();
|
|
|
|
|
|
|
|
|
2007-05-24 21:46:32 +04:00
|
|
|
switch (strtolower($this->_conn->getName())) {
|
2007-04-13 22:03:44 +04:00
|
|
|
case 'mysql':
|
|
|
|
// mysql doesn't support LIMIT in subqueries
|
2007-06-17 21:01:35 +04:00
|
|
|
$list = $this->_conn->execute($subquery, $params)->fetchAll(Doctrine::FETCH_COLUMN);
|
2007-04-13 22:03:44 +04:00
|
|
|
$subquery = implode(', ', $list);
|
2007-05-16 23:20:55 +04:00
|
|
|
break;
|
2007-04-13 22:03:44 +04:00
|
|
|
case 'pgsql':
|
|
|
|
// pgsql needs special nested LIMIT subquery
|
|
|
|
$subquery = 'SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias';
|
2007-05-16 23:20:55 +04:00
|
|
|
break;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
2007-05-24 22:00:35 +04:00
|
|
|
$field = $this->getTableAlias($rootAlias) . '.' . $table->getIdentifier();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
// only append the subquery if it actually contains something
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($subquery !== '') {
|
2007-04-13 22:03:44 +04:00
|
|
|
array_unshift($this->parts['where'], $field. ' IN (' . $subquery . ')');
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
$modifyLimit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']): '';
|
|
|
|
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($modifyLimit) {
|
2007-05-24 21:46:32 +04:00
|
|
|
$q = $this->_conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']);
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
// return to the previous state
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! empty($string)) {
|
2007-04-13 22:03:44 +04:00
|
|
|
array_pop($this->parts['where']);
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
|
|
|
if ($needsSubQuery) {
|
2007-04-13 22:03:44 +04:00
|
|
|
array_shift($this->parts['where']);
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-05-24 20:13:50 +04:00
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
return $q;
|
|
|
|
}
|
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* getLimitSubquery
|
2007-04-13 22:03:44 +04:00
|
|
|
* this is method is used by the record limit algorithm
|
|
|
|
*
|
|
|
|
* when fetching one-to-many, many-to-many associated data with LIMIT clause
|
|
|
|
* an additional subquery is needed for limiting the number of returned records instead
|
|
|
|
* of limiting the number of sql result set rows
|
|
|
|
*
|
|
|
|
* @return string the limit subquery
|
|
|
|
*/
|
|
|
|
public function getLimitSubquery()
|
|
|
|
{
|
2007-05-16 23:20:55 +04:00
|
|
|
$map = reset($this->_aliasMap);
|
|
|
|
$table = $map['table'];
|
|
|
|
$componentAlias = key($this->_aliasMap);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
// get short alias
|
2007-05-24 22:00:35 +04:00
|
|
|
$alias = $this->getTableAlias($componentAlias);
|
2007-04-13 22:03:44 +04:00
|
|
|
$primaryKey = $alias . '.' . $table->getIdentifier();
|
|
|
|
|
|
|
|
// initialize the base of the subquery
|
|
|
|
$subquery = 'SELECT DISTINCT ' . $primaryKey;
|
|
|
|
|
2007-05-24 21:46:32 +04:00
|
|
|
if ($this->_conn->getDBH()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
|
2007-04-13 22:03:44 +04:00
|
|
|
// pgsql needs the order by fields to be preserved in select clause
|
|
|
|
|
2007-04-26 21:42:03 +04:00
|
|
|
foreach ($this->parts['orderby'] as $part) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$e = explode(' ', $part);
|
|
|
|
|
|
|
|
// don't add primarykey column (its already in the select clause)
|
2007-04-26 21:42:03 +04:00
|
|
|
if ($e[0] !== $primaryKey) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$subquery .= ', ' . $e[0];
|
2007-04-26 21:42:03 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$subquery .= ' FROM';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-31 22:30:47 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
foreach ($this->parts['from'] as $part) {
|
|
|
|
// preserve LEFT JOINs only if needed
|
2007-05-24 21:40:54 +04:00
|
|
|
if (substr($part, 0, 9) === 'LEFT JOIN') {
|
2007-05-16 23:20:55 +04:00
|
|
|
$e = explode(' ', $part);
|
2007-05-31 22:30:47 +04:00
|
|
|
|
|
|
|
if (empty($this->parts['orderby']) && empty($this->parts['where'])) {
|
2007-06-01 14:17:50 +04:00
|
|
|
continue;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
|
|
|
|
$subquery .= ' ' . $part;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// all conditions must be preserved in subquery
|
|
|
|
$subquery .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
|
|
|
|
$subquery .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
|
|
|
|
$subquery .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']) : '';
|
2007-06-18 00:22:39 +04:00
|
|
|
|
|
|
|
$subquery .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
// add driver specific limit clause
|
2007-05-24 21:46:32 +04:00
|
|
|
$subquery = $this->_conn->modifyLimitQuery($subquery, $this->parts['limit'], $this->parts['offset']);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$parts = Doctrine_Tokenizer::quoteExplode($subquery, ' ', "'", "'");
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
foreach($parts as $k => $part) {
|
|
|
|
if(strpos($part, "'") !== false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-05-24 22:30:18 +04:00
|
|
|
if($this->hasTableAlias($part)) {
|
2007-05-24 22:21:26 +04:00
|
|
|
$parts[$k] = $this->generateNewTableAlias($part);
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(strpos($part, '.') !== false) {
|
|
|
|
$e = explode('.', $part);
|
|
|
|
|
|
|
|
$trimmed = ltrim($e[0], '( ');
|
|
|
|
$pos = strpos($e[0], $trimmed);
|
|
|
|
|
2007-05-24 22:21:26 +04:00
|
|
|
$e[0] = substr($e[0], 0, $pos) . $this->generateNewTableAlias($trimmed);
|
2007-04-13 22:03:44 +04:00
|
|
|
$parts[$k] = implode('.', $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$subquery = implode(' ', $parts);
|
|
|
|
|
|
|
|
return $subquery;
|
|
|
|
}
|
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* tokenizeQuery
|
2007-04-13 22:03:44 +04:00
|
|
|
* splits the given dql query into an array where keys
|
|
|
|
* represent different query part names and values are
|
|
|
|
* arrays splitted using sqlExplode method
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
*
|
|
|
|
* parameter:
|
|
|
|
* $query = "SELECT u.* FROM User u WHERE u.name LIKE ?"
|
|
|
|
* returns:
|
|
|
|
* array('select' => array('u.*'),
|
|
|
|
* 'from' => array('User', 'u'),
|
|
|
|
* 'where' => array('u.name', 'LIKE', '?'))
|
|
|
|
*
|
|
|
|
* @param string $query DQL query
|
|
|
|
* @throws Doctrine_Query_Exception if some generic parsing error occurs
|
|
|
|
* @return array an array containing the query string parts
|
|
|
|
*/
|
2007-05-16 23:20:55 +04:00
|
|
|
public function tokenizeQuery($query)
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-05-16 23:20:55 +04:00
|
|
|
$e = Doctrine_Tokenizer::sqlExplode($query, ' ');
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
foreach($e as $k=>$part) {
|
|
|
|
$part = trim($part);
|
|
|
|
switch(strtolower($part)) {
|
|
|
|
case 'delete':
|
|
|
|
case 'update':
|
|
|
|
case 'select':
|
|
|
|
case 'set':
|
|
|
|
case 'from':
|
|
|
|
case 'where':
|
|
|
|
case 'limit':
|
|
|
|
case 'offset':
|
|
|
|
case 'having':
|
|
|
|
$p = $part;
|
|
|
|
$parts[$part] = array();
|
|
|
|
break;
|
|
|
|
case 'order':
|
|
|
|
case 'group':
|
|
|
|
$i = ($k + 1);
|
|
|
|
if(isset($e[$i]) && strtolower($e[$i]) === "by") {
|
|
|
|
$p = $part;
|
|
|
|
$parts[$part] = array();
|
|
|
|
} else
|
|
|
|
$parts[$p][] = $part;
|
|
|
|
break;
|
|
|
|
case "by":
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
if( ! isset($p))
|
|
|
|
throw new Doctrine_Query_Exception("Couldn't parse query.");
|
|
|
|
|
|
|
|
$parts[$p][] = $part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $parts;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* DQL PARSER
|
|
|
|
* parses a DQL query
|
|
|
|
* first splits the query in parts and then uses individual
|
|
|
|
* parsers for each part
|
|
|
|
*
|
|
|
|
* @param string $query DQL query
|
|
|
|
* @param boolean $clear whether or not to clear the aliases
|
|
|
|
* @throws Doctrine_Query_Exception if some generic parsing error occurs
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function parseQuery($query, $clear = true)
|
|
|
|
{
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($clear) {
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->clear();
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
$query = trim($query);
|
|
|
|
$query = str_replace("\n", ' ', $query);
|
|
|
|
$query = str_replace("\r", ' ', $query);
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$parts = $this->tokenizeQuery($query);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
foreach($parts as $k => $part) {
|
|
|
|
$part = implode(' ', $part);
|
2007-05-24 23:47:28 +04:00
|
|
|
$k = strtolower($k);
|
|
|
|
switch ($k) {
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'create':
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->type = self::CREATE;
|
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'insert':
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->type = self::INSERT;
|
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'delete':
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->type = self::DELETE;
|
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'select':
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->type = self::SELECT;
|
2007-05-24 23:47:28 +04:00
|
|
|
$this->parseQueryPart($k, $part);
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'update':
|
2007-04-13 22:03:44 +04:00
|
|
|
$this->type = self::UPDATE;
|
2007-06-19 02:30:15 +04:00
|
|
|
$k = 'from';
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'from':
|
2007-05-24 23:47:28 +04:00
|
|
|
$this->parseQueryPart($k, $part);
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'set':
|
2007-05-24 23:47:28 +04:00
|
|
|
$this->parseQueryPart($k, $part, true);
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'group':
|
|
|
|
case 'order':
|
2007-04-13 22:03:44 +04:00
|
|
|
$k .= 'by';
|
2007-05-16 23:20:55 +04:00
|
|
|
case 'where':
|
|
|
|
case 'having':
|
|
|
|
case 'limit':
|
|
|
|
case 'offset':
|
2007-05-24 23:47:28 +04:00
|
|
|
$this->parseQueryPart($k, $part);
|
2007-04-13 22:03:44 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
public function load($path, $loadFields = true)
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
|
|
|
// parse custom join conditions
|
|
|
|
$e = explode(' ON ', $path);
|
|
|
|
|
|
|
|
$joinCondition = '';
|
2007-05-16 23:20:55 +04:00
|
|
|
|
|
|
|
if (count($e) > 1) {
|
2007-06-11 23:27:16 +04:00
|
|
|
$joinCondition = $e[1];
|
2007-04-13 22:03:44 +04:00
|
|
|
$path = $e[0];
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$tmp = explode(' ', $path);
|
|
|
|
$originalAlias = (count($tmp) > 1) ? end($tmp) : null;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
$e = preg_split("/[.:]/", $tmp[0], -1);
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$fullPath = $tmp[0];
|
|
|
|
$prevPath = '';
|
|
|
|
$fullLength = strlen($fullPath);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if (isset($this->_aliasMap[$e[0]])) {
|
|
|
|
$table = $this->_aliasMap[$e[0]]['table'];
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$prevPath = $parent = array_shift($e);
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
foreach ($e as $key => $name) {
|
|
|
|
// get length of the previous path
|
|
|
|
$length = strlen($prevPath);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
// build the current component path
|
|
|
|
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$delimeter = substr($fullPath, $length, 1);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
// if an alias is not given use the current path as an alias identifier
|
|
|
|
if (strlen($prevPath) === $fullLength && isset($originalAlias)) {
|
|
|
|
$componentAlias = $originalAlias;
|
|
|
|
} else {
|
|
|
|
$componentAlias = $prevPath;
|
|
|
|
}
|
2007-05-24 18:19:44 +04:00
|
|
|
|
|
|
|
// if the current alias already exists, skip it
|
|
|
|
if (isset($this->_aliasMap[$componentAlias])) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! isset($table)) {
|
|
|
|
// process the root of the path
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$table = $this->loadRoot($name, $componentAlias);
|
|
|
|
} else {
|
|
|
|
$join = ($delimeter == ':') ? 'INNER JOIN ' : 'LEFT JOIN ';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$relation = $table->getRelation($name);
|
2007-05-24 18:36:10 +04:00
|
|
|
$table = $relation->getTable();
|
|
|
|
$this->_aliasMap[$componentAlias] = array('table' => $table,
|
2007-05-16 23:20:55 +04:00
|
|
|
'parent' => $parent,
|
|
|
|
'relation' => $relation);
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
|
|
|
$this->needsSubquery = true;
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-24 22:00:35 +04:00
|
|
|
$localAlias = $this->getTableAlias($parent, $table->getTableName());
|
|
|
|
$foreignAlias = $this->getTableAlias($componentAlias, $relation->getTable()->getTableName());
|
2007-05-24 21:46:32 +04:00
|
|
|
$localSql = $this->_conn->quoteIdentifier($table->getTableName()) . ' ' . $localAlias;
|
|
|
|
$foreignSql = $this->_conn->quoteIdentifier($relation->getTable()->getTableName()) . ' ' . $foreignAlias;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$map = $relation->getTable()->inheritanceMap;
|
|
|
|
|
|
|
|
if ( ! $loadFields || ! empty($map) || $joinCondition) {
|
|
|
|
$this->subqueryAliases[] = $foreignAlias;
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($relation instanceof Doctrine_Relation_Association) {
|
|
|
|
$asf = $relation->getAssociationFactory();
|
|
|
|
|
|
|
|
$assocTableName = $asf->getTableName();
|
|
|
|
|
|
|
|
if( ! $loadFields || ! empty($map) || $joinCondition) {
|
|
|
|
$this->subqueryAliases[] = $assocTableName;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$assocPath = $prevPath . '.' . $asf->getComponentName();
|
|
|
|
|
2007-05-24 22:00:35 +04:00
|
|
|
$assocAlias = $this->getTableAlias($assocPath, $asf->getTableName());
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$queryPart = $join . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
|
2007-06-11 23:27:16 +04:00
|
|
|
. $table->getIdentifier() . ' = '
|
|
|
|
. $assocAlias . '.' . $relation->getLocal();
|
|
|
|
|
|
|
|
if ($relation->isEqual()) {
|
|
|
|
$queryPart .= ' OR ' . $localAlias . '.'
|
|
|
|
. $table->getIdentifier() . ' = '
|
|
|
|
. $assocAlias . '.' . $relation->getForeign();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$this->parts['from'][] = $queryPart;
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-11 23:27:16 +04:00
|
|
|
$queryPart = $join . $foreignSql . ' ON ';
|
|
|
|
if ($relation->isEqual()) {
|
|
|
|
$queryPart .= '(';
|
|
|
|
}
|
|
|
|
$queryPart .= $foreignAlias . '.'
|
|
|
|
. $relation->getTable()->getIdentifier() . ' = '
|
|
|
|
. $assocAlias . '.' . $relation->getForeign();
|
|
|
|
|
|
|
|
if ($relation->isEqual()) {
|
|
|
|
$queryPart .= ' OR ' . $foreignAlias . '.' . $table->getIdentifier()
|
|
|
|
. ' = ' . $assocAlias . '.' . $relation->getLocal()
|
|
|
|
. ') AND ' . $foreignAlias . '.' . $table->getIdentifier()
|
|
|
|
. ' != ' . $localAlias . '.' . $table->getIdentifier();
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
} else {
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$queryPart = $join . $foreignSql
|
|
|
|
. ' ON ' . $localAlias . '.'
|
2007-06-11 23:27:16 +04:00
|
|
|
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign();
|
|
|
|
}
|
|
|
|
$this->parts['from'][$componentAlias] = $queryPart;
|
|
|
|
if ( ! empty($joinCondition)) {
|
|
|
|
$this->_pendingJoinConditions[$componentAlias] = $joinCondition;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
|
|
|
if ($loadFields) {
|
2007-06-19 02:30:15 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$restoreState = false;
|
|
|
|
// load fields if necessary
|
|
|
|
if ($loadFields && empty($this->pendingFields)
|
|
|
|
&& empty($this->pendingAggregates)
|
|
|
|
&& empty($this->pendingSubqueries)) {
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$this->pendingFields[$componentAlias] = array('*');
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$restoreState = true;
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if(isset($this->pendingFields[$componentAlias])) {
|
|
|
|
$this->processPendingFields($componentAlias);
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
if(isset($this->pendingAggregates[$componentAlias]) || isset($this->pendingAggregates[0])) {
|
|
|
|
$this->processPendingAggregates($componentAlias);
|
|
|
|
}
|
2007-06-01 14:17:50 +04:00
|
|
|
*/
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ($restoreState) {
|
|
|
|
$this->pendingFields = array();
|
|
|
|
$this->pendingAggregates = array();
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
$parent = $prevPath;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
return end($this->_aliasMap);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* loadRoot
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $componentAlias
|
|
|
|
*/
|
|
|
|
public function loadRoot($name, $componentAlias)
|
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
// get the connection for the component
|
2007-05-24 21:46:32 +04:00
|
|
|
$this->_conn = Doctrine_Manager::getInstance()
|
2007-05-16 23:20:55 +04:00
|
|
|
->getConnectionForComponent($name);
|
|
|
|
|
2007-05-24 21:46:32 +04:00
|
|
|
$table = $this->_conn->getTable($name);
|
2007-05-16 23:20:55 +04:00
|
|
|
$tableName = $table->getTableName();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
// get the short alias for this table
|
2007-05-24 22:00:35 +04:00
|
|
|
$tableAlias = $this->getTableAlias($componentAlias, $tableName);
|
2007-05-16 23:20:55 +04:00
|
|
|
// quote table name
|
2007-05-24 21:46:32 +04:00
|
|
|
$queryPart = $this->_conn->quoteIdentifier($tableName);
|
2007-05-16 23:20:55 +04:00
|
|
|
|
|
|
|
if ($this->type === self::SELECT) {
|
|
|
|
$queryPart .= ' ' . $tableAlias;
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
$this->parts['from'][] = $queryPart;
|
|
|
|
$this->tableAliases[$tableAlias] = $componentAlias;
|
|
|
|
$this->_aliasMap[$componentAlias] = array('table' => $table);
|
|
|
|
|
2007-04-13 22:03:44 +04:00
|
|
|
return $table;
|
|
|
|
}
|
2007-06-19 02:30:15 +04:00
|
|
|
/**
|
|
|
|
* count
|
|
|
|
* fetches the count of the query
|
|
|
|
*
|
|
|
|
* This method executes the main query without all the
|
2007-05-16 23:20:55 +04:00
|
|
|
* selected fields, ORDER BY part, LIMIT part and OFFSET part.
|
2007-04-13 22:03:44 +04:00
|
|
|
*
|
2007-05-16 23:20:55 +04:00
|
|
|
* Example:
|
|
|
|
* Main query:
|
|
|
|
* SELECT u.*, p.phonenumber FROM User u
|
|
|
|
* LEFT JOIN u.Phonenumber p
|
|
|
|
* WHERE p.phonenumber = '123 123' LIMIT 10
|
|
|
|
*
|
2007-05-24 20:13:50 +04:00
|
|
|
* The modified DQL query:
|
2007-05-16 23:20:55 +04:00
|
|
|
* SELECT COUNT(DISTINCT u.id) FROM User u
|
|
|
|
* LEFT JOIN u.Phonenumber p
|
|
|
|
* WHERE p.phonenumber = '123 123'
|
|
|
|
*
|
|
|
|
* @param array $params an array of prepared statement parameters
|
2007-06-19 02:30:15 +04:00
|
|
|
* @return integer the count of this query
|
2007-04-13 22:03:44 +04:00
|
|
|
*/
|
2007-06-19 02:30:15 +04:00
|
|
|
public function count($params = array())
|
2007-04-13 22:03:44 +04:00
|
|
|
{
|
2007-06-19 02:30:15 +04:00
|
|
|
$this->getQuery();
|
|
|
|
|
|
|
|
// initialize temporary variables
|
|
|
|
$where = $this->parts['where'];
|
|
|
|
$having = $this->parts['having'];
|
|
|
|
$map = reset($this->_aliasMap);
|
|
|
|
$componentAlias = key($this->_aliasMap);
|
|
|
|
$table = $map['table'];
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
|
|
|
|
// build the query base
|
2007-06-19 02:30:15 +04:00
|
|
|
$q = 'SELECT COUNT(DISTINCT ' . $this->getTableAlias($componentAlias)
|
2007-05-16 23:20:55 +04:00
|
|
|
. '.' . $table->getIdentifier()
|
|
|
|
. ') FROM ' . $this->buildFromPart();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
// append column aggregation inheritance (if needed)
|
|
|
|
$string = $this->applyInheritance();
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! empty($string)) {
|
|
|
|
$where[] = $string;
|
|
|
|
}
|
|
|
|
// append conditions
|
|
|
|
$q .= ( ! empty($where)) ? ' WHERE ' . implode(' AND ', $where) : '';
|
2007-06-19 02:30:15 +04:00
|
|
|
$q .= ( ! empty($having)) ? ' HAVING ' . implode(' AND ', $having): '';
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if ( ! is_array($params)) {
|
|
|
|
$params = array($params);
|
|
|
|
}
|
|
|
|
// append parameters
|
2007-05-24 21:49:15 +04:00
|
|
|
$params = array_merge($this->_params, $params);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-06-19 02:30:15 +04:00
|
|
|
return (int) $this->getConnection()->fetchOne($q, $params);
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* query
|
|
|
|
* query the database with DQL (Doctrine Query Language)
|
|
|
|
*
|
|
|
|
* @param string $query DQL query
|
|
|
|
* @param array $params prepared statement parameters
|
|
|
|
* @see Doctrine::FETCH_* constants
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function query($query, $params = array())
|
|
|
|
{
|
|
|
|
$this->parseQuery($query);
|
2007-04-13 22:03:44 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
return $this->execute($params);
|
|
|
|
}
|
2007-04-13 22:03:44 +04:00
|
|
|
}
|