1
0
mirror of synced 2024-12-13 14:56:01 +03:00

DQL: support for DISTINCT keyword in aggregate functions, fixes #220

This commit is contained in:
zYne 2006-11-05 20:08:34 +00:00
parent ba4c83ef3a
commit bd776a680a
3 changed files with 23 additions and 8 deletions

View File

@ -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;
}

View File

@ -104,9 +104,7 @@ 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."'";
@ -114,7 +112,6 @@ class Doctrine_Schema_Relation extends Doctrine_Schema_Object {
/**
*
* @return bool
* @access public
*/
public function isValid( ) {

View File

@ -1,6 +1,13 @@
<?php
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
public function prepareTables() { }
public function testAggregateFunctionWithDistinctKeyword() {
$q = new Doctrine_Query();
$q->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();