/
CaseExpression.php
248 lines (218 loc) · 8.04 KB
/
CaseExpression.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Expression;
use Cake\Database\ExpressionInterface;
use Cake\Database\Type\ExpressionTypeCasterTrait;
use Cake\Database\ValueBinder;
/**
* This class represents a SQL Case statement
*/
class CaseExpression implements ExpressionInterface
{
use ExpressionTypeCasterTrait;
/**
* A list of strings or other expression objects that represent the conditions of
* the case statement. For example one key of the array might look like "sum > :value"
*
* @var array
*/
protected $_conditions = [];
/**
* Values that are associated with the conditions in the $_conditions array.
* Each value represents the 'true' value for the condition with the corresponding key.
*
* @var array
*/
protected $_values = [];
/**
* The `ELSE` value for the case statement. If null then no `ELSE` will be included.
*
* @var string|\Cake\Database\ExpressionInterface|array|null
*/
protected $_elseValue;
/**
* Constructs the case expression
*
* @param array|\Cake\Database\ExpressionInterface $conditions The conditions to test. Must be a ExpressionInterface
* instance, or an array of ExpressionInterface instances.
* @param array|\Cake\Database\ExpressionInterface $values associative array of values to be associated with the conditions
* passed in $conditions. If there are more $values than $conditions, the last $value is used as the `ELSE` value
* @param array $types associative array of types to be associated with the values
* passed in $values
*/
public function __construct($conditions = [], $values = [], $types = [])
{
if (!empty($conditions)) {
$this->add($conditions, $values, $types);
}
if (is_array($conditions) && is_array($values) && count($values) > count($conditions)) {
end($values);
$key = key($values);
$this->elseValue($values[$key], isset($types[$key]) ? $types[$key] : null);
}
}
/**
* Adds one or more conditions and their respective true values to the case object.
* Conditions must be a one dimensional array or a QueryExpression.
* The trueValues must be a similar structure, but may contain a string value.
*
* @param array|\Cake\Database\ExpressionInterface $conditions Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.
* @param array|\Cake\Database\ExpressionInterface $values associative array of values of each condition
* @param array $types associative array of types to be associated with the values
*
* @return $this
*/
public function add($conditions = [], $values = [], $types = [])
{
if (!is_array($conditions)) {
$conditions = [$conditions];
}
if (!is_array($values)) {
$values = [$values];
}
if (!is_array($types)) {
$types = [$types];
}
$this->_addExpressions($conditions, $values, $types);
return $this;
}
/**
* Iterates over the passed in conditions and ensures that there is a matching true value for each.
* If no matching true value, then it is defaulted to '1'.
*
* @param array|\Cake\Database\ExpressionInterface $conditions Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.
* @param array|\Cake\Database\ExpressionInterface $values associative array of values of each condition
* @param array $types associative array of types to be associated with the values
*
* @return void
*/
protected function _addExpressions($conditions, $values, $types)
{
$rawValues = array_values($values);
$keyValues = array_keys($values);
foreach ($conditions as $k => $c) {
$numericKey = is_numeric($k);
if ($numericKey && empty($c)) {
continue;
}
if (!$c instanceof ExpressionInterface) {
continue;
}
$this->_conditions[] = $c;
$value = isset($rawValues[$k]) ? $rawValues[$k] : 1;
if ($value === 'literal') {
$value = $keyValues[$k];
$this->_values[] = $value;
continue;
}
if ($value === 'identifier') {
$value = new IdentifierExpression($keyValues[$k]);
$this->_values[] = $value;
continue;
}
$type = isset($types[$k]) ? $types[$k] : null;
if ($type !== null && !$value instanceof ExpressionInterface) {
$value = $this->_castToExpression($value, $type);
}
if ($value instanceof ExpressionInterface) {
$this->_values[] = $value;
continue;
}
$this->_values[] = ['value' => $value, 'type' => $type];
}
}
/**
* Sets the default value
*
* @param \Cake\Database\ExpressionInterface|string|array|null $value Value to set
* @param string|null $type Type of value
*
* @return void
*/
public function elseValue($value = null, $type = null)
{
if (is_array($value)) {
end($value);
$value = key($value);
}
if ($value !== null && !$value instanceof ExpressionInterface) {
$value = $this->_castToExpression($value, $type);
}
if (!$value instanceof ExpressionInterface) {
$value = ['value' => $value, 'type' => $type];
}
$this->_elseValue = $value;
}
/**
* Compiles the relevant parts into sql
*
* @param array|string|\Cake\Database\ExpressionInterface $part The part to compile
* @param \Cake\Database\ValueBinder $generator Sql generator
*
* @return string
*/
protected function _compile($part, ValueBinder $generator)
{
if ($part instanceof ExpressionInterface) {
$part = $part->sql($generator);
} elseif (is_array($part)) {
$placeholder = $generator->placeholder('param');
$generator->bind($placeholder, $part['value'], $part['type']);
$part = $placeholder;
}
return $part;
}
/**
* Converts the Node into a SQL string fragment.
*
* @param \Cake\Database\ValueBinder $generator Placeholder generator object
*
* @return string
*/
public function sql(ValueBinder $generator)
{
$parts = [];
$parts[] = 'CASE';
foreach ($this->_conditions as $k => $part) {
$value = $this->_values[$k];
$parts[] = 'WHEN ' . $this->_compile($part, $generator) . ' THEN ' . $this->_compile($value, $generator);
}
if ($this->_elseValue !== null) {
$parts[] = 'ELSE';
$parts[] = $this->_compile($this->_elseValue, $generator);
}
$parts[] = 'END';
return implode(' ', $parts);
}
/**
* {@inheritDoc}
*
*/
public function traverse(callable $visitor)
{
foreach (['_conditions', '_values'] as $part) {
foreach ($this->{$part} as $c) {
if ($c instanceof ExpressionInterface) {
$visitor($c);
$c->traverse($visitor);
}
}
}
if ($this->_elseValue instanceof ExpressionInterface) {
$visitor($this->_elseValue);
$this->_elseValue->traverse($visitor);
}
}
}