Skip to content
Permalink
Browse files

Add identifier quoting for fields with spaces.

There is one caveat in that fields containing `AS` will still be
misquoted.

Refs #12038
  • Loading branch information...
markstory committed May 4, 2018
1 parent 0fb1cc7 commit c51f6d11e31131ab47002a9d13f8e42b4a95d0f8
Showing with 23 additions and 4 deletions.
  1. +11 −4 src/Database/SqlDialectTrait.php
  2. +12 −0 tests/TestCase/Database/ConnectionTest.php
@@ -46,28 +46,35 @@ public function quoteIdentifier($identifier)
return $this->_startQuote . $identifier . $this->_endQuote;
}
// string.string
if (preg_match('/^[\w-]+\.[^ \*]*$/', $identifier)) {
// string.string
$items = explode('.', $identifier);
return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote;
}
// string.*
if (preg_match('/^[\w-]+\.\*$/', $identifier)) {
// string.*
return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier);
}
if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) {
// Functions
// Functions
return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
}
// Alias.field AS thing
if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) {
if (preg_match('/^([\w-]+(\.[\w-\s]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) {
return $this->quoteIdentifier($matches[1]) . ' AS ' . $this->quoteIdentifier($matches[3]);
}
// string.string with spaces
if (preg_match('/^[\w-_]+\.[\w-_\s]+[\w_]*/', $identifier)) {
$items = explode('.', $identifier);
return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote;
}
if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) {
return $this->_startQuote . $identifier . $this->_endQuote;
}
@@ -765,6 +765,18 @@ public function testQuoteIdentifier()
$expected = '"Model".*';
$this->assertEquals($expected, $result);
$result = $connection->quoteIdentifier('Items.No_ 2');
$expected = '"Items"."No_ 2"';
$this->assertEquals($expected, $result);
$result = $connection->quoteIdentifier('Items.No_ 2 thing');
$expected = '"Items"."No_ 2 thing"';
$this->assertEquals($expected, $result);
$result = $connection->quoteIdentifier('Items.No_ 2 thing AS thing');
$expected = '"Items"."No_ 2 thing" AS "thing"';
$this->assertEquals($expected, $result);
$result = $connection->quoteIdentifier('MTD()');
$expected = 'MTD()';
$this->assertEquals($expected, $result);

0 comments on commit c51f6d1

Please sign in to comment.
You can’t perform that action at this time.