Skip to content

Commit

Permalink
Adding test for SortColumn to SQLQueryTest
Browse files Browse the repository at this point in the history
Mostly for the benefit of MSSQLDatabase which is having problems
with subselects and alias functions.
  • Loading branch information
Sean Harvey committed Dec 20, 2012
1 parent 9b3aebd commit aa3b358
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions model/SQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ public function addOrderBy($clauses = null, $direction = null) {
if($this->orderby) {
$i = 0;
foreach($this->orderby as $clause => $dir) {

// public function calls and multi-word columns like "CASE WHEN ..."
if(strpos($clause, '(') !== false || strpos($clause, " ") !== false ) {
// remove the old orderby
Expand Down
26 changes: 26 additions & 0 deletions tests/model/SQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,38 @@ public function testSelectLast() {
}
}

/**
* Test that "_SortColumn0" is added for an aggregate in the ORDER BY
* clause, in combination with a LIMIT and GROUP BY clause.
* For some databases, like MSSQL, this is a complicated scenario
* because a subselect needs to be done to query paginated data.
*/
public function testOrderByContainingAggregateAndLimitOffset() {
$query = new SQLQuery();
$query->setSelect(array('"Name"', '"Meta"'));
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy(array('MAX(Date)'));
$query->setGroupBy(array('"Name"', '"Meta"'));
$query->setLimit('1', '1');

$records = array();
foreach($query->execute() as $record) {
$records[] = $record;
}

$this->assertCount(1, $records);

$this->assertEquals('Object 2', $records[0]['Name']);
$this->assertEquals('2012-05-01 09:00:00', $records['0']['_SortColumn0']);
}

}

class SQLQueryTest_DO extends DataObject implements TestOnly {
static $db = array(
"Name" => "Varchar",
"Meta" => "Varchar",
"Date" => "SS_Datetime"
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/model/SQLQueryTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ SQLQueryTest_DO:
test1:
Name: 'Object 1'
Meta: 'Details 1'
Date: 2012-01-01 10:00:00
test2:
Name: 'Object 2'
Meta: 'Details 2'
Meta: 'Details 2'
Date: 2012-05-01 09:00:00

0 comments on commit aa3b358

Please sign in to comment.