/
FunctionsBuilder.php
293 lines (270 loc) · 10.8 KB
/
FunctionsBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database;
use Cake\Database\Expression\FunctionExpression;
/**
* Contains methods related to generating FunctionExpression objects
* with most commonly used SQL functions.
* This acts as a factory for FunctionExpression objects.
*/
class FunctionsBuilder
{
/**
* Returns a new instance of a FunctionExpression. This is used for generating
* arbitrary function calls in the final SQL string.
*
* @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
* @param string $return The return type of the function expression
* @return \Cake\Database\Expression\FunctionExpression
*/
protected function _build($name, $params = [], $types = [], $return = 'string')
{
return new FunctionExpression($name, $params, $types, $return);
}
/**
* Helper function to build a function expression that only takes one literal
* argument.
*
* @param string $name name of the function to build
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @param string $return The return type for the function
* @return \Cake\Database\Expression\FunctionExpression
*/
protected function _literalArgumentFunction($name, $expression, $types = [], $return = 'string')
{
if (!is_string($expression)) {
$expression = [$expression];
} else {
$expression = [$expression => 'literal'];
}
return $this->_build($name, $expression, $types, $return);
}
/**
* Returns a FunctionExpression representing a call to SQL RAND function.
*
* @return \Cake\Database\Expression\FunctionExpression
*/
public function rand()
{
return $this->_build('RAND', [], [], 'float');
}
/**
* Returns a FunctionExpression representing a call to SQL SUM function.
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function sum($expression, $types = [])
{
$returnType = 'float';
if (current($types) === 'integer') {
$returnType = 'integer';
}
return $this->_literalArgumentFunction('SUM', $expression, $types, $returnType);
}
/**
* Returns a FunctionExpression representing a call to SQL AVG function.
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function avg($expression, $types = [])
{
return $this->_literalArgumentFunction('AVG', $expression, $types, 'float');
}
/**
* Returns a FunctionExpression representing a call to SQL MAX function.
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function max($expression, $types = [])
{
return $this->_literalArgumentFunction('MAX', $expression, $types, current($types) ?: 'string');
}
/**
* Returns a FunctionExpression representing a call to SQL MIN function.
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function min($expression, $types = [])
{
return $this->_literalArgumentFunction('MIN', $expression, $types, current($types) ?: 'string');
}
/**
* Returns a FunctionExpression representing a call to SQL COUNT function.
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function count($expression, $types = [])
{
return $this->_literalArgumentFunction('COUNT', $expression, $types, 'integer');
}
/**
* Returns a FunctionExpression representing a string concatenation
*
* @param array $args List of strings or expressions to concatenate
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function concat($args, $types = [])
{
return $this->_build('CONCAT', $args, $types, 'string');
}
/**
* Returns a FunctionExpression representing a call to SQL COALESCE function.
*
* @param array $args List of expressions to evaluate as function parameters
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function coalesce($args, $types = [])
{
return $this->_build('COALESCE', $args, $types, current($types) ?: 'string');
}
/**
* Returns a FunctionExpression representing the difference in days between
* two dates.
*
* @param array $args List of expressions to obtain the difference in days.
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function dateDiff($args, $types = [])
{
return $this->_build('DATEDIFF', $args, $types, 'integer');
}
/**
* Returns the specified date part from the SQL expression.
*
* @param string $part Part of the date to return.
* @param string|\Cake\Database\ExpressionInterface $expression Expression to obtain the date part from.
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function datePart($part, $expression, $types = [])
{
return $this->extract($part, $expression, $types);
}
/**
* Returns the specified date part from the SQL expression.
*
* @param string $part Part of the date to return.
* @param string|\Cake\Database\ExpressionInterface $expression Expression to obtain the date part from.
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function extract($part, $expression, $types = [])
{
$expression = $this->_literalArgumentFunction('EXTRACT', $expression, $types, 'integer');
$expression->setConjunction(' FROM')->add([$part => 'literal'], [], true);
return $expression;
}
/**
* Add the time unit to the date expression
*
* @param string|\Cake\Database\ExpressionInterface $expression Expression to obtain the date part from.
* @param string|int $value Value to be added. Use negative to subtract.
* @param string $unit Unit of the value e.g. hour or day.
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function dateAdd($expression, $value, $unit, $types = [])
{
if (!is_numeric($value)) {
$value = 0;
}
$interval = $value . ' ' . $unit;
$expression = $this->_literalArgumentFunction('DATE_ADD', $expression, $types, 'datetime');
$expression->setConjunction(', INTERVAL')->add([$interval => 'literal']);
return $expression;
}
/**
* Returns a FunctionExpression representing a call to SQL WEEKDAY function.
* 1 - Sunday, 2 - Monday, 3 - Tuesday...
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function dayOfWeek($expression, $types = [])
{
return $this->_literalArgumentFunction('DAYOFWEEK', $expression, $types, 'integer');
}
/**
* Returns a FunctionExpression representing a call to SQL WEEKDAY function.
* 1 - Sunday, 2 - Monday, 3 - Tuesday...
*
* @param string|\Cake\Database\ExpressionInterface $expression the function argument
* @param array $types list of types to bind to the arguments
* @return \Cake\Database\Expression\FunctionExpression
*/
public function weekday($expression, $types = [])
{
return $this->dayOfWeek($expression, $types);
}
/**
* Returns a FunctionExpression representing a call that will return the current
* date and time. By default it returns both date and time, but you can also
* make it generate only the date or only the time.
*
* @param string $type (datetime|date|time)
* @return \Cake\Database\Expression\FunctionExpression
*/
public function now($type = 'datetime')
{
if ($type === 'datetime') {
return $this->_build('NOW')->setReturnType('datetime');
}
if ($type === 'date') {
return $this->_build('CURRENT_DATE')->setReturnType('date');
}
if ($type === 'time') {
return $this->_build('CURRENT_TIME')->setReturnType('time');
}
}
/**
* Magic method dispatcher to create custom SQL function calls
*
* @param string $name the SQL function name to construct
* @param array $args list with up to 3 arguments, first one being an array with
* parameters for the SQL function, the second one a list of types to bind to those
* params, and the third one the return type of the function
* @return \Cake\Database\Expression\FunctionExpression
*/
public function __call($name, $args)
{
switch (count($args)) {
case 0:
return $this->_build($name);
case 1:
return $this->_build($name, $args[0]);
case 2:
return $this->_build($name, $args[0], $args[1]);
default:
return $this->_build($name, $args[0], $args[1], $args[2]);
}
}
}