Comparison.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\ValueBinder;
  18. /**
  19. * A Comparison is a type of query expression that represents an operation
  20. * involving a field an operator and a value. In its most common form the
  21. * string representation of a comparison is `field = value`
  22. *
  23. * @internal
  24. */
  25. class Comparison extends QueryExpression {
  26. /**
  27. * The field name or expression to be used in the left hand side of the operator
  28. *
  29. * @var string
  30. */
  31. protected $_field;
  32. /**
  33. * The value to be used in the right hand side of the operation
  34. *
  35. * @var mixed
  36. */
  37. protected $_value;
  38. /**
  39. * The type to be used for casting the value to a database representation
  40. *
  41. * @var string
  42. */
  43. protected $_type;
  44. /**
  45. * Constructor
  46. *
  47. * @param string $field the field name to compare to a value
  48. * @param mixed $value The value to be used in comparison
  49. * @param string $type the type name used to cast the value
  50. * @param string $conjunction the operator used for comparing field and value
  51. */
  52. public function __construct($field, $value, $type, $conjunction) {
  53. $this->field($field);
  54. $this->value($value);
  55. $this->type($conjunction);
  56. if (is_string($type)) {
  57. $this->_type = $type;
  58. }
  59. }
  60. /**
  61. * Sets the field name
  62. *
  63. * @param string $field The field to compare with.
  64. * @return void
  65. */
  66. public function field($field) {
  67. $this->_field = $field;
  68. }
  69. /**
  70. * Sets the value
  71. *
  72. * @param mixed $value The value to compare
  73. * @return void
  74. */
  75. public function value($value) {
  76. $this->_value = $value;
  77. }
  78. /**
  79. * Returns the field name
  80. *
  81. * @return string
  82. */
  83. public function getField() {
  84. return $this->_field;
  85. }
  86. /**
  87. * Returns the value used for comparison
  88. *
  89. * @return mixed
  90. */
  91. public function getValue() {
  92. return $this->_value;
  93. }
  94. /**
  95. * Convert the expression into a SQL fragment.
  96. *
  97. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  98. * @return string
  99. */
  100. public function sql(ValueBinder $generator) {
  101. if ($this->_value instanceof ExpressionInterface) {
  102. $template = '%s %s (%s)';
  103. $value = $this->_value->sql($generator);
  104. } else {
  105. list($template, $value) = $this->_stringExpression($generator);
  106. }
  107. return sprintf($template, $this->_field, $this->_conjunction, $value);
  108. }
  109. /**
  110. * Returns a template and an placeholder for the value after registering it
  111. * with the placeholder $generator
  112. *
  113. * @param \Cake\Database\ValueBinder $generator The value binder to use.
  114. * @return array First position containing the template and the second a placeholder
  115. */
  116. protected function _stringExpression($generator) {
  117. if (strpos($this->_type, '[]') !== false) {
  118. $template = '%s %s (%s)';
  119. $type = str_replace('[]', '', $this->_type);
  120. $value = $this->_flattenValue($this->_value, $generator, $type);
  121. // To avoid SQL erros when comparing a field to a list of empty values,
  122. // generate a condition that will always evaluate to false
  123. if ($value === '') {
  124. return ['1 != 1', ''];
  125. }
  126. } else {
  127. $template = '%s %s %s';
  128. $value = $this->_bindValue($this->_value, $generator, $this->_type);
  129. }
  130. return [$template, $value];
  131. }
  132. /**
  133. * Registers a value in the placeholder generator and returns the generated placeholder
  134. *
  135. * @param mixed $value The value to bind
  136. * @param \Cake\Database\ValueBinder $generator The value binder to use
  137. * @param string $type The type of $value
  138. * @return string generated placeholder
  139. */
  140. protected function _bindValue($value, $generator, $type) {
  141. $placeholder = $generator->placeholder($this->_field);
  142. $generator->bind($placeholder, $value, $type);
  143. return $placeholder;
  144. }
  145. /**
  146. * Converts a traversable value into a set of placeholders generated by
  147. * $generator and separated by `,`
  148. *
  149. * @param array|\Traversable $value the value to flatten
  150. * @param \Cake\Database\ValueBinder $generator The value binder to use
  151. * @param string|array $type the type to cast values to
  152. * @return string
  153. */
  154. protected function _flattenValue($value, $generator, $type = null) {
  155. $parts = [];
  156. foreach ($value as $k => $v) {
  157. $parts[] = $this->_bindValue($v, $generator, $type);
  158. }
  159. return implode(',', $parts);
  160. }
  161. /**
  162. * Returns the number of expression this class represents
  163. *
  164. * @return int
  165. */
  166. public function count() {
  167. return 1;
  168. }
  169. }