1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ActiveRecord for API |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\base\NotSupportedException; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract QueryBuilder. |
19
|
|
|
* |
20
|
|
|
* QueryBuilder builds a request from the specification given as a [[Query]] object. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractQueryBuilder extends \yii\base\Object implements QueryBuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AbstractConnection |
26
|
|
|
*/ |
27
|
|
|
public $db; |
28
|
|
|
|
29
|
2 |
|
public function __construct($connection, $config = []) |
30
|
|
|
{ |
31
|
2 |
|
$this->db = $connection; |
32
|
2 |
|
parent::__construct($config); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Builds config array to create Command. |
37
|
|
|
* @param Query $query |
38
|
|
|
* @throws NotSupportedException |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
2 |
|
public function build(Query $query) |
42
|
|
|
{ |
43
|
2 |
|
return ['request' => $this->createRequest($query)]; |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
public function createRequest($query) |
47
|
|
|
{ |
48
|
3 |
|
return new $this->db->requestClass($this, $query); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Prepares query before actual building. |
53
|
|
|
* This function for you to redefine. |
54
|
|
|
* It will be called before other build functions. |
55
|
|
|
* @param Query $query |
56
|
|
|
*/ |
57
|
2 |
|
public function prepare(Query $query) |
58
|
|
|
{ |
59
|
2 |
|
return $query->prepare($this); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This function is for you to provide your authentication. |
64
|
|
|
* @param Query $query |
65
|
|
|
*/ |
66
|
|
|
abstract public function buildAuth(Query $query); |
67
|
|
|
|
68
|
|
|
abstract public function buildMethod(Query $query); |
69
|
|
|
|
70
|
|
|
abstract public function buildUri(Query $query); |
71
|
|
|
|
72
|
|
|
abstract public function buildHeaders(Query $query); |
73
|
|
|
|
74
|
|
|
abstract public function buildProtocolVersion(Query $query); |
75
|
|
|
|
76
|
|
|
abstract public function buildQueryParams(Query $query); |
77
|
|
|
|
78
|
|
|
abstract public function buildFormParams(Query $query); |
79
|
|
|
|
80
|
|
|
abstract public function buildBody(Query $query); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates insert request. |
84
|
|
|
* @param string $table |
85
|
|
|
* @param array $columns |
86
|
|
|
* @param array $options |
87
|
|
|
* @return AbstractRequest |
88
|
|
|
*/ |
89
|
1 |
|
public function insert($table, $columns, array $options = []) |
90
|
|
|
{ |
91
|
1 |
|
return $this->perform('insert', $table, $columns, $options); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Creates update request. |
96
|
|
|
* @param string $table |
97
|
|
|
* @param array $columns |
98
|
|
|
* @param array $options |
99
|
|
|
* @return AbstractRequest |
100
|
|
|
*/ |
101
|
|
|
public function update($table, $columns, $condition = [], array $options = []) |
102
|
|
|
{ |
103
|
|
|
$query = $this->createQuery('update', $table, $options)->body($columns)->where($condition); |
104
|
|
|
|
105
|
|
|
return $this->createRequest($query); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function delete($table, $condition = [], array $options = []) |
109
|
|
|
{ |
110
|
|
|
$query = $this->createQuery('delete', $table, $options)->where($condition); |
111
|
|
|
|
112
|
|
|
return $this->createRequest($query); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function perform($action, $table, $body, $options = []) |
116
|
|
|
{ |
117
|
1 |
|
$query = $this->createQuery($action, $table, $options)->body($body); |
118
|
|
|
|
119
|
1 |
|
return $this->createRequest($query); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function createQuery($action, $table, array $options = []) |
123
|
|
|
{ |
124
|
1 |
|
$class = $this->db->queryClass; |
125
|
|
|
|
126
|
1 |
|
return $class::instantiate($action, $table, $options); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function buildCondition($condition) |
130
|
|
|
{ |
131
|
|
|
static $builders = [ |
132
|
|
|
'and' => 'buildAndCondition', |
133
|
|
|
'between' => 'buildBetweenCondition', |
134
|
|
|
'eq' => 'buildEqCondition', |
135
|
|
|
'ne' => 'buildNotEqCondition', |
136
|
|
|
'in' => 'buildInCondition', |
137
|
|
|
'ni' => 'buildNotInCondition', |
138
|
|
|
'like' => 'buildLikeCondition', |
139
|
|
|
'ilike' => 'buildIlikeCondition', |
140
|
|
|
'gt' => 'buildCompareCondition', |
141
|
|
|
'ge' => 'buildCompareCondition', |
142
|
|
|
'lt' => 'buildCompareCondition', |
143
|
|
|
'le' => 'buildCompareCondition', |
144
|
|
|
]; |
145
|
|
|
if (empty($condition)) { |
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
if (!is_array($condition)) { |
149
|
|
|
throw new NotSupportedException('String conditions in where() are not supported by HiArt.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ... |
153
|
|
|
$operator = strtolower($condition[0]); |
154
|
|
|
if (isset($builders[$operator])) { |
155
|
|
|
$method = $builders[$operator]; |
156
|
|
|
array_shift($condition); // Shift build condition |
157
|
|
|
|
158
|
|
|
return $this->$method($operator, $condition); |
159
|
|
|
} else { |
160
|
|
|
throw new InvalidParamException('Found unknown operator in query: ' . $operator); |
161
|
|
|
} |
162
|
|
|
} else { |
163
|
|
|
return $this->buildHashCondition($condition); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
protected function buildHashCondition($condition) |
168
|
|
|
{ |
169
|
|
|
$parts = []; |
170
|
|
|
foreach ($condition as $attribute => $value) { |
171
|
|
|
if (is_array($value)) { // IN condition |
172
|
|
|
// $parts[] = [$attribute.'s' => join(',',$value)]; |
|
|
|
|
173
|
|
|
$parts[$attribute . 's'] = implode(',', $value); |
174
|
|
|
} else { |
175
|
|
|
$parts[$attribute] = $value; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $parts; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function buildLikeCondition($operator, $operands) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
return [$operands[0] . '_like' => $operands[1]]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function buildIlikeCondition($operator, $operands) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
return [$operands[0] . '_ilike' => $operands[1]]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function buildCompareCondition($operator, $operands) |
193
|
|
|
{ |
194
|
|
|
if (!isset($operands[0], $operands[1])) { |
195
|
|
|
throw new InvalidParamException("Operator '$operator' requires three operands."); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return [$operands[0] . '_' . $operator => $operands[1]]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function buildAndCondition($operator, $operands) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$parts = []; |
204
|
|
|
foreach ($operands as $operand) { |
205
|
|
|
if (is_array($operand)) { |
206
|
|
|
$parts = ArrayHelper::merge($this->buildCondition($operand), $parts); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
if (!empty($parts)) { |
210
|
|
|
return $parts; |
211
|
|
|
} else { |
212
|
|
|
return []; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function buildBetweenCondition($operator, $operands) |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
throw new NotSupportedException('Between condition is not supported by HiArt.'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function buildInCondition($operator, $operands, $not = false) |
222
|
|
|
{ |
223
|
|
|
if (!isset($operands[0], $operands[1])) { |
224
|
|
|
throw new InvalidParamException("Operator '$operator' requires two operands."); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
list($column, $values) = $operands; |
228
|
|
|
|
229
|
|
|
if (count($column) > 1) { |
230
|
|
|
return $this->buildCompositeInCondition($operator, $column, $values); |
231
|
|
|
} elseif (is_array($column)) { |
232
|
|
|
$column = reset($column); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
foreach ((array) $values as $i => $value) { |
236
|
|
|
if (is_array($value)) { |
237
|
|
|
$values[$i] = $value = isset($value[$column]) ? $value[$column] : null; |
238
|
|
|
} |
239
|
|
|
if ($value === null) { |
240
|
|
|
unset($values[$i]); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ($not) { |
245
|
|
|
$key = $column . '_ni'; // not in |
246
|
|
|
} else { |
247
|
|
|
$key = $column . '_in'; |
248
|
|
|
} |
249
|
|
|
return [$key => $values]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function buildNotInCondition($operator, $operands) |
253
|
|
|
{ |
254
|
|
|
return $this->buildInCondition($operator, $operands, true); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
protected function buildEqCondition($operator, $operands) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
$key = array_shift($operands); |
260
|
|
|
|
261
|
|
|
return [$key => reset($operands)]; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
protected function buildNotEqCondition($operator, $operands) |
265
|
|
|
{ |
266
|
|
|
$key = array_shift($operands); |
267
|
|
|
|
268
|
|
|
return [$key . '_' . $operator => reset($operands)]; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
protected function buildCompositeInCondition($operator, $columns, $values) |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
throw new NotSupportedException('composite in is not supported by HiArt.'); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: