TupleComparison.php 6.1 KB

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