FunctionExpression.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Expression;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\TypedResultInterface;
  18. use Cake\Database\TypedResultTrait;
  19. use Cake\Database\Type\ExpressionTypeCasterTrait;
  20. use Cake\Database\ValueBinder;
  21. /**
  22. * This class represents a function call string in a SQL statement. Calls can be
  23. * constructed by passing the name of the function and a list of params.
  24. * For security reasons, all params passed are quoted by default unless
  25. * explicitly told otherwise.
  26. *
  27. * @internal
  28. */
  29. class FunctionExpression extends QueryExpression implements TypedResultInterface
  30. {
  31. use ExpressionTypeCasterTrait;
  32. use TypedResultTrait;
  33. /**
  34. * The name of the function to be constructed when generating the SQL string
  35. *
  36. * @var string
  37. */
  38. protected $_name;
  39. /**
  40. * Constructor. Takes a name for the function to be invoked and a list of params
  41. * to be passed into the function. Optionally you can pass a list of types to
  42. * be used for each bound param.
  43. *
  44. * By default, all params that are passed will be quoted. If you wish to use
  45. * literal arguments, you need to explicitly hint this function.
  46. *
  47. * ### Examples:
  48. *
  49. * `$f = new FunctionExpression('CONCAT', ['CakePHP', ' rules']);`
  50. *
  51. * Previous line will generate `CONCAT('CakePHP', ' rules')`
  52. *
  53. * `$f = new FunctionExpression('CONCAT', ['name' => 'literal', ' rules']);`
  54. *
  55. * Will produce `CONCAT(name, ' rules')`
  56. *
  57. * @param string $name the name of the function to be constructed
  58. * @param array $params list of arguments to be passed to the function
  59. * If associative the key would be used as argument when value is 'literal'
  60. * @param array $types associative array of types to be associated with the
  61. * passed arguments
  62. * @param string $returnType The return type of this expression
  63. */
  64. public function __construct($name, $params = [], $types = [], $returnType = 'string')
  65. {
  66. $this->_name = $name;
  67. $this->_returnType = $returnType;
  68. parent::__construct($params, $types, ',');
  69. }
  70. /**
  71. * Sets the name of the SQL function to be invoke in this expression.
  72. *
  73. * @param string $name The name of the function
  74. * @return $this
  75. */
  76. public function setName($name)
  77. {
  78. $this->_name = $name;
  79. return $this;
  80. }
  81. /**
  82. * Gets the name of the SQL function to be invoke in this expression.
  83. *
  84. * @return string
  85. */
  86. public function getName()
  87. {
  88. return $this->_name;
  89. }
  90. /**
  91. * Sets the name of the SQL function to be invoke in this expression,
  92. * if no value is passed it will return current name
  93. *
  94. * @deprecated 3.4.0 Use setName()/getName() instead.
  95. * @param string|null $name The name of the function
  96. * @return string|$this
  97. */
  98. public function name($name = null)
  99. {
  100. if ($name !== null) {
  101. return $this->setName($name);
  102. }
  103. return $this->getName();
  104. }
  105. /**
  106. * Adds one or more arguments for the function call.
  107. *
  108. * @param array $params list of arguments to be passed to the function
  109. * If associative the key would be used as argument when value is 'literal'
  110. * @param array $types associative array of types to be associated with the
  111. * passed arguments
  112. * @param bool $prepend Whether to prepend or append to the list of arguments
  113. * @see \Cake\Database\Expression\FunctionExpression::__construct() for more details.
  114. * @return $this
  115. */
  116. public function add($params, $types = [], $prepend = false)
  117. {
  118. $put = $prepend ? 'array_unshift' : 'array_push';
  119. $typeMap = $this->getTypeMap()->setTypes($types);
  120. foreach ($params as $k => $p) {
  121. if ($p === 'literal') {
  122. $put($this->_conditions, $k);
  123. continue;
  124. }
  125. if ($p === 'identifier') {
  126. $put($this->_conditions, new IdentifierExpression($k));
  127. continue;
  128. }
  129. $type = $typeMap->type($k);
  130. if ($type !== null && !$p instanceof ExpressionInterface) {
  131. $p = $this->_castToExpression($p, $type);
  132. }
  133. if ($p instanceof ExpressionInterface) {
  134. $put($this->_conditions, $p);
  135. continue;
  136. }
  137. $put($this->_conditions, ['value' => $p, 'type' => $type]);
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Returns the string representation of this object so that it can be used in a
  143. * SQL query. Note that values condition values are not included in the string,
  144. * in their place placeholders are put and can be replaced by the quoted values
  145. * accordingly.
  146. *
  147. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  148. * @return string
  149. */
  150. public function sql(ValueBinder $generator)
  151. {
  152. $parts = [];
  153. foreach ($this->_conditions as $condition) {
  154. if ($condition instanceof ExpressionInterface) {
  155. $condition = sprintf('(%s)', $condition->sql($generator));
  156. } elseif (is_array($condition)) {
  157. $p = $generator->placeholder('param');
  158. $generator->bind($p, $condition['value'], $condition['type']);
  159. $condition = $p;
  160. }
  161. $parts[] = $condition;
  162. }
  163. return $this->_name . sprintf('(%s)', implode(
  164. $this->_conjunction . ' ',
  165. $parts
  166. ));
  167. }
  168. /**
  169. * The name of the function is in itself an expression to generate, thus
  170. * always adding 1 to the amount of expressions stored in this object.
  171. *
  172. * @return int
  173. */
  174. public function count()
  175. {
  176. return 1 + count($this->_conditions);
  177. }
  178. }