FunctionsBuilder.php 12 KB

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