TupleComparison.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Expression;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\ValueBinder;
  18. /**
  19. * This expression represents SQL fragments that are used for comparing one tuple
  20. * to another, one tuple to a set of other tuples or one tuple to an expression
  21. */
  22. class TupleComparison extends Comparison
  23. {
  24. /**
  25. * Constructor
  26. *
  27. * @param string|array|\Cake\Database\ExpressionInterface $fields the fields to use to form a tuple
  28. * @param array|\Cake\Database\ExpressionInterface $values the values to use to form a tuple
  29. * @param array $types the types names to use for casting each of the values, only
  30. * one type per position in the value array in needed
  31. * @param string $conjunction the operator used for comparing field and value
  32. */
  33. public function __construct($fields, $values, $types = [], $conjunction = '=')
  34. {
  35. parent::__construct($fields, $values, $types, $conjunction);
  36. $this->_type = (array)$types;
  37. }
  38. /**
  39. * Convert the expression into a SQL fragment.
  40. *
  41. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  42. * @return string
  43. */
  44. public function sql(ValueBinder $generator)
  45. {
  46. $template = '(%s) %s (%s)';
  47. $fields = [];
  48. $originalFields = $this->getField();
  49. if (!is_array($originalFields)) {
  50. $originalFields = [$originalFields];
  51. }
  52. foreach ($originalFields as $field) {
  53. $fields[] = $field instanceof ExpressionInterface ? $field->sql($generator) : $field;
  54. }
  55. $values = $this->_stringifyValues($generator);
  56. $field = implode(', ', $fields);
  57. return sprintf($template, $field, $this->_operator, $values);
  58. }
  59. /**
  60. * Returns a string with the values as placeholders in a string to be used
  61. * for the SQL version of this expression
  62. *
  63. * @param \Cake\Database\ValueBinder $generator The value binder to convert expressions with.
  64. * @return string
  65. */
  66. protected function _stringifyValues($generator)
  67. {
  68. $values = [];
  69. $parts = $this->getValue();
  70. if ($parts instanceof ExpressionInterface) {
  71. return $parts->sql($generator);
  72. }
  73. foreach ($parts as $i => $value) {
  74. if ($value instanceof ExpressionInterface) {
  75. $values[] = $value->sql($generator);
  76. continue;
  77. }
  78. $type = $this->_type;
  79. $multiType = is_array($type);
  80. $isMulti = $this->isMulti();
  81. $type = $multiType ? $type : str_replace('[]', '', $type);
  82. $type = $type ?: null;
  83. if ($isMulti) {
  84. $bound = [];
  85. foreach ($value as $k => $val) {
  86. $valType = $multiType ? $type[$k] : $type;
  87. $bound[] = $this->_bindValue($generator, $val, $valType);
  88. }
  89. $values[] = sprintf('(%s)', implode(',', $bound));
  90. continue;
  91. }
  92. $valType = $multiType && isset($type[$i]) ? $type[$i] : $type;
  93. $values[] = $this->_bindValue($generator, $value, $valType);
  94. }
  95. return implode(', ', $values);
  96. }
  97. /**
  98. * Registers a value in the placeholder generator and returns the generated
  99. * placeholder
  100. *
  101. * @param \Cake\Database\ValueBinder $generator The value binder
  102. * @param mixed $value The value to bind
  103. * @param string $type The type to use
  104. * @return string generated placeholder
  105. */
  106. protected function _bindValue($generator, $value, $type)
  107. {
  108. $placeholder = $generator->placeholder('tuple');
  109. $generator->bind($placeholder, $value, $type);
  110. return $placeholder;
  111. }
  112. /**
  113. * Traverses the tree of expressions stored in this object, visiting first
  114. * expressions in the left hand side and then the rest.
  115. *
  116. * Callback function receives as its only argument an instance of an ExpressionInterface
  117. *
  118. * @param callable $callable The callable to apply to sub-expressions
  119. * @return void
  120. */
  121. public function traverse(callable $callable)
  122. {
  123. foreach ($this->getField() as $field) {
  124. $this->_traverseValue($field, $callable);
  125. }
  126. $value = $this->getValue();
  127. if ($value instanceof ExpressionInterface) {
  128. $callable($value);
  129. $value->traverse($callable);
  130. return;
  131. }
  132. foreach ($value as $i => $val) {
  133. if ($this->isMulti()) {
  134. foreach ($val as $v) {
  135. $this->_traverseValue($v, $callable);
  136. }
  137. } else {
  138. $this->_traverseValue($val, $callable);
  139. }
  140. }
  141. }
  142. /**
  143. * Conditionally executes the callback for the passed value if
  144. * it is an ExpressionInterface
  145. *
  146. * @param mixed $value The value to traverse
  147. * @param callable $callable The callable to use when traversing
  148. * @return void
  149. */
  150. protected function _traverseValue($value, $callable)
  151. {
  152. if ($value instanceof ExpressionInterface) {
  153. $callable($value);
  154. $value->traverse($callable);
  155. }
  156. }
  157. /**
  158. * Determines if each of the values in this expressions is a tuple in
  159. * itself
  160. *
  161. * @return bool
  162. */
  163. public function isMulti()
  164. {
  165. return in_array(strtolower($this->_operator), ['in', 'not in']);
  166. }
  167. }