FunctionsTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database;
  18. use Cake\Database\Expression\FunctionExpression;
  19. /**
  20. * Contains methods related to generating FunctionExpression objects
  21. * with most commonly used SQL functions.
  22. * This trait is just a factory for FunctionExpression objects.
  23. */
  24. trait FunctionsTrait {
  25. /**
  26. * Returns a new instance of a FunctionExpression. This is used for generating
  27. * arbitrary function calls in the final SQL string.
  28. *
  29. * @param string $name the name of the SQL function to constructed
  30. * @param array $params list of params to be passed to the function
  31. * @param array $types list of types for each function param
  32. * @return FunctionExpression
  33. */
  34. public function func($name, $params = [], $types = []) {
  35. return new FunctionExpression($name, $params, $types);
  36. }
  37. /**
  38. * Helper function to build a function expression that only takes one literal
  39. * argument.
  40. *
  41. * @param string $name name of the function to build
  42. * @param mixed $expression the function argument
  43. * @param array $types list of types to bind to the arguments
  44. * @return FunctionExpression
  45. */
  46. protected function _literalArgumentFunction($name, $expression, $types = []) {
  47. if (!is_string($expression)) {
  48. $expression = [$expression];
  49. } else {
  50. $expression = [$expression => 'literal'];
  51. }
  52. return $this->func($name, $expression, $types);
  53. }
  54. /**
  55. * Returns a FunctionExpression representing a call to SQL SUM function.
  56. *
  57. * @param mixed $expression the function argument
  58. * @param array $types list of types to bind to the arguments
  59. * @return FunctionExpression
  60. */
  61. public function sum($expression, $types = []) {
  62. return $this->_literalArgumentFunction('SUM', $expression, $types);
  63. }
  64. /**
  65. * Returns a FunctionExpression representing a call to SQL AVG function.
  66. *
  67. * @param mixed $expression the function argument
  68. * @param array $types list of types to bind to the arguments
  69. * @return FunctionExpression
  70. */
  71. public function avg($expression, $types = []) {
  72. return $this->_literalArgumentFunction('AVG', $expression, $types);
  73. }
  74. /**
  75. * Returns a FunctionExpression representing a call to SQL MAX function.
  76. *
  77. * @param mixed $expression the function argument
  78. * @param array $types list of types to bind to the arguments
  79. * @return FunctionExpression
  80. */
  81. public function max($expression, $types = []) {
  82. return $this->_literalArgumentFunction('MAX', $expression, $types);
  83. }
  84. /**
  85. * Returns a FunctionExpression representing a call to SQL MIN function.
  86. *
  87. * @param mixed $expression the function argument
  88. * @param array $types list of types to bind to the arguments
  89. * @return FunctionExpression
  90. */
  91. public function min($expression, $types = []) {
  92. return $this->_literalArgumentFunction('MIN', $expression, $types);
  93. }
  94. /**
  95. * Returns a FunctionExpression representing a call to SQL COUNT function.
  96. *
  97. * @param mixed $expression the function argument
  98. * @param array $types list of types to bind to the arguments
  99. * @return FunctionExpression
  100. */
  101. public function count($expression, $types = []) {
  102. return $this->_literalArgumentFunction('COUNT', $expression, $types);
  103. }
  104. /**
  105. * Returns a FunctionExpression representing a string concatenation
  106. *
  107. * @param array $args List of strings or expressions to concatenate
  108. * @param array $types list of types to bind to the arguments
  109. * @return FunctionExpression
  110. */
  111. public function concat($args, $types = []) {
  112. return $this->func('CONCAT', $args, $types);
  113. }
  114. /**
  115. * Returns a FunctionExpression representing a call to SQL COALESCE function.
  116. *
  117. * @param array $args List of expressions to evaluate as function parameters
  118. * @param array $types list of types to bind to the arguments
  119. * @return FunctionExpression
  120. */
  121. public function coalesce($args, $types = []) {
  122. return $this->func('COALESCE', $args, $types);
  123. }
  124. /**
  125. * Returns a FunctionExpression representing the difference in days between
  126. * two dates.
  127. *
  128. * @param array $args List of expressions to obtain the difference in days.
  129. * @param array $types list of types to bind to the arguments
  130. * @return FunctionExpression
  131. */
  132. public function dateDiff($dates, $types = []) {
  133. return $this->func('DATEDIFF', $dates, $types);
  134. }
  135. /**
  136. * Returns a FunctionExpression representing a call that will return the current
  137. * date and time. By default it returns both date and time, but you can also
  138. * make it generate only the date or only the time.
  139. *
  140. * @param string $type (datetime|date|time)
  141. * @return FunctionExpression
  142. */
  143. public function now($type = 'datetime') {
  144. if ($type === 'datetime') {
  145. return $this->func('NOW');
  146. }
  147. if ($type === 'date') {
  148. return $this->func('CURRENT_DATE');
  149. }
  150. if ($type === 'time') {
  151. return $this->func('CURRENT_TIME');
  152. }
  153. }
  154. }