diff --git a/libraries/joomla/database/query/postgresql.php b/libraries/joomla/database/query/postgresql.php index e52998a6f9de6..23537ef080d82 100644 --- a/libraries/joomla/database/query/postgresql.php +++ b/libraries/joomla/database/query/postgresql.php @@ -123,23 +123,36 @@ public function __toString() if ($this->join) { - $onWord = ' ON '; + $tmpFrom = $this->from; + $tmpWhere = $this->where ? clone $this->where : null; + $this->from = null; // Workaround for special case of JOIN with UPDATE foreach ($this->join as $join) { $joinElem = $join->getElements(); - $joinArray = explode($onWord, $joinElem[0]); + $joinArray = preg_split('/\sON\s/i', $joinElem[0], 2); $this->from($joinArray[0]); - $this->where($joinArray[1]); + + if (isset($joinArray[1])) + { + $this->where($joinArray[1]); + } } $query .= (string) $this->from; - } - if ($this->where) + if ($this->where) + { + $query .= (string) $this->where; + } + + $this->from = $tmpFrom; + $this->where = $tmpWhere; + } + elseif ($this->where) { $query .= (string) $this->where; } diff --git a/tests/unit/suites/database/driver/postgresql/JDatabaseQueryPostgresqlTest.php b/tests/unit/suites/database/driver/postgresql/JDatabaseQueryPostgresqlTest.php index fd5e35640ef05..b6de5d4cb0014 100644 --- a/tests/unit/suites/database/driver/postgresql/JDatabaseQueryPostgresqlTest.php +++ b/tests/unit/suites/database/driver/postgresql/JDatabaseQueryPostgresqlTest.php @@ -190,19 +190,43 @@ public function test__toStringSelect() */ public function test__toStringUpdate() { - $q = new JDatabaseQueryPostgresql($this->dbo); + // Test on ugly query + $this->_instance + ->update('#__foo AS a') + ->join('INNER', "b\roN\nb.id = a.id") + ->set('a.hits = 0'); + + $string = (string) $this->_instance; + + $this->assertEquals( + PHP_EOL . "UPDATE #__foo AS a" . + PHP_EOL . "SET a.hits = 0" . + PHP_EOL . "FROM b" . + PHP_EOL . "WHERE b.id = a.id", + $string + ); - $q->update('#__foo AS a') + $this->_instance + ->clear() + ->update('#__foo AS a') ->join('INNER', 'b ON b.id = a.id') ->set('a.id = 2') ->where('b.id = 1'); + $string = (string) $this->_instance; + $this->assertEquals( PHP_EOL . "UPDATE #__foo AS a" . PHP_EOL . "SET a.id = 2" . PHP_EOL . "FROM b" . PHP_EOL . "WHERE b.id = 1 AND b.id = a.id", - (string) $q + $string + ); + + // Run method __toString() again on the same query + $this->assertEquals( + $string, + (string) $this->_instance ); }