Allow SQL json selects without defining table name

This commit is contained in:
Cory Thompson 2017-01-24 20:54:07 +11:00
parent 990d0a962f
commit 55324c0b2a
2 changed files with 12 additions and 3 deletions

View file

@ -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
);

View file

@ -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