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