Skip to content

Commit

Permalink
Fixing bug in auto quoting and adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 6, 2013
1 parent c036270 commit 842ea5b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cake/Database/SqlDialectTrait.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -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.
Expand Down

0 comments on commit 842ea5b

Please sign in to comment.