diff --git a/Cake/Database/SqlDialectTrait.php b/Cake/Database/SqlDialectTrait.php index dfcfda4f1f8..beef1bdf899 100644 --- a/Cake/Database/SqlDialectTrait.php +++ b/Cake/Database/SqlDialectTrait.php @@ -184,7 +184,7 @@ protected function _transformDistinct($query) { * Iterates over each of the clauses in a query looking for identifiers and * quotes them * - * @param string $type the type of query to be quoted + * @param string $type the type of query to be quoted * @param Query $query The query to have its identifiers quoted * @return Query */ @@ -217,6 +217,11 @@ protected function _quoteQueryIdentifiers($type, $query) { foreach ((array)$query->clause('join') as $value) { $alias = empty($value['alias']) ? null : $this->quoteIdentifier($value['alias']); $value['alias'] = $alias; + + if (is_string($value['table'])) { + $value['table'] = $this->quoteIdentifier($value['table']); + } + $result[$alias] = $value; } $query->join($result, [], true); diff --git a/Cake/Test/TestCase/Database/QueryTest.php b/Cake/Test/TestCase/Database/QueryTest.php index 1cef5a8b1a3..75d0129b958 100644 --- a/Cake/Test/TestCase/Database/QueryTest.php +++ b/Cake/Test/TestCase/Database/QueryTest.php @@ -2072,6 +2072,37 @@ public function testQuotingFromAndAlias() { $this->assertRegExp('/FROM \(bar\) AS [`"]foo[`"]$/', $sql); } +/** + * Tests automatic identifier quoting for DISTINCT ON + * + * @return void + */ + public function testQuotingDistinctOn() { + $this->connection->driver()->autoQuoting(true); + $query = new Query($this->connection); + $sql = $query->select('*')->distinct(['something'])->sql(); + $this->assertRegExp('/[`"]something[`"]/', $sql); + } + +/** + * Tests automatic identifier quoting in the join clause + * + * @return void + */ + public function testQuotingJoinsAndAlias() { + $this->connection->driver()->autoQuoting(true); + $query = new Query($this->connection); + $sql = $query->select('*')->join(['something'])->sql(); + $this->assertRegExp('/JOIN [`"]something[`"]/', $sql); + + $query = new Query($this->connection); + $sql = $query->select('*')->join(['foo' => 'something'])->sql(); + $this->assertRegExp('/JOIN [`"]something[`"] [`"]foo[`"]/', $sql); + + $query = new Query($this->connection); + $sql = $query->select('*')->join(['foo' => $query->newExpr()->add('bar')])->sql(); + $this->assertRegExp('/JOIN \(bar\) [`"]foo[`"]/', $sql); + } /** * Assertion for comparing a table's contents with what is in it.