Merge pull request #5579 from tractorcow/pulls/3.3/fix-finalised-query

BUG Fix getFinalisedQuery not including all queried columns
This commit is contained in:
Daniel Hensby 2016-05-26 01:32:59 +01:00
commit 4afdb630b5
2 changed files with 61 additions and 12 deletions

View File

@ -170,7 +170,9 @@ class DataQuery {
* @return SQLQuery The finalised sql query * @return SQLQuery The finalised sql query
*/ */
public function getFinalisedQuery($queriedColumns = null) { public function getFinalisedQuery($queriedColumns = null) {
if(!$queriedColumns) $queriedColumns = $this->queriedColumns; if(!$queriedColumns) {
$queriedColumns = $this->queriedColumns;
}
if($queriedColumns) { if($queriedColumns) {
$queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName')); $queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName'));
} }
@ -185,11 +187,19 @@ class DataQuery {
// Specifying certain columns allows joining of child tables // Specifying certain columns allows joining of child tables
$tableClasses = ClassInfo::dataClassesFor($this->dataClass); $tableClasses = ClassInfo::dataClassesFor($this->dataClass);
// Ensure that any filtered columns are included in the selected columns
foreach ($query->getWhereParameterised($parameters) as $where) { foreach ($query->getWhereParameterised($parameters) as $where) {
// Check for just the column, in the form '"Column" = ?' and the form '"Table"."Column"' = ? // Check for any columns in the form '"Column" = ?' or '"Table"."Column"' = ?
if (preg_match('/^"([^"]+)"/', $where, $matches) || if(preg_match_all(
preg_match('/^"([^"]+)"\."[^"]+"/', $where, $matches)) { '/(?:"(?<table>[^"]+)"\.)?"(?<column>[^"]+)"(?:[^\.]|$)/',
if (!in_array($matches[1], $queriedColumns)) $queriedColumns[] = $matches[1]; $where, $matches, PREG_SET_ORDER
)) {
foreach($matches as $match) {
$column = $match['column'];
if (!in_array($column, $queriedColumns)) {
$queriedColumns[] = $column;
}
}
} }
} }
} else { } else {

View File

@ -277,6 +277,45 @@ class DataQueryTest extends SapphireTest {
$this->resetDBSchema(true); $this->resetDBSchema(true);
} }
/**
* Tests that getFinalisedQuery can include all tables
*/
public function testConditionsIncludeTables() {
// Including filter on parent table only doesn't pull in second
$query = new DataQuery('DataQueryTest_C');
$query->sort('"SortOrder"');
$query->where(array(
'"DataQueryTest_C"."Title" = ?' => array('First')
));
$result = $query->getFinalisedQuery(array('Title'));
$from = $result->getFrom();
$this->assertContains('DataQueryTest_C', array_keys($from));
$this->assertNotContains('DataQueryTest_E', array_keys($from));
// Including filter on sub-table requires it
$query = new DataQuery('DataQueryTest_C');
$query->sort('"SortOrder"');
$query->where(array(
'"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => array(
'First', 2
)
));
$result = $query->getFinalisedQuery(array('Title'));
$from = $result->getFrom();
// Check that including "SortOrder" prompted inclusion of DataQueryTest_E table
$this->assertContains('DataQueryTest_C', array_keys($from));
$this->assertContains('DataQueryTest_E', array_keys($from));
$arrayResult = iterator_to_array($result->execute());
$first = array_shift($arrayResult);
$this->assertNotNull($first);
$this->assertEquals('First', $first['Title']);
$second = array_shift($arrayResult);
$this->assertNotNull($second);
$this->assertEquals('Last', $second['Title']);
$this->assertEmpty(array_shift($arrayResult));
}
} }