1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\TestSupport; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
10
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
11
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
12
|
|
|
use Yiisoft\Db\Expression\Expression; |
13
|
|
|
use Yiisoft\Db\Query\Query; |
14
|
|
|
use Yiisoft\Db\Schema\Schema; |
15
|
|
|
|
16
|
|
|
trait TestQueryTrait |
17
|
|
|
{ |
18
|
|
|
use GetTablesAliasTestTrait; |
19
|
|
|
|
20
|
|
|
public function testSelect(): void |
21
|
|
|
{ |
22
|
|
|
$db = $this->getConnection(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
/* default */ |
25
|
|
|
$query = new Query($db); |
26
|
|
|
|
27
|
|
|
$query->select('*'); |
28
|
|
|
|
29
|
|
|
$this->assertEquals(['*' => '*'], $query->getSelect()); |
|
|
|
|
30
|
|
|
$this->assertNull($query->getDistinct()); |
|
|
|
|
31
|
|
|
$this->assertNull($query->getSelectOption()); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$query = new Query($db); |
34
|
|
|
|
35
|
|
|
$query->select('id, name', 'something')->distinct(true); |
36
|
|
|
|
37
|
|
|
$this->assertEquals(['id' => 'id', 'name' => 'name'], $query->getSelect()); |
38
|
|
|
$this->assertTrue($query->getDistinct()); |
|
|
|
|
39
|
|
|
$this->assertEquals('something', $query->getSelectOption()); |
40
|
|
|
|
41
|
|
|
$query = new Query($db); |
42
|
|
|
|
43
|
|
|
$query->addSelect('email'); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(['email' => 'email'], $query->getSelect()); |
46
|
|
|
|
47
|
|
|
$query = new Query($db); |
48
|
|
|
|
49
|
|
|
$query->select('id, name'); |
50
|
|
|
$query->addSelect('email'); |
51
|
|
|
|
52
|
|
|
$this->assertEquals(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect()); |
53
|
|
|
|
54
|
|
|
$query = new Query($db); |
55
|
|
|
|
56
|
|
|
$query->select('name, lastname'); |
57
|
|
|
$query->addSelect('name'); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect()); |
60
|
|
|
|
61
|
|
|
$query = new Query($db); |
62
|
|
|
|
63
|
|
|
$query->addSelect(['*', 'abc']); |
64
|
|
|
$query->addSelect(['*', 'bca']); |
65
|
|
|
|
66
|
|
|
$this->assertEquals(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect()); |
67
|
|
|
|
68
|
|
|
$query = new Query($db); |
69
|
|
|
|
70
|
|
|
$query->addSelect(['field1 as a', 'field 1 as b']); |
71
|
|
|
|
72
|
|
|
$this->assertEquals(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
73
|
|
|
|
74
|
|
|
$query = new Query($db); |
75
|
|
|
|
76
|
|
|
$query->addSelect(['field1 a', 'field 1 b']); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
79
|
|
|
|
80
|
|
|
$query = new Query($db); |
81
|
|
|
|
82
|
|
|
$query->select(['name' => 'firstname', 'lastname']); |
83
|
|
|
$query->addSelect(['firstname', 'surname' => 'lastname']); |
84
|
|
|
$query->addSelect(['firstname', 'lastname']); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( |
87
|
|
|
['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'], |
88
|
|
|
$query->getSelect() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$query = new Query($db); |
92
|
|
|
|
93
|
|
|
$query->select('name, name, name as X, name as X'); |
94
|
|
|
|
95
|
|
|
$this->assertEquals(['name' => 'name', 'X' => 'name'], $query->getSelect()); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
99
|
|
|
*/ |
100
|
|
|
$query = (new Query($db))->select('id'); |
101
|
|
|
|
102
|
|
|
$this->assertSame(['id' => 'id'], $query->getSelect()); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$query->select(['id', 'brand_id']); |
105
|
|
|
|
106
|
|
|
$this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect()); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
110
|
|
|
*/ |
111
|
|
|
$query = (new Query($db)) |
112
|
|
|
->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']); |
113
|
|
|
|
114
|
|
|
$this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect()); |
115
|
|
|
|
116
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
117
|
|
|
|
118
|
|
|
$this->assertSame( |
119
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
120
|
|
|
$query->getSelect() |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
124
|
|
|
|
125
|
|
|
$this->assertSame( |
126
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
127
|
|
|
$query->getSelect() |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$query->addSelect(['test' => 'LEFT(name,7)']); |
131
|
|
|
|
132
|
|
|
$this->assertSame( |
133
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
134
|
|
|
$query->getSelect() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15731} |
139
|
|
|
*/ |
140
|
|
|
$selectedCols = [ |
141
|
|
|
'total_sum' => 'SUM(f.amount)', |
142
|
|
|
'in_sum' => 'SUM(IF(f.type = :type_in, f.amount, 0))', |
143
|
|
|
'out_sum' => 'SUM(IF(f.type = :type_out, f.amount, 0))', |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
$query = (new Query($db))->select($selectedCols)->addParams([ |
147
|
|
|
':type_in' => 'in', |
148
|
|
|
':type_out' => 'out', |
149
|
|
|
':type_partner' => 'partner', |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
153
|
|
|
|
154
|
|
|
$query->select($selectedCols); |
155
|
|
|
|
156
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/17384} |
160
|
|
|
*/ |
161
|
|
|
$query = new Query($db); |
162
|
|
|
|
163
|
|
|
$query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id'); |
164
|
|
|
|
165
|
|
|
$this->assertEquals( |
166
|
|
|
['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'], |
167
|
|
|
$query->getSelect() |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testFrom(): void |
172
|
|
|
{ |
173
|
|
|
$db = $this->getConnection(); |
174
|
|
|
|
175
|
|
|
$query = new Query($db); |
176
|
|
|
|
177
|
|
|
$query->from('user'); |
178
|
|
|
|
179
|
|
|
$this->assertEquals(['user'], $query->getFrom()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testFromTableIsArrayWithExpression(): void |
183
|
|
|
{ |
184
|
|
|
$db = $this->getConnection(); |
185
|
|
|
|
186
|
|
|
$query = new Query($db); |
187
|
|
|
|
188
|
|
|
$tables = new Expression('(SELECT id,name FROM user) u'); |
189
|
|
|
|
190
|
|
|
$query->from($tables); |
191
|
|
|
|
192
|
|
|
$this->assertInstanceOf(Expression::class, $query->getFrom()[0]); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function createQuery(): Query |
196
|
|
|
{ |
197
|
|
|
return new Query($this->getConnection()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testWhere(): void |
201
|
|
|
{ |
202
|
|
|
$db = $this->getConnection(); |
203
|
|
|
|
204
|
|
|
$query = new Query($db); |
205
|
|
|
|
206
|
|
|
$query->where('id = :id', [':id' => 1]); |
207
|
|
|
|
208
|
|
|
$this->assertEquals('id = :id', $query->getWhere()); |
209
|
|
|
$this->assertEquals([':id' => 1], $query->getParams()); |
210
|
|
|
|
211
|
|
|
$query->andWhere('name = :name', [':name' => 'something']); |
212
|
|
|
|
213
|
|
|
$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->getWhere()); |
214
|
|
|
$this->assertEquals([':id' => 1, ':name' => 'something'], $query->getParams()); |
215
|
|
|
|
216
|
|
|
$query->orWhere('age = :age', [':age' => '30']); |
217
|
|
|
|
218
|
|
|
$this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere()); |
219
|
|
|
$this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testFilterWhereWithHashFormat(): void |
223
|
|
|
{ |
224
|
|
|
$db = $this->getConnection(); |
225
|
|
|
|
226
|
|
|
$query = new Query($db); |
227
|
|
|
|
228
|
|
|
$query->filterWhere([ |
229
|
|
|
'id' => 0, |
230
|
|
|
'title' => ' ', |
231
|
|
|
'author_ids' => [], |
232
|
|
|
]); |
233
|
|
|
|
234
|
|
|
$this->assertEquals(['id' => 0], $query->getWhere()); |
235
|
|
|
|
236
|
|
|
$query->andFilterWhere(['status' => null]); |
237
|
|
|
|
238
|
|
|
$this->assertEquals(['id' => 0], $query->getWhere()); |
239
|
|
|
|
240
|
|
|
$query->orFilterWhere(['name' => '']); |
241
|
|
|
|
242
|
|
|
$this->assertEquals(['id' => 0], $query->getWhere()); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testFilterWhereWithOperatorFormat(): void |
246
|
|
|
{ |
247
|
|
|
$db = $this->getConnection(); |
248
|
|
|
|
249
|
|
|
$query = new Query($db); |
250
|
|
|
|
251
|
|
|
$condition = ['like', 'name', 'Alex']; |
252
|
|
|
|
253
|
|
|
$query->filterWhere($condition); |
254
|
|
|
|
255
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
256
|
|
|
|
257
|
|
|
$query->andFilterWhere(['between', 'id', null, null]); |
258
|
|
|
|
259
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
260
|
|
|
|
261
|
|
|
$query->orFilterWhere(['not between', 'id', null, null]); |
262
|
|
|
|
263
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
264
|
|
|
|
265
|
|
|
$query->andFilterWhere(['in', 'id', []]); |
266
|
|
|
|
267
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
268
|
|
|
|
269
|
|
|
$query->andFilterWhere(['not in', 'id', []]); |
270
|
|
|
|
271
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
272
|
|
|
|
273
|
|
|
$query->andFilterWhere(['like', 'id', '']); |
274
|
|
|
|
275
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
276
|
|
|
|
277
|
|
|
$query->andFilterWhere(['or like', 'id', '']); |
278
|
|
|
|
279
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
280
|
|
|
|
281
|
|
|
$query->andFilterWhere(['not like', 'id', ' ']); |
282
|
|
|
|
283
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
284
|
|
|
|
285
|
|
|
$query->andFilterWhere(['or not like', 'id', null]); |
286
|
|
|
|
287
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
288
|
|
|
|
289
|
|
|
$query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
290
|
|
|
|
291
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testFilterHavingWithHashFormat(): void |
295
|
|
|
{ |
296
|
|
|
$db = $this->getConnection(); |
297
|
|
|
|
298
|
|
|
$query = new Query($db); |
299
|
|
|
|
300
|
|
|
$query->filterHaving([ |
301
|
|
|
'id' => 0, |
302
|
|
|
'title' => ' ', |
303
|
|
|
'author_ids' => [], |
304
|
|
|
]); |
305
|
|
|
|
306
|
|
|
$this->assertEquals(['id' => 0], $query->getHaving()); |
307
|
|
|
|
308
|
|
|
$query->andFilterHaving(['status' => null]); |
309
|
|
|
|
310
|
|
|
$this->assertEquals(['id' => 0], $query->getHaving()); |
311
|
|
|
|
312
|
|
|
$query->orFilterHaving(['name' => '']); |
313
|
|
|
|
314
|
|
|
$this->assertEquals(['id' => 0], $query->getHaving()); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function testFilterHavingWithOperatorFormat(): void |
318
|
|
|
{ |
319
|
|
|
$db = $this->getConnection(); |
320
|
|
|
|
321
|
|
|
$query = new Query($db); |
322
|
|
|
|
323
|
|
|
$condition = ['like', 'name', 'Alex']; |
324
|
|
|
|
325
|
|
|
$query->filterHaving($condition); |
326
|
|
|
|
327
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
328
|
|
|
|
329
|
|
|
$query->andFilterHaving(['between', 'id', null, null]); |
330
|
|
|
|
331
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
332
|
|
|
|
333
|
|
|
$query->orFilterHaving(['not between', 'id', null, null]); |
334
|
|
|
|
335
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
336
|
|
|
|
337
|
|
|
$query->andFilterHaving(['in', 'id', []]); |
338
|
|
|
|
339
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
340
|
|
|
|
341
|
|
|
$query->andFilterHaving(['not in', 'id', []]); |
342
|
|
|
|
343
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
344
|
|
|
|
345
|
|
|
$query->andFilterHaving(['like', 'id', '']); |
346
|
|
|
|
347
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
348
|
|
|
|
349
|
|
|
$query->andFilterHaving(['or like', 'id', '']); |
350
|
|
|
|
351
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
352
|
|
|
|
353
|
|
|
$query->andFilterHaving(['not like', 'id', ' ']); |
354
|
|
|
|
355
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
356
|
|
|
|
357
|
|
|
$query->andFilterHaving(['or not like', 'id', null]); |
358
|
|
|
|
359
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
360
|
|
|
|
361
|
|
|
$query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
362
|
|
|
|
363
|
|
|
$this->assertEquals($condition, $query->getHaving()); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function testFilterRecursively(): void |
367
|
|
|
{ |
368
|
|
|
$db = $this->getConnection(); |
369
|
|
|
|
370
|
|
|
$query = new Query($db); |
371
|
|
|
|
372
|
|
|
$query->filterWhere( |
373
|
|
|
['and', ['like', 'name', ''], |
374
|
|
|
['like', 'title', ''], |
375
|
|
|
['id' => 1], |
376
|
|
|
['not', |
377
|
|
|
['like', 'name', ''], ], ] |
378
|
|
|
); |
379
|
|
|
|
380
|
|
|
$this->assertEquals(['and', ['id' => 1]], $query->getWhere()); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function testGroup(): void |
384
|
|
|
{ |
385
|
|
|
$db = $this->getConnection(); |
386
|
|
|
|
387
|
|
|
$query = new Query($db); |
388
|
|
|
|
389
|
|
|
$query->groupBy('team'); |
390
|
|
|
|
391
|
|
|
$this->assertEquals(['team'], $query->getGroupBy()); |
392
|
|
|
|
393
|
|
|
$query->addGroupBy('company'); |
394
|
|
|
|
395
|
|
|
$this->assertEquals(['team', 'company'], $query->getGroupBy()); |
396
|
|
|
|
397
|
|
|
$query->addGroupBy('age'); |
398
|
|
|
|
399
|
|
|
$this->assertEquals(['team', 'company', 'age'], $query->getGroupBy()); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function testHaving(): void |
403
|
|
|
{ |
404
|
|
|
$db = $this->getConnection(); |
405
|
|
|
|
406
|
|
|
$query = new Query($db); |
407
|
|
|
|
408
|
|
|
$query->having('id = :id', [':id' => 1]); |
409
|
|
|
|
410
|
|
|
$this->assertEquals('id = :id', $query->getHaving()); |
411
|
|
|
$this->assertEquals([':id' => 1], $query->getParams()); |
412
|
|
|
|
413
|
|
|
$query->andHaving('name = :name', [':name' => 'something']); |
414
|
|
|
$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->getHaving()); |
415
|
|
|
$this->assertEquals([':id' => 1, ':name' => 'something'], $query->getParams()); |
416
|
|
|
|
417
|
|
|
$query->orHaving('age = :age', [':age' => '30']); |
418
|
|
|
$this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving()); |
419
|
|
|
$this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function testOrder(): void |
423
|
|
|
{ |
424
|
|
|
$db = $this->getConnection(); |
425
|
|
|
|
426
|
|
|
$query = new Query($db); |
427
|
|
|
|
428
|
|
|
$query->orderBy('team'); |
429
|
|
|
|
430
|
|
|
$this->assertEquals(['team' => SORT_ASC], $query->getOrderBy()); |
431
|
|
|
|
432
|
|
|
$query->addOrderBy('company'); |
433
|
|
|
|
434
|
|
|
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy()); |
435
|
|
|
|
436
|
|
|
$query->addOrderBy('age'); |
437
|
|
|
|
438
|
|
|
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy()); |
439
|
|
|
|
440
|
|
|
$query->addOrderBy(['age' => SORT_DESC]); |
441
|
|
|
|
442
|
|
|
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy()); |
443
|
|
|
|
444
|
|
|
$query->addOrderBy('age ASC, company DESC'); |
445
|
|
|
|
446
|
|
|
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy()); |
447
|
|
|
|
448
|
|
|
$expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
449
|
|
|
|
450
|
|
|
$query->orderBy($expression); |
451
|
|
|
|
452
|
|
|
$this->assertEquals([$expression], $query->getOrderBy()); |
453
|
|
|
|
454
|
|
|
$expression = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
455
|
|
|
|
456
|
|
|
$query->addOrderBy($expression); |
457
|
|
|
|
458
|
|
|
$this->assertEquals([$expression, $expression], $query->getOrderBy()); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function testLimitOffset(): void |
462
|
|
|
{ |
463
|
|
|
$db = $this->getConnection(); |
464
|
|
|
|
465
|
|
|
$query = new Query($db); |
466
|
|
|
|
467
|
|
|
$query->limit(10)->offset(5); |
468
|
|
|
|
469
|
|
|
$this->assertEquals(10, $query->getLimit()); |
470
|
|
|
$this->assertEquals(5, $query->getOffset()); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
public function testLimitOffsetWithExpression(): void |
474
|
|
|
{ |
475
|
|
|
$db = $this->getConnection(); |
476
|
|
|
|
477
|
|
|
$query = (new Query($db))->from('customer')->select('id')->orderBy('id'); |
478
|
|
|
|
479
|
|
|
$query |
480
|
|
|
->limit(new Expression('1 + 1')) |
481
|
|
|
->offset(new Expression('1 + 0')); |
482
|
|
|
|
483
|
|
|
$result = $query->column(); |
484
|
|
|
|
485
|
|
|
$this->assertCount(2, $result); |
|
|
|
|
486
|
|
|
|
487
|
|
|
if ($db->getDriverName() !== 'sqlsrv' && $db->getDriverName() !== 'oci') { |
488
|
|
|
$this->assertContains(2, $result); |
|
|
|
|
489
|
|
|
$this->assertContains(3, $result); |
490
|
|
|
} else { |
491
|
|
|
$this->assertContains('2', $result); |
492
|
|
|
$this->assertContains('3', $result); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
$this->assertNotContains(1, $result); |
|
|
|
|
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
public function testOne(): void |
499
|
|
|
{ |
500
|
|
|
$db = $this->getConnection(true); |
501
|
|
|
|
502
|
|
|
$result = (new Query($db))->from('customer')->where(['status' => 2])->one(); |
503
|
|
|
|
504
|
|
|
$this->assertEquals('user3', $result['name']); |
505
|
|
|
|
506
|
|
|
$result = (new Query($db))->from('customer')->where(['status' => 3])->one(); |
507
|
|
|
|
508
|
|
|
$this->assertFalse($result); |
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
public function testExists(): void |
512
|
|
|
{ |
513
|
|
|
$db = $this->getConnection(); |
514
|
|
|
|
515
|
|
|
$result = (new Query($db))->from('customer')->where(['status' => 2])->exists(); |
516
|
|
|
|
517
|
|
|
$this->assertTrue($result); |
518
|
|
|
|
519
|
|
|
$result = (new Query($db))->from('customer')->where(['status' => 3])->exists(); |
520
|
|
|
|
521
|
|
|
$this->assertFalse($result); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
public function testColumn(): void |
525
|
|
|
{ |
526
|
|
|
$db = $this->getConnection(); |
527
|
|
|
|
528
|
|
|
$result = (new Query($db)) |
529
|
|
|
->select('name') |
530
|
|
|
->from('customer') |
531
|
|
|
->orderBy(['id' => SORT_DESC]) |
532
|
|
|
->column(); |
533
|
|
|
|
534
|
|
|
$this->assertEquals(['user3', 'user2', 'user1'], $result); |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/7515} |
538
|
|
|
*/ |
539
|
|
|
$result = (new Query($db))->from('customer') |
540
|
|
|
->select('name') |
541
|
|
|
->orderBy(['id' => SORT_DESC]) |
542
|
|
|
->indexBy('id') |
543
|
|
|
->column(); |
544
|
|
|
|
545
|
|
|
$this->assertEquals([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result); |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/12649} |
549
|
|
|
*/ |
550
|
|
|
$result = (new Query($db))->from('customer') |
551
|
|
|
->select(['name', 'id']) |
552
|
|
|
->orderBy(['id' => SORT_DESC]) |
553
|
|
|
->indexBy(function ($row) { |
554
|
|
|
return $row['id'] * 2; |
555
|
|
|
}) |
556
|
|
|
->column(); |
557
|
|
|
|
558
|
|
|
$this->assertEquals([6 => 'user3', 4 => 'user2', 2 => 'user1'], $result); |
559
|
|
|
|
560
|
|
|
$result = (new Query($db))->from('customer') |
561
|
|
|
->select(['name']) |
562
|
|
|
->indexBy('name') |
563
|
|
|
->orderBy(['id' => SORT_DESC]) |
564
|
|
|
->column($db); |
|
|
|
|
565
|
|
|
|
566
|
|
|
$this->assertEquals(['user3' => 'user3', 'user2' => 'user2', 'user1' => 'user1'], $result); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
public function testCount(): void |
570
|
|
|
{ |
571
|
|
|
$db = $this->getConnection(); |
572
|
|
|
|
573
|
|
|
$count = (new Query($db))->from('customer')->count('*'); |
574
|
|
|
|
575
|
|
|
$this->assertEquals(3, $count); |
576
|
|
|
|
577
|
|
|
$count = (new Query($db))->from('customer')->where(['status' => 2])->count('*'); |
578
|
|
|
|
579
|
|
|
$this->assertEquals(1, $count); |
580
|
|
|
|
581
|
|
|
$count = (new Query($db)) |
582
|
|
|
->select('[[status]], COUNT([[id]]) cnt') |
583
|
|
|
->from('customer') |
584
|
|
|
->groupBy('status') |
585
|
|
|
->count('*'); |
586
|
|
|
|
587
|
|
|
$this->assertEquals(2, $count); |
588
|
|
|
|
589
|
|
|
/* testing that orderBy() should be ignored here as it does not affect the count anyway. */ |
590
|
|
|
$count = (new Query($db))->from('customer')->orderBy('status')->count('*'); |
591
|
|
|
|
592
|
|
|
$this->assertEquals(3, $count); |
593
|
|
|
|
594
|
|
|
$count = (new Query($db))->from('customer')->orderBy('id')->limit(1)->count('*'); |
595
|
|
|
|
596
|
|
|
$this->assertEquals(3, $count); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* @depends testFilterWhereWithHashFormat |
601
|
|
|
* @depends testFilterWhereWithOperatorFormat |
602
|
|
|
*/ |
603
|
|
|
public function testAndFilterCompare(): void |
604
|
|
|
{ |
605
|
|
|
$db = $this->getConnection(); |
606
|
|
|
|
607
|
|
|
$query = new Query($db); |
608
|
|
|
|
609
|
|
|
$result = $query->andFilterCompare('name', null); |
610
|
|
|
|
611
|
|
|
$this->assertInstanceOf(Query::class, $result); |
612
|
|
|
$this->assertNull($query->getWhere()); |
613
|
|
|
|
614
|
|
|
$query->andFilterCompare('name', ''); |
615
|
|
|
|
616
|
|
|
$this->assertNull($query->getWhere()); |
617
|
|
|
|
618
|
|
|
$query->andFilterCompare('name', 'John Doe'); |
619
|
|
|
$condition = ['=', 'name', 'John Doe']; |
620
|
|
|
|
621
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
622
|
|
|
|
623
|
|
|
$condition = ['and', $condition, ['like', 'name', 'Doe']]; |
624
|
|
|
$query->andFilterCompare('name', 'Doe', 'like'); |
625
|
|
|
|
626
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
627
|
|
|
|
628
|
|
|
$condition[] = ['>', 'rating', '9']; |
629
|
|
|
$query->andFilterCompare('rating', '>9'); |
630
|
|
|
|
631
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
632
|
|
|
|
633
|
|
|
$condition[] = ['<=', 'value', '100']; |
634
|
|
|
$query->andFilterCompare('value', '<=100'); |
635
|
|
|
|
636
|
|
|
$this->assertEquals($condition, $query->getWhere()); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
public function testEmulateExecution(): void |
640
|
|
|
{ |
641
|
|
|
$db = $this->getConnection(); |
642
|
|
|
|
643
|
|
|
$this->assertGreaterThan(0, (new Query($db))->from('customer')->count('*')); |
|
|
|
|
644
|
|
|
|
645
|
|
|
$rows = (new Query($db))->from('customer')->emulateExecution()->all(); |
646
|
|
|
|
647
|
|
|
$this->assertSame([], $rows); |
648
|
|
|
|
649
|
|
|
$row = (new Query($db))->from('customer')->emulateExecution()->one(); |
650
|
|
|
|
651
|
|
|
$this->assertFalse($row); |
652
|
|
|
|
653
|
|
|
$exists = (new Query($db))->from('customer')->emulateExecution()->exists($db); |
|
|
|
|
654
|
|
|
|
655
|
|
|
$this->assertFalse($exists); |
656
|
|
|
|
657
|
|
|
$count = (new Query($db))->from('customer')->emulateExecution()->count('*'); |
658
|
|
|
|
659
|
|
|
$this->assertSame(0, $count); |
660
|
|
|
|
661
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->sum('id'); |
662
|
|
|
|
663
|
|
|
$this->assertSame(0, $sum); |
664
|
|
|
|
665
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->average('id'); |
666
|
|
|
|
667
|
|
|
$this->assertSame(0, $sum); |
668
|
|
|
|
669
|
|
|
$max = (new Query($db))->from('customer')->emulateExecution()->max('id'); |
670
|
|
|
|
671
|
|
|
$this->assertNull($max); |
672
|
|
|
|
673
|
|
|
$min = (new Query($db))->from('customer')->emulateExecution()->min('id'); |
674
|
|
|
|
675
|
|
|
$this->assertNull($min); |
676
|
|
|
|
677
|
|
|
$scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar(); |
678
|
|
|
|
679
|
|
|
$this->assertNull($scalar); |
680
|
|
|
|
681
|
|
|
$column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column(); |
682
|
|
|
|
683
|
|
|
$this->assertSame([], $column); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @param Connection $db |
|
|
|
|
688
|
|
|
* @param string $tableName |
689
|
|
|
* @param string $columnName |
690
|
|
|
* @param array $condition |
691
|
|
|
* @param string $operator |
692
|
|
|
* |
693
|
|
|
* @throws Exception |
694
|
|
|
* @throws InvalidArgumentException |
695
|
|
|
* @throws InvalidConfigException |
696
|
|
|
* @throws NotSupportedException |
697
|
|
|
* |
698
|
|
|
* @return int |
699
|
|
|
*/ |
700
|
|
|
protected function countLikeQuery( |
701
|
|
|
ConnectionInterface $db, |
|
|
|
|
702
|
|
|
string $tableName, |
703
|
|
|
string $columnName, |
704
|
|
|
array $condition, |
705
|
|
|
string $operator = 'or' |
706
|
|
|
): int { |
707
|
|
|
$db = $this->getConnection(); |
708
|
|
|
|
709
|
|
|
$whereCondition = [$operator]; |
710
|
|
|
|
711
|
|
|
foreach ($condition as $value) { |
712
|
|
|
$whereCondition[] = ['like', $columnName, $value]; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
$result = (new Query($db))->from($tableName)->where($whereCondition)->count('*'); |
716
|
|
|
|
717
|
|
|
if (is_numeric($result)) { |
718
|
|
|
$result = (int) $result; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
return $result; |
|
|
|
|
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
/** |
725
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/13745} |
726
|
|
|
*/ |
727
|
|
|
public function testMultipleLikeConditions(): void |
728
|
|
|
{ |
729
|
|
|
$db = $this->getConnection(); |
730
|
|
|
|
731
|
|
|
$tableName = 'like_test'; |
732
|
|
|
$columnName = 'col'; |
733
|
|
|
|
734
|
|
|
if ($db->getSchema()->getTableSchema($tableName) !== null) { |
735
|
|
|
$db->createCommand()->dropTable($tableName)->execute(); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
$db->createCommand()->createTable($tableName, [ |
739
|
|
|
$columnName => $db->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, 64), |
740
|
|
|
])->execute(); |
741
|
|
|
|
742
|
|
|
$db->createCommand()->batchInsert($tableName, ['col'], [ |
743
|
|
|
['test0'], |
744
|
|
|
['test\1'], |
745
|
|
|
['test\2'], |
746
|
|
|
['foo%'], |
747
|
|
|
['%bar'], |
748
|
|
|
['%baz%'], |
749
|
|
|
])->execute(); |
750
|
|
|
|
751
|
|
|
/* Basic tests */ |
752
|
|
|
$this->assertSame(1, $this->countLikeQuery($db, $tableName, $columnName, ['test0'])); |
753
|
|
|
$this->assertSame(2, $this->countLikeQuery($db, $tableName, $columnName, ['test\\'])); |
754
|
|
|
$this->assertSame(0, $this->countLikeQuery($db, $tableName, $columnName, ['test%'])); |
755
|
|
|
$this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, ['%'])); |
756
|
|
|
|
757
|
|
|
/* Multiple condition tests */ |
758
|
|
|
$this->assertSame(2, $this->countLikeQuery($db, $tableName, $columnName, [ |
759
|
|
|
'test0', |
760
|
|
|
'test\1', |
761
|
|
|
])); |
762
|
|
|
$this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, [ |
763
|
|
|
'test0', |
764
|
|
|
'test\1', |
765
|
|
|
'test\2', |
766
|
|
|
])); |
767
|
|
|
$this->assertSame(3, $this->countLikeQuery($db, $tableName, $columnName, [ |
768
|
|
|
'foo', |
769
|
|
|
'%ba', |
770
|
|
|
])); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15355} |
775
|
|
|
*/ |
776
|
|
|
public function testExpressionInFrom(): void |
777
|
|
|
{ |
778
|
|
|
$db = $this->getConnection(true); |
779
|
|
|
|
780
|
|
|
$query = (new Query($db)) |
781
|
|
|
->from( |
782
|
|
|
new Expression( |
783
|
|
|
'(SELECT [[id]], [[name]], [[email]], [[address]], [[status]] FROM {{customer}}) c' |
784
|
|
|
) |
785
|
|
|
) |
786
|
|
|
->where(['status' => 2]); |
787
|
|
|
|
788
|
|
|
$result = $query->one(); |
789
|
|
|
|
790
|
|
|
$this->assertEquals('user3', $result['name']); |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
public function testQueryCache() |
794
|
|
|
{ |
795
|
|
|
$db = $this->getConnection(); |
796
|
|
|
|
797
|
|
|
$this->queryCache->setEnable(true); |
798
|
|
|
|
799
|
|
|
$query = (new Query($db)) |
800
|
|
|
->select(['name']) |
801
|
|
|
->from('customer'); |
802
|
|
|
|
803
|
|
|
$update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id'); |
804
|
|
|
|
805
|
|
|
$this->assertEquals('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value'); |
806
|
|
|
|
807
|
|
|
/* No cache */ |
808
|
|
|
$update->bindValues([':id' => 1, ':name' => 'user11'])->execute(); |
809
|
|
|
|
810
|
|
|
$this->assertEquals( |
811
|
|
|
'user11', |
812
|
|
|
$query->where(['id' => 1])->scalar(), |
813
|
|
|
'Query reflects DB changes when caching is disabled' |
814
|
|
|
); |
815
|
|
|
|
816
|
|
|
/* Connection cache */ |
817
|
|
|
$db->cache(function (ConnectionInterface $db) use ($query, $update) { |
818
|
|
|
$this->assertEquals( |
819
|
|
|
'user2', |
820
|
|
|
$query->where(['id' => 2])->scalar(), |
821
|
|
|
'Asserting initial value for user #2' |
822
|
|
|
); |
823
|
|
|
|
824
|
|
|
$update->bindValues([':id' => 2, ':name' => 'user22'])->execute(); |
825
|
|
|
|
826
|
|
|
$this->assertEquals( |
827
|
|
|
'user2', |
828
|
|
|
$query->where(['id' => 2])->scalar(), |
829
|
|
|
'Query does NOT reflect DB changes when wrapped in connection caching' |
830
|
|
|
); |
831
|
|
|
|
832
|
|
|
$db->noCache(function () use ($query) { |
833
|
|
|
$this->assertEquals( |
834
|
|
|
'user22', |
835
|
|
|
$query->where(['id' => 2])->scalar(), |
836
|
|
|
'Query reflects DB changes when wrapped in connection caching and noCache simultaneously' |
837
|
|
|
); |
838
|
|
|
}); |
839
|
|
|
|
840
|
|
|
$this->assertEquals( |
841
|
|
|
'user2', |
842
|
|
|
$query->where(['id' => 2])->scalar(), |
843
|
|
|
'Cache does not get changes after getting newer data from DB in noCache block.' |
844
|
|
|
); |
845
|
|
|
}, 10); |
846
|
|
|
|
847
|
|
|
$this->queryCache->setEnable(false); |
848
|
|
|
|
849
|
|
|
$db->cache(function () use ($query, $update) { |
850
|
|
|
$this->assertEquals( |
851
|
|
|
'user22', |
852
|
|
|
$query->where(['id' => 2])->scalar(), |
853
|
|
|
'When cache is disabled for the whole connection, Query inside cache block does not get cached' |
854
|
|
|
); |
855
|
|
|
|
856
|
|
|
$update->bindValues([':id' => 2, ':name' => 'user2'])->execute(); |
857
|
|
|
|
858
|
|
|
$this->assertEquals('user2', $query->where(['id' => 2])->scalar()); |
859
|
|
|
}, 10); |
860
|
|
|
|
861
|
|
|
$this->queryCache->setEnable(true); |
862
|
|
|
|
863
|
|
|
$query->cache(); |
864
|
|
|
|
865
|
|
|
$this->assertEquals('user11', $query->where(['id' => 1])->scalar()); |
866
|
|
|
|
867
|
|
|
$update->bindValues([':id' => 1, ':name' => 'user1'])->execute(); |
868
|
|
|
|
869
|
|
|
$this->assertEquals( |
870
|
|
|
'user11', |
871
|
|
|
$query->where(['id' => 1])->scalar(), |
872
|
|
|
'When both Connection and Query have cache enabled, we get cached value' |
873
|
|
|
); |
874
|
|
|
$this->assertEquals( |
875
|
|
|
'user1', |
876
|
|
|
$query->noCache()->where(['id' => 1])->scalar(), |
877
|
|
|
'When Query has disabled cache, we get actual data' |
878
|
|
|
); |
879
|
|
|
|
880
|
|
|
$db->cache(function () use ($query) { |
881
|
|
|
$this->assertEquals('user1', $query->noCache()->where(['id' => 1])->scalar()); |
882
|
|
|
$this->assertEquals('user11', $query->cache()->where(['id' => 1])->scalar()); |
883
|
|
|
}, 10); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* checks that all needed properties copied from source to new query |
888
|
|
|
*/ |
889
|
|
|
public function testQueryCreation(): void |
890
|
|
|
{ |
891
|
|
|
$db = $this->getConnection(); |
892
|
|
|
|
893
|
|
|
$where = 'id > :min_user_id'; |
894
|
|
|
$limit = 50; |
895
|
|
|
$offset = 2; |
896
|
|
|
$orderBy = ['name' => SORT_ASC]; |
897
|
|
|
$indexBy = 'id'; |
898
|
|
|
$select = ['id' => 'id', 'name' => 'name', 'articles_count' => 'count(*)']; |
899
|
|
|
$selectOption = 'SQL_NO_CACHE'; |
900
|
|
|
$from = 'recent_users'; |
901
|
|
|
$groupBy = 'id'; |
902
|
|
|
$having = ['>', 'articles_count', 0]; |
903
|
|
|
$params = [':min_user_id' => 100]; |
904
|
|
|
|
905
|
|
|
[$joinType, $joinTable, $joinOn] = $join = ['INNER', 'articles', 'articles.author_id=users.id']; |
906
|
|
|
|
907
|
|
|
$unionQuery = (new Query($db)) |
908
|
|
|
->select('id, name, 1000 as articles_count') |
909
|
|
|
->from('admins'); |
910
|
|
|
|
911
|
|
|
$withQuery = (new Query($db)) |
912
|
|
|
->select('id, name') |
913
|
|
|
->from('users') |
914
|
|
|
->where('DATE(registered_at) > "2020-01-01"'); |
915
|
|
|
|
916
|
|
|
/** build target query */ |
917
|
|
|
$sourceQuery = (new Query($db)) |
918
|
|
|
->where($where) |
919
|
|
|
->limit($limit) |
920
|
|
|
->offset($offset) |
921
|
|
|
->orderBy($orderBy) |
922
|
|
|
->indexBy($indexBy) |
923
|
|
|
->select($select, $selectOption) |
924
|
|
|
->distinct() |
925
|
|
|
->from($from) |
926
|
|
|
->groupBy($groupBy) |
927
|
|
|
->having($having) |
928
|
|
|
->addParams($params) |
929
|
|
|
->join($joinType, $joinTable, $joinOn) |
930
|
|
|
->union($unionQuery) |
931
|
|
|
->withQuery($withQuery, $from); |
932
|
|
|
|
933
|
|
|
$newQuery = Query::create($db, $sourceQuery); |
934
|
|
|
|
935
|
|
|
$this->assertEquals($where, $newQuery->getWhere()); |
936
|
|
|
$this->assertEquals($limit, $newQuery->getLimit()); |
937
|
|
|
$this->assertEquals($offset, $newQuery->getOffset()); |
938
|
|
|
$this->assertEquals($orderBy, $newQuery->getOrderBy()); |
939
|
|
|
$this->assertEquals($indexBy, $newQuery->getIndexBy()); |
940
|
|
|
$this->assertEquals($select, $newQuery->getSelect()); |
941
|
|
|
$this->assertEquals($selectOption, $newQuery->getSelectOption()); |
942
|
|
|
$this->assertTrue($newQuery->getDistinct()); |
943
|
|
|
$this->assertEquals([$from], $newQuery->getFrom()); |
944
|
|
|
$this->assertEquals([$groupBy], $newQuery->getGroupBy()); |
945
|
|
|
$this->assertEquals($having, $newQuery->getHaving()); |
946
|
|
|
$this->assertEquals($params, $newQuery->getParams()); |
947
|
|
|
$this->assertEquals([$join], $newQuery->getJoin()); |
948
|
|
|
$this->assertEquals([['query' => $unionQuery, 'all' => false]], $newQuery->getUnion()); |
949
|
|
|
$this->assertEquals( |
950
|
|
|
[['query' => $withQuery, 'alias' => $from, 'recursive' => false]], |
951
|
|
|
$newQuery->getWithQueries() |
952
|
|
|
); |
953
|
|
|
} |
954
|
|
|
} |
955
|
|
|
|