From bd776a680a497878787fa8d3754b69a4dc10fae3 Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 5 Nov 2006 20:08:34 +0000 Subject: [PATCH] DQL: support for DISTINCT keyword in aggregate functions, fixes #220 --- lib/Doctrine/Query.php | 16 +++++++++++++--- lib/Doctrine/Schema/Relation.php | 7 ++----- tests/QuerySelectTestCase.php | 8 ++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 48b2dae1e..633da7820 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -147,10 +147,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { case 'COUNT': case 'AVG': $reference = substr($func, ($pos + 1), -1); + $e2 = explode(' ', $reference); + + $distinct = ''; + if(count($e2) > 1) { + if(strtoupper($e2[0]) == 'DISTINCT') + $distinct = 'DISTINCT '; + + $reference = $e2[1]; + } $parts = explode('.', $reference); + $alias = (isset($e[1])) ? $e[1] : $name; - $this->pendingAggregates[$parts[0]][] = array($alias, $parts[1]); + $this->pendingAggregates[$parts[0]][] = array($alias, $parts[1], $distinct); break; default: throw new Doctrine_Query_Exception('Unknown aggregate function '.$name); @@ -167,9 +177,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $table = $this->components[$componentPath]; foreach($this->pendingAggregates[$componentAlias] as $args) { - list($name, $arg) = $args; + list($name, $arg, $distinct) = $args; - $this->parts["select"][] = $name . '(' . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap); + $this->parts["select"][] = $name . '(' . $distinct . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap); $this->aggregateMap[] = $table; } diff --git a/lib/Doctrine/Schema/Relation.php b/lib/Doctrine/Schema/Relation.php index 8be36972b..041e29681 100644 --- a/lib/Doctrine/Schema/Relation.php +++ b/lib/Doctrine/Schema/Relation.php @@ -104,17 +104,14 @@ class Doctrine_Schema_Relation extends Doctrine_Schema_Object { $this->referencedColumn = $referencedColumn; } /** - * - * @return - * @access public + * @return string */ public function __toString( ) { - return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'"; + return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'"; } /** * * @return bool - * @access public */ public function isValid( ) { diff --git a/tests/QuerySelectTestCase.php b/tests/QuerySelectTestCase.php index 849968a38..ee036e8b1 100644 --- a/tests/QuerySelectTestCase.php +++ b/tests/QuerySelectTestCase.php @@ -1,6 +1,13 @@ parseQuery('SELECT COUNT(DISTINCT u.name) FROM User u'); + + $this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)'); + } public function testAggregateFunction() { $q = new Doctrine_Query(); @@ -9,6 +16,7 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)'); } + public function testMultipleAggregateFunctions() { $q = new Doctrine_Query();