Passed
Pull Request — master (#163)
by Wilmer
19:50 queued 04:50
created

buildFilterConditionProviderTrait()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 0
dl 0
loc 46
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
6
7
use Yiisoft\Db\Connection\Connection;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Query\Conditions\InCondition;
10
use Yiisoft\Db\Query\Conditions\LikeCondition;
11
use Yiisoft\Db\Query\Conditions\BetweenColumnsCondition;
12
use Yiisoft\Db\Query\Query;
13
use Yiisoft\Db\Query\QueryBuilder;
14
use Yiisoft\Db\Schema\Schema;
15
use Yiisoft\Db\Schema\SchemaBuilderTrait;
16
17
use function array_key_exists;
18
use function array_merge;
19
use function array_values;
20
use function is_array;
21
use function preg_match_all;
22
use function str_replace;
23
use function strncmp;
24
use function substr;
25
26
trait TestQueryBuilderTrait
27
{
28
    use SchemaBuilderTrait;
29
30
    public function getDb(): Connection
31
    {
32
        return $this->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        return $this->/** @scrutinizer ignore-call */ getConnection();
Loading history...
33
    }
34
35
    /**
36
     * This is not used as a dataprovider for testGetColumnType to speed up the test when used as dataprovider every
37
     * single line will cause a reconnect with the database which is not needed here.
38
     */
39
    public function columnTypes(): array
40
    {
41
        $items = [
42
            [
43
                Schema::TYPE_BIGINT,
44
                $this->bigInteger(),
45
                [
46
                    'mysql' => 'bigint(20)',
47
                    'pgsql' => 'bigint',
48
                    'sqlite' => 'bigint',
49
                    'oci' => 'NUMBER(20)',
50
                    'sqlsrv' => 'bigint'
51
                ]
52
            ],
53
            [
54
                Schema::TYPE_BIGINT . ' NOT NULL',
55
                $this->bigInteger()->notNull(),
56
                [
57
                    'mysql' => 'bigint(20) NOT NULL',
58
                    'pgsql' => 'bigint NOT NULL',
59
                    'sqlite' => 'bigint NOT NULL',
60
                    'oci' => 'NUMBER(20) NOT NULL',
61
                    'sqlsrv' => 'bigint NOT NULL'
62
                ]
63
            ],
64
            [
65
                Schema::TYPE_BIGINT . ' CHECK (value > 5)',
66
                $this->bigInteger()->check('value > 5'),
67
                [
68
                    'mysql' => 'bigint(20) CHECK (value > 5)',
69
                    'pgsql' => 'bigint CHECK (value > 5)',
70
                    'sqlite' => 'bigint CHECK (value > 5)',
71
                    'oci' => 'NUMBER(20) CHECK (value > 5)',
72
                    'sqlsrv' => 'bigint CHECK (value > 5)'
73
                ]
74
            ],
75
            [
76
                Schema::TYPE_BIGINT . '(8)',
77
                $this->bigInteger(8),
78
                [
79
                    'mysql' => 'bigint(8)',
80
                    'pgsql' => 'bigint',
81
                    'sqlite' => 'bigint',
82
                    'oci' => 'NUMBER(8)',
83
                    'sqlsrv' => 'bigint'
84
                ]
85
            ],
86
            [
87
                Schema::TYPE_BIGINT . '(8) CHECK (value > 5)',
88
                $this->bigInteger(8)->check('value > 5'),
89
                [
90
                    'mysql' => 'bigint(8) CHECK (value > 5)',
91
                    'pgsql' => 'bigint CHECK (value > 5)',
92
                    'sqlite' => 'bigint CHECK (value > 5)',
93
                    'oci' => 'NUMBER(8) CHECK (value > 5)',
94
                    'sqlsrv' => 'bigint CHECK (value > 5)'
95
                ]
96
            ],
97
            [
98
                Schema::TYPE_BIGPK,
99
                $this->bigPrimaryKey(),
100
                [
101
                    'mysql' => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY',
102
                    'pgsql' => 'bigserial NOT NULL PRIMARY KEY',
103
                    'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL'
104
                ]
105
            ],
106
            [
107
                Schema::TYPE_BINARY,
108
                $this->binary(),
109
                [
110
                    'mysql' => 'blob',
111
                    'pgsql' => 'bytea',
112
                    'sqlite' => 'blob',
113
                    'oci' => 'BLOB',
114
                    'sqlsrv' => 'varbinary(max)'
115
                ]
116
            ],
117
            [
118
                Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
119
                $this->boolean()->notNull()->defaultValue(1),
120
                [
121
                    'mysql' => 'tinyint(1) NOT NULL DEFAULT 1',
122
                    'sqlite' => 'boolean NOT NULL DEFAULT 1',
123
                    'sqlsrv' => 'bit NOT NULL DEFAULT 1'
124
                ]
125
            ],
126
            [
127
                Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT TRUE',
128
                $this->boolean()->notNull()->defaultValue(true),
129
                [
130
                    'pgsql' => 'boolean NOT NULL DEFAULT TRUE'
131
                ]
132
            ],
133
            [
134
                Schema::TYPE_BOOLEAN,
135
                $this->boolean(),
136
                [
137
                    'mysql' => 'tinyint(1)',
138
                    'pgsql' => 'boolean',
139
                    'sqlite' => 'boolean',
140
                    'oci' => 'NUMBER(1)',
141
                    'sqlsrv' => 'bit'
142
                ]
143
            ],
144
            [
145
                Schema::TYPE_CHAR . ' CHECK (value LIKE \'test%\')',
146
                $this->char()->check('value LIKE \'test%\''),
147
                [
148
                    'pgsql' => 'char(1) CHECK (value LIKE \'test%\')'
149
                ]
150
            ],
151
            [
152
                Schema::TYPE_CHAR . ' CHECK (value LIKE "test%")',
153
                $this->char()->check('value LIKE "test%"'),
154
                [
155
                    'mysql' => 'char(1) CHECK (value LIKE "test%")',
156
                    'sqlite' => 'char(1) CHECK (value LIKE "test%")'
157
                ]
158
            ],
159
            [
160
                Schema::TYPE_CHAR . ' NOT NULL',
161
                $this->char()->notNull(),
162
                [
163
                    'mysql' => 'char(1) NOT NULL',
164
                    'pgsql' => 'char(1) NOT NULL',
165
                    'sqlite' => 'char(1) NOT NULL',
166
                    'oci' => 'CHAR(1) NOT NULL'
167
                ]
168
            ],
169
            [
170
                Schema::TYPE_CHAR . '(6) CHECK (value LIKE "test%")',
171
                $this->char(6)->check('value LIKE "test%"'),
172
                [
173
                    'mysql' => 'char(6) CHECK (value LIKE "test%")',
174
                    'sqlite' => 'char(6) CHECK (value LIKE "test%")'
175
                ]
176
            ],
177
            [
178
                Schema::TYPE_CHAR . '(6)',
179
                $this->char(6)->unsigned(),
180
                [
181
                    'pgsql' => 'char(6)'
182
                ]
183
            ],
184
            [
185
                Schema::TYPE_CHAR . '(6)',
186
                $this->char(6),
187
                [
188
                    'mysql' => 'char(6)',
189
                    'pgsql' => 'char(6)',
190
                    'sqlite' => 'char(6)',
191
                    'oci' => 'CHAR(6)'
192
                ]
193
            ],
194
            [
195
                Schema::TYPE_CHAR,
196
                $this->char(),
197
                [
198
                    'mysql' => 'char(1)',
199
                    'pgsql' => 'char(1)',
200
                    'sqlite' => 'char(1)',
201
                    'oci' => 'CHAR(1)'
202
                ]
203
            ],
204
            [
205
                Schema::TYPE_DATE . ' NOT NULL',
206
                $this->date()->notNull(),
207
                [
208
                    'mysql' => 'date NOT NULL',
209
                    'pgsql' => 'date NOT NULL',
210
                    'sqlite' => 'date NOT NULL',
211
                    'oci' => 'DATE NOT NULL',
212
                    'sqlsrv' => 'date NOT NULL'
213
                ]
214
            ],
215
            [
216
                Schema::TYPE_DATE,
217
                $this->date(),
218
                [
219
                    'mysql' => 'date',
220
                    'pgsql' => 'date',
221
                    'sqlite' => 'date',
222
                    'oci' => 'DATE',
223
                    'sqlsrv' => 'date'
224
                ]
225
            ],
226
            [
227
                Schema::TYPE_DATETIME . ' NOT NULL',
228
                $this->dateTime()->notNull(),
229
                [
230
                    'pgsql' => 'timestamp(0) NOT NULL',
231
                    'sqlite' => 'datetime NOT NULL',
232
                    'oci' => 'TIMESTAMP NOT NULL',
233
                    'sqlsrv' => 'datetime NOT NULL'
234
                ]
235
            ],
236
            [
237
                Schema::TYPE_DATETIME,
238
                $this->dateTime(),
239
                [
240
                    'pgsql' => 'timestamp(0)',
241
                    'sqlite' => 'datetime',
242
                    'oci' => 'TIMESTAMP',
243
                    'sqlsrv' => 'datetime'
244
                ]
245
            ],
246
            [
247
                Schema::TYPE_DECIMAL . ' CHECK (value > 5.6)',
248
                $this->decimal()->check('value > 5.6'),
249
                [
250
                    'mysql' => 'decimal(10,0) CHECK (value > 5.6)',
251
                    'pgsql' => 'numeric(10,0) CHECK (value > 5.6)',
252
                    'sqlite' => 'decimal(10,0) CHECK (value > 5.6)',
253
                    'oci' => 'NUMBER CHECK (value > 5.6)',
254
                    'sqlsrv' => 'decimal(18,0) CHECK (value > 5.6)'
255
                ]
256
            ],
257
            [
258
                Schema::TYPE_DECIMAL . ' NOT NULL',
259
                $this->decimal()->notNull(),
260
                [
261
                    'mysql' => 'decimal(10,0) NOT NULL',
262
                    'pgsql' => 'numeric(10,0) NOT NULL',
263
                    'sqlite' => 'decimal(10,0) NOT NULL',
264
                    'oci' => 'NUMBER NOT NULL',
265
                    'sqlsrv' => 'decimal(18,0) NOT NULL'
266
                ]
267
            ],
268
            [
269
                Schema::TYPE_DECIMAL . '(12,4) CHECK (value > 5.6)',
270
                $this->decimal(12, 4)->check('value > 5.6'),
271
                [
272
                    'mysql' => 'decimal(12,4) CHECK (value > 5.6)',
273
                    'pgsql' => 'numeric(12,4) CHECK (value > 5.6)',
274
                    'sqlite' => 'decimal(12,4) CHECK (value > 5.6)',
275
                    'oci' => 'NUMBER CHECK (value > 5.6)',
276
                    'sqlsrv' => 'decimal(12,4) CHECK (value > 5.6)'
277
                ]
278
            ],
279
            [
280
                Schema::TYPE_DECIMAL . '(12,4)',
281
                $this->decimal(12, 4),
282
                [
283
                    'mysql' => 'decimal(12,4)',
284
                    'pgsql' => 'numeric(12,4)',
285
                    'sqlite' => 'decimal(12,4)',
286
                    'oci' => 'NUMBER',
287
                    'sqlsrv' => 'decimal(12,4)'
288
                ]
289
            ],
290
            [
291
                Schema::TYPE_DECIMAL,
292
                $this->decimal(),
293
                [
294
                    'mysql' => 'decimal(10,0)',
295
                    'pgsql' => 'numeric(10,0)',
296
                    'sqlite' => 'decimal(10,0)',
297
                    'oci' => 'NUMBER',
298
                    'sqlsrv' => 'decimal(18,0)'
299
                ]
300
            ],
301
            [
302
                Schema::TYPE_DOUBLE . ' CHECK (value > 5.6)',
303
                $this->double()->check('value > 5.6'),
304
                [
305
                    'mysql' => 'double CHECK (value > 5.6)',
306
                    'pgsql' => 'double precision CHECK (value > 5.6)',
307
                    'sqlite' => 'double CHECK (value > 5.6)',
308
                    'oci' => 'NUMBER CHECK (value > 5.6)',
309
                    'sqlsrv' => 'float CHECK (value > 5.6)'
310
                ]
311
            ],
312
            [
313
                Schema::TYPE_DOUBLE . ' NOT NULL',
314
                $this->double()->notNull(),
315
                [
316
                    'mysql' => 'double NOT NULL',
317
                    'pgsql' => 'double precision NOT NULL',
318
                    'sqlite' => 'double NOT NULL',
319
                    'oci' => 'NUMBER NOT NULL',
320
                    'sqlsrv' => 'float NOT NULL'
321
                ]
322
            ],
323
            [
324
                Schema::TYPE_DOUBLE . '(16) CHECK (value > 5.6)',
325
                $this->double(16)->check('value > 5.6'),
326
                [
327
                    'mysql' => 'double CHECK (value > 5.6)',
328
                    'pgsql' => 'double precision CHECK (value > 5.6)',
329
                    'sqlite' => 'double CHECK (value > 5.6)',
330
                    'oci' => 'NUMBER CHECK (value > 5.6)',
331
                    'sqlsrv' => 'float CHECK (value > 5.6)'
332
                ]
333
            ],
334
            [
335
                Schema::TYPE_DOUBLE . '(16)',
336
                $this->double(16),
337
                [
338
                    'mysql' => 'double',
339
                    'sqlite' => 'double',
340
                    'oci' => 'NUMBER',
341
                    'sqlsrv' => 'float'
342
                ]
343
            ],
344
            [
345
                Schema::TYPE_DOUBLE,
346
                $this->double(),
347
                [
348
                    'mysql' => 'double',
349
                    'pgsql' => 'double precision',
350
                    'sqlite' => 'double',
351
                    'oci' => 'NUMBER',
352
                    'sqlsrv' => 'float'
353
                ]
354
            ],
355
            [
356
                Schema::TYPE_FLOAT . ' CHECK (value > 5.6)',
357
                $this->float()->check('value > 5.6'),
358
                [
359
                    'mysql' => 'float CHECK (value > 5.6)',
360
                    'pgsql' => 'double precision CHECK (value > 5.6)',
361
                    'sqlite' => 'float CHECK (value > 5.6)',
362
                    'oci' => 'NUMBER CHECK (value > 5.6)',
363
                    'sqlsrv' => 'float CHECK (value > 5.6)'
364
                ]
365
            ],
366
            [
367
                Schema::TYPE_FLOAT . ' NOT NULL',
368
                $this->float()->notNull(),
369
                [
370
                    'mysql' => 'float NOT NULL',
371
                    'pgsql' => 'double precision NOT NULL',
372
                    'sqlite' => 'float NOT NULL',
373
                    'oci' => 'NUMBER NOT NULL',
374
                    'sqlsrv' => 'float NOT NULL'
375
                ]
376
            ],
377
            [
378
                Schema::TYPE_FLOAT . '(16) CHECK (value > 5.6)',
379
                $this->float(16)->check('value > 5.6'),
380
                [
381
                    'mysql' => 'float CHECK (value > 5.6)',
382
                    'pgsql' => 'double precision CHECK (value > 5.6)',
383
                    'sqlite' => 'float CHECK (value > 5.6)',
384
                    'oci' => 'NUMBER CHECK (value > 5.6)',
385
                    'sqlsrv' => 'float CHECK (value > 5.6)'
386
                ]
387
            ],
388
            [
389
                Schema::TYPE_FLOAT . '(16)',
390
                $this->float(16),
391
                [
392
                    'mysql' => 'float',
393
                    'sqlite' => 'float',
394
                    'oci' => 'NUMBER',
395
                    'sqlsrv' => 'float'
396
                ]
397
            ],
398
            [
399
                Schema::TYPE_FLOAT,
400
                $this->float(),
401
                [
402
                    'mysql' => 'float',
403
                    'pgsql' => 'double precision',
404
                    'sqlite' => 'float',
405
                    'oci' => 'NUMBER',
406
                    'sqlsrv' => 'float'
407
                ]
408
            ],
409
            [
410
                Schema::TYPE_INTEGER . ' CHECK (value > 5)',
411
                $this->integer()->check('value > 5'),
412
                [
413
                    'mysql' => 'int(11) CHECK (value > 5)',
414
                    'pgsql' => 'integer CHECK (value > 5)',
415
                    'sqlite' => 'integer CHECK (value > 5)',
416
                    'oci' => 'NUMBER(10) CHECK (value > 5)',
417
                    'sqlsrv' => 'int CHECK (value > 5)'
418
                ]
419
            ],
420
            [
421
                Schema::TYPE_INTEGER . ' NOT NULL',
422
                $this->integer()->notNull(),
423
                [
424
                    'mysql' => 'int(11) NOT NULL',
425
                    'pgsql' => 'integer NOT NULL',
426
                    'sqlite' => 'integer NOT NULL',
427
                    'oci' => 'NUMBER(10) NOT NULL',
428
                    'sqlsrv' => 'int NOT NULL'
429
                ]
430
            ],
431
            [
432
                Schema::TYPE_INTEGER . '(8) CHECK (value > 5)',
433
                $this->integer(8)->check('value > 5'),
434
                [
435
                    'mysql' => 'int(8) CHECK (value > 5)',
436
                    'pgsql' => 'integer CHECK (value > 5)',
437
                    'sqlite' => 'integer CHECK (value > 5)',
438
                    'oci' => 'NUMBER(8) CHECK (value > 5)',
439
                    'sqlsrv' => 'int CHECK (value > 5)'
440
                ]
441
            ],
442
            [
443
                Schema::TYPE_INTEGER . '(8)',
444
                $this->integer(8)->unsigned(),
445
                [
446
                    'pgsql' => 'integer'
447
                ]
448
            ],
449
            [
450
                Schema::TYPE_INTEGER . '(8)',
451
                $this->integer(8),
452
                [
453
                    'mysql' => 'int(8)',
454
                    'pgsql' => 'integer',
455
                    'sqlite' => 'integer',
456
                    'oci' => 'NUMBER(8)',
457
                    'sqlsrv' => 'int'
458
                ]
459
            ],
460
            [
461
                Schema::TYPE_INTEGER,
462
                $this->integer(),
463
                [
464
                    'mysql' => 'int(11)',
465
                    'pgsql' => 'integer',
466
                    'sqlite' => 'integer',
467
                    'oci' => 'NUMBER(10)',
468
                    'sqlsrv' => 'int'
469
                ]
470
            ],
471
            [
472
                Schema::TYPE_MONEY . ' CHECK (value > 0.0)',
473
                $this->money()->check('value > 0.0'),
474
                [
475
                    'mysql' => 'decimal(19,4) CHECK (value > 0.0)',
476
                    'pgsql' => 'numeric(19,4) CHECK (value > 0.0)',
477
                    'sqlite' => 'decimal(19,4) CHECK (value > 0.0)',
478
                    'oci' => 'NUMBER(19,4) CHECK (value > 0.0)',
479
                    'sqlsrv' => 'decimal(19,4) CHECK (value > 0.0)'
480
                ]
481
            ],
482
            [
483
                Schema::TYPE_MONEY . ' NOT NULL',
484
                $this->money()->notNull(),
485
                [
486
                    'mysql' => 'decimal(19,4) NOT NULL',
487
                    'pgsql' => 'numeric(19,4) NOT NULL',
488
                    'sqlite' => 'decimal(19,4) NOT NULL',
489
                    'oci' => 'NUMBER(19,4) NOT NULL',
490
                    'sqlsrv' => 'decimal(19,4) NOT NULL'
491
                ]
492
            ],
493
            [
494
                Schema::TYPE_MONEY . '(16,2) CHECK (value > 0.0)',
495
                $this->money(16, 2)->check('value > 0.0'),
496
                [
497
                    'mysql' => 'decimal(16,2) CHECK (value > 0.0)',
498
                    'pgsql' => 'numeric(16,2) CHECK (value > 0.0)',
499
                    'sqlite' => 'decimal(16,2) CHECK (value > 0.0)',
500
                    'oci' => 'NUMBER(16,2) CHECK (value > 0.0)',
501
                    'sqlsrv' => 'decimal(16,2) CHECK (value > 0.0)'
502
                ]
503
            ],
504
            [
505
                Schema::TYPE_MONEY . '(16,2)',
506
                $this->money(16, 2),
507
                [
508
                    'mysql' => 'decimal(16,2)',
509
                    'pgsql' => 'numeric(16,2)',
510
                    'sqlite' => 'decimal(16,2)',
511
                    'oci' => 'NUMBER(16,2)',
512
                    'sqlsrv' => 'decimal(16,2)'
513
                ]
514
            ],
515
            [
516
                Schema::TYPE_MONEY,
517
                $this->money(),
518
                [
519
                    'mysql' => 'decimal(19,4)',
520
                    'pgsql' => 'numeric(19,4)',
521
                    'sqlite' => 'decimal(19,4)',
522
                    'oci' => 'NUMBER(19,4)',
523
                    'sqlsrv' => 'decimal(19,4)'
524
                ]
525
            ],
526
            [
527
                Schema::TYPE_PK . ' CHECK (value > 5)',
528
                $this->primaryKey()->check('value > 5'),
529
                [
530
                    'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)',
531
                    'pgsql' => 'serial NOT NULL PRIMARY KEY CHECK (value > 5)',
532
                    'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL CHECK (value > 5)',
533
                    'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY CHECK (value > 5)',
534
                    'sqlsrv' => 'int IDENTITY PRIMARY KEY CHECK (value > 5)'
535
                ]
536
            ],
537
            [
538
                Schema::TYPE_PK . '(8) CHECK (value > 5)',
539
                $this->primaryKey(8)->check('value > 5'),
540
                [
541
                    'mysql' => 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)',
542
                    'oci' => 'NUMBER(8) NOT NULL PRIMARY KEY CHECK (value > 5)'
543
                ]
544
            ],
545
            [
546
                Schema::TYPE_PK . '(8)',
547
                $this->primaryKey(8),
548
                [
549
                    'mysql' => 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY',
550
                    'oci' => 'NUMBER(8) NOT NULL PRIMARY KEY'
551
                ]
552
            ],
553
            [
554
                Schema::TYPE_PK,
555
                $this->primaryKey(),
556
                [
557
                    'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
558
                    'pgsql' => 'serial NOT NULL PRIMARY KEY',
559
                    'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
560
                    'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY',
561
                    'sqlsrv' => 'int IDENTITY PRIMARY KEY'
562
                ]
563
            ],
564
            [
565
                Schema::TYPE_TINYINT . '(2)',
566
                $this->tinyInteger(2),
567
                [
568
                    'mysql' => 'tinyint(2)',
569
                    'pgsql' => 'smallint',
570
                    'sqlite' => 'tinyint',
571
                    'oci' => 'NUMBER(2)',
572
                    'sqlsrv' => 'tinyint'
573
                ]
574
            ],
575
            [
576
                Schema::TYPE_TINYINT . ' UNSIGNED',
577
                $this->tinyInteger()->unsigned(),
578
                [
579
                    'mysql' => 'tinyint(3) UNSIGNED',
580
                    'sqlite' => 'tinyint UNSIGNED'
581
                ]
582
            ],
583
            [
584
                Schema::TYPE_TINYINT,
585
                $this->tinyInteger(),
586
                [
587
                    'mysql' => 'tinyint(3)',
588
                    'pgsql' => 'smallint',
589
                    'sqlite' => 'tinyint',
590
                    'oci' => 'NUMBER(3)',
591
                    'sqlsrv' => 'tinyint'
592
                ]
593
            ],
594
            [
595
                Schema::TYPE_SMALLINT . '(8)',
596
                $this->smallInteger(8),
597
                [
598
                    'mysql' => 'smallint(8)',
599
                    'pgsql' => 'smallint',
600
                    'sqlite' => 'smallint',
601
                    'oci' => 'NUMBER(8)',
602
                    'sqlsrv' => 'smallint'
603
                ]
604
            ],
605
            [
606
                Schema::TYPE_SMALLINT,
607
                $this->smallInteger(),
608
                [
609
                    'mysql' => 'smallint(6)',
610
                    'pgsql' => 'smallint',
611
                    'sqlite' => 'smallint',
612
                    'oci' => 'NUMBER(5)',
613
                    'sqlsrv' => 'smallint'
614
                ]
615
            ],
616
            [
617
                Schema::TYPE_STRING . " CHECK (value LIKE 'test%')",
618
                $this->string()->check("value LIKE 'test%'"),
619
                [
620
                    'mysql' => "varchar(255) CHECK (value LIKE 'test%')",
621
                    'sqlite' => "varchar(255) CHECK (value LIKE 'test%')",
622
                    'sqlsrv' => "nvarchar(255) CHECK (value LIKE 'test%')"
623
                ]
624
            ],
625
            [
626
                Schema::TYPE_STRING . ' CHECK (value LIKE \'test%\')',
627
                $this->string()->check('value LIKE \'test%\''),
628
                [
629
                    'pgsql' => 'varchar(255) CHECK (value LIKE \'test%\')',
630
                    'oci' => 'VARCHAR2(255) CHECK (value LIKE \'test%\')'
631
                ]
632
            ],
633
            [
634
                Schema::TYPE_STRING . ' NOT NULL',
635
                $this->string()->notNull(),
636
                [
637
                    'mysql' => 'varchar(255) NOT NULL',
638
                    'pgsql' => 'varchar(255) NOT NULL',
639
                    'sqlite' => 'varchar(255) NOT NULL',
640
                    'oci' => 'VARCHAR2(255) NOT NULL',
641
                    'sqlsrv' => 'nvarchar(255) NOT NULL'
642
                ]
643
            ],
644
            [
645
                Schema::TYPE_STRING . "(32) CHECK (value LIKE 'test%')",
646
                $this->string(32)->check("value LIKE 'test%'"),
647
                [
648
                    'mysql' => "varchar(32) CHECK (value LIKE 'test%')",
649
                    'sqlite' => "varchar(32) CHECK (value LIKE 'test%')",
650
                    'sqlsrv' => "nvarchar(32) CHECK (value LIKE 'test%')"
651
                ]
652
            ],
653
            [
654
                Schema::TYPE_STRING . '(32) CHECK (value LIKE \'test%\')',
655
                $this->string(32)->check('value LIKE \'test%\''),
656
                [
657
                    'pgsql' => 'varchar(32) CHECK (value LIKE \'test%\')',
658
                    'oci' => 'VARCHAR2(32) CHECK (value LIKE \'test%\')'
659
                ]
660
            ],
661
            [
662
                Schema::TYPE_STRING . '(32)',
663
                $this->string(32),
664
                [
665
                    'mysql' => 'varchar(32)',
666
                    'pgsql' => 'varchar(32)',
667
                    'sqlite' => 'varchar(32)',
668
                    'oci' => 'VARCHAR2(32)',
669
                    'sqlsrv' => 'nvarchar(32)'
670
                ]
671
            ],
672
            [
673
                Schema::TYPE_STRING,
674
                $this->string(),
675
                [
676
                    'mysql' => 'varchar(255)',
677
                    'pgsql' => 'varchar(255)',
678
                    'sqlite' => 'varchar(255)',
679
                    'oci' => 'VARCHAR2(255)',
680
                    'sqlsrv' => 'nvarchar(255)'
681
                ]
682
            ],
683
            [
684
                Schema::TYPE_TEXT . " CHECK (value LIKE 'test%')",
685
                $this->text()->check("value LIKE 'test%'"),
686
                [
687
                    'mysql' => "text CHECK (value LIKE 'test%')",
688
                    'sqlite' => "text CHECK (value LIKE 'test%')",
689
                    'sqlsrv' => "nvarchar(max) CHECK (value LIKE 'test%')"
690
                ]
691
            ],
692
            [
693
                Schema::TYPE_TEXT . ' CHECK (value LIKE \'test%\')',
694
                $this->text()->check('value LIKE \'test%\''),
695
                [
696
                    'pgsql' => 'text CHECK (value LIKE \'test%\')',
697
                    'oci' => 'CLOB CHECK (value LIKE \'test%\')'
698
                ]
699
            ],
700
            [
701
                Schema::TYPE_TEXT . ' NOT NULL',
702
                $this->text()->notNull(),
703
                [
704
                    'mysql' => 'text NOT NULL',
705
                    'pgsql' => 'text NOT NULL',
706
                    'sqlite' => 'text NOT NULL',
707
                    'oci' => 'CLOB NOT NULL',
708
                    'sqlsrv' => 'nvarchar(max) NOT NULL'
709
                ]
710
            ],
711
            [
712
                Schema::TYPE_TEXT . " CHECK (value LIKE 'test%')",
713
                $this->text()->check("value LIKE 'test%'"),
714
                [
715
                    'mysql' => "text CHECK (value LIKE 'test%')",
716
                    'sqlite' => "text CHECK (value LIKE 'test%')",
717
                    'sqlsrv' => "nvarchar(max) CHECK (value LIKE 'test%')"
718
                ],
719
                Schema::TYPE_TEXT . " CHECK (value LIKE 'test%')"
720
            ],
721
            [
722
                Schema::TYPE_TEXT . ' CHECK (value LIKE \'test%\')',
723
                $this->text()->check('value LIKE \'test%\''),
724
                [
725
                    'pgsql' => 'text CHECK (value LIKE \'test%\')',
726
                    'oci' => 'CLOB CHECK (value LIKE \'test%\')'
727
                ],
728
                Schema::TYPE_TEXT . ' CHECK (value LIKE \'test%\')'
729
            ],
730
            [
731
                Schema::TYPE_TEXT . ' NOT NULL',
732
                $this->text()->notNull(),
733
                [
734
                    'mysql' => 'text NOT NULL',
735
                    'pgsql' => 'text NOT NULL',
736
                    'sqlite' => 'text NOT NULL',
737
                    'oci' => 'CLOB NOT NULL',
738
                    'sqlsrv' => 'nvarchar(max) NOT NULL'
739
                ],
740
                Schema::TYPE_TEXT . ' NOT NULL'
741
            ],
742
            [
743
                Schema::TYPE_TEXT,
744
                $this->text(),
745
                [
746
                    'mysql' => 'text',
747
                    'pgsql' => 'text',
748
                    'sqlite' => 'text',
749
                    'oci' => 'CLOB',
750
                    'sqlsrv' => 'nvarchar(max)'
751
                ],
752
                Schema::TYPE_TEXT
753
            ],
754
            [
755
                Schema::TYPE_TEXT,
756
                $this->text(),
757
                [
758
                    'mysql' => 'text',
759
                    'pgsql' => 'text',
760
                    'sqlite' => 'text',
761
                    'oci' => 'CLOB',
762
                    'sqlsrv' => 'nvarchar(max)'
763
                ]
764
            ],
765
            [
766
                Schema::TYPE_TIME . ' NOT NULL',
767
                $this->time()->notNull(),
768
                [
769
                    'pgsql' => 'time(0) NOT NULL',
770
                    'sqlite' => 'time NOT NULL',
771
                    'oci' => 'TIMESTAMP NOT NULL',
772
                    'sqlsrv' => 'time NOT NULL'
773
                ]
774
            ],
775
            [
776
                Schema::TYPE_TIME,
777
                $this->time(),
778
                [
779
                    'pgsql' => 'time(0)',
780
                    'sqlite' => 'time',
781
                    'oci' => 'TIMESTAMP',
782
                    'sqlsrv' => 'time'
783
                ]
784
            ],
785
            [
786
                Schema::TYPE_TIMESTAMP . ' NOT NULL',
787
                $this->timestamp()->notNull(),
788
                [
789
                    'pgsql' => 'timestamp(0) NOT NULL',
790
                    'sqlite' => 'timestamp NOT NULL',
791
                    'oci' => 'TIMESTAMP NOT NULL',
792
                    'sqlsrv' => 'datetime NOT NULL'
793
                ]
794
            ],
795
            [
796
                Schema::TYPE_TIMESTAMP . ' NULL DEFAULT NULL',
797
                $this->timestamp()->defaultValue(null),
798
                [
799
                    'pgsql' => 'timestamp(0) NULL DEFAULT NULL',
800
                    'sqlite' => 'timestamp NULL DEFAULT NULL',
801
                    'sqlsrv' => 'datetime NULL DEFAULT NULL'
802
                ]
803
            ],
804
            [
805
                Schema::TYPE_TIMESTAMP . '(4)',
806
                $this->timestamp(4),
807
                [
808
                    'pgsql' => 'timestamp(4)'
809
                ]
810
            ],
811
            [
812
                Schema::TYPE_TIMESTAMP,
813
                $this->timestamp(),
814
                [
815
                    /**
816
                     * MySQL has its own TIMESTAMP test realization.
817
                     *
818
                     * {@see QueryBuilderTest::columnTypes()}
819
                     */
820
                    'pgsql' => 'timestamp(0)',
821
                    'sqlite' => 'timestamp',
822
                    'oci' => 'TIMESTAMP',
823
                    'sqlsrv' => 'datetime'
824
                ]
825
            ],
826
            [
827
                Schema::TYPE_UPK,
828
                $this->primaryKey()->unsigned(),
829
                [
830
                    'mysql' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
831
                    'pgsql' => 'serial NOT NULL PRIMARY KEY',
832
                    'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL'
833
                ]
834
            ],
835
            [
836
                Schema::TYPE_UBIGPK,
837
                $this->bigPrimaryKey()->unsigned(),
838
                [
839
                    'mysql' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
840
                    'pgsql' => 'bigserial NOT NULL PRIMARY KEY',
841
                    'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL'
842
                ]
843
            ],
844
            [
845
                Schema::TYPE_INTEGER . " COMMENT 'test comment'",
846
                $this->integer()->comment('test comment'),
847
                [
848
                    'mysql' => "int(11) COMMENT 'test comment'",
849
                    'sqlsrv' => 'int'
850
                ],
851
                [
852
                    'sqlsrv' => 'integer'
853
                ]
854
            ],
855
            [
856
                Schema::TYPE_PK . " COMMENT 'test comment'",
857
                $this->primaryKey()->comment('test comment'),
858
                [
859
                    'mysql' => "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test comment'",
860
                    'sqlsrv' => 'int IDENTITY PRIMARY KEY'
861
                ],
862
                [
863
                    'sqlsrv' => 'pk'
864
                ]
865
            ],
866
            [
867
                Schema::TYPE_PK . ' FIRST',
868
                $this->primaryKey()->first(),
869
                [
870
                    'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST',
871
                    'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY',
872
                    'sqlsrv' => 'int IDENTITY PRIMARY KEY'
873
                ],
874
                [
875
                    'sqlsrv' => 'pk'
876
                ]
877
            ],
878
            [
879
                Schema::TYPE_INTEGER . ' FIRST',
880
                $this->integer()->first(),
881
                [
882
                    'mysql' => 'int(11) FIRST',
883
                    'oci' => 'NUMBER(10)',
884
                    'sqlsrv' => 'int'
885
                ],
886
                [
887
                    'pgsql' => 'integer',
888
                    'sqlsrv' => 'integer'
889
                ]
890
            ],
891
            [
892
                Schema::TYPE_STRING . ' FIRST',
893
                $this->string()->first(),
894
                [
895
                    'mysql' => 'varchar(255) FIRST',
896
                    'oci' => 'VARCHAR2(255)',
897
                    'sqlsrv' => 'nvarchar(255)'
898
                ],
899
                [
900
                    'sqlsrv' => 'string'
901
                ]
902
            ],
903
            [
904
                Schema::TYPE_INTEGER . ' NOT NULL FIRST',
905
                $this->integer()->append('NOT NULL')->first(),
906
                [
907
                    'mysql' => 'int(11) NOT NULL FIRST',
908
                    'oci' => 'NUMBER(10) NOT NULL',
909
                    'sqlsrv' => 'int NOT NULL'
910
                ],
911
                [
912
                    'sqlsrv' => 'integer NOT NULL'
913
                ]
914
            ],
915
            [
916
                Schema::TYPE_STRING . ' NOT NULL FIRST',
917
                $this->string()->append('NOT NULL')->first(),
918
                [
919
                    'mysql' => 'varchar(255) NOT NULL FIRST',
920
                    'oci' => 'VARCHAR2(255) NOT NULL',
921
                    'sqlsrv' => 'nvarchar(255) NOT NULL'
922
                ],
923
                [
924
                    'sqlsrv' => 'string NOT NULL'
925
                ]
926
            ],
927
            [
928
                Schema::TYPE_JSON,
929
                $this->json(),
930
                [
931
                    'pgsql' => 'jsonb'
932
                ]
933
            ],
934
        ];
935
936
        foreach ($items as $i => $item) {
937
            if (array_key_exists($this->getConnection()->getDriverName(), $item[2])) {
938
                $item[2] = $item[2][$this->getConnection()->getDriverName()];
939
                $items[$i] = $item;
940
            } else {
941
                unset($items[$i]);
942
            }
943
        }
944
945
        return array_values($items);
946
    }
947
948
    public function testGetColumnType(): void
949
    {
950
        $qb = $this->getQueryBuilder();
0 ignored issues
show
Bug introduced by
It seems like getQueryBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

950
        /** @scrutinizer ignore-call */ 
951
        $qb = $this->getQueryBuilder();
Loading history...
951
952
        foreach ($this->columnTypes() as $item) {
953
            [$column, $builder, $expected] = $item;
954
955
            if (isset($item[3][$this->getConnection()->getDriverName()])) {
956
                $expectedColumnSchemaBuilder = $item[3][$this->getConnection()->getDriverName()];
957
            } elseif (isset($item[3]) && !is_array($item[3])) {
958
                $expectedColumnSchemaBuilder = $item[3];
959
            } else {
960
                $expectedColumnSchemaBuilder = $column;
961
            }
962
963
            $this->assertEquals($expected, $qb->getColumnType($column));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

963
            $this->/** @scrutinizer ignore-call */ 
964
                   assertEquals($expected, $qb->getColumnType($column));
Loading history...
964
            $this->assertEquals($expected, $qb->getColumnType($builder));
965
            $this->assertEquals($expectedColumnSchemaBuilder, $builder->__toString());
966
        }
967
    }
968
969
    public function testCreateTableColumnTypes(): void
970
    {
971
        $qb = $this->getQueryBuilder();
972
973
        if ($qb->getDb()->getTableSchema('column_type_table', true) !== null) {
974
            $this->getConnection(false)->createCommand($qb->dropTable('column_type_table'))->execute();
975
        }
976
977
        $columns = [];
978
        $i = 0;
979
980
        foreach ($this->columnTypes() as [$column, $builder, $expected]) {
981
            if (
982
                !(
983
                    strncmp($column, Schema::TYPE_PK, 2) === 0 ||
984
                    strncmp($column, Schema::TYPE_UPK, 3) === 0 ||
985
                    strncmp($column, Schema::TYPE_BIGPK, 5) === 0 ||
986
                    strncmp($column, Schema::TYPE_UBIGPK, 6) === 0 ||
987
                    strncmp(substr($column, -5), 'FIRST', 5) === 0
988
                )
989
            ) {
990
                $columns['col' . ++$i] = str_replace('CHECK (value', 'CHECK ([[col' . $i . ']]', $column);
991
            }
992
        }
993
994
        $this->getConnection(false)->createCommand($qb->createTable('column_type_table', $columns))->execute();
995
996
        $this->assertNotEmpty($qb->getDb()->getTableSchema('column_type_table', true));
0 ignored issues
show
Bug introduced by
It seems like assertNotEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

996
        $this->/** @scrutinizer ignore-call */ 
997
               assertNotEmpty($qb->getDb()->getTableSchema('column_type_table', true));
Loading history...
997
    }
998
999
    public function testBuildWhereExistsWithParameters(): void
1000
    {
1001
        $db = $this->getConnection();
1002
1003
        $expectedQuerySql = $this->replaceQuotes(
0 ignored issues
show
Bug introduced by
It seems like replaceQuotes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1003
        /** @scrutinizer ignore-call */ 
1004
        $expectedQuerySql = $this->replaceQuotes(
Loading history...
1004
            'SELECT [[id]] FROM [[TotalExample]] [[t]] WHERE (EXISTS (SELECT [[1]] FROM [[Website]] [[w]]'
1005
            . ' WHERE (w.id = t.website_id) AND (w.merchant_id = :merchant_id))) AND (t.some_column = :some_value)'
1006
        );
1007
1008
        $expectedQueryParams = [':some_value' => 'asd', ':merchant_id' => 6];
1009
1010
        $subQuery = new Query($db);
1011
1012
        $subQuery->select('1')
1013
            ->from('Website w')
1014
            ->where('w.id = t.website_id')
1015
            ->andWhere('w.merchant_id = :merchant_id', [':merchant_id' => 6]);
1016
1017
        $query = new Query($db);
1018
1019
        $query->select('id')
1020
            ->from('TotalExample t')
1021
            ->where(['exists', $subQuery])
1022
            ->andWhere('t.some_column = :some_value', [':some_value' => 'asd']);
1023
1024
        [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query);
1025
1026
        $this->assertEquals($expectedQuerySql, $actualQuerySql);
1027
        $this->assertEquals($expectedQueryParams, $queryParams);
1028
    }
1029
1030
    public function testBuildWhereExistsWithArrayParameters(): void
1031
    {
1032
        $db = $this->getConnection();
1033
1034
        $expectedQuerySql = $this->replaceQuotes(
1035
            'SELECT [[id]] FROM [[TotalExample]] [[t]] WHERE (EXISTS (SELECT [[1]] FROM [[Website]] [[w]]'
1036
            . ' WHERE (w.id = t.website_id) AND (([[w]].[[merchant_id]]=:qp0) AND ([[w]].[[user_id]]=:qp1))))'
1037
            . ' AND ([[t]].[[some_column]]=:qp2)'
1038
        );
1039
1040
        $expectedQueryParams = [':qp0' => 6, ':qp1' => 210, ':qp2' => 'asd'];
1041
1042
        $subQuery = new Query($db);
1043
1044
        $subQuery->select('1')
1045
            ->from('Website w')
1046
            ->where('w.id = t.website_id')
1047
            ->andWhere(['w.merchant_id' => 6, 'w.user_id' => '210']);
1048
1049
        $query = new Query($db);
1050
1051
        $query->select('id')
1052
            ->from('TotalExample t')
1053
            ->where(['exists', $subQuery])
1054
            ->andWhere(['t.some_column' => 'asd']);
1055
1056
        [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query);
1057
1058
        $this->assertEquals($expectedQuerySql, $actualQuerySql);
1059
        $this->assertEquals($expectedQueryParams, $queryParams);
1060
    }
1061
1062
    /**
1063
     * This test contains three select queries connected with UNION and UNION ALL constructions.
1064
     * It could be useful to use "phpunit --group=db --filter testBuildUnion" command for run it.
1065
     */
1066
    public function testBuildUnion(): void
1067
    {
1068
        $db = $this->getConnection();
1069
1070
        $expectedQuerySql = $this->replaceQuotes(
1071
            '(SELECT [[id]] FROM [[TotalExample]] [[t1]] WHERE (w > 0) AND (x < 2)) UNION ( SELECT [[id]]'
1072
            . ' FROM [[TotalTotalExample]] [[t2]] WHERE w > 5 ) UNION ALL ( SELECT [[id]] FROM [[TotalTotalExample]]'
1073
            . ' [[t3]] WHERE w = 3 )'
1074
        );
1075
1076
        $query = new Query($db);
1077
        $secondQuery = new Query($db);
1078
1079
        $secondQuery->select('id')
1080
              ->from('TotalTotalExample t2')
1081
              ->where('w > 5');
1082
1083
        $thirdQuery = new Query($db);
1084
1085
        $thirdQuery->select('id')
1086
              ->from('TotalTotalExample t3')
1087
              ->where('w = 3');
1088
1089
        $query->select('id')
1090
              ->from('TotalExample t1')
1091
              ->where(['and', 'w > 0', 'x < 2'])
1092
              ->union($secondQuery)
1093
              ->union($thirdQuery, true);
1094
1095
        [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query);
1096
1097
        $this->assertEquals($expectedQuerySql, $actualQuerySql);
1098
        $this->assertEquals([], $queryParams);
1099
    }
1100
1101
    public function testBuildWithQuery(): void
1102
    {
1103
        $db = $this->getConnection();
1104
1105
        $expectedQuerySql = $this->replaceQuotes(
1106
            'WITH a1 AS (SELECT [[id]] FROM [[t1]] WHERE expr = 1), a2 AS ((SELECT [[id]] FROM [[t2]]'
1107
            . ' INNER JOIN [[a1]] ON t2.id = a1.id WHERE expr = 2) UNION ( SELECT [[id]] FROM [[t3]] WHERE expr = 3 ))'
1108
            . ' SELECT * FROM [[a2]]'
1109
        );
1110
1111
        $with1Query = (new Query($db))
1112
            ->select('id')
1113
            ->from('t1')
1114
            ->where('expr = 1');
1115
1116
        $with2Query = (new Query($db))
1117
            ->select('id')
1118
            ->from('t2')
1119
            ->innerJoin('a1', 't2.id = a1.id')
1120
            ->where('expr = 2');
1121
1122
        $with3Query = (new Query($db))
1123
            ->select('id')
1124
            ->from('t3')
1125
            ->where('expr = 3');
1126
1127
        $query = (new Query($db))
1128
            ->withQuery($with1Query, 'a1')
1129
            ->withQuery($with2Query->union($with3Query), 'a2')
1130
            ->from('a2');
1131
1132
        [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query);
1133
1134
        $this->assertEquals($expectedQuerySql, $actualQuerySql);
1135
        $this->assertEquals([], $queryParams);
1136
    }
1137
1138
    public function testBuildWithQueryRecursive(): void
1139
    {
1140
        $db = $this->getConnection();
1141
1142
        $expectedQuerySql = $this->replaceQuotes(
1143
            'WITH RECURSIVE a1 AS (SELECT [[id]] FROM [[t1]] WHERE expr = 1) SELECT * FROM [[a1]]'
1144
        );
1145
1146
        $with1Query = (new Query($db))
1147
            ->select('id')
1148
            ->from('t1')
1149
            ->where('expr = 1');
1150
1151
        $query = (new Query($db))
1152
            ->withQuery($with1Query, 'a1', true)
1153
            ->from('a1');
1154
1155
        [$actualQuerySql, $queryParams] = $this->getQueryBuilder()->build($query);
1156
1157
        $this->assertEquals($expectedQuerySql, $actualQuerySql);
1158
        $this->assertEquals([], $queryParams);
1159
    }
1160
1161
    public function testSelectSubquery(): void
1162
    {
1163
        $db = $this->getConnection();
1164
1165
        $subquery = (new Query($db))
1166
            ->select('COUNT(*)')
1167
            ->from('operations')
1168
            ->where('account_id = accounts.id');
1169
1170
        $query = (new Query($db))
1171
            ->select('*')
1172
            ->from('accounts')
1173
            ->addSelect(['operations_count' => $subquery]);
1174
1175
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1176
1177
        $expected = $this->replaceQuotes(
1178
            'SELECT *, (SELECT COUNT(*) FROM [[operations]] WHERE account_id = accounts.id) AS [[operations_count]]'
1179
            . ' FROM [[accounts]]'
1180
        );
1181
1182
        $this->assertEquals($expected, $sql);
1183
        $this->assertEmpty($params);
0 ignored issues
show
Bug introduced by
It seems like assertEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1183
        $this->/** @scrutinizer ignore-call */ 
1184
               assertEmpty($params);
Loading history...
1184
    }
1185
1186
    public function testComplexSelect(): void
1187
    {
1188
        $db = $this->getConnection();
1189
1190
        $query = (new Query($db))
1191
            ->select([
1192
                'ID' => 't.id',
1193
                'gsm.username as GSM',
1194
                'part.Part',
1195
                'Part Cost' => 't.Part_Cost',
1196
                'st_x(location::geometry) as lon',
1197
                new Expression(
1198
                    $this->replaceQuotes(
1199
                        "case t.Status_Id when 1 then 'Acknowledge' when 2 then 'No Action' else 'Unknown Action'"
1200
                        . " END as [[Next Action]]"
1201
                    )
1202
                ),
1203
            ])
1204
            ->from('tablename');
1205
1206
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1207
1208
        $expected = $this->replaceQuotes(
1209
            'SELECT [[t]].[[id]] AS [[ID]], [[gsm]].[[username]] AS [[GSM]], [[part]].[[Part]], [[t]].[[Part_Cost]]'
1210
            . ' AS [[Part Cost]], st_x(location::geometry) AS [[lon]], case t.Status_Id when 1 then \'Acknowledge\''
1211
            . ' when 2 then \'No Action\' else \'Unknown Action\' END as [[Next Action]] FROM [[tablename]]'
1212
        );
1213
1214
        $this->assertEquals($expected, $sql);
1215
        $this->assertEmpty($params);
1216
    }
1217
1218
    public function testSelectExpression(): void
1219
    {
1220
        $db = $this->getConnection();
1221
1222
        $query = (new Query($db))
1223
            ->select(new Expression('1 AS ab'))
1224
            ->from('tablename');
1225
1226
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1227
1228
        $expected = $this->replaceQuotes('SELECT 1 AS ab FROM [[tablename]]');
1229
1230
        $this->assertEquals($expected, $sql);
1231
        $this->assertEmpty($params);
1232
1233
        $query = (new Query($db))
1234
            ->select(new Expression('1 AS ab'))
1235
            ->addSelect(new Expression('2 AS cd'))
1236
            ->addSelect(['ef' => new Expression('3')])
1237
            ->from('tablename');
1238
1239
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1240
1241
        $expected = $this->replaceQuotes('SELECT 1 AS ab, 2 AS cd, 3 AS [[ef]] FROM [[tablename]]');
1242
1243
        $this->assertEquals($expected, $sql);
1244
        $this->assertEmpty($params);
1245
1246
        $query = (new Query($db))
1247
            ->select(new Expression('SUBSTR(name, 0, :len)', [':len' => 4]))
1248
            ->from('tablename');
1249
1250
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1251
1252
        $expected = $this->replaceQuotes('SELECT SUBSTR(name, 0, :len) FROM [[tablename]]');
1253
1254
        $this->assertEquals($expected, $sql);
1255
        $this->assertEquals([':len' => 4], $params);
1256
    }
1257
1258
    /**
1259
     * {@see https://github.com/yiisoft/yii2/issues/10869}
1260
     */
1261
    public function testFromIndexHint(): void
1262
    {
1263
        $db = $this->getConnection();
1264
1265
        $query = (new Query($db))->from([new Expression('{{%user}} USE INDEX (primary)')]);
1266
1267
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1268
1269
        $expected = $this->replaceQuotes('SELECT * FROM {{%user}} USE INDEX (primary)');
1270
1271
        $this->assertEquals($expected, $sql);
1272
        $this->assertEmpty($params);
1273
1274
        $query = (new Query($db))
1275
            ->from([new Expression('{{user}} {{t}} FORCE INDEX (primary) IGNORE INDEX FOR ORDER BY (i1)')])
1276
            ->leftJoin(['p' => 'profile'], 'user.id = profile.user_id USE INDEX (i2)');
1277
1278
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1279
1280
        $expected = $this->replaceQuotes(
1281
            'SELECT * FROM {{user}} {{t}} FORCE INDEX (primary) IGNORE INDEX FOR ORDER BY (i1)'
1282
            . ' LEFT JOIN [[profile]] [[p]] ON user.id = profile.user_id USE INDEX (i2)'
1283
        );
1284
1285
        $this->assertEquals($expected, $sql);
1286
        $this->assertEmpty($params);
1287
    }
1288
1289
    public function testFromSubquery(): void
1290
    {
1291
        $db = $this->getConnection();
1292
1293
        /* query subquery */
1294
        $subquery = (new Query($db))->from('user')->where('account_id = accounts.id');
1295
1296
        $query = (new Query($db))->from(['activeusers' => $subquery]);
1297
1298
        /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */
1299
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1300
1301
        $expected = $this->replaceQuotes(
1302
            'SELECT * FROM (SELECT * FROM [[user]] WHERE account_id = accounts.id) [[activeusers]]'
1303
        );
1304
1305
        $this->assertEquals($expected, $sql);
1306
        $this->assertEmpty($params);
1307
1308
        /* query subquery with params */
1309
        $subquery = (new Query($db))->from('user')->where('account_id = :id', ['id' => 1]);
1310
1311
        $query = (new Query($db))->from(['activeusers' => $subquery])->where('abc = :abc', ['abc' => 'abc']);
1312
1313
        /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */
1314
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1315
1316
        $expected = $this->replaceQuotes(
1317
            'SELECT * FROM (SELECT * FROM [[user]] WHERE account_id = :id) [[activeusers]] WHERE abc = :abc'
1318
        );
1319
1320
        $this->assertEquals($expected, $sql);
1321
        $this->assertEquals(['id'  => 1, 'abc' => 'abc'], $params);
1322
1323
        /* simple subquery */
1324
        $subquery = '(SELECT * FROM user WHERE account_id = accounts.id)';
1325
1326
        $query = (new Query($db))->from(['activeusers' => $subquery]);
1327
1328
        /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */
1329
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1330
1331
        $expected = $this->replaceQuotes(
1332
            'SELECT * FROM (SELECT * FROM user WHERE account_id = accounts.id) [[activeusers]]'
1333
        );
1334
1335
        $this->assertEquals($expected, $sql);
1336
        $this->assertEmpty($params);
1337
    }
1338
1339
    public function testOrderBy(): void
1340
    {
1341
        $db = $this->getConnection();
1342
1343
        /* simple string */
1344
        $query = (new Query($db))
1345
            ->select('*')
1346
            ->from('operations')
1347
            ->orderBy('name ASC, date DESC');
1348
1349
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1350
1351
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC');
1352
1353
        $this->assertEquals($expected, $sql);
1354
        $this->assertEmpty($params);
1355
1356
        /* array syntax */
1357
        $query = (new Query($db))
1358
            ->select('*')
1359
            ->from('operations')
1360
            ->orderBy(['name' => SORT_ASC, 'date' => SORT_DESC]);
1361
1362
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1363
1364
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC');
1365
1366
        $this->assertEquals($expected, $sql);
1367
        $this->assertEmpty($params);
1368
1369
        /* expression */
1370
        $query = (new Query($db))
1371
            ->select('*')
1372
            ->from('operations')
1373
            ->where('account_id = accounts.id')
1374
            ->orderBy(new Expression('SUBSTR(name, 3, 4) DESC, x ASC'));
1375
1376
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1377
1378
        $expected = $this->replaceQuotes(
1379
            'SELECT * FROM [[operations]] WHERE account_id = accounts.id ORDER BY SUBSTR(name, 3, 4) DESC, x ASC'
1380
        );
1381
1382
        $this->assertEquals($expected, $sql);
1383
        $this->assertEmpty($params);
1384
1385
        /* expression with params */
1386
        $query = (new Query($db))
1387
            ->select('*')
1388
            ->from('operations')
1389
            ->orderBy(new Expression('SUBSTR(name, 3, :to) DESC, x ASC', [':to' => 4]));
1390
1391
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1392
1393
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY SUBSTR(name, 3, :to) DESC, x ASC');
1394
1395
        $this->assertEquals($expected, $sql);
1396
        $this->assertEquals([':to' => 4], $params);
1397
    }
1398
1399
    public function testGroupBy(): void
1400
    {
1401
        $db = $this->getConnection();
1402
1403
        /* simple string */
1404
        $query = (new Query($db))
1405
            ->select('*')
1406
            ->from('operations')
1407
            ->groupBy('name, date');
1408
1409
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1410
1411
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]');
1412
1413
        $this->assertEquals($expected, $sql);
1414
        $this->assertEmpty($params);
1415
1416
        /* array syntax */
1417
        $query = (new Query($db))
1418
            ->select('*')
1419
            ->from('operations')
1420
            ->groupBy(['name', 'date']);
1421
1422
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1423
1424
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]');
1425
1426
        $this->assertEquals($expected, $sql);
1427
        $this->assertEmpty($params);
1428
1429
        /* expression */
1430
        $query = (new Query($db))
1431
            ->select('*')
1432
            ->from('operations')
1433
            ->where('account_id = accounts.id')
1434
            ->groupBy(new Expression('SUBSTR(name, 0, 1), x'));
1435
1436
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1437
1438
        $expected = $this->replaceQuotes(
1439
            'SELECT * FROM [[operations]] WHERE account_id = accounts.id GROUP BY SUBSTR(name, 0, 1), x'
1440
        );
1441
1442
        $this->assertEquals($expected, $sql);
1443
        $this->assertEmpty($params);
1444
1445
        /* expression with params */
1446
        $query = (new Query($db))
1447
            ->select('*')
1448
            ->from('operations')
1449
            ->groupBy(new Expression('SUBSTR(name, 0, :to), x', [':to' => 4]));
1450
1451
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1452
1453
        $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY SUBSTR(name, 0, :to), x');
1454
1455
        $this->assertEquals($expected, $sql);
1456
        $this->assertEquals([':to' => 4], $params);
1457
    }
