TupleComparison.php 5.0 KB

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