From 6915e58a39daa6792dd8aaf0210b4dcea03b483c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 30 Mar 2011 18:06:33 +1300 Subject: [PATCH] BUGFIX: Fix SQLQuery::queriedTables() and added test. --- model/SQLQuery.php | 9 +++++++-- tests/model/VersionedTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/model/SQLQuery.php b/model/SQLQuery.php index 0c63f8adc..b25832811 100755 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -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; } diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index 1b60aa824..d88e97ec8 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -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 {