1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/lib/Doctrine/RawSql.php

300 lines
11 KiB
PHP
Raw Normal View History

2006-07-22 03:22:15 +04:00
<?php
2006-12-29 17:01:31 +03: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>.
*/
Doctrine::autoload('Doctrine_Query_Abstract');
/**
* Doctrine_RawSql
*
* @package Doctrine
* @subpackage RawSql
* @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>
*/
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
/**
* parseQueryPart
* parses given query part
2006-07-22 03:22:15 +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
*/
public function parseQueryPart($queryPartName, $queryPart, $append = false)
2006-12-29 17:40:47 +03:00
{
if ($queryPartName == 'select') {
$this->parseSelectFields($queryPart);
return $this;
}
if( ! isset($this->parts[$queryPartName])) {
$this->parts[$queryPartName] = array();
}
2006-07-22 03:22:15 +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;
}
/**
* Add select parts to fields
*
* @param $queryPart sting The name of the querypart
*/
private function parseSelectFields($queryPart){
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
$this->fields = $m[1];
$this->parts['select'] = array();
}
2006-07-22 03:22:15 +04:00
/**
* 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)
{
$this->parseSelectFields($query);
2006-07-22 03:22:15 +04:00
$this->clear();
$tokens = Doctrine_Tokenizer::sqlExplode($query,' ');
2006-07-22 03:22:15 +04: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':
$type = $partLowerCase;
if ( ! isset($parts[$partLowerCase])) {
$parts[$partLowerCase] = array();
}
break;
2007-05-19 21:29:43 +04:00
case 'order':
case 'group':
$i = $key + 1;
if (isset($tokens[$i]) && strtolower($tokens[$i]) === 'by') {
$type = $partLowerCase . 'by';
$parts[$type] = array();
} else {
//not a keyword so we add it to the previous type
$parts[$type][] = $part;
}
break;
2007-05-19 21:29:43 +04:00
case 'by':
continue;
default:
//not a keyword so we add it to the previous type.
if ( ! isset($parts[$type][0])) {
$parts[$type][0] = $part;
} else {
// 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-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
}
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
*
2007-05-19 21:49:16 +04:00
* @return string the built sql query
2006-07-22 03:22:15 +04:00
*/
2007-11-19 20:55:23 +03:00
public function getQuery($params = array())
2006-12-29 17:40:47 +03: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-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]));
} 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-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;
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 03:22:15 +04:00
// force-add all primary key fields
2007-05-27 22:56:04 +04:00
foreach ($this->getTableAliases() as $tableAlias => $componentAlias) {
2007-05-19 21:49:16 +04:00
$map = $this->_aliasMap[$componentAlias];
2007-09-21 00:21:08 +04:00
foreach ((array) $map['table']->getIdentifier() as $key) {
2007-05-19 21:49:16 +04:00
$field = $tableAlias . '.' . $key;
2007-05-19 21:29:43 +04:00
if ( ! isset($this->parts['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
reset($this->_aliasMap);
$componentAlias = key($this->_aliasMap);
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 03:22:15 +04:00
$string = $this->applyInheritance();
2006-12-29 17:01:31 +03:00
if ( ! empty($string)) {
$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']);
$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
}
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;
}
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-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
}
}