BUGFIX: Fix SQLQuery::queriedTables() and added test.

This commit is contained in:
Sam Minnee 2011-03-30 18:06:33 +13:00
parent 397d19f23b
commit 6915e58a39
2 changed files with 19 additions and 2 deletions

View File

@ -230,7 +230,7 @@ class SQLQuery {
public function queriedTables() {
$tables = array();
foreach($this->from as $key => $tableClause) {
if(is_array($tableClause)) $table = $tableClause['table'];
if(is_array($tableClause)) $table = '"'.$tableClause['table'].'"';
else if(is_string($tableClause) && preg_match('/JOIN +("[^"]+") +(AS|ON) +/i', $tableClause, $matches)) $table = $matches[1];
else $table = $tableClause;
@ -455,6 +455,9 @@ class SQLQuery {
* @return string
*/
function sql() {
// TODO: Don't require this internal-state manipulate-and-preserve - let sqlQueryToString() handle the new syntax
$origFrom = $this->from;
// Build from clauses
foreach($this->from as $alias => $join) {
// $join can be something like this array structure
@ -468,9 +471,11 @@ class SQLQuery {
}
}
$sql = DB::getConn()->sqlQueryToString($this);
if($this->replacementsOld) $sql = str_replace($this->replacementsOld, $this->replacementsNew, $sql);
$this->from = $origFrom;
return $sql;
}

View File

@ -230,6 +230,18 @@ class VersionedTest extends SapphireTest {
'Models w/o Versioned can have their own Version field.'
);
}
/**
* Test that SQLQuery::queriedTables() applies the version-suffixes properly.
*/
public function testQueriedTables() {
Versioned::reading_stage('Live');
$this->assertEquals(array(
'VersionedTest_DataObject_Live',
'VersionedTest_Subclass_Live',
), DataObject::get('VersionedTest_DataObject')->dataQuery()->query()->queriedTables());
}
}
class VersionedTest_DataObject extends DataObject implements TestOnly {