1458
1459
    /**
1460
     * Dummy test to speed up QB's tests which rely on DB schema.
1461
     */
1462
    public function testInitFixtures(): void
1463
    {
1464
        $this->assertInstanceOf(QueryBuilder::class, $this->getQueryBuilder(true, true));
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1464
        $this->/** @scrutinizer ignore-call */ 
1465
               assertInstanceOf(QueryBuilder::class, $this->getQueryBuilder(true, true));
Loading history...
1465
    }
1466
1467
    /**
1468
     * {@see https://github.com/yiisoft/yii2/issues/15653}
1469
     */
1470
    public function testIssue15653(): void
1471
    {
1472
        $db = $this->getConnection();
1473
1474
        $query = (new Query($db))
1475
            ->from('admin_user')
1476
            ->where(['is_deleted' => false]);
1477
1478
        $query
1479
            ->where([])
1480
            ->andWhere(['in', 'id', ['1', '0']]);
1481
1482
        [$sql, $params] = $this->getQueryBuilder()->build($query);
1483
1484
        $this->assertSame($this->replaceQuotes('SELECT * FROM [[admin_user]] WHERE [[id]] IN (:qp0, :qp1)'), $sql);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1484
        $this->/** @scrutinizer ignore-call */ 
1485
               assertSame($this->replaceQuotes('SELECT * FROM [[admin_user]] WHERE [[id]] IN (:qp0, :qp1)'), $sql);
Loading history...
1485
        $this->assertSame([':qp0' => '1', ':qp1' => '0'], $params);
1486
    }
