Skip to content

Commit 4730e77

Browse files
committed
Extracting SQL functions to its own class to avoid function name
clashing
1 parent ef4311e commit 4730e77

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

Cake/Database/FunctionsTrait.php renamed to Cake/Database/FunctionsBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
/**
2222
* Contains methods related to generating FunctionExpression objects
2323
* with most commonly used SQL functions.
24-
* This trait is just a factory for FunctionExpression objects.
24+
* This acts as a factory for FunctionExpression objects.
2525
*/
26-
trait FunctionsTrait {
26+
class FunctionsBuilder {
2727

2828
/**
2929
* Returns a new instance of a FunctionExpression. This is used for generating

Cake/Database/Query.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
*/
3636
class Query implements ExpressionInterface, IteratorAggregate {
3737

38-
use FunctionsTrait;
39-
4038
/**
4139
* Connection instance to be used to execute this query
4240
*
@@ -148,6 +146,13 @@ class Query implements ExpressionInterface, IteratorAggregate {
148146
*/
149147
protected $_valueBinder;
150148

149+
/**
150+
* Instance of functions builder object used for generating arbitrary SQL functions
151+
*
152+
* @var FunctionsBuilder
153+
*/
154+
protected $_functionsBuilder;
155+
151156
/**
152157
* Constructor
153158
*
@@ -1372,16 +1377,23 @@ public function newExpr() {
13721377
}
13731378

13741379
/**
1375-
* Returns a new instance of a FunctionExpression. This is used for generating
1376-
* arbitrary function calls in the final SQL string.
1380+
* Returns an instance of a functions builder object that can be used for
1381+
* generating arbitrary SQL functions.
13771382
*
1378-
* @param string $name the name of the SQL function to constructed
1379-
* @param array $params list of params to be passed to the function
1380-
* @param array $types list of types for each function param
1381-
* @return FunctionExpression
1383+
* ### Example:
1384+
*
1385+
* {{{
1386+
* $query->func()->count('*');
1387+
* $query->func()->dateDiff(['2012-01-05', '2012-01-02'])
1388+
* }}}
1389+
*
1390+
* @return FunctionsBuilder
13821391
*/
1383-
public function func($name, $params = [], $types = []) {
1384-
return new FunctionExpression($name, $params, $types);
1392+
public function func() {
1393+
if (empty($this->_functionsBuilder)) {
1394+
$this->_functionsBuilder = new FunctionsBuilder;
1395+
}
1396+
return $this->_functionsBuilder;
13851397
}
13861398

13871399
/**

Cake/Test/TestCase/Database/FunctionsTraitTest.php renamed to Cake/Test/TestCase/Database/FunctionsBuilderTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@
1616
*/
1717
namespace Cake\Test\TestCase\Database;
1818

19-
use Cake\Database\FunctionsTrait;
19+
use Cake\Database\FunctionsBuilder;
2020
use Cake\Database\ValueBinder;
2121

2222
/**
23-
* Tests FunctionsTrait class
23+
* Tests FunctionsBuilder class
2424
*
2525
**/
26-
class FunctionsTraitTest extends \Cake\TestSuite\TestCase {
26+
class FunctionsBuilderTest extends \Cake\TestSuite\TestCase {
2727

2828
/**
29-
* Setups a mock for FunctionsTrait
29+
* Setups a mock for FunctionsBuilder
3030
*
3131
* @return void
3232
*/
3333
public function setUp() {
3434
parent::setUp();
35-
$this->functions = $this->getObjectForTrait(
36-
'\Cake\Database\FunctionsTrait'
37-
);
35+
$this->functions = new FunctionsBuilder;
3836
}
3937

4038
/**

Cake/Test/TestCase/Database/QueryTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ public function testSQLFunctions() {
18501850
$query = new Query($this->connection);
18511851
$result = $query->select(
18521852
function($q) {
1853-
return ['total' => $q->count('*')];
1853+
return ['total' => $q->func()->count('*')];
18541854
}
18551855
)
18561856
->from('articles')
@@ -1859,7 +1859,9 @@ function($q) {
18591859
$this->assertEquals($expected, $result->fetchAll('assoc'));
18601860

18611861
$query = new Query($this->connection);
1862-
$result = $query->select(['c' => $query->concat(['title' => 'literal', ' is appended'])])
1862+
$result = $query->select([
1863+
'c' => $query->func()->concat(['title' => 'literal', ' is appended'])
1864+
])
18631865
->from('articles')
18641866
->order(['c' => 'ASC'])
18651867
->execute();
@@ -1872,19 +1874,19 @@ function($q) {
18721874

18731875
$query = new Query($this->connection);
18741876
$result = $query
1875-
->select(['d' => $query->dateDiff(['2012-01-05', '2012-01-02'])])
1877+
->select(['d' => $query->func()->dateDiff(['2012-01-05', '2012-01-02'])])
18761878
->execute();
18771879
$this->assertEquals([['d' => '3.0']], $result->fetchAll('assoc'));
18781880

18791881
$query = new Query($this->connection);
18801882
$result = $query
1881-
->select(['d' => $query->now('date')])
1883+
->select(['d' => $query->func()->now('date')])
18821884
->execute();
18831885
$this->assertEquals([['d' => date('Y-m-d')]], $result->fetchAll('assoc'));
18841886

18851887
$query = new Query($this->connection);
18861888
$result = $query
1887-
->select(['d' => $query->now('time')])
1889+
->select(['d' => $query->func()->now('time')])
18881890
->execute();
18891891
$this->assertWithinMargin(
18901892
date('U'),
@@ -1894,7 +1896,7 @@ function($q) {
18941896

18951897
$query = new Query($this->connection);
18961898
$result = $query
1897-
->select(['d' => $query->now()])
1899+
->select(['d' => $query->func()->now()])
18981900
->execute();
18991901
$this->assertWithinMargin(
19001902
date('U'),

0 commit comments

Comments
 (0)