|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
9
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
10
|
|
|
use Yiisoft\Db\Query\Query; |
|
11
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
12
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractQueryTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
use TestTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @depends testFilterWhereWithHashFormat |
|
20
|
|
|
* @depends testFilterWhereWithOperatorFormat |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testAndFilterCompare(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$db = $this->getConnection(); |
|
25
|
|
|
|
|
26
|
|
|
$query = new Query($db); |
|
27
|
|
|
$result = $query->andFilterCompare('name', null); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertInstanceOf(QueryInterface::class, $result); |
|
30
|
|
|
$this->assertNull($query->getWhere()); |
|
31
|
|
|
|
|
32
|
|
|
$query->andFilterCompare('name', ''); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertNull($query->getWhere()); |
|
35
|
|
|
|
|
36
|
|
|
$query->andFilterCompare('name', 'John Doe'); |
|
37
|
|
|
$condition = ['=', 'name', 'John Doe']; |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
40
|
|
|
|
|
41
|
|
|
$condition = ['and', $condition, ['like', 'name', 'Doe']]; |
|
42
|
|
|
$query->andFilterCompare('name', 'Doe', 'like'); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
45
|
|
|
|
|
46
|
|
|
$condition[] = ['>', 'rating', '9']; |
|
47
|
|
|
$query->andFilterCompare('rating', '>9'); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
50
|
|
|
|
|
51
|
|
|
$condition[] = ['<=', 'value', '100']; |
|
52
|
|
|
$query->andFilterCompare('value', '<=100'); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testEmulateExecution(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$db = $this->getConnectionWithData(); |
|
60
|
|
|
|
|
61
|
|
|
$rows = (new Query($db))->from('customer')->emulateExecution()->all(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertSame([], $rows); |
|
64
|
|
|
|
|
65
|
|
|
$row = (new Query($db))->from('customer')->emulateExecution()->one(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertNull($row); |
|
68
|
|
|
|
|
69
|
|
|
$exists = (new Query($db))->from('customer')->emulateExecution()->exists(); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertFalse($exists); |
|
72
|
|
|
|
|
73
|
|
|
$count = (new Query($db))->from('customer')->emulateExecution()->count(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertSame(0, $count); |
|
76
|
|
|
|
|
77
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->sum('id'); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertNull($sum); |
|
80
|
|
|
|
|
81
|
|
|
$sum = (new Query($db))->from('customer')->emulateExecution()->average('id'); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertNull($sum); |
|
84
|
|
|
|
|
85
|
|
|
$max = (new Query($db))->from('customer')->emulateExecution()->max('id'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertNull($max); |
|
88
|
|
|
|
|
89
|
|
|
$min = (new Query($db))->from('customer')->emulateExecution()->min('id'); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertNull($min); |
|
92
|
|
|
|
|
93
|
|
|
$scalar = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->scalar(); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertNull($scalar); |
|
96
|
|
|
|
|
97
|
|
|
$column = (new Query($db))->select(['id'])->from('customer')->emulateExecution()->column(); |
|
98
|
|
|
$this->assertSame([], $column); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testFilterHavingWithHashFormat(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$db = $this->getConnection(); |
|
104
|
|
|
|
|
105
|
|
|
$query = new Query($db); |
|
106
|
|
|
$query->filterHaving(['id' => 0, 'title' => ' ', 'author_ids' => [],]); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
109
|
|
|
|
|
110
|
|
|
$query->andFilterHaving(['status' => null]); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
113
|
|
|
|
|
114
|
|
|
$query->orFilterHaving(['name' => '']); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertSame(['id' => 0], $query->getHaving()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testFilterHavingWithOperatorFormat(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$db = $this->getConnection(); |
|
122
|
|
|
|
|
123
|
|
|
$query = new Query($db); |
|
124
|
|
|
$condition = ['like', 'name', 'Alex']; |
|
125
|
|
|
$query->filterHaving($condition); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
128
|
|
|
|
|
129
|
|
|
$query->andFilterHaving(['between', 'id', null, null]); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
132
|
|
|
|
|
133
|
|
|
$query->orFilterHaving(['not between', 'id', null, null]); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
136
|
|
|
|
|
137
|
|
|
$query->andFilterHaving(['in', 'id', []]); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
140
|
|
|
|
|
141
|
|
|
$query->andFilterHaving(['not in', 'id', []]); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
144
|
|
|
|
|
145
|
|
|
$query->andFilterHaving(['like', 'id', '']); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
148
|
|
|
|
|
149
|
|
|
$query->andFilterHaving(['or like', 'id', '']); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
152
|
|
|
|
|
153
|
|
|
$query->andFilterHaving(['not like', 'id', ' ']); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
156
|
|
|
|
|
157
|
|
|
$query->andFilterHaving(['or not like', 'id', null]); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
160
|
|
|
|
|
161
|
|
|
$query->andFilterHaving(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertSame($condition, $query->getHaving()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testFilterRecursively(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$db = $this->getConnection(); |
|
169
|
|
|
|
|
170
|
|
|
$query = new Query($db); |
|
171
|
|
|
$query->filterWhere( |
|
172
|
|
|
['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]] |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertSame(['and', ['id' => 1]], $query->getWhere()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testFilterWhereWithHashFormat(): void |
|
179
|
|
|
{ |
|
180
|
|
|
$db = $this->getConnection(); |
|
181
|
|
|
|
|
182
|
|
|
$query = new Query($db); |
|
183
|
|
|
$query->filterWhere(['id' => 0, 'title' => ' ', 'author_ids' => []]); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
186
|
|
|
|
|
187
|
|
|
$query->andFilterWhere(['status' => null]); |
|
188
|
|
|
|
|
189
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
190
|
|
|
|
|
191
|
|
|
$query->orFilterWhere(['name' => '']); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame(['id' => 0], $query->getWhere()); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function testFilterWhereWithOperatorFormat(): void |
|
197
|
|
|
{ |
|
198
|
|
|
$db = $this->getConnection(); |
|
199
|
|
|
|
|
200
|
|
|
$query = new Query($db); |
|
201
|
|
|
$condition = ['like', 'name', 'Alex']; |
|
202
|
|
|
$query->filterWhere($condition); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
205
|
|
|
|
|
206
|
|
|
$query->andFilterWhere(['between', 'id', null, null]); |
|
207
|
|
|
|
|
208
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
209
|
|
|
|
|
210
|
|
|
$query->orFilterWhere(['not between', 'id', null, null]); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
213
|
|
|
|
|
214
|
|
|
$query->andFilterWhere(['in', 'id', []]); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
217
|
|
|
|
|
218
|
|
|
$query->andFilterWhere(['not in', 'id', []]); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
221
|
|
|
|
|
222
|
|
|
$query->andFilterWhere(['like', 'id', '']); |
|
223
|
|
|
|
|
224
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
225
|
|
|
|
|
226
|
|
|
$query->andFilterWhere(['or like', 'id', '']); |
|
227
|
|
|
|
|
228
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
229
|
|
|
|
|
230
|
|
|
$query->andFilterWhere(['not like', 'id', ' ']); |
|
231
|
|
|
|
|
232
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
233
|
|
|
|
|
234
|
|
|
$query->andFilterWhere(['or not like', 'id', null]); |
|
235
|
|
|
|
|
236
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
237
|
|
|
|
|
238
|
|
|
$query->andFilterWhere(['or', ['eq', 'id', null], ['eq', 'id', []]]); |
|
239
|
|
|
|
|
240
|
|
|
$this->assertSame($condition, $query->getWhere()); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function testFrom(): void |
|
244
|
|
|
{ |
|
245
|
|
|
$db = $this->getConnection(); |
|
246
|
|
|
|
|
247
|
|
|
$query = new Query($db); |
|
248
|
|
|
$query->from('user'); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertSame(['user'], $query->getFrom()); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function testFromTableIsArrayWithExpression(): void |
|
254
|
|
|
{ |
|
255
|
|
|
$db = $this->getConnection(); |
|
256
|
|
|
|
|
257
|
|
|
$query = new Query($db); |
|
258
|
|
|
$tables = new Expression('(SELECT id,name FROM user) u'); |
|
259
|
|
|
$query->from($tables); |
|
260
|
|
|
$from = $query->getFrom(); |
|
261
|
|
|
|
|
262
|
|
|
$this->assertIsArray($from); |
|
263
|
|
|
$this->assertInstanceOf(ExpressionInterface::class, $from[0]); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
public function testGroup(): void |
|
267
|
|
|
{ |
|
268
|
|
|
$db = $this->getConnection(); |
|
269
|
|
|
|
|
270
|
|
|
$query = new Query($db); |
|
271
|
|
|
$query->groupBy('team'); |
|
272
|
|
|
|
|
273
|
|
|
$this->assertSame(['team'], $query->getGroupBy()); |
|
274
|
|
|
|
|
275
|
|
|
$query->addGroupBy('company'); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertSame(['team', 'company'], $query->getGroupBy()); |
|
278
|
|
|
|
|
279
|
|
|
$query->addGroupBy('age'); |
|
280
|
|
|
|
|
281
|
|
|
$this->assertSame(['team', 'company', 'age'], $query->getGroupBy()); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function testHaving(): void |
|
285
|
|
|
{ |
|
286
|
|
|
$db = $this->getConnection(); |
|
287
|
|
|
|
|
288
|
|
|
$query = new Query($db); |
|
289
|
|
|
$query->having('id = :id', [':id' => 1]); |
|
290
|
|
|
|
|
291
|
|
|
$this->assertSame('id = :id', $query->getHaving()); |
|
292
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
|
293
|
|
|
|
|
294
|
|
|
$query->andHaving('name = :name', [':name' => 'something']); |
|
295
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getHaving()); |
|
296
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
|
297
|
|
|
|
|
298
|
|
|
$query->orHaving('age = :age', [':age' => '30']); |
|
299
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getHaving()); |
|
300
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function testLimitOffset(): void |
|
304
|
|
|
{ |
|
305
|
|
|
$db = $this->getConnection(); |
|
306
|
|
|
|
|
307
|
|
|
$query = new Query($db); |
|
308
|
|
|
$query->limit(10)->offset(5); |
|
309
|
|
|
|
|
310
|
|
|
$this->assertSame(10, $query->getLimit()); |
|
311
|
|
|
$this->assertSame(5, $query->getOffset()); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
public function testOrder(): void |
|
315
|
|
|
{ |
|
316
|
|
|
$db = $this->getConnection(); |
|
317
|
|
|
|
|
318
|
|
|
$query = new Query($db); |
|
319
|
|
|
$query->orderBy('team'); |
|
320
|
|
|
|
|
321
|
|
|
$this->assertSame(['team' => SORT_ASC], $query->getOrderBy()); |
|
322
|
|
|
|
|
323
|
|
|
$query->addOrderBy('company'); |
|
324
|
|
|
|
|
325
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC], $query->getOrderBy()); |
|
326
|
|
|
|
|
327
|
|
|
$query->addOrderBy('age'); |
|
328
|
|
|
|
|
329
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->getOrderBy()); |
|
330
|
|
|
|
|
331
|
|
|
$query->addOrderBy(['age' => SORT_DESC]); |
|
332
|
|
|
|
|
333
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->getOrderBy()); |
|
334
|
|
|
|
|
335
|
|
|
$query->addOrderBy('age ASC, company DESC'); |
|
336
|
|
|
|
|
337
|
|
|
$this->assertSame(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->getOrderBy()); |
|
338
|
|
|
|
|
339
|
|
|
$expression1 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
|
340
|
|
|
|
|
341
|
|
|
$query->orderBy($expression1); |
|
342
|
|
|
|
|
343
|
|
|
$this->assertSame([$expression1], $query->getOrderBy()); |
|
344
|
|
|
|
|
345
|
|
|
$expression2 = new Expression('SUBSTR(name, 3, 4) DESC, x ASC'); |
|
346
|
|
|
|
|
347
|
|
|
$query->addOrderBy($expression2); |
|
348
|
|
|
|
|
349
|
|
|
$this->assertSame([$expression1, $expression2], $query->getOrderBy()); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function testSelect(): void |
|
353
|
|
|
{ |
|
354
|
|
|
$db = $this->getConnection(); |
|
355
|
|
|
|
|
356
|
|
|
/* default */ |
|
357
|
|
|
$query = new Query($db); |
|
358
|
|
|
$query->select('*'); |
|
359
|
|
|
|
|
360
|
|
|
$this->assertSame(['*' => '*'], $query->getSelect()); |
|
361
|
|
|
$this->assertNull($query->getDistinct()); |
|
362
|
|
|
$this->assertNull($query->getSelectOption()); |
|
|
|
|
|
|
363
|
|
|
|
|
364
|
|
|
$query = new Query($db); |
|
365
|
|
|
$query->select('id, name', 'something')->distinct(true); |
|
366
|
|
|
|
|
367
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name'], $query->getSelect()); |
|
368
|
|
|
$this->assertTrue($query->getDistinct()); |
|
369
|
|
|
$this->assertSame('something', $query->getSelectOption()); |
|
370
|
|
|
|
|
371
|
|
|
$query = new Query($db); |
|
372
|
|
|
$query->addSelect('email'); |
|
373
|
|
|
|
|
374
|
|
|
$this->assertSame(['email' => 'email'], $query->getSelect()); |
|
375
|
|
|
|
|
376
|
|
|
$query = new Query($db); |
|
377
|
|
|
$query->select('id, name'); |
|
378
|
|
|
$query->addSelect('email'); |
|
379
|
|
|
|
|
380
|
|
|
$this->assertSame(['id' => 'id', 'name' => 'name', 'email' => 'email'], $query->getSelect()); |
|
381
|
|
|
|
|
382
|
|
|
$query = new Query($db); |
|
383
|
|
|
$query->select('name, lastname'); |
|
384
|
|
|
$query->addSelect('name'); |
|
385
|
|
|
|
|
386
|
|
|
$this->assertSame(['name' => 'name', 'lastname' => 'lastname'], $query->getSelect()); |
|
387
|
|
|
|
|
388
|
|
|
$query = new Query($db); |
|
389
|
|
|
$query->addSelect(['*', 'abc']); |
|
390
|
|
|
$query->addSelect(['*', 'bca']); |
|
391
|
|
|
|
|
392
|
|
|
$this->assertSame(['*' => '*', 'abc' => 'abc', 'bca' => 'bca'], $query->getSelect()); |
|
393
|
|
|
|
|
394
|
|
|
$query = new Query($db); |
|
395
|
|
|
$query->addSelect(['field1 as a', 'field 1 as b']); |
|
396
|
|
|
|
|
397
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
|
398
|
|
|
|
|
399
|
|
|
$query = new Query($db); |
|
400
|
|
|
$query->addSelect(['field1 a', 'field 1 b']); |
|
401
|
|
|
|
|
402
|
|
|
$this->assertSame(['a' => 'field1', 'b' => 'field 1'], $query->getSelect()); |
|
403
|
|
|
|
|
404
|
|
|
$query = new Query($db); |
|
405
|
|
|
$query->select(['name' => 'firstname', 'lastname']); |
|
406
|
|
|
$query->addSelect(['firstname', 'surname' => 'lastname']); |
|
407
|
|
|
$query->addSelect(['firstname', 'lastname']); |
|
408
|
|
|
|
|
409
|
|
|
$this->assertSame( |
|
410
|
|
|
['name' => 'firstname', 'lastname' => 'lastname', 'firstname' => 'firstname', 'surname' => 'lastname'], |
|
411
|
|
|
$query->getSelect(), |
|
412
|
|
|
); |
|
413
|
|
|
|
|
414
|
|
|
$query = new Query($db); |
|
415
|
|
|
$query->select('name, name, name as X, name as X'); |
|
416
|
|
|
|
|
417
|
|
|
$this->assertSame(['name' => 'name', 'X' => 'name'], $query->getSelect()); |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
|
421
|
|
|
*/ |
|
422
|
|
|
$query = (new Query($db))->select('id'); |
|
423
|
|
|
|
|
424
|
|
|
$this->assertSame(['id' => 'id'], $query->getSelect()); |
|
425
|
|
|
|
|
426
|
|
|
$query->select(['id', 'brand_id']); |
|
427
|
|
|
|
|
428
|
|
|
$this->assertSame(['id' => 'id', 'brand_id' => 'brand_id'], $query->getSelect()); |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15676} |
|
432
|
|
|
*/ |
|
433
|
|
|
$query = (new Query($db))->select(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)']); |
|
434
|
|
|
|
|
435
|
|
|
$this->assertSame(['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)'], $query->getSelect()); |
|
436
|
|
|
|
|
437
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
|
438
|
|
|
|
|
439
|
|
|
$this->assertSame( |
|
440
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
441
|
|
|
$query->getSelect() |
|
442
|
|
|
); |
|
443
|
|
|
|
|
444
|
|
|
$query->addSelect(['LEFT(name,7) as test']); |
|
445
|
|
|
|
|
446
|
|
|
$this->assertSame( |
|
447
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
448
|
|
|
$query->getSelect() |
|
449
|
|
|
); |
|
450
|
|
|
|
|
451
|
|
|
$query->addSelect(['test' => 'LEFT(name,7)']); |
|
452
|
|
|
|
|
453
|
|
|
$this->assertSame( |
|
454
|
|
|
['prefix' => 'LEFT(name, 7)', 'prefix_key' => 'LEFT(name, 7)', 'test' => 'LEFT(name,7)'], |
|
455
|
|
|
$query->getSelect() |
|
456
|
|
|
); |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/15731} |
|
460
|
|
|
*/ |
|
461
|
|
|
$selectedCols = [ |
|
462
|
|
|
'total_sum' => 'SUM(f.amount)', |
|
463
|
|
|
'in_sum' => 'SUM(IF(f.type = :type_in, f.amount, 0))', |
|
464
|
|
|
'out_sum' => 'SUM(IF(f.type = :type_out, f.amount, 0))', |
|
465
|
|
|
]; |
|
466
|
|
|
$query = (new Query($db)) |
|
467
|
|
|
->select($selectedCols) |
|
468
|
|
|
->addParams([':type_in' => 'in', ':type_out' => 'out', ':type_partner' => 'partner']); |
|
469
|
|
|
|
|
470
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
|
471
|
|
|
|
|
472
|
|
|
$query->select($selectedCols); |
|
473
|
|
|
|
|
474
|
|
|
$this->assertSame($selectedCols, $query->getSelect()); |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/17384} |
|
478
|
|
|
*/ |
|
479
|
|
|
$query = new Query($db); |
|
480
|
|
|
$query->select('DISTINCT ON(tour_dates.date_from) tour_dates.date_from, tour_dates.id'); |
|
481
|
|
|
|
|
482
|
|
|
$this->assertSame( |
|
483
|
|
|
['DISTINCT ON(tour_dates.date_from) tour_dates.date_from', 'tour_dates.id' => 'tour_dates.id'], |
|
484
|
|
|
$query->getSelect() |
|
485
|
|
|
); |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
public function testWhere(): void |
|
489
|
|
|
{ |
|
490
|
|
|
$db = $this->getConnection(); |
|
491
|
|
|
|
|
492
|
|
|
$query = new Query($db); |
|
493
|
|
|
$query->where('id = :id', [':id' => 1]); |
|
494
|
|
|
|
|
495
|
|
|
$this->assertSame('id = :id', $query->getWhere()); |
|
496
|
|
|
$this->assertSame([':id' => 1], $query->getParams()); |
|
497
|
|
|
|
|
498
|
|
|
$query->andWhere('name = :name', [':name' => 'something']); |
|
499
|
|
|
|
|
500
|
|
|
$this->assertSame(['and', 'id = :id', 'name = :name'], $query->getWhere()); |
|
501
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something'], $query->getParams()); |
|
502
|
|
|
|
|
503
|
|
|
$query->orWhere('age = :age', [':age' => '30']); |
|
504
|
|
|
|
|
505
|
|
|
$this->assertSame(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->getWhere()); |
|
506
|
|
|
$this->assertSame([':id' => 1, ':name' => 'something', ':age' => '30'], $query->getParams()); |
|
507
|
|
|
} |
|
508
|
|
|
} |
|
509
|
|
|
|
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.