Commit 60276421 authored by zYne's avatar zYne

--no commit message

--no commit message
parent ce4ea888
...@@ -79,15 +79,175 @@ class Doctrine_Query_From extends Doctrine_Query_Part ...@@ -79,15 +79,175 @@ class Doctrine_Query_From extends Doctrine_Query_Part
$reference = array_shift($e) . $operator . implode('.', $e); $reference = array_shift($e) . $operator . implode('.', $e);
} }
$table = $this->query->load($reference); $table = $this->query->load($reference);
} }
$operator = ($last == 'INNER') ? ':' : '.'; $operator = ($last == 'INNER') ? ':' : '.';
} }
} }
public function load($path, $loadFields = true)
{
// parse custom join conditions
$e = explode(' ON ', $path);
$joinCondition = '';
if (count($e) > 1) {
$joinCondition = ' AND ' . $e[1];
$path = $e[0];
}
$tmp = explode(' ', $path);
$componentAlias = (count($tmp) > 1) ? end($tmp) : false;
$e = preg_split("/[.:]/", $tmp[0], -1);
$fullPath = $tmp[0];
$prevPath = '';
$fullLength = strlen($fullPath);
if (isset($this->_aliasMap[$e[0]])) {
$table = $this->_aliasMap[$e[0]]['table'];
$prevPath = $parent = array_shift($e);
}
foreach ($e as $key => $name) {
// get length of the previous path
$length = strlen($prevPath);
// build the current component path
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
$delimeter = substr($fullPath, $length, 1);
// if an alias is not given use the current path as an alias identifier
if (strlen($prevPath) !== $fullLength || ! $componentAlias) {
$componentAlias = $prevPath;
}
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);
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
'parent' => $parent,
'relation' => $relation);
if( ! $relation->isOneToOne()) {
$this->needsSubquery = true;
}
$localAlias = $this->getShortAlias($parent, $table->getTableName());
$foreignAlias = $this->getShortAlias($componentAlias, $relation->getTable()->getTableName());
$localSql = $this->conn->quoteIdentifier($table->getTableName()) . ' ' . $localAlias;
$foreignSql = $this->conn->quoteIdentifier($relation->getTable()->getTableName()) . ' ' . $foreignAlias;
$map = $relation->getTable()->inheritanceMap;
if ( ! $loadFields || ! empty($map) || $joinCondition) {
$this->subqueryAliases[] = $foreignAlias;
}
if ($relation instanceof Doctrine_Relation_Association) {
$asf = $relation->getAssociationFactory();
$assocTableName = $asf->getTableName();
if( ! $loadFields || ! empty($map) || $joinCondition) {
$this->subqueryAliases[] = $assocTableName;
}
$assocPath = $prevPath . '.' . $asf->getComponentName();
$assocAlias = $this->getShortAlias($assocPath, $asf->getTableName());
$queryPart = $join . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
. $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getLocal();
if ($relation instanceof Doctrine_Relation_Association_Self) {
$queryPart .= ' OR ' . $localAlias . '.' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign();
}
public function __toString() $this->parts['from'][] = $queryPart;
$queryPart = $join . $foreignSql . ' ON ' . $foreignAlias . '.'
. $relation->getTable()->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign()
. $joinCondition;
if ($relation instanceof Doctrine_Relation_Association_Self) {
$queryPart .= ' OR ' . $foreignTable . '.' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getLocal();
}
} else {
$queryPart = $join . $foreignSql
. ' ON ' . $localAlias . '.'
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign()
. $joinCondition;
}
$this->parts['from'][] = $queryPart;
}
if ($loadFields) {
$restoreState = false;
// load fields if necessary
if ($loadFields && empty($this->pendingFields)) {
$this->pendingFields[$componentAlias] = array('*');
$restoreState = true;
}
if(isset($this->pendingFields[$componentAlias])) {
$this->processPendingFields($componentAlias);
}
if ($restoreState) {
$this->pendingFields = array();
}
if(isset($this->pendingAggregates[$componentAlias]) || isset($this->pendingAggregates[0])) {
$this->processPendingAggregates($componentAlias);
}
}
}
}
/**
* loadRoot
*
* @param string $name
* @param string $componentAlias
*/
public function loadRoot($name, $componentAlias)
{ {
return ( ! empty($this->parts))?implode(", ", $this->parts):''; // get the connection for the component
$this->conn = Doctrine_Manager::getInstance()
->getConnectionForComponent($name);
$table = $this->conn->getTable($name);
$tableName = $table->getTableName();
// get the short alias for this table
$tableAlias = $this->aliasHandler->getShortAlias($componentAlias, $tableName);
// quote table name
$queryPart = $this->conn->quoteIdentifier($tableName);
if ($this->type === self::SELECT) {
$queryPart .= ' ' . $tableAlias;
}
$this->parts['from'][] = $queryPart;
$this->tableAliases[$tableAlias] = $componentAlias;
$this->_aliasMap[$componentAlias] = array('table' => $table);
return $table;
} }
} }
...@@ -53,9 +53,4 @@ class Doctrine_Query_Groupby extends Doctrine_Query_Part ...@@ -53,9 +53,4 @@ class Doctrine_Query_Groupby extends Doctrine_Query_Part
} }
return implode(', ', $r); return implode(', ', $r);
} }
public function __toString()
{
return ( ! empty($this->parts))?implode(", ", $this->parts):'';
}
} }
...@@ -60,7 +60,7 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition ...@@ -60,7 +60,7 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition
} else { } else {
if ( ! is_numeric($func)) { if ( ! is_numeric($func)) {
$a = explode('.', $func); $a = explode('.', $func);
if (count($a) > 1) { if (count($a) > 1) {
$field = array_pop($a); $field = array_pop($a);
$reference = implode('.', $a); $reference = implode('.', $a);
...@@ -99,13 +99,4 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition ...@@ -99,13 +99,4 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition
return $r; return $r;
} }
/**
* __toString
*
* @return string
*/
public function __toString()
{
return ( ! empty($this->parts))?implode(' AND ', $this->parts):'';
}
} }
...@@ -74,8 +74,4 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part ...@@ -74,8 +74,4 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part
return implode(', ', $ret); return implode(', ', $ret);
} }
public function __toString()
{
return ( ! empty($this->parts))?implode(', ', $this->parts):'';
}
} }
This diff is collapsed.
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload("Doctrine_Access");
/** /**
* Doctrine_Query_Part * Doctrine_Query_Part
* *
...@@ -30,34 +30,19 @@ Doctrine::autoload("Doctrine_Access"); ...@@ -30,34 +30,19 @@ Doctrine::autoload("Doctrine_Access");
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
abstract class Doctrine_Query_Part extends Doctrine_Access abstract class Doctrine_Query_Part
{ {
/** /**
* @var Doctrine_Query $query the query object associated with this parser * @var Doctrine_Query $query the query object associated with this parser
*/ */
protected $query; protected $query;
/**
* @var string $name the name of this parser
*/
protected $name;
/**
* @var array $parts
*/
protected $parts = array();
/** /**
* @param Doctrine_Query $query the query object associated with this parser * @param Doctrine_Query $query the query object associated with this parser
*/ */
public function __construct(Doctrine_Query $query) public function __construct($query)
{ {
$this->query = $query; $this->query = $query;
} }
/**
* @return string $name the name of this parser
*/
public function getName()
{
return $this->name;
}
/** /**
* @return Doctrine_Query $query the query object associated with this parser * @return Doctrine_Query $query the query object associated with this parser
*/ */
...@@ -65,20 +50,12 @@ abstract class Doctrine_Query_Part extends Doctrine_Access ...@@ -65,20 +50,12 @@ abstract class Doctrine_Query_Part extends Doctrine_Access
{ {
return $this->query; return $this->query;
} }
/** public function parse($dql, $append = false)
* add
*
* @param string $value
* @return void
*/
public function add($value)
{ {
$method = "parse".$this->name; $e = explode(' ', __CLASS__);
$this->query->$method($value); $name = end($e);
}
public function get($name) $this->query->addDqlPart($name, $dql);
{ } $this->_parse($dql);
public function set($name, $value) }
{ }
} }
<?php
/*
* $Id: From.php 1080 2007-02-10 18:17:08Z romanb $
*
* 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_Part");
/**
* Doctrine_Query_Select
*
* @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: 1080 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Query_Select extends Doctrine_Query_Part
{
/**
* processPendingFields
* the fields in SELECT clause cannot be parsed until the components
* in FROM clause are parsed, hence this method is called everytime a
* specific component is being parsed.
*
* @throws Doctrine_Query_Exception if unknown component alias has been given
* @param string $componentAlias the alias of the component
* @return void
*/
public function processPendingFields($componentAlias)
{
$tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_aliasMap[$componentAlias]['table'];
if (isset($this->pendingFields[$componentAlias])) {
$fields = $this->pendingFields[$componentAlias];
// check for wildcards
if (in_array('*', $fields)) {
$fields = $table->getColumnNames();
} else {
// only auto-add the primary key fields if this query object is not
// a subquery of another query object
if ( ! $this->isSubquery) {
$fields = array_unique(array_merge($table->getPrimaryKeys(), $fields));
}
}
}
foreach ($fields as $name) {
$name = $table->getColumnName($name);
$this->parts['select'][] = $tableAlias . '.' .$name . ' AS ' . $tableAlias . '__' . $name;
}
$this->neededTables[] = $tableAlias;
}
/**
* parseSelect
* parses the query select part and
* adds selected fields to pendingFields array
*
* @param string $dql
*/
public function parseSelect($dql)
{
$refs = Doctrine_Query::bracketExplode($dql, ',');
foreach ($refs as $reference) {
if (strpos($reference, '(') !== false) {
if (substr($reference, 0, 1) === '(') {
// subselect found in SELECT part
$this->parseSubselect($reference);
} else {
$this->parseAggregateFunction2($reference);
}
} else {
$e = explode('.', $reference);
if (count($e) > 2) {
$this->pendingFields[] = $reference;
} else {
$this->pendingFields[$e[0]][] = $e[1];
}
}
}
}
/**
* parseSubselect
*
* parses the subquery found in DQL SELECT part and adds the
* parsed form into $pendingSubqueries stack
*
* @param string $reference
* @return void
*/
public function parseSubselect($reference)
{
$e = Doctrine_Query::bracketExplode($reference, ' ');
$alias = $e[1];
if (count($e) > 2) {
if (strtoupper($e[1]) !== 'AS') {
throw new Doctrine_Query_Exception('Syntax error near: ' . $reference);
}
$alias = $e[2];
}
$subquery = substr($e[0], 1, -1);
$this->pendingSubqueries[] = array($subquery, $alias);
}
public function parseAggregateFunction2($func)
{
$e = Doctrine_Query::bracketExplode($func, ' ');
$func = $e[0];
$pos = strpos($func, '(');
$name = substr($func, 0, $pos);
try {
$argStr = substr($func, ($pos + 1), -1);
$args = explode(',', $argStr);
$func = call_user_func_array(array($this->conn->expression, $name), $args);
if(substr($func, 0, 1) !== '(') {
$pos = strpos($func, '(');
$name = substr($func, 0, $pos);
} else {
$name = $func;
}
$e2 = explode(' ', $args[0]);
$distinct = '';
if(count($e2) > 1) {
if(strtoupper($e2[0]) == 'DISTINCT')
$distinct = 'DISTINCT ';
$args[0] = $e2[1];
}
$parts = explode('.', $args[0]);
$owner = $parts[0];
$alias = (isset($e[1])) ? $e[1] : $name;
$e3 = explode('.', $alias);
if(count($e3) > 1) {
$alias = $e3[1];
$owner = $e3[0];
}
// a function without parameters eg. RANDOM()
if ($owner === '') {
$owner = 0;
}
$this->pendingAggregates[$owner][] = array($name, $args, $distinct, $alias);
} catch(Doctrine_Expression_Exception $e) {
throw new Doctrine_Query_Exception('Unknown function ' . $func . '.');
}
}
public function processPendingSubqueries()
{
if ($this->subqueriesProcessed === true) {
return false;
}
foreach ($this->pendingSubqueries as $value) {
list($dql, $alias) = $value;
$sql = $this->createSubquery()->parseQuery($dql, false)->getQuery();
reset($this->tableAliases);
$tableAlias = current($this->tableAliases);
reset($this->compAliases);
$componentAlias = key($this->compAliases);
$sqlAlias = $tableAlias . '__' . count($this->aggregateMap);
$this->parts['select'][] = '(' . $sql . ') AS ' . $sqlAlias;
$this->aggregateMap[$alias] = $sqlAlias;
$this->subqueryAggregates[$componentAlias][] = $alias;
}
$this->subqueriesProcessed = true;
return true;
}
public function processPendingAggregates($componentAlias)
{
$tableAlias = $this->getTableAlias($componentAlias);
if ( ! isset($this->tables[$tableAlias])) {
throw new Doctrine_Query_Exception('Unknown component path ' . $componentAlias);
}
$root = current($this->tables);
$table = $this->tables[$tableAlias];
$aggregates = array();
if(isset($this->pendingAggregates[$componentAlias])) {
$aggregates = $this->pendingAggregates[$componentAlias];
}
if ($root === $table) {
if (isset($this->pendingAggregates[0])) {
$aggregates += $this->pendingAggregates[0];
}
}
foreach($aggregates as $parts) {
list($name, $args, $distinct, $alias) = $parts;
$arglist = array();
foreach($args as $arg) {
$e = explode('.', $arg);
if (is_numeric($arg)) {
$arglist[] = $arg;
} elseif (count($e) > 1) {
//$tableAlias = $this->getTableAlias($e[0]);
$table = $this->tables[$tableAlias];
$e[1] = $table->getColumnName($e[1]);
if( ! $table->hasColumn($e[1])) {
throw new Doctrine_Query_Exception('Unknown column ' . $e[1]);
}
$arglist[] = $tableAlias . '.' . $e[1];
} else {
$arglist[] = $e[0];
}
}
$sqlAlias = $tableAlias . '__' . count($this->aggregateMap);
if(substr($name, 0, 1) !== '(') {
$this->parts['select'][] = $name . '(' . $distinct . implode(', ', $arglist) . ') AS ' . $sqlAlias;
} else {
$this->parts['select'][] = $name . ' AS ' . $sqlAlias;
}
$this->aggregateMap[$alias] = $sqlAlias;
$this->neededTables[] = $tableAlias;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment