-
Notifications
You must be signed in to change notification settings - Fork 6
/
ActiveQuery.php
383 lines (348 loc) · 11.9 KB
/
ActiveQuery.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
namespace xj\xunsearch;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\base\NotSupportedException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationTrait;
use yii\db\QueryTrait;
/**
* Normal Query
* ------------
*
* ActiveQuery mainly provides the following methods to retrieve the query results:
*
* - [[one()]]: returns a single record populated with the first row of data.
* - [[all()]]: returns all records based on the query results.
* - [[count()]]: returns the number of records.
*
* You can use query methods, such as [[where()]], [[limit()]] and [[orderBy()]] to customize the query options.
*
* - [[asArray()]]: whether to return each record as an array.
*
* ```php
* $demos = Demo::find()->asArray()->all();
* ```
* @author xjflyttp <[email protected]>
*/
class ActiveQuery extends Component implements ActiveQueryInterface {
use QueryTrait;
use ActiveQueryTrait;
/**
* @event Event an event that is triggered when the query is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/**
* @var array map of query condition to builder methods.
* These methods are used by [[buildCondition]] to build SQL conditions from array syntax.
*/
protected $conditionBuilders = [
'NOT' => 'buildNotCondition',
'AND' => 'buildAndCondition',
'OR' => 'buildAndCondition',
'WILD' => 'buildWildCondition',
];
/**
* Search Query String Cache
* @var string
*/
public $query;
/**
* enable fuzzy query
* @var bool
* @see http://www.xunsearch.com/doc/php/api/XSSearch
*/
public $fuzzy = false;
/**
* Constructor.
* @param array $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = []) {
$this->modelClass = $modelClass;
parent::__construct($config);
}
/**
* Initializes the object.
* This method is called at the end of the constructor. The default implementation will trigger
* an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end
* to ensure triggering of the event.
*/
public function init() {
parent::init();
$this->trigger(self::EVENT_INIT);
}
private function setCondition(XSSearch $search) {
$params = [];
// $search->setLimit($this->limit, $this->offset);
$this->buildLimit($this->limit, $this->offset);
$this->buildOrderBy($this->orderBy);
$this->query = $query = $this->buildWhere($this->where, $params);
$search->setQuery($query);
//fuzzy query
if ($this->fuzzy) {
$search->setFuzzy();
}
}
/**
* Parses the condition specification and generates the corresponding SQL expression.
* @param string|array $condition the condition specification. Please refer to [[Query::where()]]
* on how to specify a condition.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function buildCondition($condition, &$params) {
if (!is_array($condition)) {
return (string) $condition;
} elseif (empty($condition)) {
return '';
}
if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
$operator = strtoupper($condition[0]);
if (isset($this->conditionBuilders[$operator])) {
$method = $this->conditionBuilders[$operator];
} else {
$method = 'buildSimpleCondition';
}
array_shift($condition);
return $this->$method($operator, $condition, $params);
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return $this->buildHashCondition($condition, $params);
}
}
/**
* Creates a condition based on column-value pairs.
* @param array $condition the condition specification.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function buildHashCondition($condition, &$params) {
$parts = [];
foreach ($condition as $column => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$parts[] = "$column:$v";
}
return count($parts) === 1 ? $parts[0] : '(' . implode(') OR (', $parts) . ')';
} else {
if ($value !== null) {
$parts[] = "$column:$value";
}
}
}
return count($parts) === 1 ? $parts[0] : '(' . implode(') AND (', $parts) . ')';
}
/**
* @param string|array $condition
* @param array $params the binding parameters to be populated
* @return string the WHERE clause built from [[Query::$where]].
*/
public function buildWhere($condition, &$params) {
$where = $this->buildCondition($condition, $params);
return $where === '' ? '' : $where;
}
/**
* @param array $columns
* @return string the ORDER BY clause built from [[Query::$orderBy]].
*/
public function buildOrderBy($columns) {
if (empty($columns)) {
return '';
}
$search = $this->getSearch();
if (count($columns) === 1) {
foreach ($columns as $name => $direction) {
$search->setSort($name, $direction === SORT_DESC ? false : true);
}
} else {
$multiSort = [];
foreach ($columns as $name => $direction) {
$multiSort[$name] = $direction === SORT_DESC ? false : true;
}
$search->setMultiSort($multiSort);
}
// return 'ORDER BY ' . implode(', ', $orders);
}
/**
* @param integer $limit
* @param integer $offset
* @return string the LIMIT and OFFSET clauses
*/
public function buildLimit($limit, $offset) {
if (!$this->hasOffset($offset)) {
$offset = 0;
}
if ($this->hasLimit($limit)) {
$this->getSearch()->setLimit($limit, $offset);
}
}
/**
* Checks to see if the given limit is effective.
* @param mixed $limit the given limit
* @return boolean whether the limit is effective
*/
protected function hasLimit($limit) {
return is_string($limit) && ctype_digit($limit) || is_integer($limit) && $limit >= 0;
}
/**
* Checks to see if the given offset is effective.
* @param mixed $offset the given offset
* @return boolean whether the offset is effective
*/
protected function hasOffset($offset) {
return is_integer($offset) && $offset > 0 || is_string($offset) && ctype_digit($offset) && $offset !== '0';
}
/**
* Connects two or more SQL expressions with the `AND` or `OR` operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the SQL expressions to connect.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function buildAndCondition($operator, $operands, &$params) {
$parts = [];
foreach ($operands as $operand) {
if (is_array($operand)) {
$operand = $this->buildCondition($operand, $params);
}
if ($operand !== '') {
$parts[] = $operand;
}
}
if (!empty($parts)) {
return '(' . implode(") $operator (", $parts) . ')';
} else {
return '';
}
}
/**
* Inverts an SQL expressions with `NOT` operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the SQL expressions to connect.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function buildNotCondition($operator, $operands, &$params) {
if (count($operands) != 1) {
throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
}
$operand = reset($operands);
if (is_array($operand)) {
$operand = $this->buildCondition($operand, $params);
}
if ($operand === '') {
return '';
}
return "$operator ($operand)";
}
/**
* Creates an SQL expressions like `"column" operator value`.
* @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
* @param array $operands contains two column names.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function buildWildCondition($operator, $operands, &$params) {
$parts = [];
foreach ($operands as $operand) {
if (is_array($operand)) {
$operand = $this->buildCondition($operand, $params);
}
if ($operand !== '') {
$parts[] = $operand;
}
}
if (!empty($parts)) {
return implode(' ', $operands);
} else {
return '';
}
}
public function buildSimpleCondition($operator, $operands, &$params) {
return $operator . ' ' . implode(' ', $operands);
}
private function getDb() {
$modelClass = $this->modelClass;
return $modelClass::getDb();
}
/**
* get getSearch
* return \xj\xunsearch\XSSearch
*/
private function getSearch() {
return $this->getDb()->getSearch();
}
/**
*
* @param Databases $db
* @return type
*/
public function one($db = null) {
if ($db === null) {
$db = $this->getDb();
}
$search = $db->getSearch();
$this->limit(1);
$this->setCondition($search);
$docs = $search->search();
$rows = [];
foreach ($docs as $doc) {
if ($doc instanceof XSDocument) {
$rows[] = $doc->getFields();
}
}
if (!empty($rows)) {
$models = $this->createModels($rows);
if (!$this->asArray) {
foreach ($models as $model) {
$model->afterFind();
}
}
return $models[0];
}
return null;
}
public function all($db = null) {
if ($db === null) {
$db = $this->getDb();
}
$search = $db->getSearch();
$this->setCondition($search);
$docs = $search->search();
$rows = [];
foreach ($docs as $doc) {
if ($doc instanceof XSDocument) {
$rows[] = $doc->getFields();
}
}
if (!empty($rows)) {
$models = $this->createModels($rows);
if (!$this->asArray) {
foreach ($models as $model) {
$model->afterFind();
}
}
return $models;
}
return [];
}
public function count($q = '*', $db = null) {
if ($db === null) {
$db = $this->getDb();
}
$search = $db->getSearch();
$this->setCondition($search);
return $docs = $search->count();
}
public function exists($db = null) {
throw new NotSupportedException();
}
public function findFor($name, $model) {
throw new NotSupportedException();
}
public function via($relationName, callable $callable = null) {
throw new NotSupportedException();
}
}