1487
1488
    public function addDropChecksProviderTrait(): array
1489
    {
1490
        $tableName = 'T_constraints_1';
1491
        $name = 'CN_check';
1492
1493
        return [
1494
            'drop' => [
1495
                "ALTER TABLE {{{$tableName}}} DROP CONSTRAINT [[$name]]",
1496
                static function (QueryBuilder $qb) use ($tableName, $name) {
1497
                    return $qb->dropCheck($name, $tableName);
1498
                },
1499
            ],
1500
            'add' => [
1501
                "ALTER TABLE {{{$tableName}}} ADD CONSTRAINT [[$name]] CHECK ([[C_not_null]] > 100)",
1502
                static function (QueryBuilder $qb) use ($tableName, $name) {
1503
                    return $qb->addCheck($name, $tableName, '[[C_not_null]] > 100');
1504
                },
1505
            ],
1506
        ];
1507
    }
1508
1509
    public function addDropForeignKeysTrait(): array
1510
    {
1511
        $tableName = 'T_constraints_3';
1512
        $name = 'CN_constraints_3';
1513
        $pkTableName = 'T_constraints_2';
1514
1515
        return [
1516
            'drop' => [
1517
                "ALTER TABLE {{{$tableName}}} DROP CONSTRAINT [[$name]]",
1518
                static function (QueryBuilder $qb) use ($tableName, $name) {
1519
                    return $qb->dropForeignKey($name, $tableName);
1520
                },
1521
            ],
1522
            'add' => [
1523
                "ALTER TABLE {{{$tableName}}} ADD CONSTRAINT [[$name]] FOREIGN KEY ([[C_fk_id_1]])"
1524
                . " REFERENCES {{{$pkTableName}}} ([[C_id_1]]) ON DELETE CASCADE ON UPDATE CASCADE",
1525
                static function (QueryBuilder $qb) use ($tableName, $name, $pkTableName) {
1526
                    return $qb->addForeignKey(
1527
                        $name,
1528
                        $tableName,
1529
                        'C_fk_id_1',
1530
                        $pkTableName,
1531
                        'C_id_1',
1532
                        'CASCADE',
1533
                        'CASCADE'
1534
                    );
1535
                },
1536
            ],
1537
            'add (2 columns)' => [
1538
                "ALTER TABLE {{{$tableName}}} ADD CONSTRAINT [[$name]] FOREIGN KEY ([[C_fk_id_1]], [[C_fk_id_2]])"
1539
                . " REFERENCES {{{$pkTableName}}} ([[C_id_1]], [[C_id_2]]) ON DELETE CASCADE ON UPDATE CASCADE",
1540
                static function (QueryBuilder $qb) use ($tableName, $name, $pkTableName) {
1541
                    return $qb->addForeignKey(
1542
                        $name,
1543
                        $tableName,
1544
                        'C_fk_id_1, C_fk_id_2',
1545
                        $pkTableName,
1546
                        'C_id_1, C_id_2',
1547
                        'CASCADE',
1548
                        'CASCADE'
1549
                    );
1550
                },
1551
            ],
1552
        ];
1553
    }
