1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Db\Exception\Exception; |
10
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
11
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
12
|
|
|
use Yiisoft\Db\Expression\Expression; |
13
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
14
|
|
|
use Yiisoft\Db\Query\Query; |
15
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
16
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
17
|
|
|
|
18
|
|
|
abstract class AbstractQueryTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
use TestTrait; |
21
|
|
|
|
22
|
|
|
public function testAddGroupByExpression(): void |
23
|
|
|
{ |
24
|
|
|
$db = $this->getConnection(); |
25
|
|
|
|
26
|
|
|
$expression = new Expression('[[id]] + 1'); |
27
|
|
|
$query = new Query($db); |
28
|
|
|
$query->addGroupBy($expression); |
29
|
|
|
|
30
|
|
|
$this->assertSame([$expression], $query->getGroupBy()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testAddOrderByEmpty(): void |
34
|
|
|
{ |
35
|
|
|
$db = $this->getConnection(); |
36
|
|
|
|
37
|
|
|
$query = new Query($db); |
38
|
|
|
$query->addOrderBy([]); |
39
|
|
|
|
40
|
|
|
$this->assertSame([], $query->getOrderBy()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testAddParamsWithNameInt(): void |
44
|
|
|
{ |
45
|
|
|
$db = $this->getConnection(); |
46
|
|
|
|
47
|
|
|
$query = new Query($db); |
48
|
|
|
$query->params([1 => 'value']); |
49
|
|
|
$query->addParams([2 => 'test']); |
50
|
|
|
|
51
|
|
|
$this->assertSame([1 => 'value', 2 => 'test'], $query->getParams()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @depends testFilterWhereWithHashFormat |
56
|
|
|
* @depends testFilterWhereWithOperatorFormat |
57
|
|
|
* |
58
|
|
|
* @throws NotSupportedException |
59
|
|
|
*/ |
60
|
|
|
public function testAndFilterCompare(): void |
61
|
|
|
{ |
62
|
|
|
$db = $this->getConnection(); |
63
|
|
|
|
64
|
|
|
$query = new Query($db); |
65
|
|
|
$result = $query->andFilterCompare('name', null); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
68
|
|
|
$this->assertNull($query->getWhere()); |
69
|
|
|
|
70
|
|
|
$query->andFilterCompare('name', ''); |
71
|
|
|
|
72
|
|
|
$this->assertNull($query->getWhere()); |
73
|
|
|
|
74
|
|
|
$query->andFilterCompare('name', 'John Doe'); |
75
|
|
|
$condition = ['=', 'name', 'John Doe']; |
76
|
|
|
|
77
|
|
|
$this->assertSame($condition, $query->getWhere()); |
78
|
|
|
|
79
|
|
|
$condition = ['and', $condition, ['like', 'name', 'Doe']]; |
80
|
|
|
$query->andFilterCompare('name', 'Doe', 'like'); |
81
|
|
|
|
82
|
|
|
$this->assertSame($condition, $query->getWhere()); |
83
|
|
|
|
84
|
|
|
$condition[] = ['>', 'rating', '9']; |
85
|
|
|
$query->andFilterCompare('rating', '>9'); |
86
|
|
|
|
87
|
|
|
$this->assertSame($condition, $query->getWhere()); |
88
|
|
|
|
89
|
|
|
$condition[] = ['<=', 'value', '100']; |
90
|
|
|
$query->andFilterCompare('value', '<=100'); |
91
|
|
|
|
92
|
|
|
$this->assertSame($condition, $query->getWhere()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws NotSupportedException |
97
|
|
|
*/ |
98
|
|
|
public function testAndFilterHaving(): void |
99
|
|
|
{ |
100
|
|
|
$db = $this->getConnection(); |
101
|
|
|
|
102
|
|
|
$query = new Query($db); |
103
|
|
|
$result = $query->andFilterHaving(['>', 'id', 1]); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
106
|
|
|
$this->assertSame(['>', 'id', 1], $query->getHaving()); |
107
|
|
|
|
108
|
|
|
$query->andFilterHaving(['>', 'id', 2]); |
109
|
|
|
|
110
|
|
|
$this->assertSame(['and', ['>', 'id', 1], ['>', 'id', 2]], $query->getHaving()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @throws NotSupportedException |
115
|
|
|
*/ |
116
|
|
|
public function testAndFilterHavingWithHashFormat(): void |
117
|
|
|
{ |
118
|
|
|
$db = $this->getConnection(); |
119
|
|
|
|
120
|
|
|
$query = new Query($db); |
121
|
|
|
$result = $query->andFilterHaving(['status' => 1]); |
122
|
|
|
|
123
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
124
|
|
|
$this->assertSame(['status' => 1], $query->getHaving()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @throws Exception |
129
|
|
|
* @throws InvalidConfigException |
130
|
|
|
* @throws Throwable |
131
|
|
|
*/ |
132
|
|
|
public function testEmulateExecution(): void |
133
|
|
|
{ |
134
|
|
|
$db = $this->getConnection(true); |
135
|
|
|
|
136
|
|
|
$rows = (new Query($db))->from('customer')->emulateExecution()->all(); |
137
|
|
|
|
138
|
|
|
$this->assertSame([], $rows); |
139
|
|
|
|
140
|
|
|
$row = (new Query($db))->from('customer')->emulateExecution()->one(); |
141
|
|
|
|
142
|
|
|
$this->assertNull($row); |
143
|
|
|
|
144
|
|
|
$exists = (new Query($db))->from('customer')->emulateExecution()->exists(); |
145
|
|
|
|
146
|
|
|
$this->assertFalse($exists); |
147
|
|
|
|
148
|
|
|
$count = (new Query($db))->from('customer')->emulateExecution()->count(); |
149
|
|
|
|
150
|
|
|
$this->assertSame(0, $count); |
151
|
|
|
|
152
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->sum('id'); |
153
|
|
|
|
154
|
|
|
$this->assertNull($sum); |
155
|
|
|
|
156
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->average('id'); |
157
|
|
|
|
158
|
|
|
$this->assertNull($sum); |
159
|
|
|
|
160
|
|
|
$max = (new Query($db))->from('customer')->emulateExecution()->max('id'); |
161
|
|
|
|
162
|
|
|
$this->assertNull($max); |
163
|
|
|
|
164
|
|
|
$min = (new Query($db))->from('customer')->emulateExecution()->min('id'); |
165
|
|
|
|
166
|
|
|
$this->assertNull($min); |
167
|
|
|
|
168
|
|
|
$scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar(); |
169
|
|
|
|
170
|
|
|
$this->assertNull($scalar); |
171
|
|
|
|
172
|
|
|
$column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column(); |
173
|
|
|
$this->assertSame([], $column); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @throws NotSupportedException |
178
|
|
|
*/ |
179
|
|
|
public function testFilterHavingWithHashFormat(): void |
180
|
|
|
{ |
181
|
|
|
$db = $this->getConnection(); |
182
|
|
|
|
183
|
|
|
$query = new Query($db); |
184
|
|
|
$query->filterHaving(['id' => 0, 'title' => ' ', 'author_ids' => []]); |
185
|
|
|
|
186
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
187
|
|
|
|
188
|
|
|
$query->andFilterHaving(['status' => null]); |
189
|
|
|
|
190
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
191
|
|
|
|
192
|
|
|
$query->orFilterHaving(['name' => '']); |
193
|
|
|
|
194
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @throws NotSupportedException |
199
|
|
|
*/ |
200
|
|
|
public function testFilterHavingWithOperatorFormat(): void |
201
|
|
|
{ |
202
|
|
|
$db = $this->getConnection(); |
203
|
|
|
|
204
|
|
|
$query = new Query($db); |
205
|
|
|
$condition = ['like', 'name', 'Alex']; |
206
|
|
|
$query->filterHaving($condition); |
207
|
|
|
|
208
|
|
|
$this->assertSame($condition, $query->getHaving()); |
209
|
|
|
|
210
|
|
|
$query->andFilterHaving(['between', 'id', null, null]); |
211
|
|
|
|
212
|
|
|
$this->assertSame($condition, $query->getHaving()); |
213
|
|
|
|
214
|
|
|
$query->orFilterHaving(['not between', 'id', null, null]); |
215
|
|
|
|
216
|
|
|
$this->assertSame($condition, $query->getHaving()); |
217
|
|
|
|
218
|
|
|
$query->andFilterHaving(['in', 'id', []]); |
219
|
|
|
|
220
|
|
|
$this->assertSame($condition, $query->getHaving()); |
221
|
|
|
|
222
|
|
|
$query->andFilterHaving(['not in', 'id', []]); |
223
|
|
|
|
224
|
|
|
$this->assertSame($condition, $query->getHaving()); |
225
|
|
|
|
226
|
|
|
$query->andFilterHaving(['like', 'id', '']); |
227
|
|
|
|
228
|
|
|
$this->assertSame($condition, $query->getHaving()); |
229
|
|
|
|
230
|
|
|
$query->andFilterHaving(['or like', 'id', '']); |
231
|
|
|
|
232
|
|
|
$this->assertSame($condition, $query->getHaving()); |
233
|
|
|
|
234
|
|
|
$query->andFilterHaving(['not like', 'id', ' ']); |
235
|
|
|
|
236
|
|
|
$this->assertSame($condition, $query->getHaving()); |
237
|
|
|
|
238
|
|
|
$query->andFilterHaving(['or not like', 'id', null]); |
239
|
|
|
|
240
|
|
|
$this->assertSame($condition, $query->getHaving()); |
241
|
|
|
|
242
|
|
|
$query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
243
|
|
|
|
244
|
|
|
$this->assertSame($condition, $query->getHaving()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @throws NotSupportedException |
249
|
|
|
*/ |
250
|
|
|
public function testFilterRecursively(): void |
251
|
|
|
{ |
252
|
|
|
$db = $this->getConnection(); |
253
|
|
|
|
254
|
|
|
$query = new Query($db); |
255
|
|
|
$query->filterWhere( |
256
|
|
|
['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]] |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$this->assertSame(['and', ['id' => 1]], $query->getWhere()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @throws NotSupportedException |
264
|
|
|
*/ |
265
|
|
|
public function testFilterWhereWithHashFormat(): void |
266
|
|
|
{ |
267
|
|
|
$db = $this->getConnection(); |
268
|
|
|
|
269
|
|
|
$query = new Query($db); |
270
|
|
|
$query->filterWhere(['id' => 0, 'title' => ' ', 'author_ids' => []]); |
271
|
|
|
|
272
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
273
|
|
|
|
274
|
|
|
$query->andFilterWhere(['status' => null]); |
275
|
|
|
|
276
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
277
|
|
|
|
278
|
|
|
$query->orFilterWhere(['name' => '']); |
279
|
|
|
|
280
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @throws NotSupportedException |
285
|
|
|
*/ |
286
|
|
|
public function testFilterWhereWithOperatorFormat(): void |
287
|
|
|
{ |
288
|
|
|
$db = $this->getConnection(); |
289
|
|
|
|
290
|
|
|
$query = new Query($db); |
291
|
|
|
$condition = ['like', 'name', 'Alex']; |
292
|
|
|
$query->filterWhere($condition); |
293
|
|
|
|
294
|
|
|
$this->assertSame($condition, $query->getWhere()); |
295
|
|
|
|
296
|
|
|
$query->andFilterWhere(['between', 'id', null, null]); |
297
|
|
|
|
298
|
|
|
$this->assertSame($condition, $query->getWhere()); |
299
|
|
|
|
300
|
|
|
$query->orFilterWhere(['not between', 'id', null, null]); |
301
|
|
|
|
302
|
|
|
$this->assertSame($condition, $query->getWhere()); |
303
|
|
|
|
304
|
|
|
$query->andFilterWhere(['in', 'id', []]); |
305
|
|
|
|
306
|
|
|
$this->assertSame($condition, $query->getWhere()); |
307
|
|
|
|
308
|
|
|
$query->andFilterWhere(['not in', 'id', []]); |
309
|
|
|
|
310
|
|
|
$this->assertSame($condition, $query->getWhere()); |
311
|
|
|
|
312
|
|
|
$query->andFilterWhere(['like', 'id', '']); |
313
|
|
|
|
314
|
|
|
$this->assertSame($condition, $query->getWhere()); |
315
|
|
|
|
316
|
|
|
$query->andFilterWhere(['or like', 'id', '']); |
317
|
|
|
|
318
|
|
|
$this->assertSame($condition, $query->getWhere()); |
319
|
|
|
|
320
|
|
|
$query->andFilterWhere(['not like', 'id', ' ']); |
321
|
|
|
|
322
|
|
|
$this->assertSame($condition, $query->getWhere()); |
323
|
|
|
|
324
|
|
|
$query->andFilterWhere(['or not like', 'id', null]); |
325
|
|
|
|
326
|
|
|
$this->assertSame($condition, $query->getWhere()); |
327
|
|
|
|
328
|
|
|
$query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
329
|
|
|
|
330
|
|
|
$this->assertSame($condition, $query->getWhere()); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testFrom(): void |
334
|
|
|
{ |
335
|
|
|
$db = $this->getConnection(); |
336
|
|
|
|
337
|
|
|
$query = new Query($db); |
338
|
|
|
$query->from('user'); |
339
|
|
|
|
340
|
|
|
$this->assertSame(['user'], $query->getFrom()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function testFromTableIsArrayWithExpression(): void |
344
|
|
|
{ |
345
|
|
|
$db = $this->getConnection(); |
346
|
|
|
|
347
|
|
|
$query = new Query($db); |
348
|
|
|
$tables = new Expression('(SELECT id,name FROM user) u'); |
349
|
|
|
$query->from($tables); |
350
|
|
|
$from = $query->getFrom(); |
351
|
|
|
|
352
|
|
|
$this->assertIsArray($from); |
353
|
|
|
$this->assertInstanceOf(ExpressionInterface::class, $from[0]); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testGroup(): void |
357
|
|
|
{ |
358
|
|
|
$db = $this->getConnection(); |
359
|
|
|
|
360
|
|
|
$query = new Query($db); |
361
|
|
|
$query->groupBy('team'); |
362
|
|
|
|
363
|
|
|
$this->assertSame(['team'], $query->getGroupBy()); |
364
|
|
|
|
365
|
|
|
$query->addGroupBy('company'); |
366
|
|
|
|
367
|
|
|
$this->assertSame(['team', 'company'], $query->getGroupBy()); |
368
|
|
|
|
369
|
|
|
$query->addGroupBy('age'); |
370
|
|
|
|
371
|
|
|
$this->assertSame(['team', 'company', 'age'], $query->getGroupBy()); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function testHaving(): void |
375
|
|
|
{ |
376
|
|
|
$db = $this->getConnection(); |
377
|
|
|
|
378
|
|
|
$query = new Query($db); |
379
|
|
|
$query->having('id = :id', [':id' => 1]); |
380
|
|
|
|
381
|
|
|
$this->assertSame('id = :id', $query->getHaving()); |
382
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
383
|
|
|
|
384
|
|
|
$query->andHaving('name = :name', [':name' => 'something']); |
385
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getHaving()); |
386
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
387
|
|
|
|
388
|
|
|
$query->orHaving('age = :age', [':age' => '30']); |
389
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving()); |
390
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function testJoin(): void |
394
|
|
|
{ |
395
|
|
|
$db = $this->getConnection(); |
396
|
|
|
|
397
|
|
|
$query = new Query($db); |
398
|
|
|
$query->join('INNER JOIN', 'profile', 'user.id = profile.user_id'); |
399
|
|
|
|
400
|
|
|
$this->assertSame([['INNER JOIN', 'profile', 'user.id = profile.user_id']], $query->getJoin()); |
401
|
|
|
|
402
|
|
|
$query->join('LEFT JOIN', 'order', 'user.id = order.user_id'); |
403
|
|
|
|
404
|
|
|
$this->assertSame( |
405
|
|
|
[['INNER JOIN', 'profile', 'user.id = profile.user_id'], ['LEFT JOIN', 'order', 'user.id = order.user_id']], |
406
|
|
|
$query->getJoin() |
407
|
|
|
); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function testLimitOffset(): void |
411
|
|
|
{ |
412
|
|
|
$db = $this->getConnection(); |
413
|
|
|
|
414
|
|
|
$query = new Query($db); |
415
|
|
|
$query->limit(10)->offset(5); |
416
|
|
|
|
417
|
|
|
$this->assertSame(10, $query->getLimit()); |
418
|
|
|
$this->assertSame(5, $query->getOffset()); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @throws NotSupportedException |
423
|
|
|
*/ |
424
|
|
|
public function testOrFilterHavingHashFormat(): void |
425
|
|
|
{ |
426
|
|
|
$db = $this->getConnection(); |
427
|
|
|
|
428
|
|
|
$query = new Query($db); |
429
|
|
|
$query->orFilterHaving(['status' => 1]); |
430
|
|
|
|
431
|
|
|
$this->assertSame(['status' => 1], $query->getHaving()); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @throws NotSupportedException |
436
|
|
|
*/ |
437
|
|
|
public function testOrFilterWhereHashFormat(): void |
438
|
|
|
{ |
439
|
|
|
$db = $this->getConnection(); |
440
|
|
|
|
441
|
|
|
$query = new Query($db); |
442
|
|
|
$query->orFilterWhere(['status' => 1]); |
443
|
|
|
|
444
|
|
|
$this->assertSame(['status' => 1], $query->getWhere()); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
public function testOrder(): void |
448
|
|
|
{ |
449
|
|
|
$db = $this->getConnection(); |
450
|
|
|
|
451
|
|
|
$query = new Query($db); |
452
|
|
|
$query->orderBy('team'); |
453
|
|
|
|
454
|
|
|
$this->assertSame(['team' => SORT_ASC], $query->getOrderBy()); |
455
|
|
|
|
456
|
|
|
$query->addOrderBy('company'); |
457
|
|
|
|
458
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy()); |
459
|
|
|
|
460
|
|
|
$query->addOrderBy('age'); |
461
|
|
|
|
462
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy()); |
463
|
|
|
|
464
|
|
|
$query->addOrderBy(['age' => SORT_DESC]); |
465
|
|
|
|
466
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy()); |
467
|
|
|
|
468
|
|
|
$query->addOrderBy('age ASC, company DESC'); |
469
|
|
|
|
470
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy()); |
471
|
|
|
|
472
|
|
|
$expression1 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
473
|
|
|
|
474
|
|
|
$query->orderBy($expression1); |
475
|
|
|
|
476
|
|
|
$this->assertSame([$expression1], $query->getOrderBy()); |
477
|
|
|
|
478
|
|
|
$expression2 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
479
|
|
|
|
480
|
|
|
$query->addOrderBy($expression2); |
481
|
|
|
|
482
|
|
|
$this->assertSame([$expression1, $expression2], $query->getOrderBy()); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
public function testRightJoin(): void |
486
|
|
|
{ |
487
|
|
|
$db = $this->getConnection(); |
488
|
|
|
|
489
|
|
|
$query = new Query($db); |
490
|
|
|
$query->rightJoin('profile', 'user.id = profile.user_id'); |
491
|
|
|
|
492
|
|
|
$this->assertSame([['RIGHT JOIN', 'profile', 'user.id = profile.user_id']], $query->getJoin()); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
public function testSelect(): void |
496
|
|
|
{ |
497
|
|
|
$db = $this->getConnection(); |
498
|
|
|
|
499
|
|
|
/* default */ |
500
|
|
|
$query = new Query($db); |
501
|
|
|
$query->select('*'); |
502
|
|
|
|
503
|
|
|
$this->assertSame(['*' => '*'], $query->getSelect()); |
504
|
|
|
$this->assertNull($query->getDistinct()); |
505
|
|
|
$this->assertNull($query->getSelectOption()); |
|
|
|
|
506
|
|
|
|
507
|
|
|
$query = new Query($db); |
508
|
|
|
$query->select('id, name', 'something')->distinct(); |
509
|
|
|
|
510
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name'], $query->getSelect()); |
511
|
|
|
$this->assertTrue($query->getDistinct()); |
512
|
|
|
$this->assertSame('something', $query->getSelectOption()); |
513
|
|
|
|
514
|
|
|
$query = new Query($db); |
515
|
|
|
$query->addSelect('email'); |
516
|
|
|
|
517
|
|
|
$this->assertSame(['email' => 'email'], $query->getSelect()); |
518
|
|
|
|
519
|
|
|
$query = new Query($db); |
520
|
|
|
$query->select('id, name'); |
521
|
|
|
$query->addSelect('email'); |
522
|
|
|
|
523
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect()); |
524
|
|
|
|
525
|
|
|
$query = new Query($db); |
526
|
|
|
$query->select('name, lastname'); |
527
|
|
|
$query->addSelect('name'); |
528
|
|
|
|
529
|
|
|
$this->assertSame(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect()); |
530
|
|
|
|
531
|
|
|
$query = new Query($db); |
532
|
|
|
$query->addSelect(['*', 'abc']); |
533
|
|
|
$query->addSelect(['*', 'bca']); |
534
|
|
|
|
535
|
|
|
$this->assertSame(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect()); |
536
|
|
|
|
537
|
|
|
$query = new Query($db); |
538
|
|
|
$query->addSelect(['field1 as a', 'field 1 as b']); |
539
|
|
|
|
540
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
541
|
|
|
|
542
|
|
|
$query = new Query($db); |
543
|
|
|
$query->addSelect(['field1 a', 'field 1 b']); |
544
|
|
|
|
545
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
546
|
|
|
|
547
|
|
|
$query = new Query($db); |
548
|
|
|
$query->select(['name' => 'firstname', 'lastname']); |
549
|
|
|
$query->addSelect(['firstname', 'surname' => 'lastname']); |
550
|
|
|
$query->addSelect(['firstname', 'lastname']); |
551
|
|
|
|
552
|
|
|
$this->assertSame( |
553
|
|
|
['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'], |
554
|
|
|
$query->getSelect(), |
555
|
|
|
); |
556
|
|
|
|
557
|
|
|
$query = new Query($db); |
558
|
|
|
$query->select('name, name, name as X, name as X'); |
559
|
|
|
|
560
|
|
|
$this->assertSame(['name' => 'name', 'X' => 'name'], $query->getSelect()); |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
564
|
|
|
*/ |
565
|
|
|
$query = (new Query($db))->select('id'); |
566
|
|
|
|
567
|
|
|
$this->assertSame(['id' => 'id'], $query->getSelect()); |
568
|
|
|
|
569
|
|
|
$query->select(['id', 'brand_id']); |
570
|
|
|
|
571
|
|
|
$this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect()); |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
575
|
|
|
*/ |
576
|
|
|
$query = (new Query($db))->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']); |
577
|
|
|
|
578
|
|
|
$this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect()); |
579
|
|
|
|
580
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
581
|
|
|
|
582
|
|
|
$this->assertSame( |
583
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
584
|
|
|
$query->getSelect() |
585
|
|
|
); |
586
|
|
|
|
587
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
588
|
|
|
|
589
|
|
|
$this->assertSame( |
590
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
591
|
|
|
$query->getSelect() |
592
|
|
|
); |
593
|
|
|
|
594
|
|
|
$query->addSelect(['test' => 'LEFT(name,7)']); |
595
|
|
|
|
596
|
|
|
$this->assertSame( |
597
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
598
|
|
|
$query->getSelect() |
599
|
|
|
); |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15731} |
603
|
|
|
*/ |
604
|
|
|
$selectedCols = [ |
605
|
|
|
'total_sum' => 'SUM(f.amount)', |
606
|
|
|
'in_sum' => 'SUM(IF(f.type = :type_in, f.amount, 0))', |
607
|
|
|
'out_sum' => 'SUM(IF(f.type = :type_out, f.amount, 0))', |
608
|
|
|
]; |
609
|
|
|
$query = (new Query($db)) |
610
|
|
|
->select($selectedCols) |
611
|
|
|
->addParams([':type_in' => 'in', ':type_out' => 'out', ':type_partner' => 'partner']); |
612
|
|
|
|
613
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
614
|
|
|
|
615
|
|
|
$query->select($selectedCols); |
616
|
|
|
|
617
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/17384} |
621
|
|
|
*/ |
622
|
|
|
$query = new Query($db); |
623
|
|
|
$query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id'); |
624
|
|
|
|
625
|
|
|
$this->assertSame( |
626
|
|
|
['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'], |
627
|
|
|
$query->getSelect() |
628
|
|
|
); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
public function testSetJoin(): void |
632
|
|
|
{ |
633
|
|
|
$db = $this->getConnection(); |
634
|
|
|
|
635
|
|
|
$query = new Query($db); |
636
|
|
|
$query->setJoin(['INNER JOIN', 'table1', 'table1.id = table2.id']); |
637
|
|
|
|
638
|
|
|
$this->assertSame(['INNER JOIN', 'table1', 'table1.id = table2.id'], $query->getJoin()); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
public function testSetUnion(): void |
642
|
|
|
{ |
643
|
|
|
$db = $this->getConnection(); |
644
|
|
|
|
645
|
|
|
$query = new Query($db); |
646
|
|
|
$query->setUnion(['SELECT * FROM table1', 'SELECT * FROM table2']); |
647
|
|
|
|
648
|
|
|
$this->assertSame(['SELECT * FROM table1', 'SELECT * FROM table2'], $query->getUnion()); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
public function testShouldEmulateExecution(): void |
652
|
|
|
{ |
653
|
|
|
$db = $this->getConnection(); |
654
|
|
|
|
655
|
|
|
$query = new Query($db); |
656
|
|
|
$this->assertFalse($query->shouldEmulateExecution()); |
657
|
|
|
|
658
|
|
|
$query = new Query($db); |
659
|
|
|
$query->emulateExecution(); |
660
|
|
|
|
661
|
|
|
$this->assertTrue($query->shouldEmulateExecution()); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
public function testToString(): void |
665
|
|
|
{ |
666
|
|
|
$db = $this->getConnection(); |
667
|
|
|
|
668
|
|
|
$query = new Query($db); |
669
|
|
|
$query->select('id')->from('user')->where(['id' => 1]); |
670
|
|
|
|
671
|
|
|
$this->assertSame(serialize($query), (string) $query); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
public function testWhere(): void |
675
|
|
|
{ |
676
|
|
|
$db = $this->getConnection(); |
677
|
|
|
|
678
|
|
|
$query = new Query($db); |
679
|
|
|
$query->where('id = :id', [':id' => 1]); |
680
|
|
|
|
681
|
|
|
$this->assertSame('id = :id', $query->getWhere()); |
682
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
683
|
|
|
|
684
|
|
|
$query->andWhere('name = :name', [':name' => 'something']); |
685
|
|
|
|
686
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getWhere()); |
687
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
688
|
|
|
|
689
|
|
|
$query->orWhere('age = :age', [':age' => '30']); |
690
|
|
|
|
691
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere()); |
692
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
public function testWithQueries(): void |
696
|
|
|
{ |
697
|
|
|
$db = $this->getConnection(); |
698
|
|
|
|
699
|
|
|
$query = new Query($db); |
700
|
|
|
$query->withQueries(['query1', 'query2']); |
701
|
|
|
|
702
|
|
|
$this->assertSame(['query1', 'query2'], $query->getWithQueries()); |
703
|
|
|
} |
704
|
|
|
} |
705
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.