-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathQuery.php
More file actions
376 lines (335 loc) · 9.34 KB
/
Query.php
File metadata and controls
376 lines (335 loc) · 9.34 KB
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\db;
use ArrayAccess;
use ArrayIterator;
use craft\base\ClonefixTrait;
use craft\events\DefineBehaviorsEvent;
use craft\helpers\ArrayHelper;
use Illuminate\Support\Collection;
use IteratorAggregate;
use yii\base\Exception;
use yii\base\NotSupportedException;
use yii\base\UnknownPropertyException;
use yii\db\Connection as YiiConnection;
/**
* Class Query
*
* @template TKey of array-key
* @template TValue
* @implements ArrayAccess<TKey, TValue>
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class Query extends \yii\db\Query implements ArrayAccess, IteratorAggregate
{
use ClonefixTrait;
/**
* @event \yii\base\Event The event that is triggered after the query's init cycle
* @see init()
*/
public const EVENT_INIT = 'init';
/**
* @event DefineBehaviorsEvent The event that is triggered when defining the class behaviors
* @see behaviors()
*/
public const EVENT_DEFINE_BEHAVIORS = 'defineBehaviors';
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if ($this->hasEventHandlers(self::EVENT_INIT)) {
$this->trigger(self::EVENT_INIT);
}
}
/**
* Required by the IteratorAggregate interface.
*
* @return ArrayIterator
* @since 4.2.0
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->all());
}
/**
* Required by the ArrayAccess interface.
*
* @param mixed $offset
* @return bool
* @since 4.2.0
*/
public function offsetExists(mixed $offset): bool
{
if (is_numeric($offset)) {
$offset = $this->offset;
$limit = $this->limit;
$this->offset = $offset;
$this->limit = 1;
$exists = $this->exists();
$this->offset = $offset;
$this->limit = $limit;
return $exists;
}
return $this->__isset($offset);
}
/**
* Required by the ArrayAccess interface.
*
* @param mixed $offset
* @return mixed
* @throws UnknownPropertyException
* @since 4.2.0
*/
public function offsetGet(mixed $offset): mixed
{
if (is_numeric($offset)) {
$element = $this->nth($offset);
if ($element) {
return $element;
}
}
return $this->__get($offset);
}
/**
* Required by the ArrayAccess interface.
*
* @param mixed $offset
* @param mixed $value
* @throws NotSupportedException
* @throws UnknownPropertyException
* @since 4.2.0
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (is_numeric($offset)) {
throw new NotSupportedException('Queries do not support setting values using array syntax.');
}
$this->__set($offset, $value);
}
/**
* Required by the ArrayAccess interface.
*
* @param mixed $offset
* @return void
* @throws NotSupportedException
* @since 4.2.0
*/
public function offsetUnset(mixed $offset): void
{
if (is_numeric($offset)) {
throw new NotSupportedException('Queries do not support unsetting values using array syntax.');
}
$this->__unset($offset);
}
/**
* @inheritdoc
*/
public function behaviors(): array
{
// Fire a 'defineBehaviors' event
$event = new DefineBehaviorsEvent();
$this->trigger(self::EVENT_DEFINE_BEHAVIORS, $event);
return $event->behaviors;
}
/**
* Returns whether a given table has been joined in this query.
*
* @param string $table
* @return bool
*/
public function isJoined(string $table): bool
{
foreach ($this->join as $join) {
if ($join[1] === $table || str_starts_with($join[1], $table)) {
return true;
}
}
return false;
}
/**
* @inheritdoc
*/
public function where($condition, $params = []): static
{
if (!$condition) {
$condition = null;
}
return parent::where($condition, $params);
}
/**
* @inheritdoc
*/
public function andWhere($condition, $params = []): static
{
if (!$condition) {
return $this;
}
return parent::andWhere($condition, $params);
}
/**
* @inheritdoc
*/
public function orWhere($condition, $params = []): static
{
if (!$condition) {
return $this;
}
return parent::orWhere($condition, $params);
}
// Execution functions
// -------------------------------------------------------------------------
/**
* Executes the query and returns the first two columns in the results as key/value pairs.
*
* @param YiiConnection|null $db The database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return array the query results. If the query results in nothing, an empty array will be returned.
* @throws Exception if less than two columns were selected
*/
public function pairs(?YiiConnection $db = null): array
{
try {
$rows = $this->createCommand($db)->queryAll();
} catch (QueryAbortedException) {
return [];
}
if (!empty($rows)) {
$columns = array_keys($rows[0]);
if (count($columns) < 2) {
throw new Exception('Less than two columns were selected');
}
$rows = ArrayHelper::map($rows, $columns[0], $columns[1]);
}
return $rows;
}
/**
* @inheritdoc
* @return array<TKey,TValue>
*/
public function all($db = null): array
{
try {
return parent::all($db);
} catch (QueryAbortedException) {
return [];
}
}
/**
* Executes the query and returns all results as a collection.
*
* @param YiiConnection|null $db The database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return Collection<TKey,TValue> A collection of the resulting elements.
* @since 4.0.0
*/
public function collect(?YiiConnection $db = null): Collection
{
return Collection::make($this->all($db));
}
/**
* @inheritdoc
* @return TValue|null
*/
public function one($db = null): mixed
{
$limit = $this->limit;
$this->limit = 1;
try {
$result = parent::one($db);
// Be more like Yii 2.1
if ($result === false) {
$result = null;
}
} catch (QueryAbortedException) {
$result = null;
}
$this->limit = $limit;
return $result;
}
/**
* @inheritdoc
*/
public function scalar($db = null): bool|int|string|null
{
$limit = $this->limit;
$this->limit = 1;
try {
$result = parent::scalar($db);
} catch (QueryAbortedException) {
$result = false;
}
$this->limit = $limit;
return $result;
}
/**
* @inheritdoc
*/
public function column($db = null): array
{
try {
return parent::column($db);
} catch (QueryAbortedException) {
return [];
}
}
/**
* @inheritdoc
*/
public function exists($db = null): bool
{
try {
return parent::exists($db);
} catch (QueryAbortedException) {
return false;
}
}
/**
* Executes the query and returns a single row of result at a given offset.
*
* @param int $n The offset of the row to return. If [[offset]] is set, $offset will be added to it.
* @param YiiConnection|null $db The database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return mixed The row (in terms of an array) of the query result. Null is returned if the query
* results in nothing.
*/
public function nth(int $n, ?YiiConnection $db = null): mixed
{
$offset = $this->offset;
$this->offset = ($offset ?: 0) + $n;
$result = $this->one($db);
$this->offset = $offset;
return $result;
}
/**
* Shortcut for `createCommand()->getRawSql()`.
*
* @param YiiConnection|null $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return string
* @see createCommand()
* @see \yii\db\Command::getRawSql()
*/
public function getRawSql(?YiiConnection $db = null): string
{
return $this->createCommand($db)->getRawSql();
}
/**
* @inheritdoc
*/
protected function queryScalar($selectExpression, $db): bool|string|null
{
try {
return parent::queryScalar($selectExpression, $db);
} catch (QueryAbortedException) {
return false;
}
}
}