Skip to content

Commit

Permalink
Adding simple CONCAT() function transformer for Sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 8, 2013
1 parent cb306bd commit b160a2d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
Expand Up @@ -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 {

Expand Down
42 changes: 42 additions & 0 deletions lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php
@@ -0,0 +1,42 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Datasource\Database\Dialect;

use Cake\Model\Datasource\Database\Expression\FunctionExpression;
use Cake\Model\Datasource\Database\Query;

trait SqliteDialectTrait {

protected function _selectQueryTranslator($query) {
return parent::_selectQueryTranslator($query)
->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(' ||');
}
}

}

2 changes: 2 additions & 0 deletions lib/Cake/Model/Datasource/Database/Driver/Sqlite.php
Expand Up @@ -18,13 +18,15 @@
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 {

use PDODriverTrait {
connect as protected _connect;
}
use SqliteDialectTrait;

/**
* Base configuration settings for Sqlite driver
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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] . ')';
}

}
12 changes: 7 additions & 5 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -1944,25 +1944,27 @@ public function testInsertFailureMixingTypesQueryFirst() {
}

/**
* undocumented function
* Tests that functions are correctly transformed and their parameters are bound
*
* @group FunctionExpression
* @return void
*/
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'));
}


Expand Down

0 comments on commit b160a2d

Please sign in to comment.