1554
1555
    public function addDropPrimaryKeysTrait(): array
1556
    {
1557
        $tableName = 'T_constraints_1';
1558
        $name = 'CN_pk';
1559
1560
        return [
1561
            'drop' => [
1562
                "ALTER TABLE {{{$tableName}}} DROP CONSTRAINT [[$name]]",
1563
                static function (QueryBuilder $qb) use ($tableName, $name) {
1564
                    return $qb->dropPrimaryKey($name, $tableName);
1565
                },
1566
            ],
1567
            'add' => [
1568
                "ALTER TABLE {{{$tableName}}} ADD CONSTRAINT [[$name]] PRIMARY KEY ([[C_id_1]])",
1569
                static function (QueryBuilder $qb) use ($tableName, $name) {
1570
                    return $qb->addPrimaryKey($name, $tableName, 'C_id_1');
1571
                },
1572
            ],
1573
            'add (2 columns)' => [
1574
                "ALTER TABLE {{{$tableName}}} ADD CONSTRAINT [[$name]] PRIMARY KEY ([[C_id_1]], [[C_id_2]])",
1575
                static function (QueryBuilder $qb) use ($tableName, $name) {
1576
                    return $qb->addPrimaryKey($name, $tableName, 'C_id_1, C_id_2');
1577
                },
1578
            ],
1579
        ];
1580
    }
