diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index da9d4027d..7f1080fdc 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -858,40 +858,29 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $driverName = $this->_conn->getAttribute(Doctrine::ATTR_DRIVER_NAME); - - /** - foreach ($this->parts['orderby'] as $part) { - $part = trim($part); - $e = Doctrine_Tokenizer::bracketExplode($part, ' '); - $part = trim($e[0]); - - $aggregate = false; - - foreach ($this->parts['select'] as $select) { - $e = explode(' AS ', trim($select)); - - if (count($e) > 1) { - if ($part === $e[1]) { - $part = $select; - $aggregate = true; - break; - } + // pgsql needs the order by fields to be preserved in select clause + if ($driverName == 'pgsql') { + foreach ($this->parts['orderby'] as $part) { + $part = trim($part); + $e = Doctrine_Tokenizer::bracketExplode($part, ' '); + $part = trim($e[0]); + + if (strpos($part, '.') === false) { + continue; } - } - - // don't add primarykey column (its already in the select clause) - if ($part !== $primaryKey) { - if ($driverName == 'mysql') { - if ($aggregate) { - $subquery .= ', ' . $part; - } - } elseif ($driverName == 'pgsql') { - // pgsql needs the order by fields to be preserved in select clause + + // don't add functions + if (strpos($part, '(') !== false) { + continue; + } + + // don't add primarykey column (its already in the select clause) + if ($part !== $primaryKey) { $subquery .= ', ' . $part; } } } - */ + if ($driverName == 'mysql' || $driverName == 'pgsql') { foreach ($this->_expressionMap as $dqlAlias => $expr) { if (isset($expr[1])) { @@ -933,13 +922,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable if (strpos($part, "'") !== false) { continue; } - + if ($this->hasTableAlias($part)) { $parts[$k] = $this->generateNewTableAlias($part); continue; } - - if (strpos($part, '.') == false) { + + if (strpos($part, '.') === false) { continue; } preg_match_all("/[a-zA-Z0-9_]+\.[a-z0-9_]+/i", $part, $m); @@ -971,29 +960,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } } } - /** - - $separator = false; - if (strpos($part, '.') !== false) { - $separator = '.'; - } - - if ($driverName == 'mysql' || $driverName == 'pgsql') { - if (strpos($part, '__') !== false) { - $separator = '__'; - } - } - - if ($separator) { - $e = explode($separator, $part); - - $trimmed = ltrim($e[0], '( '); - $pos = strpos($e[0], $trimmed); - - $e[0] = substr($e[0], 0, $pos) . $this->generateNewTableAlias($trimmed); - $parts[$k] = implode($separator, $e); - }*/ $subquery = implode(' ', $parts); return $subquery; diff --git a/tests/Query/PgsqlSubqueryTestCase.php b/tests/Query/PgsqlSubqueryTestCase.php index e9167b0d1..7da254bb5 100644 --- a/tests/Query/PgsqlSubqueryTestCase.php +++ b/tests/Query/PgsqlSubqueryTestCase.php @@ -37,7 +37,7 @@ class Doctrine_Query_PgsqlSubquery_TestCase extends Doctrine_UnitTestCase $this->dbh = new Doctrine_Adapter_Mock('pgsql'); $this->conn = Doctrine_Manager::getInstance()->openConnection($this->dbh); } - /** + public function testGetLimitSubqueryWithOrderByOnAggregateValues() { $q = new Doctrine_Query(); @@ -49,9 +49,11 @@ class Doctrine_Query_PgsqlSubquery_TestCase extends Doctrine_UnitTestCase $q->execute(); - $this->dbh->pop(); + $this->assertEqual($this->dbh->pop(), 'SELECT e.id AS e__id, e.name AS e__name, COUNT(DISTINCT a.id) AS a__0 FROM entity e LEFT JOIN album a ON e.id = a.user_id WHERE e.id IN ' + . '(SELECT doctrine_subquery_alias.id FROM ' + . '(SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 LIMIT 5) ' + . 'AS doctrine_subquery_alias) AND (e.type = 0) GROUP BY e.id ORDER BY a__0'); - $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 LIMIT 5'); } public function testGetLimitSubqueryWithOrderByOnAggregateValuesAndColumns() @@ -65,9 +67,7 @@ class Doctrine_Query_PgsqlSubquery_TestCase extends Doctrine_UnitTestCase $q->execute(); - $this->dbh->pop(); - - $this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0, e2.name FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0, e2.name LIMIT 5'); + $this->assertEqual($this->dbh->pop(), 'SELECT e.id AS e__id, e.name AS e__name, COUNT(DISTINCT a.id) AS a__0 FROM entity e LEFT JOIN album a ON e.id = a.user_id WHERE e.id IN (SELECT doctrine_subquery_alias.id FROM (SELECT DISTINCT e2.id, e2.name, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0, e2.name LIMIT 5) AS doctrine_subquery_alias) AND (e.type = 0) GROUP BY e.id ORDER BY a__0, e.name'); } - */ + }