Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding test for SortColumn to SQLQueryTest

Mostly for the benefit of MSSQLDatabase which is having problems
with subselects and alias functions.
  • Loading branch information...
commit aa3b35846968f2b540f5df17e6b70acb86a8b16e 1 parent 9b3aebd
@halkyon halkyon authored
View
1  model/SQLQuery.php
@@ -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
View
26 tests/model/SQLQueryTest.php
@@ -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"
);
}
View
4 tests/model/SQLQueryTest.yml
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.