From 55324c0b2a1d68e46380683ff1fdc655210f2f37 Mon Sep 17 00:00:00 2001 From: Cory Thompson Date: Tue, 24 Jan 2017 20:54:07 +1100 Subject: [PATCH] Allow SQL json selects without defining table name --- lib/Cake/Model/Datasource/DboSource.php | 3 ++- .../Test/Case/Model/Datasource/DboSourceTest.php | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 2f5489778..089fe68dc 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -2958,10 +2958,11 @@ class DboSource extends DataSource { if (!empty($this->endQuote)) { $end = preg_quote($this->endQuote); } + // Remove quotes and requote all the Model.field names. $conditions = str_replace(array($start, $end), '', $conditions); $conditions = preg_replace_callback( - '/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_][a-z0-9\\-_]*\\.[a-z0-9_][a-z0-9_\\-]*[a-z0-9_])/i', + '/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_][a-z0-9\\-_]*\\.[a-z0-9_][a-z0-9_\\-]*[a-z0-9_])|([a-z0-9_][a-z0-9_\\-]*)(?=->)/i', array(&$this, '_quoteMatchedField'), $conditions ); diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 0ce8a937a..68e46ab0c 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -249,12 +249,20 @@ class DboSourceTest extends CakeTestCase { * @return void */ public function testColumnHyphenOperator() { + //PostgreSQL style $result = $this->testDb->conditions(array('Foo.bar->>\'fieldName\'' => 42)); $this->assertEquals(' WHERE `Foo`.`bar`->>\'fieldName\' = 42', $result, 'SQL JSON operator failed'); - $result = $this->testDb->conditions(array('Foo.bar->\'fieldName\'' => 42)); $this->assertEquals(' WHERE `Foo`.`bar`->\'fieldName\' = 42', $result, 'SQL JSON operator failed'); - } + + // MYSQL style + $result = $this->testDb->conditions(array('Foo.bar->>\'$.fieldName\'' => 42)); + $this->assertEquals(' WHERE `Foo`.`bar`->>\'$.fieldName\' = 42', $result, 'SQL JSON operator failed'); + + //Without defining table name. + $result = $this->testDb->conditions(array('bar->>\'$.fieldName\'' => 42)); + $this->assertEquals(' WHERE `bar`->>\'$.fieldName\' = 42', $result, 'SQL JSON operator failed'); + } /** * test that order() will accept objects made from DboSource::expression