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
|
|
|
|
*
|
2006-12-03 23:05:09 +03:00
|
|
|
* @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_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();
|
2006-07-22 03:22:15 +04:00
|
|
|
/**
|
2007-05-19 22:06:42 +04:00
|
|
|
* parseQueryPart
|
|
|
|
* parses given query part
|
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
|
|
|
|
* if false is given, this method will overwrite
|
|
|
|
* the given query part stack with $queryPart
|
|
|
|
* @return Doctrine_Query this object
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2007-05-19 22:06:42 +04:00
|
|
|
public function parseQueryPart($queryPartName, $queryPart, $append = false)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-05-19 22:06:42 +04:00
|
|
|
if ($queryPartName == 'select') {
|
|
|
|
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
|
|
|
$this->fields = $m[1];
|
2007-05-19 22:06:42 +04:00
|
|
|
$this->parts['select'] = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2007-05-19 22:06:42 +04:00
|
|
|
if ( ! $append) {
|
|
|
|
$this->parts[$queryPartName] = array($queryPart);
|
|
|
|
} else {
|
|
|
|
$this->parts[$queryPartName][] = $queryPart;
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* parseQuery
|
2007-05-19 21:49:16 +04:00
|
|
|
* parses an sql query and adds the parts to internal array
|
2006-07-22 03:22:15 +04:00
|
|
|
*
|
2007-05-19 21:49:16 +04:00
|
|
|
* @param string $query query to be parsed
|
|
|
|
* @return Doctrine_RawSql this object
|
2006-07-22 03:22:15 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function parseQuery($query)
|
|
|
|
{
|
2006-07-22 05:01:01 +04:00
|
|
|
preg_match_all('/{([^}{]*)}/U', $query, $m);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
|
|
|
$this->fields = $m[1];
|
|
|
|
$this->clear();
|
|
|
|
|
2007-05-19 21:29:43 +04:00
|
|
|
$e = Doctrine_Tokenizer::sqlExplode($query,' ');
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($e as $k => $part) {
|
2006-07-22 03:22:15 +04:00
|
|
|
$low = strtolower($part);
|
2006-12-29 17:01:31 +03:00
|
|
|
switch (strtolower($part)) {
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'select':
|
|
|
|
case 'from':
|
|
|
|
case 'where':
|
|
|
|
case 'limit':
|
|
|
|
case 'offset':
|
|
|
|
case 'having':
|
2006-07-22 03:22:15 +04:00
|
|
|
$p = $low;
|
2006-12-30 00:30:37 +03:00
|
|
|
if ( ! isset($parts[$low])) {
|
|
|
|
$parts[$low] = array();
|
|
|
|
}
|
|
|
|
break;
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'order':
|
|
|
|
case 'group':
|
2006-12-30 00:30:37 +03:00
|
|
|
$i = ($k + 1);
|
2007-05-19 21:29:43 +04:00
|
|
|
if (isset($e[$i]) && strtolower($e[$i]) === 'by') {
|
2006-12-30 00:30:37 +03:00
|
|
|
$p = $low;
|
2007-05-19 21:29:43 +04:00
|
|
|
$p .= 'by';
|
|
|
|
$parts[$low . 'by'] = array();
|
2006-12-30 00:46:14 +03:00
|
|
|
|
2006-12-30 00:30:37 +03:00
|
|
|
} else {
|
|
|
|
$parts[$p][] = $part;
|
|
|
|
}
|
|
|
|
break;
|
2007-05-19 21:29:43 +04:00
|
|
|
case 'by':
|
2006-12-30 00:30:37 +03:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
if ( ! isset($parts[$p][0])) {
|
|
|
|
$parts[$p][0] = $part;
|
|
|
|
} else {
|
|
|
|
$parts[$p][0] .= ' '.$part;
|
|
|
|
}
|
2007-05-19 22:06:42 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
|
|
|
|
$this->parts = $parts;
|
2007-05-19 21:29:43 +04:00
|
|
|
$this->parts['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
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getQuery
|
2007-05-19 21:49:16 +04:00
|
|
|
* builds the sql query from the given query parts
|
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
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getQuery()
|
|
|
|
{
|
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-05-24 22:30:18 +04:00
|
|
|
if ( ! $this->hasTableAlias($e[0])) {
|
2006-07-22 03:22:15 +04:00
|
|
|
try {
|
|
|
|
$this->addComponent($e[0], ucwords($e[0]));
|
2006-07-22 05:01:01 +04: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($e[1] == '*') {
|
2007-05-24 21:40:54 +04:00
|
|
|
$componentAlias = $this->getComponentAlias($e[0]);
|
2007-05-19 21:49:16 +04:00
|
|
|
|
|
|
|
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
|
2007-05-19 21:29:43 +04:00
|
|
|
$field = $e[0] . '.' . $name;
|
|
|
|
$this->parts['select'][$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];
|
|
|
|
$this->parts['select'][$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-05-24 21:40:54 +04:00
|
|
|
foreach ($this->getAliases() as $tableAlias => $componentAlias) {
|
2007-05-19 21:49:16 +04:00
|
|
|
$map = $this->_aliasMap[$componentAlias];
|
|
|
|
|
|
|
|
foreach ($map['table']->getPrimaryKeys() as $key) {
|
|
|
|
$field = $tableAlias . '.' . $key;
|
|
|
|
|
2007-05-19 21:29:43 +04:00
|
|
|
if ( ! isset($this->parts['select'][$field])) {
|
2007-05-19 21:49:16 +04:00
|
|
|
$this->parts['select'][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 21:05:25 +04:00
|
|
|
$q = 'SELECT ' . implode(', ', $this->parts['select']);
|
2006-07-22 05:01:01 +04:00
|
|
|
|
2006-07-22 03:22:15 +04:00
|
|
|
$string = $this->applyInheritance();
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($string)) {
|
2006-12-06 00:46:38 +03:00
|
|
|
$this->parts['where'][] = $string;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-07-22 03:22:15 +04:00
|
|
|
$copy = $this->parts;
|
|
|
|
unset($copy['select']);
|
|
|
|
|
2006-12-06 00:46:38 +03:00
|
|
|
$q .= ( ! empty($this->parts['from']))? ' FROM ' . implode(' ', $this->parts['from']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
|
2007-05-19 22:29:29 +04:00
|
|
|
$q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
|
2006-12-28 03:55:48 +03:00
|
|
|
$q .= ( ! empty($this->parts['limit']))? ' LIMIT ' . implode(' ', $this->parts['limit']) : '';
|
|
|
|
$q .= ( ! empty($this->parts['offset']))? ' OFFSET ' . implode(' ', $this->parts['offset']) : '';
|
2006-08-22 23:34:40 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($string)) {
|
2006-08-22 23:34:40 +04:00
|
|
|
array_pop($this->parts['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
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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-05-19 21:29:43 +04:00
|
|
|
if (isset($this->_aliasMap[$e[0]])) {
|
|
|
|
$table = $this->_aliasMap[$e[0]]['table'];
|
|
|
|
|
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);
|
|
|
|
$this->_aliasMap[$componentAlias] = array('table' => $table);
|
|
|
|
} else {
|
2007-05-19 21:49:16 +04:00
|
|
|
$relation = $table->getRelation($component);
|
2006-07-22 03:22:15 +04:00
|
|
|
|
2007-05-19 21:29:43 +04:00
|
|
|
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
|
|
|
|
'parent' => $parent,
|
|
|
|
'relation' => $relation);
|
2006-11-10 21:24:57 +03:00
|
|
|
}
|
2007-05-24 22:00:35 +04:00
|
|
|
$this->addTableAlias($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
|
|
|
}
|
|
|
|
|
|
|
|
}
|