1581
1582
    public function addDropUniquesProviderTrait(): array
1583
    {
1584
        $tableName1 = 'T_constraints_1';
1585
        $name1 = 'CN_unique';
1586
        $tableName2 = 'T_constraints_2';
1587
        $name2 = 'CN_constraints_2_multi';
1588
1589
        return [
1590
            'drop' => [
1591
                "ALTER TABLE {{{$tableName1}}} DROP CONSTRAINT [[$name1]]",
1592
                static function (QueryBuilder $qb) use ($tableName1, $name1) {
1593
                    return $qb->dropUnique($name1, $tableName1);
1594
                },
1595
            ],
1596
            'add' => [
1597
                "ALTER TABLE {{{$tableName1}}} ADD CONSTRAINT [[$name1]] UNIQUE ([[C_unique]])",
1598
                static function (QueryBuilder $qb) use ($tableName1, $name1) {
1599
                    return $qb->addUnique($name1, $tableName1, 'C_unique');
1600
                },
1601
            ],
1602
            'add (2 columns)' => [
1603
                "ALTER TABLE {{{$tableName2}}} ADD CONSTRAINT [[$name2]] UNIQUE ([[C_index_2_1]], [[C_index_2_2]])",
1604
                static function (QueryBuilder $qb) use ($tableName2, $name2) {
1605
                    return $qb->addUnique($name2, $tableName2, 'C_index_2_1, C_index_2_2');
1606
                },
1607
            ],
1608
        ];
1609
    }
