BUGFIX: DataQuery overwriting _SortColumn selects (#8974)

* BUGFIX: DataQuery overwriting _SortColumn selects

* FIX DataQuery _SortColumn handling
This commit is contained in:
Aaron Carlino 2019-05-15 11:42:10 +12:00 committed by GitHub
parent 3a5c14f7c2
commit 3f1479edbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions

View File

@ -354,7 +354,6 @@ class DataQuery
{
if ($orderby = $query->getOrderBy()) {
$newOrderby = array();
$i = 0;
foreach ($orderby as $k => $dir) {
$newOrderby[$k] = $dir;
@ -372,7 +371,6 @@ class DataQuery
if (isset($originalSelect[$col])) {
$query->selectField($originalSelect[$col], $col);
}
continue;
}
@ -402,10 +400,15 @@ class DataQuery
if (!in_array($qualCol, $query->getSelect())) {
unset($newOrderby[$k]);
$newOrderby["\"_SortColumn$i\""] = $dir;
$query->selectField($qualCol, "_SortColumn$i");
// Find the first free "_SortColumnX" slot
// and assign it to $key
$i = 0;
while (isset($orderby[$key = "\"_SortColumn$i\""])) {
++$i;
}
$i++;
$newOrderby[$key] = $dir;
$query->selectField($qualCol, "_SortColumn$i");
}
}
}

View File

@ -296,6 +296,32 @@ class DataQueryTest extends SapphireTest
static::resetDBSchema(true);
}
public function testSurrogateFieldSort()
{
$query = new DataQuery(DataQueryTest\ObjectE::class);
$query->sort(
sprintf(
'(case when "Title" = %s then 1 else 0 end)',
DB::get_conn()->quoteString('Second')
),
'DESC',
true
);
$query->sort('SortOrder', 'ASC', false);
$query->sort(
sprintf(
'(case when "Title" = %s then 0 else 1 end)',
DB::get_conn()->quoteString('Fourth')
),
'DESC',
false
);
$this->assertEquals(
$query->execute()->column('Title'),
$query->column('Title')
);
}
public function testComparisonClauseDateStartsWith()
{
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");