Skip to content

Commit

Permalink
Improve quoting for string conditions.
Browse files Browse the repository at this point in the history
Correctly quote string conditions that include bound parameters. This
helps improve handling of columns wth spaces.
  • Loading branch information
markstory committed May 19, 2018
1 parent 04e0468 commit 0b1fa8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Database/SqlDialectTrait.php
Expand Up @@ -54,8 +54,8 @@ public function quoteIdentifier($identifier)
return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier);
}

// Functions
if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) {
// Functions
return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
}

Expand All @@ -65,10 +65,11 @@ public function quoteIdentifier($identifier)
}

// string.string with spaces
if (preg_match('/^[\w-_]+\.[\w-_\s]+[\w_]*/', $identifier)) {
$items = explode('.', $identifier);
if (preg_match('/^([\w-]+\.[\w][\w\s\-]*[\w])(.*)/', $identifier, $matches)) {
$items = explode('.', $matches[1]);
$field = implode($this->_endQuote . '.' . $this->_startQuote, $items);

return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote;
return $this->_startQuote . $field . $this->_endQuote . $matches[2];
}

if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -777,6 +777,10 @@ public function testQuoteIdentifier()
$expected = '"Items"."No_ 2 thing" AS "thing"';
$this->assertEquals($expected, $result);

$result = $connection->quoteIdentifier('Items.Item Category Code = :c1');
$expected = '"Items"."Item Category Code" = :c1';
$this->assertEquals($expected, $result);

$result = $connection->quoteIdentifier('MTD()');
$expected = 'MTD()';
$this->assertEquals($expected, $result);
Expand Down

0 comments on commit 0b1fa8e

Please sign in to comment.