1610
1611
    public function batchInsertProviderTrait(): array
1612
    {
1613
        return [
1614
            [
1615
                'customer',
1616
                ['email', 'name', 'address'],
1617
                [['[email protected]', 'silverfire', 'Kyiv {{city}}, Ukraine']],
1618
                $this->replaceQuotes(
1619
                    "INSERT INTO [[customer]] ([[email]], [[name]], [[address]])"
1620
                    . " VALUES ('[email protected]', 'silverfire', 'Kyiv {{city}}, Ukraine')"
1621
                )
1622
            ],
1623
            'escape-danger-chars' => [
1624
                'customer',
1625
                ['address'],
1626
                [["SQL-danger chars are escaped: '); --"]],
1627
                'expected' => $this->replaceQuotes(
1628
                    "INSERT INTO [[customer]] ([[address]]) VALUES ('SQL-danger chars are escaped: \'); --')"
1629
                )
1630
            ],
1631
            [
1632
                'customer',
1633
                ['address'],
1634
                [],
1635
                ''
1636
            ],
1637
            [
1638
                'customer',
1639
                [],
1640
                [['no columns passed']],
1641
                $this->replaceQuotes("INSERT INTO [[customer]] () VALUES ('no columns passed')")
1642
            ],
1643
            'bool-false, bool2-null' => [
1644
                'type',
1645
                ['bool_col', 'bool_col2'],
1646
                [[false, null]],
1647
                'expected' => $this->replaceQuotes(
1648
                    'INSERT INTO [[type]] ([[bool_col]], [[bool_col2]]) VALUES (0, NULL)'
1649
                )
1650
            ],
1651
            [
1652
                '{{%type}}',
1653
                ['{{%type}}.[[float_col]]', '[[time]]'],
1654
                [[null, new Expression('now()')]],
1655
                'INSERT INTO {{%type}} ({{%type}}.[[float_col]], [[time]]) VALUES (NULL, now())'
1656
            ],
1657
            'bool-false, time-now()' => [
1658
                '{{%type}}',
1659
                ['{{%type}}.[[bool_col]]', '[[time]]'],
1660
                [[false, new Expression('now()')]],
1661
                'expected' => 'INSERT INTO {{%type}} ({{%type}}.[[bool_col]], [[time]]) VALUES (0, now())'
1662
            ],
1663
        ];
1664
    }
1665
1666
    public function buildConditionsProviderTrait(): array
1667
    {
1668
        $conditions = [
1669
            /* empty values */
1670
            [['like', 'name', []], '0=1', []],
1671
            [['not like', 'name', []], '', []],
1672
            [['or like', 'name', []], '0=1', []],
1673
            [['or not like', 'name', []], '', []],
1674
1675
            /* not */
1676
            [['not', 'name'], 'NOT (name)', []],
1677
            [
1678
                [
1679
                    'not',
1680
                    (new Query($this->getConnection()))
1681
                        ->select('exists')
1682
                        ->from('some_table')
1683
                    ],
1684
                'NOT ((SELECT [[exists]] FROM [[some_table]]))', []
1685
            ],
1686
1687
            /* and */
1688
            [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []],
1689
            [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []],
1690
            [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]],
1691
            [
1692
                [
1693
                    'and',
1694
                    ['expired' => false],
1695
                    (new Query($this->getConnection()))
1696
                        ->select('count(*) > 1')
1697
                        ->from('queue')
1698
                    ],
1699
                '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))',
1700
                [':qp0' => false]
1701
            ],
1702
1703
            /* or */
1704
            [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []],
1705
            [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []],
1706
            [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]],
1707
1708
            /* between */
1709
            [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]],
1710
            [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]],
1711
            [
1712
                ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')],
1713
                '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()',
1714
                []
1715
            ],
1716
            [
1717
                ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123],
1718
                '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0',
1719
                [':qp0' => 123]
1720
            ],
1721
            [
1722
                ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')],
1723
                '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()',
1724
                []
1725
            ],
1726
            [
1727
                ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123],
1728
                '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0',
1729
                [':qp0' => 123]
1730
            ],
1731
            [
1732
                new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'),
1733
                ':qp0 BETWEEN [[create_time]] AND [[update_time]]',
1734
                [':qp0' => '2018-02-11']
1735
            ],
1736
            [
1737
                new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'),
1738
                ':qp0 NOT BETWEEN NOW() AND [[update_time]]',
1739
                [':qp0' => '2018-02-11']
1740
            ],
1741
            [
1742
                new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'),
1743
                'NOW() BETWEEN [[create_time]] AND [[update_time]]',
1744
                []
1745
            ],
1746
            [
1747
                new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'),
1748
                'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]',
1749
                []
1750
            ],
1751
            [
1752
                new BetweenColumnsCondition(
1753
                    new Expression('NOW()'),
1754
                    'NOT BETWEEN',
1755
                    (new Query($this->getConnection()))
1756
                        ->select('min_date')
1757
                        ->from('some_table'),
1758
                    'max_date'
1759
                ),
1760
                'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]',
1761
                []
1762
            ],
1763
1764
            /* in */
1765
            [
1766
                [
1767
                    'in',
1768
                    'id',
1769
                    [
1770
                        1,
1771
                        2,
1772
                        (new Query($this->getConnection()))
1773
                            ->select('three')
1774
                            ->from('digits')
1775
                    ]
1776
                ],
1777
                '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))',
1778
                [':qp0' => 1, ':qp1' => 2]
1779
            ],
1780
            [
1781
                ['not in', 'id', [1, 2, 3]],
1782
                '[[id]] NOT IN (:qp0, :qp1, :qp2)',
1783
                [':qp0' => 1, ':qp1' => 2, ':qp2' => 3]
1784
            ],
1785
            [
1786
                [
1787
                    'in',
1788
                    'id',
1789
                    (new Query($this->getConnection()))
1790
                        ->select('id')
1791
                        ->from('users')
1792
                        ->where(['active' => 1])
1793
                ],
1794
                '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
1795
                [':qp0' => 1]
1796
            ],
1797
            [
1798
                [
1799
                    'not in',
1800
                    'id',
1801
                    (new Query($this->getConnection()))
1802
                        ->select('id')
1803
                        ->from('users')
1804
                        ->where(['active' => 1])
1805
                ],
1806
                '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
1807
                [':qp0' => 1]
1808
            ],
1809
            [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]],
1810
            [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]],
1811
            [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]],
1812
            'composite in' => [
1813
                ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]],
1814
                '([[id]], [[name]]) IN ((:qp0, :qp1))',
1815
                [':qp0' => 1, ':qp1' => 'oy'],
1816
            ],
1817
            'composite in (just one column)' => [
1818
                ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]],
1819
                '[[id]] IN (:qp0, :qp1)',
1820
                [':qp0' => 1, ':qp1' => 2],
1821
            ],
1822
            'composite in using array objects (just one column)' => [
1823
                ['in', new TraversableObject(['id']), new TraversableObject([
1824
                    ['id' => 1, 'name' => 'Name1'],
1825
                    ['id' => 2, 'name' => 'Name2'],
1826
                ])],
1827
                '[[id]] IN (:qp0, :qp1)',
1828
                [':qp0' => 1, ':qp1' => 2],
1829
            ],
1830
1831
            /* in using array objects. */
1832
            [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
1833
            [
1834
                ['in', 'id', new TraversableObject([1, 2, 3])],
1835
                '[[id]] IN (:qp0, :qp1, :qp2)',
1836
                [':qp0' => 1, ':qp1' => 2, ':qp2' => 3]
1837
            ],
1838
1839
            /* in using array objects containing null value */
1840
            [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]],
1841
            [
1842
                ['in', 'id', new TraversableObject([1, 2, null])],
1843
                '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2]
1844
            ],
1845
1846
            /* not in using array object containing null value */
1847
            [
1848
                ['not in', 'id', new TraversableObject([1, null])],
1849
                '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1]
1850
            ],
1851
            [
1852
                ['not in', 'id', new TraversableObject([1, 2, null])],
1853
                '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL',
1854
                [':qp0' => 1, ':qp1' => 2]
1855
            ],
1856
1857
            /* in using array object containing only null value */
1858
            [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []],
1859
            [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []],
1860
            'composite in using array objects' => [
1861
                ['in', new TraversableObject(['id', 'name']), new TraversableObject([
1862
                    ['id' => 1, 'name' => 'oy'],
1863
                    ['id' => 2, 'name' => 'yo'],
1864
                ])],
1865
                '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))',
1866
                [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'],
1867
            ],
1868
1869
            /* in object conditions */
1870
            [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]],
1871
            [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]],
1872
            [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]],
1873
            [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]],
1874
            [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
1875
            [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
1876
1877
            /* exists */
1878
            [
1879
                [
1880
                    'exists',
1881
                    (new Query($this->getConnection()))
1882
                        ->select('id')
1883
                        ->from('users')
1884
                        ->where(['active' => 1])
1885
                    ],
1886
                'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
1887
                [':qp0' => 1]
1888
            ],
1889
            [
1890
                [
1891
                    'not exists',
1892
                    (new Query($this->getConnection()))
1893
                        ->select('id')
1894
                        ->from('users')
1895
                        ->where(['active' => 1])
1896
                    ],
1897
                'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1]
1898
            ],
1899
1900
            /* simple conditions */
1901
            [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']],
1902
            [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]],
1903
            [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']],
1904
            [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]],
1905
            [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']],
1906
            [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]],
1907
            [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']],
1908
            [
1909
                ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')],
1910
                '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)',
1911
                []
1912
            ],
1913
            [
1914
                ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])],
1915
                '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)',
1916
                [':month' => 2]
1917
            ],
1918
            [
1919
                [
1920
                    '=',
1921
                    'date',
1922
                    (new Query($this->getConnection()))
1923
                        ->select('max(date)')
1924
                        ->from('test')
1925
                        ->where(['id' => 5])
1926
                ],
1927
                '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)',
1928
                [':qp0' => 5]
1929
            ],
1930
1931
            /* operand1 is Expression */
1932
            [
1933
                ['=', new Expression('date'), '2019-08-01'],
1934
                'date = :qp0',
1935
                [':qp0' => '2019-08-01']
1936
            ],
1937
            [
1938
                [
1939
                    '=',
1940
                    (new Query($this->getConnection()))
1941
                        ->select('COUNT(*)')
1942
                        ->from('test')
1943
                        ->where(['id' => 6]),
1944
                    0
1945
                ],
1946
                '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1',
1947
                [':qp0' => 6, ':qp1' => 0]
1948
            ],
1949
1950
            /* hash condition */
1951
            [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]],
1952
            [
1953
                ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2],
1954
                '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)',
1955
                [':qp0' => 2]
1956
            ],
1957
1958
            /* direct conditions */
1959
            ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []],
1960
            [
1961
                new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']),
1962
                'a = CONCAT(col1, :param1)',
1963
                ['param1' => 'value1']
1964
            ],
1965
1966
            /* Expression with params as operand of 'not' */
1967
            [
1968
                ['not', new Expression('any_expression(:a)', [':a' => 1])],
1969
                'NOT (any_expression(:a))', [':a' => 1]
1970
            ],
1971
            [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]],
1972
        ];
