From 2a2cb285a0d97e749addcac87869bb564a7bd266 Mon Sep 17 00:00:00 2001 From: zYne Date: Wed, 27 Jun 2007 18:42:47 +0000 Subject: [PATCH] --- lib/Doctrine/Query.php | 19 ++++++++++++++-- lib/Doctrine/Query/Having.php | 2 +- tests/Query/MysqlSubqueryTestCase.php | 31 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 52f22a15b..d57ae0a36 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -96,6 +96,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable */ protected $_pendingJoinConditions = array(); + protected $_expressionMap = array(); + protected $_state = Doctrine_Query::STATE_CLEAN; /** @@ -115,6 +117,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $this->pendingSubqueries = array(); $this->pendingFields = array(); $this->_neededTables = array(); + $this->_expressionMap = array(); $this->subqueryAliases = array(); $this->needsSubquery = false; $this->isLimitSubqueryUsed = false; @@ -228,6 +231,9 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable public function getAggregateAlias($dqlAlias) { if (isset($this->aggregateMap[$dqlAlias])) { + // mark the expression as used + $this->_expressionMap[$dqlAlias][1] = true; + return $this->aggregateMap[$dqlAlias]; } if ( ! empty($this->pendingAggregates)) { @@ -577,6 +583,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable $this->parts['select'][] = $expression . ' AS ' . $sqlAlias; $this->aggregateMap[$alias] = $sqlAlias; + $this->_expressionMap[$alias][0] = $expression; $this->_aliasMap[$componentAlias]['agg'][$index] = $alias; @@ -852,7 +859,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable - + /** foreach ($this->parts['orderby'] as $part) { $part = trim($part); $e = Doctrine_Tokenizer::bracketExplode($part, ' '); @@ -884,6 +891,14 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } } } + */ + if ($driverName == 'mysql' || $driverName == 'pgsql') { + foreach ($this->_expressionMap as $dqlAlias => $expr) { + if (isset($expr[1])) { + $subquery .= ', ' . $expr[0] . ' AS ' . $this->aggregateMap[$dqlAlias]; + } + } + } $subquery .= ' FROM'; @@ -928,7 +943,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable if (strpos($part, '.') !== false) { $separator = '.'; } - + if ($driverName == 'mysql' || $driverName == 'pgsql') { if (strpos($part, '__') !== false) { $separator = '__'; diff --git a/lib/Doctrine/Query/Having.php b/lib/Doctrine/Query/Having.php index edd92ebed..509c4e15d 100644 --- a/lib/Doctrine/Query/Having.php +++ b/lib/Doctrine/Query/Having.php @@ -69,7 +69,7 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition $func = $this->query->getTableAlias($reference) . '.' . $field; } else { $field = end($a); - $func = $this->query->getAggregateAlias($field); + $func = $this->query->getAggregateAlias($field); } return $func; } else { diff --git a/tests/Query/MysqlSubqueryTestCase.php b/tests/Query/MysqlSubqueryTestCase.php index 144642de8..a0e09aa01 100644 --- a/tests/Query/MysqlSubqueryTestCase.php +++ b/tests/Query/MysqlSubqueryTestCase.php @@ -83,4 +83,35 @@ class Doctrine_Query_MysqlSubquery_TestCase extends Doctrine_UnitTestCase $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, e2.name LIMIT 5'); } + public function testGetLimitSubquerySupportsOrderByAndHavingWithAggregateValues() + { + $q = new Doctrine_Query(); + $q->select('u.name, COUNT(DISTINCT a.id) num_albums'); + $q->from('User u, u.Album a'); + $q->orderby('num_albums DESC'); + $q->having('num_albums > 0'); + $q->groupby('u.id'); + $q->limit(5); + + $q->execute(); + + $this->dbh->pop(); + + $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 HAVING a2__0 > 0 ORDER BY a2__0 DESC LIMIT 5'); + } + public function testGetLimitSubquerySupportsHavingWithAggregateValues() + { + $q = new Doctrine_Query(); + $q->select('u.name, COUNT(DISTINCT a.id) num_albums'); + $q->from('User u, u.Album a'); + $q->having('num_albums > 0'); + $q->groupby('u.id'); + $q->limit(5); + + $q->execute(); + + $this->dbh->pop(); + + $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 HAVING a2__0 > 0 LIMIT 5'); + } }