Skip to content
Permalink
Browse files

Allow SQL json selects without defining table name

  • Loading branch information...
coryjthompson committed Jan 24, 2017
1 parent 990d0a9 commit 55324c0b2a1d68e46380683ff1fdc655210f2f37
Showing with 12 additions and 3 deletions.
  1. +2 −1 lib/Cake/Model/Datasource/DboSource.php
  2. +10 −2 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -2958,10 +2958,11 @@ protected function _quoteFields($conditions) {
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
);
@@ -249,12 +249,20 @@ public function testBooleanEmptyConditionsParsing() {
* @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

0 comments on commit 55324c0

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.