diff --git a/lib/Cake/Model/Datasource/Database/Dialect/PostgresDialectTrait.php b/lib/Cake/Model/Datasource/Database/Dialect/PostgresDialectTrait.php index bdd51db897d..1303a7bd6e7 100644 --- a/lib/Cake/Model/Datasource/Database/Dialect/PostgresDialectTrait.php +++ b/lib/Cake/Model/Datasource/Database/Dialect/PostgresDialectTrait.php @@ -19,7 +19,6 @@ use Cake\Model\Datasource\Database\Expression\UnaryExpression; use Cake\Model\Datasource\Database\Query; -use Cake\Model\Datasource\Database\SqlDialectTrait; trait PostgresDialectTrait { diff --git a/lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php b/lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php new file mode 100644 index 00000000000..d342e8df715 --- /dev/null +++ b/lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php @@ -0,0 +1,42 @@ +traverseExpressions(function($expression) { + if ($expression instanceof FunctionExpression) { + $this->_transformFunctionExpression($expression); + } + }); + } + + protected function _transformFunctionExpression(FunctionExpression $expression) { + if ($expression->name() === 'CONCAT') { + // CONCAT function is expressed as exp1 || exp2 + $expression->name('')->type(' ||'); + } + } + +} + diff --git a/lib/Cake/Model/Datasource/Database/Driver/Sqlite.php b/lib/Cake/Model/Datasource/Database/Driver/Sqlite.php index 607b345db3b..ce2c882e8d9 100644 --- a/lib/Cake/Model/Datasource/Database/Driver/Sqlite.php +++ b/lib/Cake/Model/Datasource/Database/Driver/Sqlite.php @@ -18,6 +18,7 @@ namespace Cake\Model\Datasource\Database\Driver; use Cake\Model\Datasource\Database\Statement\BufferedStatement; +use Cake\Model\Datasource\Database\Dialect\SqliteDialectTrait; use PDO; class Sqlite extends \Cake\Model\Datasource\Database\Driver { @@ -25,6 +26,7 @@ class Sqlite extends \Cake\Model\Datasource\Database\Driver { use PDODriverTrait { connect as protected _connect; } + use SqliteDialectTrait; /** * Base configuration settings for Sqlite driver diff --git a/lib/Cake/Model/Datasource/Database/Expression/FunctionExpression.php b/lib/Cake/Model/Datasource/Database/Expression/FunctionExpression.php index cbebd2e57a2..3e54965992d 100644 --- a/lib/Cake/Model/Datasource/Database/Expression/FunctionExpression.php +++ b/lib/Cake/Model/Datasource/Database/Expression/FunctionExpression.php @@ -34,7 +34,7 @@ class FunctionExpression extends QueryExpression { * * ``$f = new FunctionExpression('CONCAT', ['name' => 'literal', ' rules']);`` * - * Will produce ``CONCANT(name, ' rules')`` + * Will produce ``CONCAT(name, ' rules')`` * * @param string $name the name of the function to be constructed * @param array $params list of arguments to be passed to the function diff --git a/lib/Cake/Model/Datasource/Database/Expression/UnaryExpression.php b/lib/Cake/Model/Datasource/Database/Expression/UnaryExpression.php index c7c702e83ac..0509638f96c 100644 --- a/lib/Cake/Model/Datasource/Database/Expression/UnaryExpression.php +++ b/lib/Cake/Model/Datasource/Database/Expression/UnaryExpression.php @@ -20,7 +20,7 @@ class UnaryExpression extends QueryExpression { public function sql() { - return $this->_conjunction . ' (' . ((string)current($this->_conditions)) . ')'; + return $this->_conjunction . ' (' . (string)$this->_conditions[0] . ')'; } } diff --git a/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php b/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php index bd7ac69c1b3..0169ab9ce71 100644 --- a/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php +++ b/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php @@ -1944,7 +1944,7 @@ public function testInsertFailureMixingTypesQueryFirst() { } /** - * undocumented function + * Tests that functions are correctly transformed and their parameters are bound * * @group FunctionExpression * @return void @@ -1952,17 +1952,19 @@ public function testInsertFailureMixingTypesQueryFirst() { public function testSQLFunctions() { $this->_insertTwoRecords(); $query = new Query($this->connection); - $result = $query->select(function($q) { return ['total' => $q->count('*'), 'title']; }) + $result = $query->select(function($q) { return ['total' => $q->count('*')]; }) ->from('articles') ->execute(); - $expected = [['total' => 2, 'title' => 'another title']]; + $expected = [['total' => 2]]; $this->assertEquals($expected, $result->fetchAll('assoc')); $query = new Query($this->connection); - $result = $query->select([$query->concat(['title' => 'literal', ' is appended'])]) + $result = $query->select(['c' => $query->concat(['title' => 'literal', ' is appended'])]) ->from('articles') + ->order(['c' => 'ASC']) ->execute(); - debug($result->fetchAll()); + $expected = [['c' => 'a title is appended'], ['c' => 'another title is appended']]; + $this->assertEquals($expected, $result->fetchAll('assoc')); }