FunctionsBuilder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 mixed $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 mixed $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 mixed $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 mixed $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 mixed $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 mixed $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 the difference in days between
  149. * two dates.
  150. *
  151. * @param array $args List of expressions to obtain the difference in days.
  152. * @param array $types list of types to bind to the arguments
  153. * @return \Cake\Database\Expression\FunctionExpression
  154. */
  155. public function dateDiff($args, $types = [])
  156. {
  157. return $this->_build('DATEDIFF', $args, $types, 'integer');
  158. }
  159. /**
  160. * Returns the specified date part from the SQL expression.
  161. *
  162. * @param string $part Part of the date to return.
  163. * @param string $expression Expression to obtain the date part from.
  164. * @param array $types list of types to bind to the arguments
  165. * @return \Cake\Database\Expression\FunctionExpression
  166. */
  167. public function datePart($part, $expression, $types = [])
  168. {
  169. return $this->extract($part, $expression);
  170. }
  171. /**
  172. * Returns the specified date part from the SQL expression.
  173. *
  174. * @param string $part Part of the date to return.
  175. * @param string $expression Expression to obtain the date part from.
  176. * @param array $types list of types to bind to the arguments
  177. * @return \Cake\Database\Expression\FunctionExpression
  178. */
  179. public function extract($part, $expression, $types = [])
  180. {
  181. $expression = $this->_literalArgumentFunction('EXTRACT', $expression, $types, 'integer');
  182. $expression->setConjunction(' FROM')->add([$part => 'literal'], [], true);
  183. return $expression;
  184. }
  185. /**
  186. * Add the time unit to the date expression
  187. *
  188. * @param string $expression Expression to obtain the date part from.
  189. * @param string $value Value to be added. Use negative to subtract.
  190. * @param string $unit Unit of the value e.g. hour or day.
  191. * @param array $types list of types to bind to the arguments
  192. * @return \Cake\Database\Expression\FunctionExpression
  193. */
  194. public function dateAdd($expression, $value, $unit, $types = [])
  195. {
  196. if (!is_numeric($value)) {
  197. $value = 0;
  198. }
  199. $interval = $value . ' ' . $unit;
  200. $expression = $this->_literalArgumentFunction('DATE_ADD', $expression, $types, 'datetime');
  201. $expression->setConjunction(', INTERVAL')->add([$interval => 'literal']);
  202. return $expression;
  203. }
  204. /**
  205. * Returns a FunctionExpression representing a call to SQL WEEKDAY function.
  206. * 1 - Sunday, 2 - Monday, 3 - Tuesday...
  207. *
  208. * @param mixed $expression the function argument
  209. * @param array $types list of types to bind to the arguments
  210. * @return \Cake\Database\Expression\FunctionExpression
  211. */
  212. public function dayOfWeek($expression, $types = [])
  213. {
  214. return $this->_literalArgumentFunction('DAYOFWEEK', $expression, $types, 'integer');
  215. }
  216. /**
  217. * Returns a FunctionExpression representing a call to SQL WEEKDAY function.
  218. * 1 - Sunday, 2 - Monday, 3 - Tuesday...
  219. *
  220. * @param mixed $expression the function argument
  221. * @param array $types list of types to bind to the arguments
  222. * @return \Cake\Database\Expression\FunctionExpression
  223. */
  224. public function weekday($expression, $types = [])
  225. {
  226. return $this->dayOfWeek($expression, $types);
  227. }
  228. /**
  229. * Returns a FunctionExpression representing a call that will return the current
  230. * date and time. By default it returns both date and time, but you can also
  231. * make it generate only the date or only the time.
  232. *
  233. * @param string $type (datetime|date|time)
  234. * @return \Cake\Database\Expression\FunctionExpression
  235. */
  236. public function now($type = 'datetime')
  237. {
  238. if ($type === 'datetime') {
  239. return $this->_build('NOW')->setReturnType('datetime');
  240. }
  241. if ($type === 'date') {
  242. return $this->_build('CURRENT_DATE')->setReturnType('date');
  243. }
  244. if ($type === 'time') {
  245. return $this->_build('CURRENT_TIME')->setReturnType('time');
  246. }
  247. }
  248. /**
  249. * Magic method dispatcher to create custom SQL function calls
  250. *
  251. * @param string $name the SQL function name to construct
  252. * @param array $args list with up to 3 arguments, first one being an array with
  253. * parameters for the SQL function, the second one a list of types to bind to those
  254. * params, and the third one the return type of the function
  255. * @return \Cake\Database\Expression\FunctionExpression
  256. */
  257. public function __call($name, $args)
  258. {
  259. switch (count($args)) {
  260. case 0:
  261. return $this->_build($name);
  262. case 1:
  263. return $this->_build($name, $args[0]);
  264. case 2:
  265. return $this->_build($name, $args[0], $args[1]);
  266. default:
  267. return $this->_build($name, $args[0], $args[1], $args[2]);
  268. }
  269. }
  270. }