1973
1974
        switch ($this->getConnection()->getDriverName()) {
1975
            case 'sqlsrv':
1976
            case 'sqlite':
1977
                $conditions = array_merge($conditions, [
1978
                    [
1979
                        ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
1980
                        '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))',
1981
                        [':qp0' => 1,
1982
                        ':qp1' => 'foo',
1983
                        ':qp2' => 2,
1984
                        ':qp3' => 'bar']
1985
                    ],
1986
                    [
1987
                        ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
1988
                        '(([[id]] != :qp0 OR [[name]] != :qp1) AND ([[id]] != :qp2 OR [[name]] != :qp3))',
1989
                        [':qp0' => 1,
1990
                        ':qp1' => 'foo',
1991
                        ':qp2' => 2,
1992
                        ':qp3' => 'bar']
1993
                    ],
1994
                    //[['in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id AND a.]]name[[ = ]]name`)', [':qp0' => 1] ],
1995
                    //[ ['not in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'NOT EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id]] AND a.[[name = ]]name`)', [':qp0' => 1] ],
1996
                ]);
1997
1998
                break;
1999
            default:
2000
                $conditions = array_merge($conditions, [
2001
                    [
2002
                        ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
2003
                        '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))',
2004
                        [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar']
2005
                    ],
2006
                    [
2007
                        ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
2008
                        '([[id]], [[name]]) NOT IN ((:qp0, :qp1), (:qp2, :qp3))',
2009
                        [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar']
2010
                    ],
2011
                    [
2012
                        [
2013
                            'in',
2014
                            ['id', 'name'],
2015
                            (new Query($this->getConnection()))
2016
                                ->select(['id', 'name'])
2017
                                ->from('users')
2018
                                ->where(['active' => 1])
2019
                        ],
2020
                        '([[id]], [[name]]) IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)',
2021
                        [':qp0' => 1]
2022
                    ],
2023
                    [
2024
                        [
2025
                            'not in',
2026
                            ['id', 'name'],
2027
                            (new Query($this->getConnection()))
2028
                                ->select(['id', 'name'])
2029
                                ->from('users')
2030
                                ->where(['active' => 1])
2031
                        ],
2032
                        '([[id]], [[name]]) NOT IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)',
2033
                        [':qp0' => 1]
2034
                    ],
2035
                ]);
2036
2037
                break;
2038
        }
2039
2040
        /* adjust dbms specific escaping */
2041
        foreach ($conditions as $i => $condition) {
2042
            $conditions[$i][1] = $this->replaceQuotes($condition[1]);
2043
        }
2044
2045
        return $conditions;
2046
    }
2047
2048
    public function buildFilterConditionProviderTrait(): array
2049
    {
2050
        $conditions = [
2051
            /* like */
2052
            [['like', 'name', []], '', []],
2053
            [['not like', 'name', []], '', []],
2054
            [['or like', 'name', []], '', []],
2055
            [['or not like', 'name', []], '', []],
2056
2057
            /* not */
2058
            [['not', ''], '', []],
2059
2060
            /* and */
2061
            [['and', '', ''], '', []],
2062
            [['and', '', 'id=2'], 'id=2', []],
2063
            [['and', 'id=1', ''], 'id=1', []],
2064
            [['and', 'type=1', ['or', '', 'id=2']], '(type=1) AND (id=2)', []],
2065
2066
            /* or */
2067
            [['or', 'id=1', ''], 'id=1', []],
2068
            [['or', 'type=1', ['or', '', 'id=2']], '(type=1) OR (id=2)', []],
2069
2070
            /* between */
2071
            [['between', 'id', 1, null], '', []],
2072
            [['not between', 'id', null, 10], '', []],
2073
2074
            /* in */
2075
            [['in', 'id', []], '', []],
2076
            [['not in', 'id', []], '', []],
2077
2078
            /* simple conditions */
2079
            [['=', 'a', ''], '', []],
2080
            [['>', 'a', ''], '', []],
2081
            [['>=', 'a', ''], '', []],
2082
            [['<', 'a', ''], '', []],
2083
            [['<=', 'a', ''], '', []],
2084
            [['<>', 'a', ''], '', []],
2085
            [['!=', 'a', ''], '', []],
2086
        ];
2087
2088
        /* adjust dbms specific escaping */
2089
        foreach ($conditions as $i => $condition) {
2090
            $conditions[$i][1] = $this->replaceQuotes($condition[1]);
2091
        }
2092
2093
        return $conditions;
2094
    }
2095
2096
    public function buildFromDataProviderTrait(): array
2097
    {
2098
        return [
2099
            ['test t1', '[[test]] [[t1]]'],
2100
            ['test as t1', '[[test]] [[t1]]'],
2101
            ['test AS t1', '[[test]] [[t1]]'],
2102
            ['test', '[[test]]'],
2103
        ];
2104
    }
2105
2106
    public function buildLikeConditionsProviderTrait(): array
2107
    {
2108
        $conditions = [
2109
            /* simple like */
2110
            [['like', 'name', 'foo%'], '[[name]] LIKE :qp0', [':qp0' => '%foo\%%']],
2111
            [['not like', 'name', 'foo%'], '[[name]] NOT LIKE :qp0', [':qp0' => '%foo\%%']],
2112
            [['or like', 'name', 'foo%'], '[[name]] LIKE :qp0', [':qp0' => '%foo\%%']],
2113
            [['or not like', 'name', 'foo%'], '[[name]] NOT LIKE :qp0', [':qp0' => '%foo\%%']],
2114
2115
            /* like for many values */
2116
            [
2117
                ['like', 'name', ['foo%', '[abc]']],
2118
                '[[name]] LIKE :qp0 AND [[name]] LIKE :qp1',
2119
                [':qp0' => '%foo\%%', ':qp1' => '%[abc]%']
2120
            ],
2121
            [
2122
                ['not like', 'name', ['foo%', '[abc]']],
2123
                '[[name]] NOT LIKE :qp0 AND [[name]] NOT LIKE :qp1',
2124
                [':qp0' => '%foo\%%', ':qp1' => '%[abc]%']
2125
            ],
2126
            [
2127
                ['or like', 'name', ['foo%', '[abc]']],
2128
                '[[name]] LIKE :qp0 OR [[name]] LIKE :qp1',
2129
                [':qp0' => '%foo\%%', ':qp1' => '%[abc]%']
2130
            ],
2131
            [
2132
                ['or not like', 'name', ['foo%', '[abc]']],
2133
                '[[name]] NOT LIKE :qp0 OR [[name]] NOT LIKE :qp1',
2134
                [':qp0' => '%foo\%%', ':qp1' => '%[abc]%']
2135
            ],
2136
2137
            /* like with Expression */
2138
            [
2139
                ['like', 'name', new Expression('CONCAT("test", name, "%")')],
2140
                '[[name]] LIKE CONCAT("test", name, "%")',
2141
                []
2142
            ],
2143
            [
2144
                ['not like', 'name', new Expression('CONCAT("test", name, "%")')],
2145
                '[[name]] NOT LIKE CONCAT("test", name, "%")',
2146
                []
2147
            ],
2148
            [
2149
                ['or like', 'name', new Expression('CONCAT("test", name, "%")')],
2150
                '[[name]] LIKE CONCAT("test", name, "%")',
2151
                []
2152
            ],
2153
            [
2154
                ['or not like', 'name', new Expression('CONCAT("test", name, "%")')],
2155
                '[[name]] NOT LIKE CONCAT("test", name, "%")',
2156
                []
2157
            ],
2158
            [
2159
                ['like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']],
2160
                '[[name]] LIKE CONCAT("test", name, "%") AND [[name]] LIKE :qp0',
2161
                [':qp0' => '%\\\ab\_c%']
2162
            ],
2163
            [
2164
                ['not like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']],
2165
                '[[name]] NOT LIKE CONCAT("test", name, "%") AND [[name]] NOT LIKE :qp0',
2166
                [':qp0' => '%\\\ab\_c%']
2167
            ],
2168
            [
2169
                ['or like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']],
2170
                '[[name]] LIKE CONCAT("test", name, "%") OR [[name]] LIKE :qp0',
2171
                [':qp0' => '%\\\ab\_c%']
2172
            ],
2173
            [
2174
                ['or not like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']],
2175
                '[[name]] NOT LIKE CONCAT("test", name, "%") OR [[name]] NOT LIKE :qp0',
2176
                [':qp0' => '%\\\ab\_c%']
2177
            ],
2178
2179
            /**
2180
             * {@see https://github.com/yiisoft/yii2/issues/15630}
2181
             */
2182
            [
2183
                ['like', 'location.title_ru', 'vi%', false],
2184
                '[[location]].[[title_ru]] LIKE :qp0',
2185
                [':qp0' => 'vi%'],
2186
            ],
2187
2188
            /* like object conditions */
2189
            [
2190
                new LikeCondition('name', 'like', new Expression('CONCAT("test", name, "%")')),
2191
                '[[name]] LIKE CONCAT("test", name, "%")',
2192
                []
2193
            ],
2194
            [
2195
                new LikeCondition('name', 'not like', new Expression('CONCAT("test", name, "%")')),
2196
                '[[name]] NOT LIKE CONCAT("test", name, "%")',
2197
                []
2198
            ],
2199
            [
2200
                new LikeCondition('name', 'or like', new Expression('CONCAT("test", name, "%")')),
2201
                '[[name]] LIKE CONCAT("test", name, "%")',
2202
                []
2203
            ],
2204
            [
2205
                new LikeCondition('name', 'or not like', new Expression('CONCAT("test", name, "%")')),
2206
                '[[name]] NOT LIKE CONCAT("test", name, "%")',
2207
                []
2208
            ],
2209
            [
2210
                new LikeCondition('name', 'like', [new Expression('CONCAT("test", name, "%")'), '\ab_c']),
2211
                '[[name]] LIKE CONCAT("test", name, "%") AND [[name]] LIKE :qp0',
2212
                [':qp0' => '%\\\ab\_c%']
2213
            ],
2214
            [
2215
                new LikeCondition('name', 'not like', [new Expression('CONCAT("test", name, "%")'), '\ab_c']),
2216
                '[[name]] NOT LIKE CONCAT("test", name, "%") AND [[name]] NOT LIKE :qp0',
2217
                [':qp0' => '%\\\ab\_c%']
2218
            ],
2219
            [
2220
                new LikeCondition('name', 'or like', [new Expression('CONCAT("test", name, "%")'), '\ab_c']),
2221
                '[[name]] LIKE CONCAT("test", name, "%") OR [[name]] LIKE :qp0', [':qp0' => '%\\\ab\_c%']
2222
            ],
2223
            [
2224
                new LikeCondition('name', 'or not like', [new Expression('CONCAT("test", name, "%")'), '\ab_c']),
2225
                '[[name]] NOT LIKE CONCAT("test", name, "%") OR [[name]] NOT LIKE :qp0', [':qp0' => '%\\\ab\_c%']
2226
            ],
2227
2228
            /* like with expression as columnName */
2229
            [['like', new Expression('name'), 'teststring'], 'name LIKE :qp0', [':qp0' => "%teststring%"]],
2230
        ];
2231
2232
        /* adjust dbms specific escaping */
2233
        foreach ($conditions as $i => $condition) {
2234
            $conditions[$i][1] = $this->replaceQuotes($condition[1]);
2235
            if (!empty($this->likeEscapeCharSql)) {
2236
                preg_match_all('/(?P<condition>LIKE.+?)( AND| OR|$)/', $conditions[$i][1], $matches, PREG_SET_ORDER);
2237
2238
                foreach ($matches as $match) {
2239
                    $conditions[$i][1] = str_replace(
2240
                        $match['condition'],
2241
                        $match['condition'] . $this->likeEscapeCharSql,
2242
                        $conditions[$i][1]
2243
                    );
2244
                }
2245
            }
2246
2247
            foreach ($conditions[$i][2] as $name => $value) {
2248
                $conditions[$i][2][$name] = strtr($conditions[$i][2][$name], $this->likeParameterReplacements);
2249
            }
2250
        }
2251
2252
        return $conditions;
2253
    }
2254
2255
    public function buildExistsParamsProviderTrait(): array
2256
    {
2257
        return [
2258
            [
2259
                'exists',
2260
                $this->replaceQuotes(
2261
                    'SELECT [[id]] FROM [[TotalExample]] [[t]] WHERE EXISTS (SELECT [[1]] FROM [[Website]] [[w]])'
2262
                )
2263
            ],
2264
            [
2265
                'not exists',
2266
                $this->replaceQuotes(
2267
                    'SELECT [[id]] FROM [[TotalExample]] [[t]] WHERE NOT EXISTS (SELECT [[1]] FROM [[Website]] [[w]])'
2268
                )
2269
            ],
2270
        ];
2271
    }
2272
2273
    public function createDropIndexesProviderTrait(): array
