Merge pull request #2391 from halkyon/orderby_limit_aggregate

BUG Fixing SQLQuery::aggregate() adding ORDER BY when no limit.
This commit is contained in:
Ingo Schommer 2013-09-06 02:21:30 -07:00
commit 8864256601
2 changed files with 38 additions and 4 deletions

View File

@ -1066,10 +1066,20 @@ class SQLQuery {
* @param $alias An optional alias for the aggregate column.
*/
public function aggregate($column, $alias = null) {
$clone = clone $this;
$clone->setLimit($this->limit);
$clone->setOrderBy($this->orderby);
// don't set an ORDER BY clause if no limit has been set. It doesn't make
// sense to add an ORDER BY if there is no limit, and it will break
// queries to databases like MSSQL if you do so. Note that the reason
// this came up is because DataQuery::initialiseQuery() introduces
// a default sort.
if($this->limit) {
$clone->setLimit($this->limit);
$clone->setOrderBy($this->orderby);
} else {
$clone->setOrderBy(array());
}
$clone->setGroupBy($this->groupby);
if($alias) {
$clone->setSelect(array());
@ -1077,7 +1087,6 @@ class SQLQuery {
} else {
$clone->setSelect($column);
}
return $clone;
}

View File

@ -414,6 +414,31 @@ class SQLQueryTest extends SapphireTest {
$this->assertEquals(array(2), $result->column('cnt'));
}
/**
* Tests that an ORDER BY is only added if a LIMIT is set.
*/
public function testAggregateNoOrderByIfNoLimit() {
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('Common');
$query->setLimit(array());
$aggregate = $query->aggregate('MAX("ID")');
$limit = $aggregate->getLimit();
$this->assertEquals(array(), $aggregate->getOrderBy());
$this->assertEquals(array(), $limit);
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('Common');
$query->setLimit(2);
$aggregate = $query->aggregate('MAX("ID")');
$limit = $aggregate->getLimit();
$this->assertEquals(array('Common' => 'ASC'), $aggregate->getOrderBy());
$this->assertEquals(array('start' => 0, 'limit' => 2), $limit);
}
/**
* Test that "_SortColumn0" is added for an aggregate in the ORDER BY
* clause, in combination with a LIMIT and GROUP BY clause.