Skip to content

Commit

Permalink
Extracting SQL functions to its own class to avoid function name
Browse files Browse the repository at this point in the history
clashing
  • Loading branch information
lorenzo committed Nov 18, 2013
1 parent ef4311e commit 4730e77
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
Expand Up @@ -21,9 +21,9 @@
/**
* Contains methods related to generating FunctionExpression objects
* with most commonly used SQL functions.
* This trait is just a factory for FunctionExpression objects.
* This acts as a factory for FunctionExpression objects.
*/
trait FunctionsTrait {
class FunctionsBuilder {

/**
* Returns a new instance of a FunctionExpression. This is used for generating
Expand Down
32 changes: 22 additions & 10 deletions Cake/Database/Query.php
Expand Up @@ -35,8 +35,6 @@
*/
class Query implements ExpressionInterface, IteratorAggregate {

use FunctionsTrait;

/**
* Connection instance to be used to execute this query
*
Expand Down Expand Up @@ -148,6 +146,13 @@ class Query implements ExpressionInterface, IteratorAggregate {
*/
protected $_valueBinder;

/**
* Instance of functions builder object used for generating arbitrary SQL functions
*
* @var FunctionsBuilder
*/
protected $_functionsBuilder;

/**
* Constructor
*
Expand Down Expand Up @@ -1372,16 +1377,23 @@ public function newExpr() {
}

/**
* Returns a new instance of a FunctionExpression. This is used for generating
* arbitrary function calls in the final SQL string.
* Returns an instance of a functions builder object that can be used for
* generating arbitrary SQL functions.
*
* @param string $name the name of the SQL function to constructed
* @param array $params list of params to be passed to the function
* @param array $types list of types for each function param
* @return FunctionExpression
* ### Example:
*
* {{{
* $query->func()->count('*');
* $query->func()->dateDiff(['2012-01-05', '2012-01-02'])
* }}}
*
* @return FunctionsBuilder
*/
public function func($name, $params = [], $types = []) {
return new FunctionExpression($name, $params, $types);
public function func() {
if (empty($this->_functionsBuilder)) {
$this->_functionsBuilder = new FunctionsBuilder;
}
return $this->_functionsBuilder;
}

/**
Expand Down
Expand Up @@ -16,25 +16,23 @@
*/
namespace Cake\Test\TestCase\Database;

use Cake\Database\FunctionsTrait;
use Cake\Database\FunctionsBuilder;
use Cake\Database\ValueBinder;

/**
* Tests FunctionsTrait class
* Tests FunctionsBuilder class
*
**/
class FunctionsTraitTest extends \Cake\TestSuite\TestCase {
class FunctionsBuilderTest extends \Cake\TestSuite\TestCase {

/**
* Setups a mock for FunctionsTrait
* Setups a mock for FunctionsBuilder
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->functions = $this->getObjectForTrait(
'\Cake\Database\FunctionsTrait'
);
$this->functions = new FunctionsBuilder;
}

/**
Expand Down
14 changes: 8 additions & 6 deletions Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -1850,7 +1850,7 @@ public function testSQLFunctions() {
$query = new Query($this->connection);
$result = $query->select(
function($q) {
return ['total' => $q->count('*')];
return ['total' => $q->func()->count('*')];
}
)
->from('articles')
Expand All @@ -1859,7 +1859,9 @@ function($q) {
$this->assertEquals($expected, $result->fetchAll('assoc'));

$query = new Query($this->connection);
$result = $query->select(['c' => $query->concat(['title' => 'literal', ' is appended'])])
$result = $query->select([
'c' => $query->func()->concat(['title' => 'literal', ' is appended'])
])
->from('articles')
->order(['c' => 'ASC'])
->execute();
Expand All @@ -1872,19 +1874,19 @@ function($q) {

$query = new Query($this->connection);
$result = $query
->select(['d' => $query->dateDiff(['2012-01-05', '2012-01-02'])])
->select(['d' => $query->func()->dateDiff(['2012-01-05', '2012-01-02'])])
->execute();
$this->assertEquals([['d' => '3.0']], $result->fetchAll('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['d' => $query->now('date')])
->select(['d' => $query->func()->now('date')])
->execute();
$this->assertEquals([['d' => date('Y-m-d')]], $result->fetchAll('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['d' => $query->now('time')])
->select(['d' => $query->func()->now('time')])
->execute();
$this->assertWithinMargin(
date('U'),
Expand All @@ -1894,7 +1896,7 @@ function($q) {

$query = new Query($this->connection);
$result = $query
->select(['d' => $query->now()])
->select(['d' => $query->func()->now()])
->execute();
$this->assertWithinMargin(
date('U'),
Expand Down

0 comments on commit 4730e77

Please sign in to comment.