Commit 6a7130be authored by zYne's avatar zYne

--no commit message

--no commit message
parent 4289bdd9
......@@ -477,10 +477,11 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$needsSubQuery = false;
$subquery = '';
$k = array_keys($this->_aliasMap);
$table = $this->_aliasMap[$k[0]]['table'];
$map = reset($this->_aliasMap);
$table = $map['table'];
$rootAlias = key($this->_aliasMap);
if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
if ( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
$needsSubQuery = true;
$this->limitSubqueryUsed = true;
}
......@@ -491,7 +492,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
// build the basic query
$str = '';
if($this->isDistinct()) {
if ($this->isDistinct()) {
$str = 'DISTINCT ';
}
......@@ -512,26 +513,26 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
$modifyLimit = true;
if ( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) {
if($needsSubQuery) {
if ($needsSubQuery) {
$subquery = $this->getLimitSubquery();
switch(strtolower($this->conn->getName())) {
switch (strtolower($this->conn->getName())) {
case 'mysql':
// mysql doesn't support LIMIT in subqueries
$list = $this->conn->execute($subquery, $params)->fetchAll(PDO::FETCH_COLUMN);
$subquery = implode(', ', $list);
break;
break;
case 'pgsql':
// pgsql needs special nested LIMIT subquery
$subquery = 'SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias';
break;
break;
}
$field = $this->aliasHandler->getShortAlias($table->getTableName()) . '.' . $table->getIdentifier();
$field = $this->aliasHandler->getShortAlias($rootAlias) . '.' . $table->getIdentifier();
// only append the subquery if it actually contains something
if($subquery !== '') {
if ($subquery !== '') {
array_unshift($this->parts['where'], $field. ' IN (' . $subquery . ')');
}
......@@ -569,11 +570,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
*/
public function getLimitSubquery()
{
$k = array_keys($this->tables);
$table = $this->tables[$k[0]];
$map = reset($this->_aliasMap);
$table = $map['table'];
$componentAlias = key($this->_aliasMap);
// get short alias
$alias = $this->aliasHandler->getShortAlias($table->getTableName());
$alias = $this->aliasHandler->getShortAlias($componentAlias);
$primaryKey = $alias . '.' . $table->getIdentifier();
// initialize the base of the subquery
......@@ -592,23 +594,20 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable
}
}
$subquery .= ' FROM ' . $this->conn->quoteIdentifier($table->getTableName()) . ' ' . $alias;
foreach ($this->parts['join'] as $parts) {
foreach ($parts as $part) {
// preserve LEFT JOINs only if needed
if (substr($part,0,9) === 'LEFT JOIN') {
$e = explode(' ', $part);
$subquery .= ' FROM';
if ( ! in_array($e[3], $this->subqueryAliases) &&
! in_array($e[2], $this->subqueryAliases)) {
continue;
}
}
foreach ($this->parts['from'] as $part) {
// preserve LEFT JOINs only if needed
if (substr($part,0,9) === 'LEFT JOIN') {
$e = explode(' ', $part);
$subquery .= ' ' . $part;
if ( ! in_array($e[3], $this->subqueryAliases) &&
! in_array($e[2], $this->subqueryAliases)) {
continue;
}
}
$subquery .= ' ' . $part;
}
// all conditions must be preserved in subquery
......
<?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_Query_IdentifierQuoting_TestCase
*
* This test case is used for testing DQL API quotes all identifiers properly
* if idenfitier quoting is turned on
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Query_IdentifierQuoting_TestCase extends Doctrine_UnitTestCase {
public function prepareTables() { }
public function prepareData() { }
public function testQuerySupportsIdentifierQuoting() {
$this->connection->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true);
$q = new Doctrine_Query();
$q = new Doctrine_Query2();
$q->parseQuery('SELECT MAX(u.id), MIN(u.name) FROM User u');
......@@ -13,17 +47,15 @@ class Doctrine_Query_IdentifierQuoting_TestCase extends Doctrine_UnitTestCase {
}
public function testQuerySupportsIdentifierQuotingWithJoins() {
$q = new Doctrine_Query();
$q = new Doctrine_Query2();
$q->parseQuery('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM "entity" e LEFT JOIN "phonenumber" p ON e.id = p.entity_id WHERE (e.type = 0)');
}
public function testLimitSubqueryAlgorithmSupportsIdentifierQuoting() {
$q = new Doctrine_Query();
$q = new Doctrine_Query2();
$q->parseQuery('SELECT u.name FROM User u INNER JOIN u.Phonenumber p')->limit(5);
......
This diff is collapsed.
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