Browse Source

Improve quoting for string conditions.

Correctly quote string conditions that include bound parameters. This
helps improve handling of columns wth spaces.
Mark Story 7 years ago
parent
commit
0b1fa8e84e
2 changed files with 9 additions and 4 deletions
  1. 5 4
      src/Database/SqlDialectTrait.php
  2. 4 0
      tests/TestCase/Database/ConnectionTest.php

+ 5 - 4
src/Database/SqlDialectTrait.php

@@ -54,8 +54,8 @@ trait SqlDialectTrait
             return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier);
         }
 
+        // Functions
         if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) {
-            // Functions
             return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
         }
 
@@ -65,10 +65,11 @@ trait SqlDialectTrait
         }
 
         // 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)) {

+ 4 - 0
tests/TestCase/Database/ConnectionTest.php

@@ -777,6 +777,10 @@ class ConnectionTest extends TestCase
         $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);