1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Query.php

1804 lines
64 KiB
PHP
Raw Normal View History

<?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>.
*/
Doctrine::autoload('Doctrine_Query_Abstract');
/**
* Doctrine_Query
* A Doctrine_Query object represents a DQL query. It is used to query databases for
* data in an object-oriented fashion. A DQL query understands relations and inheritance
* and is dbms independant.
*
* @package Doctrine
* @subpackage Query
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @todo Proposal: This class does far too much. It should have only 1 task: Collecting
* the DQL query parts and the query parameters (the query state and caching options/methods
* can remain here, too).
* The actual SQL construction could be done by a separate object (Doctrine_Query_SqlBuilder?)
* whose task it is to convert DQL into SQL.
* Furthermore the SqlBuilder? can then use other objects (Doctrine_Query_Tokenizer?),
* (Doctrine_Query_Parser(s)?) to accomplish his work. Doctrine_Query does not need
* to know the tokenizer/parsers. There could be extending
* implementations of SqlBuilder? that cover the specific SQL dialects.
* This would release Doctrine_Connection and the Doctrine_Connection_xxx classes
* from this tedious task.
* This would also largely reduce the currently huge interface of Doctrine_Query(_Abstract)
* and better hide all these transformation internals from the public Query API.
*
* @internal The lifecycle of a Query object is the following:
* After construction the query object is empty. Through using the fluent
* query interface the user fills the query object with DQL parts and query parameters.
* These get collected in {@link $_dqlParts} and {@link $_params}, respectively.
* When the query is executed the first time, or when {@link getSqlQuery()}
* is called the first time, the collected DQL parts get parsed and the resulting
* connection-driver specific SQL is generated. The generated SQL parts are
* stored in {@link $_sqlParts} and the final resulting SQL query is stored in
* {@link $_sql}.
*/
class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Serializable
2007-05-16 23:20:55 +04:00
{
/**
* @var array The DQL keywords.
*/
protected static $_keywords = array('ALL',
'AND',
'ANY',
'AS',
'ASC',
'AVG',
'BETWEEN',
'BIT_LENGTH',
'BY',
'CHARACTER_LENGTH',
'CHAR_LENGTH',
'CURRENT_DATE',
'CURRENT_TIME',
'CURRENT_TIMESTAMP',
'DELETE',
'DESC',
'DISTINCT',
'EMPTY',
'EXISTS',
'FALSE',
'FETCH',
'FROM',
'GROUP',
'HAVING',
'IN',
'INDEXBY',
'INNER',
'IS',
'JOIN',
'LEFT',
'LIKE',
'LOWER',
'MEMBER',
'MOD',
'NEW',
'NOT',
'NULL',
'OBJECT',
'OF',
'OR',
'ORDER',
'OUTER',
'POSITION',
'SELECT',
'SOME',
'TRIM',
'TRUE',
'UNKNOWN',
'UPDATE',
'WHERE');
/**
* @var array
*/
protected $_subqueryAliases = array();
/**
* @var array $_aggregateAliasMap an array containing all aggregate aliases, keys as dql aliases
* and values as sql aliases
*/
protected $_aggregateAliasMap = array();
/**
* @var array
*/
protected $_pendingAggregates = array();
/**
* @param boolean $needsSubquery
*/
protected $_needsSubquery = false;
/**
* @param boolean $isSubquery whether or not this query object is a subquery of another
* query object
*/
protected $_isSubquery;
2007-05-26 23:50:40 +04:00
/**
2007-09-13 01:42:42 +04:00
* @var array $_neededTables an array containing the needed table aliases
2007-05-26 23:50:40 +04:00
*/
protected $_neededTables = 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)
*/
protected $_pendingSubqueries = array();
2007-09-13 01:42:42 +04:00
/**
* @var array $_pendingFields an array of pending fields (fields waiting to be parsed)
2007-09-13 01:42:42 +04:00
*/
protected $_pendingFields = 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
*/
protected $_parsers = array();
2007-05-27 15:38:16 +04:00
/**
* @var array $_pendingJoinConditions an array containing pending joins
*/
protected $_pendingJoinConditions = array();
/**
* @var array
*/
protected $_expressionMap = array();
/**
* @var string $_sql cached SQL query
*/
protected $_sql;
/**
* create
* returns a new Doctrine_Query object
*
2007-05-24 20:13:50 +04:00
* @param Doctrine_Connection $conn optional connection parameter
* @return Doctrine_Query
*/
2007-05-24 20:13:50 +04:00
public static function create($conn = null)
{
2007-05-24 20:13:50 +04:00
return new Doctrine_Query($conn);
}
/**
* Resets the query to the state just after it has been instantiated.
*/
public function reset()
2007-06-19 02:30:15 +04:00
{
$this->_pendingJoinConditions = array();
$this->_pendingSubqueries = array();
$this->_pendingFields = array();
2007-06-19 02:30:15 +04:00
$this->_neededTables = array();
2007-06-27 22:42:47 +04:00
$this->_expressionMap = array();
$this->_subqueryAliases = array();
$this->_needsSubquery = false;
$this->_isLimitSubqueryUsed = false;
2007-06-19 02:30:15 +04:00
}
2007-05-24 20:13:50 +04:00
/**
* createSubquery
* creates a subquery
2007-05-24 20:13:50 +04:00
*
* @return Doctrine_Hydrate
2007-05-24 20:13:50 +04:00
*/
public function createSubquery()
2007-05-24 20:13:50 +04:00
{
$class = get_class($this);
$obj = new $class();
// copy the aliases to the subquery
$obj->copyAliases($this);
// this prevents the 'id' being selected, re ticket #307
$obj->isSubquery(true);
return $obj;
}
/**
* _addPendingJoinCondition
*
* @param string $componentAlias component alias
* @param string $joinCondition dql join condition
* @return Doctrine_Query this object
*/
protected 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;
}
}
2007-05-18 03:13:58 +04:00
/**
* getEnumParams
* get all enumerated parameters
*
* @return array all enumerated parameters
*/
public function getEnumParams()
{
return $this->_enumParams;
}
/**
* getDql
* returns the DQL query that is represented by this query object.
*
* the query is built from $_dqlParts
*
* @return string the DQL query
*/
public function getDql()
{
$q = '';
$q .= ( ! empty($this->_dqlParts['select']))? 'SELECT ' . implode(', ', $this->_dqlParts['select']) : '';
$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']) : '';
return $q;
}
2007-05-24 18:19:44 +04:00
/**
* getParams
2007-05-24 18:19:44 +04:00
*
* @return array
2007-05-24 18:19:44 +04:00
*/
public function getParams()
2007-05-24 18:19:44 +04:00
{
return array_merge($this->_params['join'], $this->_params['set'], $this->_params['where'], $this->_params['having']);
2007-05-24 18:19:44 +04:00
}
/**
* setParams
*
* @param array $params
*/
public function setParams(array $params = array()) {
$this->_params = $params;
}
/**
* fetchArray
* Convenience method to execute using array fetching as hydration mode.
*
* @param string $params
* @return array
*/
public function fetchArray($params = array()) {
return $this->execute($params, Doctrine::HYDRATE_ARRAY);
}
/**
* fetchOne
* Convenience method to execute the query and return the first item
* of the collection.
*
* @param string $params Parameters
* @param int $hydrationMode Hydration mode
* @return mixed Array or Doctrine_Collection or false if no result.
*/
public function fetchOne($params = array(), $hydrationMode = null)
{
$collection = $this->execute($params, $hydrationMode);
if (count($collection) === 0) {
return false;
}
if ($collection instanceof Doctrine_Collection) {
return $collection->getFirst();
} else if (is_array($collection)) {
return array_shift($collection);
}
return false;
2007-05-18 03:13:58 +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
* @return string
* @deprecated
*/
public function getAggregateAlias($dqlAlias)
{
return $this->getSqlAggregateAlias($dqlAlias);
}
2007-05-16 23:20:55 +04:00
/**
* getSqlAggregateAlias
2007-05-16 23:20:55 +04:00
*
* @param string $dqlAlias the dql alias of an aggregate value
* @return string
2007-05-16 23:20:55 +04:00
*/
public function getSqlAggregateAlias($dqlAlias)
{
if (isset($this->_aggregateAliasMap[$dqlAlias])) {
// mark the expression as used
$this->_expressionMap[$dqlAlias][1] = true;
return $this->_aggregateAliasMap[$dqlAlias];
} else if ( ! empty($this->_pendingAggregates)) {
$this->processPendingAggregates();
return $this->getSqlAggregateAlias($dqlAlias);
} else {
throw new Doctrine_Query_Exception('Unknown aggregate alias: ' . $dqlAlias);
2007-05-16 23:20:55 +04:00
}
}
2007-07-09 00:18:50 +04:00
/**
* getDqlPart
* returns a specific DQL query part.
2007-07-09 00:18:50 +04:00
*
* @param string $queryPart the name of the query part
* @return string the DQL query part
* @todo Description: List which query parts exist or point to the method/property
* where they are listed.
2007-07-09 00:18:50 +04:00
*/
public function getDqlPart($queryPart)
{
if ( ! isset($this->_dqlParts[$queryPart])) {
throw new Doctrine_Query_Exception('Unknown query part ' . $queryPart);
}
2007-07-09 00:18:50 +04:00
return $this->_dqlParts[$queryPart];
}
/**
* contains
*
* Method to check if a arbitrary piece of dql exists
*
* @param string $dql Arbitrary piece of dql to check for
* @return boolean
*/
public function contains($dql)
{
return stripos($this->getDql(), $dql) === false ? false : true;
}
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
* @todo Description: What is a 'pending field' (and are there non-pending fields, too)?
* What is 'processed'? (Meaning: What information is gathered & stored away)
2007-05-16 23:20:55 +04:00
*/
public function processPendingFields($componentAlias)
{
$tableAlias = $this->getSqlTableAlias($componentAlias);
$baseTable = $this->_queryComponents[$componentAlias]['table'];
$mapper = $this->_queryComponents[$componentAlias]['mapper'];
2007-11-19 20:55:23 +03:00
if ( ! isset($this->_pendingFields[$componentAlias])) {
return;
}
2007-11-19 20:55:23 +03:00
$fields = $this->_pendingFields[$componentAlias];
// check for wildcards
if (in_array('*', $fields)) {
$fields = $mapper->getFieldNames();
2007-11-19 20:55:23 +03:00
} 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((array) $baseTable->getIdentifier(), $fields));
}
2007-11-19 20:55:23 +03:00
}
$fields = array_unique(array_merge($fields, $mapper->getCustomFields()));
2007-11-19 20:55:23 +03:00
$sql = array();
foreach ($fields as $fieldName) {
$table = $mapper->getOwningTable($fieldName);
if ($table !== $baseTable) {
$tableAlias = $this->getSqlTableAlias($componentAlias . '.' . $table->getComponentName());
2007-11-19 20:55:23 +03:00
} else {
$tableAlias = $this->getSqlTableAlias($componentAlias);
2007-09-13 01:42:42 +04:00
}
$columnName = $table->getColumnName($fieldName);
$columnName = $table->getColumnName($fieldName);
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $columnName)
. ' AS '
. $this->_conn->quoteIdentifier($this->getSqlTableAlias($componentAlias) . '__' . $columnName);
2007-11-19 20:55:23 +03:00
if ( ! in_array($tableAlias, $this->_neededTables)) {
$this->_neededTables[] = $tableAlias;
}
}
2007-11-19 20:55:23 +03:00
return implode(', ', $sql);
2007-09-13 01:42:42 +04:00
}
2007-09-13 01:42:42 +04:00
/**
* parseSelectField
*
* @throws Doctrine_Query_Exception if unknown component alias has been given
* @return void
* @todo Description: Explain what this method does. Is there a relation to parseSelect()?
* (It doesnt seem to get called from there...?). In what circumstances is this method
* used?
2007-09-13 01:42:42 +04:00
*/
public function parseSelectField($field)
{
$terms = explode('.', $field);
if (isset($terms[1])) {
2007-09-13 01:42:42 +04:00
$componentAlias = $terms[0];
$field = $terms[1];
} else {
reset($this->_queryComponents);
$componentAlias = key($this->_queryComponents);
2007-09-13 01:42:42 +04:00
$fields = $terms[0];
}
$tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_queryComponents[$componentAlias]['table'];
2007-09-13 01:42:42 +04:00
// check for wildcards
if ($field === '*') {
$sql = array();
foreach ($table->getColumnNames() as $field) {
$sql[] = $this->parseSelectField($componentAlias . '.' . $field);
}
return implode(', ', $sql);
} else {
$name = $table->getColumnName($field);
$this->_neededTables[] = $tableAlias;
return $this->_conn->quoteIdentifier($tableAlias . '.' . $name)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
2007-09-13 01:42:42 +04:00
}
}
2007-09-13 01:42:42 +04:00
/**
* getExpressionOwner
* returns the component alias for owner of given expression
*
* @param string $expr expression from which to get to owner from
* @return string the component alias
* @todo Description: What does it mean if a component is an 'owner' of an expression?
* What kind of 'expression' are we talking about here?
2007-09-13 01:42:42 +04:00
*/
public function getExpressionOwner($expr)
{
if (strtoupper(substr(trim($expr, '( '), 0, 6)) !== 'SELECT') {
2007-09-13 01:42:42 +04:00
preg_match_all("/[a-z0-9_]+\.[a-z0-9_]+[\.[a-z0-9]+]*/i", $expr, $matches);
2007-09-13 01:42:42 +04:00
$match = current($matches);
2007-09-13 01:42:42 +04:00
if (isset($match[0])) {
$terms = explode('.', $match[0]);
2007-09-13 01:42:42 +04:00
return $terms[0];
}
}
return $this->getRootAlias();
}
/**
* parseSelect
* parses the query select part and
* adds selected fields to pendingFields array
*
* @param string $dql
* @todo Description: What information is extracted (and then stored)?
*/
public function parseSelect($dql)
{
$refs = $this->_tokenizer->sqlExplode($dql, ',');
2007-06-09 00:17:17 +04:00
$pos = strpos(trim($refs[0]), ' ');
$first = substr($refs[0], 0, $pos);
2007-09-13 01:42:42 +04:00
// check for DISTINCT keyword
2007-06-09 00:17:17 +04:00
if ($first === 'DISTINCT') {
$this->_sqlParts['distinct'] = true;
2007-06-09 00:17:17 +04:00
$refs[0] = substr($refs[0], ++$pos);
}
2007-09-13 01:42:42 +04:00
$parsedComponents = array();
2007-04-14 20:28:09 +04:00
foreach ($refs as $reference) {
2007-06-01 14:17:50 +04:00
$reference = trim($reference);
2007-09-13 01:42:42 +04:00
if (empty($reference)) {
continue;
}
$terms = $this->_tokenizer->sqlExplode($reference, ' ');
2007-09-13 01:42:42 +04:00
$pos = strpos($terms[0], '(');
2007-09-13 01:42:42 +04:00
if (count($terms) > 1 || $pos !== false) {
$expression = array_shift($terms);
$alias = array_pop($terms);
if ( ! $alias) {
$alias = substr($expression, 0, $pos);
2007-04-14 20:28:09 +04:00
}
2007-09-13 01:42:42 +04:00
$componentAlias = $this->getExpressionOwner($expression);
$expression = $this->parseClause($expression);
$tableAlias = $this->getTableAlias($componentAlias);
$index = count($this->_aggregateAliasMap);
2007-09-13 01:42:42 +04:00
$sqlAlias = $this->_conn->quoteIdentifier($tableAlias . '__' . $index);
$this->_sqlParts['select'][] = $expression . ' AS ' . $sqlAlias;
2007-09-13 01:42:42 +04:00
$this->_aggregateAliasMap[$alias] = $sqlAlias;
2007-09-13 01:42:42 +04:00
$this->_expressionMap[$alias][0] = $expression;
$this->_queryComponents[$componentAlias]['agg'][$index] = $alias;
2007-06-09 00:17:17 +04:00
2007-09-13 01:42:42 +04:00
$this->_neededTables[] = $tableAlias;
} else {
$e = explode('.', $terms[0]);
if (isset($e[1])) {
$componentAlias = $e[0];
$field = $e[1];
} else {
reset($this->_queryComponents);
$componentAlias = key($this->_queryComponents);
2007-09-13 01:42:42 +04:00
$field = $e[0];
}
$this->_pendingFields[$componentAlias][] = $field;
2007-04-14 20:28:09 +04:00
}
}
}
2007-08-08 00:41:33 +04:00
/**
* parseClause
* parses given DQL clause
*
2007-08-09 21:25:07 +04:00
* this method handles five tasks:
*
2007-08-08 00:41:33 +04:00
* 1. Converts all DQL functions to their native SQL equivalents
* 2. Converts all component references to their table alias equivalents
* 3. Converts all field names to actual column names
2007-08-09 21:25:07 +04:00
* 4. Quotes all identifiers
* 5. Parses nested clauses and subqueries recursively
2007-08-08 00:41:33 +04:00
*
* @return string SQL string
* @todo Description: What is a 'dql clause' (and what not)?
* Refactor: Too long & nesting level
2007-08-08 00:41:33 +04:00
*/
public function parseClause($clause)
2007-08-08 00:41:33 +04:00
{
$clause = trim($clause);
if (is_numeric($clause)) {
return $clause;
}
$terms = $this->_tokenizer->clauseExplode($clause, array(' ', '+', '-', '*', '/'));
2007-08-08 00:41:33 +04:00
$str = '';
foreach ($terms as $term) {
$pos = strpos($term[0], '(');
2007-12-10 21:59:12 +03:00
2007-08-08 00:41:33 +04:00
if ($pos !== false) {
$name = substr($term[0], 0, $pos);
2007-12-10 21:59:12 +03:00
$term[0] = $this->parseFunctionExpression($term[0]);
2007-08-08 00:41:33 +04:00
} else {
if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") {
2007-12-10 21:59:12 +03:00
2007-08-08 00:41:33 +04:00
if (strpos($term[0], '.') !== false) {
if ( ! is_numeric($term[0])) {
$e = explode('.', $term[0]);
$field = array_pop($e);
2007-08-09 21:25:07 +04:00
if ($this->getType() === Doctrine_Query::SELECT) {
$componentAlias = implode('.', $e);
if (empty($componentAlias)) {
$componentAlias = $this->getRootAlias();
}
2007-10-30 22:19:53 +03:00
$this->load($componentAlias);
// check the existence of the component alias
if ( ! isset($this->_queryComponents[$componentAlias])) {
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
}
$table = $this->_queryComponents[$componentAlias]['table'];
$def = $table->getDefinitionOf($field);
// get the actual field name from alias
$field = $table->getColumnName($field);
// check column existence
if ( ! $def) {
throw new Doctrine_Query_Exception('Unknown column ' . $field);
}
if (isset($def['owner'])) {
$componentAlias = $componentAlias . '.' . $def['owner'];
}
$tableAlias = $this->getTableAlias($componentAlias);
// build sql expression
$term[0] = $this->_conn->quoteIdentifier($tableAlias)
. '.'
. $this->_conn->quoteIdentifier($field);
} else {
// build sql expression
2007-12-12 19:21:54 +03:00
$field = $this->getRoot()->getColumnName($field);
$term[0] = $this->_conn->quoteIdentifier($field);
2007-08-08 00:41:33 +04:00
}
}
} else {
if ( ! empty($term[0]) &&
! in_array(strtoupper($term[0]), self::$_keywords) &&
! is_numeric($term[0])) {
$componentAlias = $this->getRootAlias();
$found = false;
if ($componentAlias !== false &&
$componentAlias !== null) {
$table = $this->_queryComponents[$componentAlias]['table'];
// check column existence
2007-12-12 19:21:54 +03:00
if ($table->hasField($term[0])) {
$found = true;
$def = $table->getDefinitionOf($term[0]);
2007-12-12 19:21:54 +03:00
// get the actual column name from field name
$term[0] = $table->getColumnName($term[0]);
if (isset($def['owner'])) {
$componentAlias = $componentAlias . '.' . $def['owner'];
}
$tableAlias = $this->getTableAlias($componentAlias);
if ($this->getType() === Doctrine_Query::SELECT) {
// build sql expression
$term[0] = $this->_conn->quoteIdentifier($tableAlias)
. '.'
. $this->_conn->quoteIdentifier($term[0]);
} else {
// build sql expression
$term[0] = $this->_conn->quoteIdentifier($term[0]);
}
} else {
$found = false;
}
}
if ( ! $found) {
$term[0] = $this->getSqlAggregateAlias($term[0]);
}
}
2007-08-08 00:41:33 +04:00
}
}
}
$str .= $term[0] . $term[1];
}
return $str;
}
2007-12-10 22:19:29 +03:00
public function parseIdentifierReference($expr)
{
}
2007-12-10 21:59:12 +03:00
public function parseFunctionExpression($expr)
{
$pos = strpos($expr, '(');
$name = substr($expr, 0, $pos);
if ($name === '') {
return $this->parseSubquery($expr);
}
$argStr = substr($expr, ($pos + 1), -1);
$args = array();
// parse args
foreach ($this->_tokenizer->sqlExplode($argStr, ',') as $arg) {
$args[] = $this->parseClause($arg);
}
// convert DQL function to its RDBMS specific equivalent
try {
$expr = call_user_func_array(array($this->_conn->expression, $name), $args);
} catch (Doctrine_Expression_Exception $e) {
throw new Doctrine_Query_Exception('Unknown function ' . $name . '.');
}
return $expr;
}
2007-12-10 21:51:22 +03:00
public function parseSubquery($subquery)
{
$trimmed = trim($this->_tokenizer->bracketTrim($subquery));
2007-12-10 21:51:22 +03:00
// check for possible subqueries
if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
// parse subquery
$trimmed = $this->createSubquery()->parseDqlQuery($trimmed)->getQuery();
} else {
// parse normal clause
$trimmed = $this->parseClause($trimmed);
}
return '(' . $trimmed . ')';
}
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
* @todo Better description. i.e. What is a 'pending subquery'? What does 'processed' mean?
* (parsed? sql is constructed? some information is gathered?)
2007-05-26 23:50:40 +04:00
*/
2007-07-09 00:18:50 +04:00
public function processPendingSubqueries()
2007-04-14 20:28:09 +04:00
{
foreach ($this->_pendingSubqueries as $value) {
2007-04-14 20:28:09 +04:00
list($dql, $alias) = $value;
2007-07-14 01:51:03 +04:00
$subquery = $this->createSubquery();
$sql = $subquery->parseDqlQuery($dql, false)->getQuery();
2007-04-14 20:28:09 +04:00
reset($this->_queryComponents);
$componentAlias = key($this->_queryComponents);
2007-05-16 23:20:55 +04:00
$tableAlias = $this->getTableAlias($componentAlias);
2007-04-14 20:28:09 +04:00
$sqlAlias = $tableAlias . '__' . count($this->_aggregateAliasMap);
2007-05-19 21:49:16 +04:00
$this->_sqlParts['select'][] = '(' . $sql . ') AS ' . $this->_conn->quoteIdentifier($sqlAlias);
2007-05-19 21:49:16 +04:00
$this->_aggregateAliasMap[$alias] = $sqlAlias;
$this->_queryComponents[$componentAlias]['agg'][] = $alias;
2007-04-14 20:28:09 +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
* @todo Better description. i.e. What is a 'pending aggregate'? What does 'processed' mean?
2007-05-26 23:50:40 +04:00
*/
2007-06-01 14:17:50 +04:00
public function processPendingAggregates()
{
2007-06-19 02:30:15 +04:00
// iterate trhough all aggregates
foreach ($this->_pendingAggregates as $aggregate) {
2007-06-01 14:17:50 +04:00
list ($expression, $components, $alias) = $aggregate;
2007-06-01 14:17:50 +04:00
$tableAliases = array();
2007-06-01 14:17:50 +04:00
// iterate through the component references within the aggregate function
if ( ! empty ($components)) {
foreach ($components as $component) {
2007-07-09 16:36:21 +04:00
if (is_numeric($component)) {
continue;
}
2007-06-01 14:17:50 +04:00
$e = explode('.', $component);
2007-06-01 14:17:50 +04:00
$field = array_pop($e);
$componentAlias = implode('.', $e);
2007-06-01 14:17:50 +04:00
// check the existence of the component alias
if ( ! isset($this->_queryComponents[$componentAlias])) {
2007-06-01 14:17:50 +04:00
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
}
$table = $this->_queryComponents[$componentAlias]['table'];
2007-06-01 14:17:50 +04:00
$field = $table->getColumnName($field);
2007-06-01 14:17:50 +04:00
// check column existence
if ( ! $table->hasColumn($field)) {
throw new Doctrine_Query_Exception('Unknown column ' . $field);
}
$sqlTableAlias = $this->getSqlTableAlias($componentAlias);
$tableAliases[$sqlTableAlias] = true;
2007-06-01 14:17:50 +04:00
// build sql expression
$identifier = $this->_conn->quoteIdentifier($sqlTableAlias . '.' . $field);
2007-07-07 00:55:15 +04:00
$expression = str_replace($component, $identifier, $expression);
}
}
2007-06-01 14:17:50 +04:00
if (count($tableAliases) !== 1) {
$componentAlias = reset($this->_tableAliasMap);
$tableAlias = key($this->_tableAliasMap);
2007-06-01 14:17:50 +04:00
}
$index = count($this->_aggregateAliasMap);
2007-07-07 00:55:15 +04:00
$sqlAlias = $this->_conn->quoteIdentifier($tableAlias . '__' . $index);
$this->_sqlParts['select'][] = $expression . ' AS ' . $sqlAlias;
2007-06-01 14:17:50 +04:00
$this->_aggregateAliasMap[$alias] = $sqlAlias;
2007-06-27 22:42:47 +04:00
$this->_expressionMap[$alias][0] = $expression;
2007-06-01 14:17:50 +04:00
$this->_queryComponents[$componentAlias]['agg'][$index] = $alias;
2007-05-26 23:50:40 +04:00
2007-09-13 01:42:42 +04:00
$this->_neededTables[] = $tableAlias;
}
2007-06-01 14:17:50 +04:00
// reset the state
$this->_pendingAggregates = array();
}
/**
* _buildSqlQueryBase
2007-05-16 23:20:55 +04:00
* returns the base of the generated sql query
* On mysql driver special strategy has to be used for DELETE statements
* (where is this special strategy??)
*
2007-05-16 23:20:55 +04:00
* @return string the base of the generated sql query
*/
protected function _buildSqlQueryBase()
{
switch ($this->_type) {
2007-05-16 23:20:55 +04:00
case self::DELETE:
$q = 'DELETE FROM ';
break;
2007-05-16 23:20:55 +04:00
case self::UPDATE:
$q = 'UPDATE ';
break;
2007-05-16 23:20:55 +04:00
case self::SELECT:
$distinct = ($this->_sqlParts['distinct']) ? 'DISTINCT ' : '';
$q = 'SELECT ' . $distinct . implode(', ', $this->_sqlParts['select']) . ' FROM ';
2007-05-16 23:20:55 +04:00
break;
}
2007-05-16 23:20:55 +04:00
return $q;
}
/**
* _buildSqlFromPart
2007-05-26 23:50:40 +04:00
* builds the from part of the query and returns it
*
2007-05-26 23:50:40 +04:00
* @return string the query sql from part
*/
protected function _buildSqlFromPart()
{
2007-06-19 02:30:15 +04:00
$q = '';
foreach ($this->_sqlParts['from'] as $k => $part) {
2007-05-16 23:20:55 +04:00
if ($k === 0) {
$q .= $part;
continue;
}
2007-11-09 00:02:56 +03:00
2007-05-16 23:20:55 +04:00
// preserve LEFT JOINs only if needed
2007-11-09 00:02:56 +03:00
// Check if it's JOIN, if not add a comma separator instead of space
if (!preg_match('/\bJOIN\b/i', $part) && !isset($this->_pendingJoinConditions[$k])) {
$q .= ', ' . $part;
} else {
if (substr($part, 0, 9) === 'LEFT JOIN') {
$e = explode(' ', $part);
$aliases = array_merge($this->_subqueryAliases,
2007-11-09 00:02:56 +03:00
array_keys($this->_neededTables));
2007-11-09 00:02:56 +03:00
if ( ! in_array($e[3], $aliases) &&
! in_array($e[2], $aliases) &&
2007-11-09 00:02:56 +03:00
! empty($this->_pendingFields)) {
continue;
}
}
2007-11-09 00:02:56 +03:00
if (isset($this->_pendingJoinConditions[$k])) {
$parser = new Doctrine_Query_JoinCondition($this, $this->_tokenizer);
2007-11-09 00:02:56 +03:00
if (strpos($part, ' ON ') !== false) {
$part .= ' AND ';
} else {
$part .= ' ON ';
}
$part .= $parser->parse($this->_pendingJoinConditions[$k]);
2007-11-09 00:02:56 +03:00
unset($this->_pendingJoinConditions[$k]);
}
2007-11-09 00:02:56 +03:00
$q .= ' ' . $part;
2007-05-16 23:20:55 +04:00
}
$this->_sqlParts['from'][$k] = $part;
}
return $q;
2007-06-19 14:43:19 +04:00
}
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
}
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-09-13 01:42:42 +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 getSqlQuery($params = array())
{
2007-06-28 02:29:57 +04:00
if ($this->_state !== self::STATE_DIRTY) {
return $this->_sql;
}
2007-06-26 00:08:16 +04:00
2007-06-19 14:43:19 +04:00
// reset the state
2007-07-14 01:51:03 +04:00
if ( ! $this->isSubquery()) {
$this->_queryComponents = array();
$this->_pendingAggregates = array();
$this->_aggregateAliasMap = array();
2007-07-14 01:51:03 +04:00
}
$this->reset();
2007-05-27 22:52:33 +04:00
// invoke the preQuery hook
$this->preQuery();
// process the DQL parts => generate the SQL parts.
// this will also populate the $_queryComponents.
2007-06-19 02:30:15 +04:00
foreach ($this->_dqlParts as $queryPartName => $queryParts) {
$this->_processDqlQueryPart($queryPartName, $queryParts);
2007-05-24 20:13:50 +04:00
}
$this->_state = self::STATE_CLEAN;
2007-11-19 20:55:23 +03:00
$params = $this->convertEnums($params);
2007-06-19 14:43:19 +04:00
// Proceed with the generated SQL
if (empty($this->_sqlParts['from'])) {
return false;
2007-05-16 23:20:55 +04:00
}
$needsSubQuery = false;
$subquery = '';
$map = reset($this->_queryComponents);
2007-05-16 23:20:55 +04:00
$table = $map['table'];
$rootAlias = key($this->_queryComponents);
if ( ! empty($this->_sqlParts['limit']) && $this->_needsSubquery &&
$table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
$this->_isLimitSubqueryUsed = true;
$needsSubQuery = true;
}
2007-09-13 01:42:42 +04:00
$sql = array();
foreach ($this->_queryComponents as $alias => $map) {
2007-09-13 01:42:42 +04:00
$fieldSql = $this->processPendingFields($alias);
if ( ! empty($fieldSql)) {
$sql[] = $fieldSql;
}
}
if ( ! empty($sql)) {
array_unshift($this->_sqlParts['select'], implode(', ', $sql));
2007-09-13 01:42:42 +04:00
}
2007-09-13 02:03:25 +04:00
$this->_pendingFields = array();
2007-04-14 20:28:09 +04:00
// build the basic query
$q = $this->_buildSqlQueryBase();
$q .= $this->_buildSqlFromPart();
2007-05-16 23:20:55 +04:00
if ( ! empty($this->_sqlParts['set'])) {
$q .= ' SET ' . implode(', ', $this->_sqlParts['set']);
}
// append discriminator column conditions (if any)
$string = $this->_createDiscriminatorConditionSql();
//echo "orig:$string<br /><br />";
2007-05-16 23:20:55 +04:00
if ( ! empty($string)) {
2007-09-13 02:03:25 +04:00
if (substr($string, 0, 1) === '(' && substr($string, -1) === ')') {
$this->_sqlParts['where'][] = $string;
2007-09-13 02:03:25 +04:00
} else {
$this->_sqlParts['where'][] = '(' . $string . ')';
2007-09-13 02:03:25 +04:00
}
2007-05-16 23:20:55 +04:00
}
$modifyLimit = true;
if ( ! empty($this->_sqlParts['limit']) || ! empty($this->_sqlParts['offset'])) {
2007-05-16 23:20:55 +04:00
if ($needsSubQuery) {
$subquery = $this->getLimitSubquery();
2007-11-19 20:55:23 +03:00
// what about composite keys?
$idColumnName = $table->getColumnName($table->getIdentifier());
switch (strtolower($this->_conn->getDriverName())) {
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-07-09 15:23:44 +04:00
$subquery = implode(', ', array_map(array($this->_conn, 'quote'), $list));
2007-05-16 23:20:55 +04:00
break;
case 'pgsql':
// pgsql needs special nested LIMIT subquery
2007-11-19 20:55:23 +03:00
$subquery = 'SELECT doctrine_subquery_alias.' . $idColumnName . ' FROM (' . $subquery . ') AS doctrine_subquery_alias';
2007-05-16 23:20:55 +04:00
break;
}
$field = $this->getSqlTableAlias($rootAlias) . '.' . $idColumnName;
// only append the subquery if it actually contains something
2007-07-09 15:23:44 +04:00
if ($subquery !== '') {
array_unshift($this->_sqlParts['where'], $this->_conn->quoteIdentifier($field) . ' IN (' . $subquery . ')');
2007-05-16 23:20:55 +04:00
}
$modifyLimit = false;
}
}
$q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : '';
$q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
$q .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']): '';
$q .= ( ! empty($this->_sqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_sqlParts['orderby']) : '';
if ($modifyLimit) {
$q = $this->_conn->modifyLimitQuery($q, $this->_sqlParts['limit'], $this->_sqlParts['offset']);
2007-05-16 23:20:55 +04:00
}
// return to the previous state
2007-05-16 23:20:55 +04:00
if ( ! empty($string)) {
array_pop($this->_sqlParts['where']);
2007-05-16 23:20:55 +04:00
}
if ($needsSubQuery) {
array_shift($this->_sqlParts['where']);
2007-05-16 23:20:55 +04:00
}
2007-06-26 00:08:16 +04:00
$this->_sql = $q;
2007-05-24 20:13:50 +04:00
return $q;
}
/**
2007-05-16 23:20:55 +04:00
* getLimitSubquery
* 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
* @todo A little refactor to make the method easier to understand & maybe shorter?
*/
public function getLimitSubquery()
{
$map = reset($this->_queryComponents);
2007-05-16 23:20:55 +04:00
$table = $map['table'];
$componentAlias = key($this->_queryComponents);
// get short alias
2007-05-24 22:00:35 +04:00
$alias = $this->getTableAlias($componentAlias);
// what about composite keys?
$primaryKey = $alias . '.' . $table->getColumnName($table->getIdentifier());
// initialize the base of the subquery
2007-07-07 00:55:15 +04:00
$subquery = 'SELECT DISTINCT ' . $this->_conn->quoteIdentifier($primaryKey);
2007-06-27 21:41:02 +04:00
$driverName = $this->_conn->getAttribute(Doctrine::ATTR_DRIVER_NAME);
2007-06-28 02:40:17 +04:00
// pgsql needs the order by fields to be preserved in select clause
if ($driverName == 'pgsql') {
foreach ($this->_sqlParts['orderby'] as $part) {
2007-06-28 02:40:17 +04:00
$part = trim($part);
$e = $this->_tokenizer->bracketExplode($part, ' ');
2007-06-28 02:40:17 +04:00
$part = trim($e[0]);
2007-06-28 02:40:17 +04:00
if (strpos($part, '.') === false) {
continue;
2007-06-27 21:41:02 +04:00
}
2007-06-28 02:40:17 +04:00
// don't add functions
if (strpos($part, '(') !== false) {
continue;
}
2007-06-28 02:40:17 +04:00
// don't add primarykey column (its already in the select clause)
if ($part !== $primaryKey) {
2007-06-27 21:41:02 +04:00
$subquery .= ', ' . $part;
2007-04-26 21:42:03 +04:00
}
}
}
2007-06-28 02:40:17 +04:00
2007-06-27 22:42:47 +04:00
if ($driverName == 'mysql' || $driverName == 'pgsql') {
foreach ($this->_expressionMap as $dqlAlias => $expr) {
if (isset($expr[1])) {
$subquery .= ', ' . $expr[0] . ' AS ' . $this->_aggregateAliasMap[$dqlAlias];
2007-06-27 22:42:47 +04:00
}
}
}
2007-05-16 23:20:55 +04:00
$subquery .= ' FROM';
foreach ($this->_sqlParts['from'] as $part) {
2007-05-16 23:20:55 +04:00
// 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);
if (empty($this->_sqlParts['orderby']) && empty($this->_sqlParts['where'])) {
2007-06-01 14:17:50 +04:00
continue;
}
}
2007-05-16 23:20:55 +04:00
$subquery .= ' ' . $part;
}
// all conditions must be preserved in subquery
$subquery .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : '';
$subquery .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
$subquery .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']) : '';
2007-06-18 00:22:39 +04:00
$subquery .= ( ! empty($this->_sqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_sqlParts['orderby']) : '';
// add driver specific limit clause
$subquery = $this->_conn->modifyLimitQuery($subquery, $this->_sqlParts['limit'], $this->_sqlParts['offset']);
$parts = $this->_tokenizer->quoteExplode($subquery, ' ', "'", "'");
2007-07-07 00:55:15 +04:00
2007-06-27 21:41:02 +04:00
foreach ($parts as $k => $part) {
2007-07-07 00:55:15 +04:00
if (strpos($part, ' ') !== false) {
continue;
}
2007-07-07 00:55:15 +04:00
$part = trim($part, "\"'`");
2007-06-28 02:40:17 +04:00
if ($this->hasSqlTableAlias($part)) {
$parts[$k] = $this->_conn->quoteIdentifier($this->generateNewSqlTableAlias($part));
2007-06-28 02:29:57 +04:00
continue;
}
2007-06-28 02:40:17 +04:00
if (strpos($part, '.') === false) {
2007-06-28 02:29:57 +04:00
continue;
}
2007-06-28 02:29:57 +04:00
preg_match_all("/[a-zA-Z0-9_]+\.[a-z0-9_]+/i", $part, $m);
foreach ($m[0] as $match) {
$e = explode('.', $match);
$e[0] = $this->generateNewSqlTableAlias($e[0]);
2007-06-28 02:29:57 +04:00
$parts[$k] = str_replace($match, implode('.', $e), $parts[$k]);
}
}
2007-06-28 02:29:57 +04:00
if ($driverName == 'mysql' || $driverName == 'pgsql') {
foreach ($parts as $k => $part) {
if (strpos($part, "'") !== false) {
continue;
}
if (strpos($part, '__') == false) {
continue;
}
preg_match_all("/[a-zA-Z0-9_]+\_\_[a-z0-9_]+/i", $part, $m);
2007-06-28 02:29:57 +04:00
foreach ($m[0] as $match) {
$e = explode('__', $match);
$e[0] = $this->generateNewTableAlias($e[0]);
2007-06-28 02:29:57 +04:00
$parts[$k] = str_replace($match, implode('__', $e), $parts[$k]);
}
}
}
$subquery = implode(' ', $parts);
return $subquery;
}
/**
* 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 parseDqlQuery($query, $clear = true)
{
2007-05-16 23:20:55 +04:00
if ($clear) {
$this->clear();
2007-05-16 23:20:55 +04:00
}
$query = trim($query);
$query = str_replace("\n", ' ', $query);
$query = str_replace("\r", ' ', $query);
2007-11-24 22:08:41 +03:00
$parts = $this->_tokenizer->tokenizeQuery($query);
foreach ($parts as $partName => $subParts) {
$subParts = trim($subParts);
$partName = strtolower($partName);
switch ($partName) {
2007-05-16 23:20:55 +04:00
case 'create':
$this->_type = self::CREATE;
break;
2007-05-16 23:20:55 +04:00
case 'insert':
$this->_type = self::INSERT;
break;
2007-05-16 23:20:55 +04:00
case 'delete':
$this->_type = self::DELETE;
break;
2007-05-16 23:20:55 +04:00
case 'select':
$this->_type = self::SELECT;
$this->_addDqlQueryPart($partName, $subParts);
break;
2007-05-16 23:20:55 +04:00
case 'update':
$this->_type = self::UPDATE;
$partName = 'from';
2007-05-16 23:20:55 +04:00
case 'from':
$this->_addDqlQueryPart($partName, $subParts);
break;
2007-05-16 23:20:55 +04:00
case 'set':
$this->_addDqlQueryPart($partName, $subParts, true);
break;
2007-05-16 23:20:55 +04:00
case 'group':
case 'order':
$partName .= 'by';
2007-05-16 23:20:55 +04:00
case 'where':
case 'having':
case 'limit':
case 'offset':
$this->_addDqlQueryPart($partName, $subParts);
break;
}
}
return $this;
}
/**
2007-11-24 22:08:41 +03:00
* @todo Describe & refactor... too long and nested.
*/
2007-10-30 22:19:53 +03:00
public function load($path, $loadFields = true)
{
if (isset($this->_queryComponents[$path])) {
return $this->_queryComponents[$path];
2007-10-30 22:19:53 +03:00
}
$e = $this->_tokenizer->quoteExplode($path, ' INDEXBY ');
2007-09-02 02:22:54 +04:00
$mapWith = null;
if (count($e) > 1) {
$mapWith = trim($e[1]);
2007-09-02 02:22:54 +04:00
$path = $e[0];
}
// parse custom join conditions
$e = explode(' ON ', $path);
$joinCondition = '';
2007-05-16 23:20:55 +04:00
if (count($e) > 1) {
$joinCondition = $e[1];
$overrideJoin = true;
$path = $e[0];
} else {
$e = explode(' WITH ', $path);
if (count($e) > 1) {
$joinCondition = $e[1];
$path = $e[0];
}
$overrideJoin = false;
}
$tmp = explode(' ', $path);
$componentAlias = $originalAlias = (count($tmp) > 1) ? end($tmp) : null;
$e = preg_split("/[.:]/", $tmp[0], -1);
2007-05-16 23:20:55 +04:00
$fullPath = $tmp[0];
$prevPath = '';
$fullLength = strlen($fullPath);
if (isset($this->_queryComponents[$e[0]])) {
$table = $this->_queryComponents[$e[0]]['table'];
$componentAlias = $e[0];
2007-05-16 23:20:55 +04:00
$prevPath = $parent = array_shift($e);
}
2007-05-16 23:20:55 +04:00
foreach ($e as $key => $name) {
// get length of the previous path
$length = strlen($prevPath);
2007-05-16 23:20:55 +04:00
// build the current component path
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
2007-05-16 23:20:55 +04:00
$delimeter = substr($fullPath, $length, 1);
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;
}
// if the current alias already exists, it's user error
if (isset($this->_queryComponents[$componentAlias])) {
throw new Doctrine_Query_Exception("Duplicate alias '$componentAlias' in query.");
2007-05-24 18:19:44 +04:00
}
2007-05-16 23:20:55 +04:00
if ( ! isset($table)) {
// process the root of the path
$table = $this->loadRoot($name, $componentAlias);
} else {
$join = ($delimeter == ':') ? 'INNER JOIN ' : 'LEFT JOIN ';
$relation = $table->getRelation($name);
2007-08-09 21:31:31 +04:00
$localTable = $table;
$table = $relation->getTable();
//echo "<br /><br />" . $table->getComponentName() . "------" . $relation->getForeignComponentName() . "<br /><br />";
$this->_queryComponents[$componentAlias] = array(
'table' => $table,
'mapper' => $this->_conn->getMapper($relation->getForeignComponentName()),
'parent' => $parent,
'relation' => $relation,
'map' => null
);
2007-05-16 23:20:55 +04:00
if ( ! $relation->isOneToOne()) {
$this->_needsSubquery = true;
2007-05-16 23:20:55 +04:00
}
2007-05-24 22:00:35 +04:00
$localAlias = $this->getTableAlias($parent, $table->getTableName());
$foreignAlias = $this->getTableAlias($componentAlias, $relation->getTable()->getTableName());
$localSql = $this->_conn->quoteIdentifier($table->getTableName())
. ' '
2007-07-07 00:55:15 +04:00
. $this->_conn->quoteIdentifier($localAlias);
$foreignSql = $this->_conn->quoteIdentifier($relation->getTable()->getTableName())
. ' '
2007-07-07 00:55:15 +04:00
. $this->_conn->quoteIdentifier($foreignAlias);
$map = $relation->getTable()->getOption('inheritanceMap');
2007-05-16 23:20:55 +04:00
if ( ! $loadFields || ! empty($map) || $joinCondition) {
$this->_subqueryAliases[] = $foreignAlias;
2007-05-16 23:20:55 +04:00
}
2007-05-16 23:20:55 +04:00
if ($relation instanceof Doctrine_Relation_Association) {
2007-08-09 21:25:07 +04:00
$asf = $relation->getAssociationTable();
2007-05-16 23:20:55 +04:00
$assocTableName = $asf->getTableName();
if ( ! $loadFields || ! empty($map) || $joinCondition) {
$this->_subqueryAliases[] = $assocTableName;
}
2007-05-16 23:20:55 +04:00
$assocPath = $prevPath . '.' . $asf->getComponentName();
//echo "<br /><br />" . $asf->getComponentName() . "---2---" . $relation->getForeignComponentName() . "<br /><br />";
$this->_queryComponents[$assocPath] = array(
'parent' => $prevPath,
'relation' => $relation,
'table' => $asf,
'mapper' => $this->_conn->getMapper($relation->getAssociationClassName())
);
2007-09-13 02:03:25 +04:00
2007-05-24 22:00:35 +04:00
$assocAlias = $this->getTableAlias($assocPath, $asf->getTableName());
$queryPart = $join . $assocTableName . ' ' . $assocAlias;
$queryPart .= ' ON ' . $localAlias
. '.'
2007-12-03 13:06:30 +03:00
. $localTable->getColumnName($localTable->getIdentifier()) // what about composite keys?
. ' = '
. $assocAlias . '.' . $relation->getLocal();
if ($relation->isEqual()) {
// equal nest relation needs additional condition
$queryPart .= ' OR ' . $localAlias
. '.'
2007-08-09 21:25:07 +04:00
. $table->getColumnName($table->getIdentifier())
. ' = '
. $assocAlias . '.' . $relation->getForeign();
}
$this->_sqlParts['from'][] = $queryPart;
$queryPart = $join . $foreignSql;
if ( ! $overrideJoin) {
$queryPart .= ' ON ';
if ($relation->isEqual()) {
$queryPart .= '(';
}
2007-12-03 13:06:30 +03:00
$relationTable = $relation->getTable();
$queryPart .= $this->_conn->quoteIdentifier($foreignAlias . '.' . $relationTable->getColumnName($relationTable->getIdentifier()))
. ' = '
. $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getForeign());
if ($relation->isEqual()) {
$queryPart .= ' OR '
2007-08-09 21:25:07 +04:00
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($table->getIdentifier()))
. ' = '
. $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getLocal())
. ') AND '
2007-12-03 13:06:30 +03:00
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($table->getIdentifier()))
. ' != '
2007-12-03 13:06:30 +03:00
. $this->_conn->quoteIdentifier($localAlias . '.' . $table->getColumnName($table->getIdentifier()));
}
}
2007-05-16 23:20:55 +04:00
} else {
$queryPart = $join . $foreignSql;
if ( ! $overrideJoin) {
$queryPart .= ' ON '
. $this->_conn->quoteIdentifier($localAlias . '.' . $relation->getLocal())
. ' = '
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getForeign());
}
}
$queryPart .= $this->_createCustomJoinSql($table->getComponentName(), $componentAlias);
2007-11-10 16:21:40 +03:00
$this->_sqlParts['from'][$componentAlias] = $queryPart;
if ( ! empty($joinCondition)) {
$this->_pendingJoinConditions[$componentAlias] = $joinCondition;
}
2007-05-16 23:20:55 +04:00
}
2008-01-11 16:53:30 +03:00
if ($loadFields) {
2007-05-16 23:20:55 +04:00
$restoreState = false;
// load fields if necessary
2007-09-13 01:42:42 +04:00
if ($loadFields && empty($this->_dqlParts['select'])) {
$this->_pendingFields[$componentAlias] = array('*');
2007-05-16 23:20:55 +04:00
}
}
2007-05-16 23:20:55 +04:00
$parent = $prevPath;
}
$table = $this->_queryComponents[$componentAlias]['table'];
$mapper = $this->_queryComponents[$componentAlias]['mapper'];
$indexBy = null;
2007-09-02 02:22:54 +04:00
if (isset($mapWith)) {
$e = explode('.', $mapWith);
if (isset($e[1])) {
$indexBy = $e[1];
2007-09-02 02:22:54 +04:00
}
} else if ($table->getBoundQueryPart('indexBy') !== null) {
$indexBy = $table->getBoundQueryPart('indexBy');
}
if ($indexBy !== null) {
if ( ! $table->hasField($indexBy)) {
throw new Doctrine_Query_Exception("Couldn't use key mapping. Column " . $indexBy . " does not exist.");
}
$this->_queryComponents[$componentAlias]['map'] = $indexBy;
2007-09-02 02:22:54 +04:00
}
return $this->_queryComponents[$componentAlias];
2007-07-09 15:23:44 +04:00
}
2007-05-16 23:20:55 +04:00
/**
* loadRoot
*
* @param string $name
* @param string $componentAlias
* @todo DESCRIBE ME!
2007-05-16 23:20:55 +04:00
*/
public function loadRoot($name, $componentAlias)
{
2007-06-19 02:30:15 +04:00
// get the connection for the component
2007-12-11 18:46:27 +03:00
$manager = Doctrine_Manager::getInstance();
if ($manager->hasConnectionForComponent($name)) {
$this->_conn = $manager->getConnectionForComponent($name);
}
2007-05-16 23:20:55 +04:00
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-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) {
2007-07-07 00:55:15 +04:00
$queryPart .= ' ' . $this->_conn->quoteIdentifier($tableAlias);
}
2007-11-10 16:21:40 +03:00
$this->_tableAliasMap[$tableAlias] = $componentAlias;
2007-11-10 16:21:40 +03:00
$queryPart .= $this->_createCustomJoinSql($name, $componentAlias);
$this->_sqlParts['from'][] = $queryPart;
//echo "<br /><br />" . $table->getComponentName() . "---3---" . $name . "<br /><br />";
$this->_queryComponents[$componentAlias] = array(
'table' => $table, 'mapper' => $this->_conn->getMapper($name), 'map' => null);
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-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
*/
public function count($params = array())
{
// triggers dql parsing/processing
$this->getQuery(); // this is ugly
// initialize temporary variables
$where = $this->_sqlParts['where'];
$having = $this->_sqlParts['having'];
$groupby = $this->_sqlParts['groupby'];
$map = reset($this->_queryComponents);
$componentAlias = key($this->_queryComponents);
$table = $map['table'];
// build the query base
$q = 'SELECT COUNT(DISTINCT ' . $this->getTableAlias($componentAlias)
2007-12-03 13:06:30 +03:00
. '.' . implode(',', $table->getIdentifierColumnNames())
. ') AS num_results';
foreach ($this->_sqlParts['select'] as $field) {
if (strpos($field, '(') !== false) {
$q .= ', ' . $field;
}
}
$q .= ' FROM ' . $this->_buildSqlFromPart();
// append discriminator column conditions (if any)
$string = $this->_createDiscriminatorConditionSql();
if ( ! empty($string)) {
$where[] = $string;
}
// append conditions
$q .= ( ! empty($where)) ? ' WHERE ' . implode(' AND ', $where) : '';
$q .= ( ! empty($groupby)) ? ' GROUP BY ' . implode(', ', $groupby) : '';
$q .= ( ! empty($having)) ? ' HAVING ' . implode(' AND ', $having): '';
if ( ! is_array($params)) {
$params = array($params);
}
// append parameters
$params = array_merge($this->_params['where'], $this->_params['having'], $params);
$params = $this->convertEnums($params);
$results = $this->getConnection()->fetchAll($q, $params);
if (count($results) > 1) {
$count = 0;
foreach ($results as $result) {
$count += $result['num_results'];
}
} else {
$count = isset($results[0]) ? $results[0]['num_results']:0;
}
return (int) $count;
}
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
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
2007-05-16 23:20:55 +04:00
* @see Doctrine::FETCH_* constants
* @return mixed
*/
public function query($query, $params = array(), $hydrationMode = null)
2007-05-16 23:20:55 +04:00
{
$this->parseDqlQuery($query);
return $this->execute($params, $hydrationMode);
2007-05-16 23:20:55 +04:00
}
/**
* Copies a Doctrine_Query object.
*
* @param Doctrine_Query Doctrine query instance.
* If ommited the instance itself will be used as source.
* @return Doctrine_Query Copy of the Doctrine_Query instance.
*/
public function copy(Doctrine_Query $query = null)
{
2007-08-30 01:36:36 +04:00
if ( ! $query) {
$query = $this;
}
$new = new Doctrine_Query();
$new->_dqlParts = $query->_dqlParts;
$new->_params = $query->_params;
$new->_hydrator = $query->_hydrator;
return $new;
}
/**
* Frees the resources used by the query object. It especially breaks a
* cyclic reference between the query object and it's parsers. This enables
* PHP's current GC to reclaim the memory.
* This method can therefore be used to reduce memory usage when creating a lot
* of query objects during a request.
2007-08-30 01:36:36 +04:00
*
* @return Doctrine_Query this object
*/
public function free()
2007-08-30 01:36:36 +04:00
{
$this->reset();
$this->_parsers = array();
$this->_dqlParts = array();
$this->_enumParams = array();
}
/**
* serialize
* this method is automatically called when this Doctrine_Hydrate is serialized
*
* @return array an array of serialized properties
*/
public function serialize()
{
$vars = get_object_vars($this);
}
/**
* unseralize
* this method is automatically called everytime a Doctrine_Hydrate object is unserialized
*
* @param string $serialized Doctrine_Record as serialized string
* @return void
*/
public function unserialize($serialized)
{
}
}