2274
    {
2275
        $tableName = 'T_constraints_2';
2276
        $name1 = 'CN_constraints_2_single';
2277
        $name2 = 'CN_constraints_2_multi';
2278
2279
        return [
2280
            'drop' => [
2281
                "DROP INDEX [[$name1]] ON {{{$tableName}}}",
2282
                static function (QueryBuilder $qb) use ($tableName, $name1) {
2283
                    return $qb->dropIndex($name1, $tableName);
2284
                }
2285
            ],
2286
            'create' => [
2287
                "CREATE INDEX [[$name1]] ON {{{$tableName}}} ([[C_index_1]])",
2288
                static function (QueryBuilder $qb) use ($tableName, $name1) {
2289
                    return $qb->createIndex($name1, $tableName, 'C_index_1');
2290
                }
2291
            ],
2292
            'create (2 columns)' => [
2293
                "CREATE INDEX [[$name2]] ON {{{$tableName}}} ([[C_index_2_1]], [[C_index_2_2]])",
2294
                static function (QueryBuilder $qb) use ($tableName, $name2) {
2295
                    return $qb->createIndex($name2, $tableName, 'C_index_2_1, C_index_2_2');
2296
                }
2297
            ],
2298
            'create unique' => [
2299
                "CREATE UNIQUE INDEX [[$name1]] ON {{{$tableName}}} ([[C_index_1]])",
2300
                static function (QueryBuilder $qb) use ($tableName, $name1) {
2301
                    return $qb->createIndex($name1, $tableName, 'C_index_1', true);
2302
                }
2303
            ],
2304
            'create unique (2 columns)' => [
2305
                "CREATE UNIQUE INDEX [[$name2]] ON {{{$tableName}}} ([[C_index_2_1]], [[C_index_2_2]])",
2306
                static function (QueryBuilder $qb) use ($tableName, $name2) {
2307
                    return $qb->createIndex($name2, $tableName, 'C_index_2_1, C_index_2_2', true);
2308
                }
2309
            ],
2310
        ];
2311
    }
2312
2313
    public function deleteProviderTrait(): array
2314
    {
2315
        return [
2316
            [
2317
                'user',
2318
                [
2319
                    'is_enabled' => false,
2320
                    'power'      => new Expression('WRONG_POWER()'),
2321
                ],
2322
                $this->replaceQuotes('DELETE FROM [[user]] WHERE ([[is_enabled]]=:qp0) AND ([[power]]=WRONG_POWER())'),
2323
                [
2324
                    ':qp0' => false,
2325
                ],
2326
            ],
2327
        ];
2328
    }
2329
2330
    public function insertProviderTrait(): array
2331
    {
2332
        return [
2333
            'regular-values' => [
2334
                'customer',
2335
                [
2336
                    'email'      => '[email protected]',
2337
                    'name'       => 'silverfire',
2338
                    'address'    => 'Kyiv {{city}}, Ukraine',
2339
                    'is_active'  => false,
2340
                    'related_id' => null,
2341
                ],
2342
                [],
2343
                $this->replaceQuotes(
2344
                    'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]])'
2345
                    . ' VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)'
2346
                ),
2347
                [
2348
                    ':qp0' => '[email protected]',
2349
                    ':qp1' => 'silverfire',
2350
                    ':qp2' => 'Kyiv {{city}}, Ukraine',
2351
                    ':qp3' => false,
2352
                    ':qp4' => null,
2353
                ],
2354
            ],
2355
            'params-and-expressions' => [
2356
                '{{%type}}',
2357
                [
2358
                    '{{%type}}.[[related_id]]' => null,
2359
                    '[[time]]' => new Expression('now()'),
2360
                ],
2361
                [],
2362
                'INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) VALUES (:qp0, now())',
2363
                [
2364
                    ':qp0' => null,
2365
                ],
2366
            ],
2367
            'carry passed params' => [
2368
                'customer',
2369
                [
2370
                    'email'      => '[email protected]',
2371
                    'name'       => 'sergeymakinen',
2372
                    'address'    => '{{city}}',
2373
                    'is_active'  => false,
2374
                    'related_id' => null,
2375
                    'col'        => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
2376
                ],
2377
                [':phBar' => 'bar'],
2378
                $this->replaceQuotes(
2379
                    'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]], [[col]])'
2380
                    . ' VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar))'
2381
                ),
2382
                [
2383
                    ':phBar' => 'bar',
2384
                    ':qp1'   => '[email protected]',
2385
                    ':qp2'   => 'sergeymakinen',
2386
                    ':qp3'   => '{{city}}',
2387
                    ':qp4'   => false,
2388
                    ':qp5'   => null,
2389
                    ':phFoo' => 'foo',
2390
                ],
2391
            ],
2392
            'carry passed params (query)' => [
2393
                'customer',
2394
                (new Query($this->getConnection()))
2395
                    ->select([
2396
                        'email',
2397
                        'name',
2398
                        'address',
2399
                        'is_active',
2400
                        'related_id',
2401
                    ])
2402
                    ->from('customer')
2403
                    ->where([
2404
                        'email'      => '[email protected]',
2405
                        'name'       => 'sergeymakinen',
2406
                        'address'    => '{{city}}',
2407
                        'is_active'  => false,
2408
                        'related_id' => null,
2409
                        'col'        => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
2410
                    ]),
2411
                [':phBar' => 'bar'],
2412
                $this->replaceQuotes(
2413
                    'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]])'
2414
                    . ' SELECT [[email]], [[name]], [[address]], [[is_active]], [[related_id]] FROM [[customer]]'
2415
                    . ' WHERE ([[email]]=:qp1) AND ([[name]]=:qp2) AND ([[address]]=:qp3) AND ([[is_active]]=:qp4)'
2416
                    . ' AND ([[related_id]] IS NULL) AND ([[col]]=CONCAT(:phFoo, :phBar))'
2417
                ),
2418
                [
2419
                    ':phBar' => 'bar',
2420
                    ':qp1'   => '[email protected]',
2421
                    ':qp2'   => 'sergeymakinen',
2422
                    ':qp3'   => '{{city}}',
2423
                    ':qp4'   => false,
2424
                    ':phFoo' => 'foo',
2425
                ],
2426
            ],
2427
        ];
2428
    }
2429
2430
    public function updateProviderTrait(): array
2431
    {
2432
        return [
2433
            [
2434
                'customer',
2435
                [
2436
                    'status' => 1,
2437
                    'updated_at' => new Expression('now()'),
2438
                ],
2439
                [
2440
                    'id' => 100,
2441
                ],
2442
                $this->replaceQuotes(
2443
                    'UPDATE [[customer]] SET [[status]]=:qp0, [[updated_at]]=now() WHERE [[id]]=:qp1'
2444
                ),
2445
                [
2446
                    ':qp0' => 1,
2447
                    ':qp1' => 100,
2448
                ],
2449
            ],
2450
        ];
2451
    }
2452
2453
    public function upsertProviderTrait(): array
2454
    {
2455
        return [
2456
            'regular values' => [
2457
                'T_upsert',
2458
                [
2459
                    'email' => '[email protected]',
2460
                    'address' => 'bar {{city}}',
2461
                    'status' => 1,
2462
                    'profile_id' => null,
2463
                ],
2464
                true,
2465
                null,
2466
                [
2467
                    ':qp0' => '[email protected]',
2468
                    ':qp1' => 'bar {{city}}',
2469
                    ':qp2' => 1,
2470
                    ':qp3' => null,
2471
                ],
2472
            ],
2473
            'regular values with update part' => [
2474
                'T_upsert',
2475
                [
2476
                    'email' => '[email protected]',
2477
                    'address' => 'bar {{city}}',
2478
                    'status' => 1,
2479
                    'profile_id' => null,
2480
                ],
2481
                [
2482
                    'address' => 'foo {{city}}',
2483
                    'status' => 2,
2484
                    'orders' => new Expression('T_upsert.orders + 1'),
2485
                ],
2486
                null,
2487
                [
2488
                    ':qp0' => '[email protected]',
2489
                    ':qp1' => 'bar {{city}}',
2490
                    ':qp2' => 1,
2491
                    ':qp3' => null,
2492
                    ':qp4' => 'foo {{city}}',
2493
                    ':qp5' => 2,
2494
                ],
2495
            ],
2496
            'regular values without update part' => [
2497
                'T_upsert',
2498
                [
2499
                    'email' => '[email protected]',
2500
                    'address' => 'bar {{city}}',
2501
                    'status' => 1,
2502
                    'profile_id' => null,
2503
                ],
2504
                false,
2505
                null,
2506
                [
2507
                    ':qp0' => '[email protected]',
2508
                    ':qp1' => 'bar {{city}}',
2509
                    ':qp2' => 1,
2510
                    ':qp3' => null,
2511
                ],
2512
            ],
2513
            'query' => [
2514
                'T_upsert',
2515
                (new Query($this->getConnection()))
2516
                    ->select([
2517
                        'email',
2518
                        'status' => new Expression('2'),
2519
                    ])
2520
                    ->from('customer')
2521
                    ->where(['name' => 'user1'])
2522
                    ->limit(1),
2523
                true,
2524
                null,
2525
                [
2526
                    ':qp0' => 'user1',
2527
                ],
2528
            ],
2529
            'query with update part' => [
2530
                'T_upsert',
2531
                (new Query($this->getConnection()))
2532
                    ->select([
2533
                        'email',
2534
                        'status' => new Expression('2'),
2535
                    ])
2536
                    ->from('customer')
2537
                    ->where(['name' => 'user1'])
2538
                    ->limit(1),
2539
                [
2540
                    'address' => 'foo {{city}}',
2541
                    'status' => 2,
2542
                    'orders' => new Expression('T_upsert.orders + 1'),
2543
                ],
2544
                null,
2545
                [
2546
                    ':qp0' => 'user1',
2547
                    ':qp1' => 'foo {{city}}',
2548
                    ':qp2' => 2,
2549
                ],
2550
            ],
2551
            'query without update part' => [
2552
                'T_upsert',
2553
                (new Query($this->getConnection()))
2554
                    ->select([
2555
                        'email',
2556
                        'status' => new Expression('2'),
2557
                    ])
2558
                    ->from('customer')
2559
                    ->where(['name' => 'user1'])
2560
                    ->limit(1),
2561
                false,
2562
                null,
2563
                [
2564
                    ':qp0' => 'user1',
2565
                ],
2566
            ],
2567
            'values and expressions' => [
2568
                '{{%T_upsert}}',
2569
                [
2570
                    '{{%T_upsert}}.[[email]]' => '[email protected]',
2571
                    '[[ts]]' => new Expression('now()'),
2572
                ],
2573
                true,
2574
                null,
2575
                [
2576
                    ':qp0' => '[email protected]',
2577
                ],
2578
            ],
2579
            'values and expressions with update part' => [
2580
                '{{%T_upsert}}',
2581
                [
2582
                    '{{%T_upsert}}.[[email]]' => '[email protected]',
2583
                    '[[ts]]' => new Expression('now()'),
2584
                ],
2585
                [
2586
                    '[[orders]]' => new Expression('T_upsert.orders + 1'),
2587
                ],
2588
                null,
2589
                [
2590
                    ':qp0' => '[email protected]',
2591
                ],
2592
            ],
2593
            'values and expressions without update part' => [
2594
                '{{%T_upsert}}',
2595
                [
2596
                    '{{%T_upsert}}.[[email]]' => '[email protected]',
2597
                    '[[ts]]' => new Expression('now()'),
2598
                ],
2599
                false,
2600
                null,
2601
                [
2602
                    ':qp0' => '[email protected]',
2603
                ],
2604
            ],
2605
            'query, values and expressions with update part' => [
2606
                '{{%T_upsert}}',
2607
                (new Query($this->getConnection()))
2608
                    ->select([
2609
                        'email' => new Expression(':phEmail', [':phEmail' => '[email protected]']),
2610
                        '[[time]]' => new Expression('now()'),
2611
                    ]),
2612
                [
2613
                    'ts' => 0,
2614
                    '[[orders]]' => new Expression('T_upsert.orders + 1'),
2615
                ],
2616
                null,
2617
                [
2618
                    ':phEmail' => '[email protected]',
2619
                    ':qp1' => 0,
2620
                ],
2621
            ],
2622
            'query, values and expressions without update part' => [
2623
                '{{%T_upsert}}',
2624
                (new Query($this->getConnection()))
2625
                    ->select([
2626
                        'email' => new Expression(':phEmail', [':phEmail' => '[email protected]']),
2627
                        '[[time]]' => new Expression('now()'),
2628
                    ]),
2629
                [
2630
                    'ts' => 0,
2631
                    '[[orders]]' => new Expression('T_upsert.orders + 1'),
2632
                ],
2633
                null,
2634
                [
2635
                    ':phEmail' => '[email protected]',
2636
                    ':qp1' => 0,
2637
                ],
2638
            ],
2639
            'no columns to update' => [
2640
                'T_upsert_1',
2641
                [
2642
                    'a' => 1,
2643
                ],
2644
                true,
2645
                null,
2646
                [
2647
                    ':qp0' => 1,
2648
                ],
2649
            ],
2650
        ];
2651
    }
2652
}
2653