2006-07-22 03:22:15 +04:00
|
|
|
<?php
|
2006-12-29 17:01:31 +03:00
|
|
|
/*
|
2006-07-27 21:51:19 +04:00
|
|
|
* $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');
|
2006-07-27 21:51:19 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_RawSql
|
|
|
|
*
|
2007-11-24 21:11:09 +03:00
|
|
|
* Doctrine_RawSql is an implementation of Doctrine_Query_Abstract that skips the entire
|
|
|
|
* DQL parsing procedure. The "DQL" that is passed to a RawSql query object for execution
|
|
|
|
* is considered to be plain SQL and will be used "as is". The only query part that is special
|
|
|
|
* in a RawSql query is the SELECT part, which has a special syntax that provides Doctrine
|
|
|
|
* with the necessary information to properly hydrate the query results.
|
|
|
|
*
|
2006-12-03 23:05:09 +03:00
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage RawSql
|
2006-12-03 23:05:09 +03:00
|
|
|
* @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>
|
|
|
|
*/
|
2007-05-19 22:06:42 +04:00
|
|
|
class Doctrine_RawSql extends Doctrine_Query_Abstract
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
|
|
|
* @var array $fields
|
|
|
|
*/
|
2007-05-19 21:29:43 +04:00
|
|
|
private $fields = array();
|
2007-11-24 21:11:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public function parseQueryPart($queryPartName, $queryPart, $append = false)
|
|
|
|
{
|
|
|
|
return $this->parseDqlQueryPart($queryPartName, $queryPart, $append);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
2007-11-24 21:11:09 +03:00
|
|
|
* parseDqlQueryPart
|
|
|
|
* parses given DQL query part. Overrides Doctrine_Query_Abstract::parseDqlQueryPart().
|
|
|
|
* This implementation does no parsing at all, except of the SELECT portion of the query
|
|
|
|
* which is special in RawSql queries. The entire remaining parts are used "as is", so
|
|
|
|
* the user of the RawSql query is responsible for writing SQL that is portable between
|
|
|
|
* different DBMS.
|
2006-07-22 03:22:15 +04:00
|
|
|
*
|
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
|
2007-11-24 21:11:09 +03:00
|
|
|
* if false is given, this method will overwrite
|
2007-05-19 22:06:42 +04:00
|
|
|
* the given query part stack with $queryPart
|
|
|
|
* @return Doctrine_Query this object
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2007-11-24 21:11:09 +03:00
|
|
|
public function parseDqlQueryPart($queryPartName, $queryPart, $append = false)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-05-19 22:06:42 +04:00
|
|
|
if ($queryPartName == 'select') {
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->_parseSelectFields($queryPart);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
if ( ! isset($this->parts[$queryPartName])) {
|
|
|
|
$this->_sqlParts[$queryPartName] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $append) {
|
|
|
|
$this->_sqlParts[$queryPartName] = array($queryPart);
|
|
|
|
} else {
|
|
|
|
$this->_sqlParts[$queryPartName][] = $queryPart;
|
|
|
|
}
|
|
|
|
return $this;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
2007-11-24 21:11:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a DQL query part. Overrides Doctrine_Query_Abstract::_addDqlQueryPart().
|
|
|
|
* This implementation for RawSql parses the new parts right away, generating the SQL.
|
|
|
|
*/
|
|
|
|
protected function _addDqlQueryPart($queryPartName, $queryPart, $append = false)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart($queryPartName, $queryPart, $append);
|
|
|
|
}
|
|
|
|
|
2007-10-29 22:50:16 +03:00
|
|
|
/**
|
2007-11-24 21:11:09 +03:00
|
|
|
* Add select parts to fields.
|
2007-10-29 22:50:16 +03:00
|
|
|
*
|
|
|
|
* @param $queryPart sting The name of the querypart
|
|
|
|
*/
|
2007-11-24 21:11:09 +03:00
|
|
|
private function _parseSelectFields($queryPart){
|
2007-10-29 22:50:16 +03:00
|
|
|
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
|
|
|
|
$this->fields = $m[1];
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->_sqlParts['select'] = array();
|
2007-10-29 22:50:16 +03:00
|
|
|
}
|
2007-11-24 21:11:09 +03:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
2007-11-24 21:11:09 +03:00
|
|
|
* parseDqlQuery
|
|
|
|
* parses an sql query and adds the parts to internal array.
|
|
|
|
* Overrides Doctrine_Query_Abstract::parseDqlQuery().
|
|
|
|
* This implementation simply tokenizes the provided query string and uses them
|
|
|
|
* as SQL parts right away.
|
2006-07-22 03:22:15 +04:00
|
|
|
*
|
2007-11-24 21:11:09 +03:00
|
|
|
* @param string $query query to be parsed
|
|
|
|
* @return Doctrine_RawSql this object
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2007-11-24 21:11:09 +03:00
|
|
|
public function parseDqlQuery($query)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->_parseSelectFields($query);
|
2006-07-22 03:22:15 +04:00
|
|
|
$this->clear();
|
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
$tokens = $this->_tokenizer->sqlExplode($query, ' ');
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-10-29 22:50:16 +03:00
|
|
|
$parts = array();
|
|
|
|
foreach ($tokens as $key => $part) {
|
|
|
|
$partLowerCase = strtolower($part);
|
|
|
|
switch ($partLowerCase) {
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'select':
|
|
|
|
case 'from':
|
|
|
|
case 'where':
|
|
|
|
case 'limit':
|
|
|
|
case 'offset':
|
|
|
|
case 'having':
|
2007-10-29 22:50:16 +03:00
|
|
|
$type = $partLowerCase;
|
|
|
|
if ( ! isset($parts[$partLowerCase])) {
|
|
|
|
$parts[$partLowerCase] = array();
|
2006-12-30 00:30:37 +03:00
|
|
|
}
|
|
|
|
break;
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'order':
|
|
|
|
case 'group':
|
2007-10-29 22:50:16 +03:00
|
|
|
$i = $key + 1;
|
|
|
|
if (isset($tokens[$i]) && strtolower($tokens[$i]) === 'by') {
|
|
|
|
$type = $partLowerCase . 'by';
|
|
|
|
$parts[$type] = array();
|
2006-12-30 00:30:37 +03:00
|
|
|
} else {
|
2007-10-29 22:50:16 +03:00
|
|
|
//not a keyword so we add it to the previous type
|
|
|
|
$parts[$type][] = $part;
|
2006-12-30 00:30:37 +03:00
|
|
|
}
|
|
|
|
break;
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'by':
|
2006-12-30 00:30:37 +03:00
|
|
|
continue;
|
|
|
|
default:
|
2007-10-29 22:50:16 +03:00
|
|
|
//not a keyword so we add it to the previous type.
|
|
|
|
if ( ! isset($parts[$type][0])) {
|
|
|
|
$parts[$type][0] = $part;
|
2006-12-30 00:30:37 +03:00
|
|
|
} else {
|
2007-10-29 22:50:16 +03:00
|
|
|
// why does this add to index 0 and not append to the
|
|
|
|
// array. If it had done that one could have used
|
|
|
|
// parseQueryPart.
|
|
|
|
$parts[$type][0] .= ' '.$part;
|
2006-12-30 00:30:37 +03:00
|
|
|
}
|
2007-05-19 22:06:42 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->_sqlParts = $parts;
|
|
|
|
$this->_sqlParts['select'] = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2006-07-22 03:44:07 +04:00
|
|
|
return $this;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
2007-11-24 21:11:09 +03:00
|
|
|
* getSqlQuery
|
|
|
|
* builds the sql query.
|
2006-07-22 05:01:01 +04:00
|
|
|
*
|
2007-05-19 21:49:16 +04:00
|
|
|
* @return string the built sql query
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2007-11-24 21:11:09 +03:00
|
|
|
public function getSqlQuery($params = array())
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$select = array();
|
2007-06-26 14:05:26 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($this->fields as $field) {
|
2007-05-19 21:05:25 +04:00
|
|
|
$e = explode('.', $field);
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($e[1])) {
|
2007-05-19 21:05:25 +04:00
|
|
|
throw new Doctrine_RawSql_Exception('All selected fields in Sql query must be in format tableAlias.fieldName');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-05-19 21:29:43 +04:00
|
|
|
// try to auto-add component
|
2007-11-24 21:11:09 +03:00
|
|
|
if ( ! $this->hasSqlTableAlias($e[0])) {
|
2006-07-22 03:22:15 +04:00
|
|
|
try {
|
|
|
|
$this->addComponent($e[0], ucwords($e[0]));
|
2007-11-24 21:11:09 +03:00
|
|
|
} catch (Doctrine_Exception $exception) {
|
2007-05-19 21:05:25 +04:00
|
|
|
throw new Doctrine_RawSql_Exception('The associated component for table alias ' . $e[0] . ' couldn\'t be found.');
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-26 14:05:26 +04:00
|
|
|
$componentAlias = $this->getComponentAlias($e[0]);
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($e[1] == '*') {
|
2007-11-24 21:11:09 +03:00
|
|
|
foreach ($this->_queryComponents[$componentAlias]['table']->getColumnNames() as $name) {
|
2007-05-19 21:29:43 +04:00
|
|
|
$field = $e[0] . '.' . $name;
|
2007-06-26 14:05:26 +04:00
|
|
|
|
|
|
|
$select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $name;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
|
|
|
} else {
|
2007-05-19 21:29:43 +04:00
|
|
|
$field = $e[0] . '.' . $e[1];
|
2007-06-26 14:05:26 +04:00
|
|
|
$select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $e[1];
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-22 05:01:01 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
// force-add all primary key fields
|
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
foreach ($this->getTableAliasMap() as $tableAlias => $componentAlias) {
|
|
|
|
$map = $this->_queryComponents[$componentAlias];
|
2007-05-19 21:49:16 +04:00
|
|
|
|
2007-12-02 15:56:15 +03:00
|
|
|
foreach ((array) $map['table']->getIdentifierColumnNames() as $key) {
|
2007-05-19 21:49:16 +04:00
|
|
|
$field = $tableAlias . '.' . $key;
|
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
if ( ! isset($this->_sqlParts['select'][$field])) {
|
2007-06-26 14:05:26 +04:00
|
|
|
$select[$componentAlias][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-26 14:05:26 +04:00
|
|
|
|
|
|
|
// first add the fields of the root component
|
2007-11-24 21:11:09 +03:00
|
|
|
reset($this->_queryComponents);
|
|
|
|
$componentAlias = key($this->_queryComponents);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-06-26 14:05:26 +04:00
|
|
|
$q = 'SELECT ' . implode(', ', $select[$componentAlias]);
|
|
|
|
unset($select[$componentAlias]);
|
|
|
|
|
|
|
|
foreach ($select as $component => $fields) {
|
|
|
|
if ( ! empty($fields)) {
|
|
|
|
$q .= ', ' . implode(', ', $fields);
|
|
|
|
}
|
|
|
|
}
|
2006-07-22 05:01:01 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$string = $this->_createDiscriminatorSql();
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($string)) {
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->_sqlParts['where'][] = $string;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-11-24 21:11:09 +03:00
|
|
|
$copy = $this->_sqlParts;
|
2006-07-22 03:22:15 +04:00
|
|
|
unset($copy['select']);
|
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
$q .= ( ! empty($this->_sqlParts['from']))? ' FROM ' . implode(' ', $this->_sqlParts['from']) : '';
|
|
|
|
$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']) : '';
|
|
|
|
$q .= ( ! empty($this->_sqlParts['limit']))? ' LIMIT ' . implode(' ', $this->_sqlParts['limit']) : '';
|
|
|
|
$q .= ( ! empty($this->_sqlParts['offset']))? ' OFFSET ' . implode(' ', $this->_sqlParts['offset']) : '';
|
2006-08-22 23:34:40 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($string)) {
|
2007-11-24 21:11:09 +03:00
|
|
|
array_pop($this->_sqlParts['where']);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-08-22 23:34:40 +04:00
|
|
|
return $q;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
|
|
|
* getFields
|
2007-05-19 21:49:16 +04:00
|
|
|
* returns the fields associated with this parser
|
2006-07-22 03:22:15 +04:00
|
|
|
*
|
2007-05-19 21:49:16 +04:00
|
|
|
* @return array all the fields associated with this parser
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getFields()
|
|
|
|
{
|
2006-07-22 03:22:15 +04:00
|
|
|
return $this->fields;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
|
|
|
* addComponent
|
|
|
|
*
|
|
|
|
* @param string $tableAlias
|
|
|
|
* @param string $componentName
|
2006-07-22 03:44:07 +04:00
|
|
|
* @return Doctrine_RawSql
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2007-05-19 21:29:43 +04:00
|
|
|
public function addComponent($tableAlias, $path)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-05-19 21:29:43 +04:00
|
|
|
$tmp = explode(' ', $path);
|
|
|
|
$originalAlias = (count($tmp) > 1) ? end($tmp) : null;
|
|
|
|
|
|
|
|
$e = explode('.', $tmp[0]);
|
|
|
|
|
|
|
|
$fullPath = $tmp[0];
|
|
|
|
$fullLength = strlen($fullPath);
|
2006-07-22 05:01:01 +04:00
|
|
|
|
2006-11-10 21:24:57 +03:00
|
|
|
$table = null;
|
2007-05-19 21:49:16 +04:00
|
|
|
|
|
|
|
$currPath = '';
|
|
|
|
|
2007-11-24 21:11:09 +03:00
|
|
|
if (isset($this->_queryComponents[$e[0]])) {
|
|
|
|
$table = $this->_queryComponents[$e[0]]['table'];
|
2007-05-19 21:29:43 +04:00
|
|
|
|
2007-05-19 21:49:16 +04:00
|
|
|
$currPath = $parent = array_shift($e);
|
2007-05-19 21:29:43 +04:00
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($e as $k => $component) {
|
2007-05-19 21:29:43 +04:00
|
|
|
// get length of the previous path
|
|
|
|
$length = strlen($currPath);
|
|
|
|
|
|
|
|
// build the current component path
|
2007-05-19 21:49:16 +04:00
|
|
|
$currPath = ($currPath) ? $currPath . '.' . $component : $component;
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-05-19 21:29:43 +04:00
|
|
|
$delimeter = substr($fullPath, $length, 1);
|
|
|
|
|
|
|
|
// if an alias is not given use the current path as an alias identifier
|
|
|
|
if (strlen($currPath) === $fullLength && isset($originalAlias)) {
|
|
|
|
$componentAlias = $originalAlias;
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2007-05-19 21:29:43 +04:00
|
|
|
$componentAlias = $currPath;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-05-19 21:29:43 +04:00
|
|
|
if ( ! isset($table)) {
|
|
|
|
$conn = Doctrine_Manager::getInstance()
|
2007-05-19 21:49:16 +04:00
|
|
|
->getConnectionForComponent($component);
|
2007-05-19 21:29:43 +04:00
|
|
|
|
|
|
|
$table = $conn->getTable($component);
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_queryComponents[$componentAlias] = array(
|
|
|
|
'table' => $table, 'mapper' => $conn->getMapper($component));
|
2007-05-19 21:29:43 +04:00
|
|
|
} else {
|
2007-05-19 21:49:16 +04:00
|
|
|
$relation = $table->getRelation($component);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_queryComponents[$componentAlias] = array(
|
|
|
|
'table' => $relation->getTable(),
|
|
|
|
'mapper' => $this->_conn->getMapper($component),
|
|
|
|
'parent' => $parent,
|
|
|
|
'relation' => $relation);
|
2006-11-10 21:24:57 +03:00
|
|
|
}
|
2007-11-24 21:11:09 +03:00
|
|
|
$this->addSqlTableAlias($tableAlias, $componentAlias);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-05-19 21:49:16 +04:00
|
|
|
$parent = $currPath;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2006-07-22 03:44:07 +04:00
|
|
|
return $this;
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
2007-10-29 22:50:16 +03:00
|
|
|
}
|