TupleComparison.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Expression;
  17. use Cake\Database\ExpressionInterface;
  18. use Cake\Database\ValueBinder;
  19. use Closure;
  20. /**
  21. * This expression represents SQL fragments that are used for comparing one tuple
  22. * to another, one tuple to a set of other tuples or one tuple to an expression
  23. */
  24. class TupleComparison extends ComparisonExpression
  25. {
  26. /**
  27. * The type to be used for casting the value to a database representation
  28. *
  29. * @var array
  30. * @psalm-suppress NonInvariantDocblockPropertyType
  31. */
  32. protected $_type;
  33. /**
  34. * Constructor
  35. *
  36. * @param string|array|\Cake\Database\ExpressionInterface $fields the fields to use to form a tuple
  37. * @param array|\Cake\Database\ExpressionInterface $values the values to use to form a tuple
  38. * @param array $types the types names to use for casting each of the values, only
  39. * one type per position in the value array in needed
  40. * @param string $conjunction the operator used for comparing field and value
  41. */
  42. public function __construct($fields, $values, array $types = [], string $conjunction = '=')
  43. {
  44. $this->_type = $types;
  45. $this->setField($fields);
  46. $this->setValue($values);
  47. $this->_operator = $conjunction;
  48. }
  49. /**
  50. * Sets the value
  51. *
  52. * @param mixed $value The value to compare
  53. * @return void
  54. */
  55. public function setValue($value): void
  56. {
  57. $this->_value = $value;
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function sql(ValueBinder $binder): string
  63. {
  64. $template = '(%s) %s (%s)';
  65. $fields = [];
  66. $originalFields = $this->getField();
  67. if (!is_array($originalFields)) {
  68. $originalFields = [$originalFields];
  69. }
  70. foreach ($originalFields as $field) {
  71. $fields[] = $field instanceof ExpressionInterface ? $field->sql($binder) : $field;
  72. }
  73. $values = $this->_stringifyValues($binder);
  74. $field = implode(', ', $fields);
  75. return sprintf($template, $field, $this->_operator, $values);
  76. }
  77. /**
  78. * Returns a string with the values as placeholders in a string to be used
  79. * for the SQL version of this expression
  80. *
  81. * @param \Cake\Database\ValueBinder $binder The value binder to convert expressions with.
  82. * @return string
  83. */
  84. protected function _stringifyValues(ValueBinder $binder): string
  85. {
  86. $values = [];
  87. $parts = $this->getValue();
  88. if ($parts instanceof ExpressionInterface) {
  89. return $parts->sql($binder);
  90. }
  91. foreach ($parts as $i => $value) {
  92. if ($value instanceof ExpressionInterface) {
  93. $values[] = $value->sql($binder);
  94. continue;
  95. }
  96. $type = $this->_type;
  97. $isMultiOperation = $this->isMulti();
  98. if (empty($type)) {
  99. $type = null;
  100. }
  101. if ($isMultiOperation) {
  102. $bound = [];
  103. foreach ($value as $k => $val) {
  104. /** @var string $valType */
  105. $valType = $type && isset($type[$k]) ? $type[$k] : $type;
  106. $bound[] = $this->_bindValue($val, $binder, $valType);
  107. }
  108. $values[] = sprintf('(%s)', implode(',', $bound));
  109. continue;
  110. }
  111. /** @var string $valType */
  112. $valType = $type && isset($type[$i]) ? $type[$i] : $type;
  113. $values[] = $this->_bindValue($value, $binder, $valType);
  114. }
  115. return implode(', ', $values);
  116. }
  117. /**
  118. * @inheritDoc
  119. */
  120. protected function _bindValue($value, ValueBinder $binder, ?string $type = null): string
  121. {
  122. $placeholder = $binder->placeholder('tuple');
  123. $binder->bind($placeholder, $value, $type);
  124. return $placeholder;
  125. }
  126. /**
  127. * @inheritDoc
  128. */
  129. public function traverse(Closure $callback)
  130. {
  131. /** @var string[] $fields */
  132. $fields = $this->getField();
  133. foreach ($fields as $field) {
  134. $this->_traverseValue($field, $callback);
  135. }
  136. $value = $this->getValue();
  137. if ($value instanceof ExpressionInterface) {
  138. $callback($value);
  139. $value->traverse($callback);
  140. return $this;
  141. }
  142. foreach ($value as $val) {
  143. if ($this->isMulti()) {
  144. foreach ($val as $v) {
  145. $this->_traverseValue($v, $callback);
  146. }
  147. } else {
  148. $this->_traverseValue($val, $callback);
  149. }
  150. }
  151. return $this;
  152. }
  153. /**
  154. * Conditionally executes the callback for the passed value if
  155. * it is an ExpressionInterface
  156. *
  157. * @param mixed $value The value to traverse
  158. * @param \Closure $callback The callable to use when traversing
  159. * @return void
  160. */
  161. protected function _traverseValue($value, Closure $callback): void
  162. {
  163. if ($value instanceof ExpressionInterface) {
  164. $callback($value);
  165. $value->traverse($callback);
  166. }
  167. }
  168. /**
  169. * Determines if each of the values in this expressions is a tuple in
  170. * itself
  171. *
  172. * @return bool
  173. */
  174. public function isMulti(): bool
  175. {
  176. return in_array(strtolower($this->_operator), ['in', 'not in']);
  177. }
  178. }