Passed
Pull Request — master (#380)
by Wilmer
13:12
created

BaseCommandProvider::batchInsertSql()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 93
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 63
nc 1
nop 1
dl 0
loc 93
rs 8.8072
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Provider;
6
7
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Query\Query;
10
use Yiisoft\Db\QueryBuilder\QueryBuilder;
11
use Yiisoft\Db\Tests\Support\DbHelper;
12
13
final class BaseCommandProvider
14
{
15
    public function batchInsertSql(ConnectionPDOInterface $db): array
16
    {
17
        return [
18
            'multirow' => [
19
                'type',
20
                ['int_col', 'float_col', 'char_col', 'bool_col'],
21
                'values' => [
22
                    ['0', '0.0', 'test string', true],
23
                    [false, 0, 'test string2', false],
24
                ],
25
                'expected' => DbHelper::replaceQuotes(
26
                    <<<SQL
27
                    INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3), (:qp4, :qp5, :qp6, :qp7)
28
                    SQL,
29
                    $db->getName(),
30
                ),
31
                'expectedParams' => [
32
                    ':qp0' => 0,
33
                    ':qp1' => 0.0,
34
                    ':qp2' => 'test string',
35
                    ':qp3' => true,
36
                    ':qp4' => 0,
37
                    ':qp5' => 0.0,
38
                    ':qp6' => 'test string2',
39
                    ':qp7' => false,
40
                ],
41
                2,
42
            ],
43
            'issue11242' => [
44
                'type',
45
                ['int_col', 'float_col', 'char_col', 'bool_col'],
46
                'values' => [[1.0, 1.1, 'Kyiv {{city}}, Ukraine', true]],
47
                /**
48
                 * {@see https://github.com/yiisoft/yii2/issues/11242}
49
                 *
50
                 * Make sure curly bracelets (`{{..}}`) in values will not be escaped
51
                 */
52
                'expected' => DbHelper::replaceQuotes(
53
                    <<<SQL
54
                    INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
55
                    SQL,
56
                    $db->getName(),
57
                ),
58
                'expectedParams' => [
59
                    ':qp0' => 1,
60
                    ':qp1' => 1.1,
61
                    ':qp2' => 'Kyiv {{city}}, Ukraine',
62
                    ':qp3' => true,
63
                ],
64
            ],
65
            'wrongBehavior' => [
66
                '{{%type}}',
67
                ['{{%type}}.[[int_col]]', '[[float_col]]', 'char_col', 'bool_col'],
68
                'values' => [['0', '0.0', 'Kyiv {{city}}, Ukraine', false]],
69
                /**
70
                 * Test covers potentially wrong behavior and marks it as expected!.
71
                 *
72
                 * In case table name or table column is passed with curly or square bracelets, QueryBuilder can not
73
                 * determine the table schema and typecast values properly.
74
                 * TODO: make it work. Impossible without BC breaking for public methods.
75
                 */
76
                'expected' => DbHelper::replaceQuotes(
77
                    <<<SQL
78
                    INSERT INTO [[type]] ([[type]].[[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
79
                    SQL,
80
                    $db->getName(),
81
                ),
82
                'expectedParams' => [
83
                    ':qp0' => '0',
84
                    ':qp1' => '0.0',
85
                    ':qp2' => 'Kyiv {{city}}, Ukraine',
86
                    ':qp3' => false,
87
                ],
88
            ],
89
            'batchInsert binds params from expression' => [
90
                '{{%type}}',
91
                ['int_col', 'float_col', 'char_col', 'bool_col'],
92
                /**
93
                 * This example is completely useless. This feature of batchInsert is intended to be used with complex
94
                 * expression objects, such as JsonExpression.
95
                 */
96
                'values' => [[new Expression(':exp1', [':exp1' => 42]), 1, 'test', false]],
97
                'expected' => DbHelper::replaceQuotes(
98
                    <<<SQL
99
                    INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:exp1, :qp1, :qp2, :qp3)
100
                    SQL,
101
                    $db->getName(),
102
                ),
103
                'expectedParams' => [
104
                    ':exp1' => 42,
105
                    ':qp1' => 1.0,
106
                    ':qp2' => 'test',
107
                    ':qp3' => false,
108
                ],
109
            ],
110
        ];
111
    }
112
113
    public function bindParamsNonWhere(): array
114
    {
115
        return [
116
            [
117
                <<<SQL
118
                SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email GROUP BY SUBSTR(name, :len)
119
                SQL,
120
            ],
121
            [
122
                <<<SQL
123
                SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email ORDER BY SUBSTR(name, :len)
124
                SQL,
125
            ],
126
            [
127
                <<<SQL
128
                SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email
129
                SQL,
130
            ],
131
        ];
132
    }
133
134
    public function createIndex(ConnectionPDOInterface $db): array
135
    {
136
        return [
137
            [
138
                'name',
139
                'table',
140
                'column',
141
                '',
142
                '',
143
                DbHelper::replaceQuotes(
144
                    <<<SQL
145
                    CREATE INDEX [[name]] ON [[table]] ([[column]])
146
                    SQL,
147
                    $db->getName(),
148
                ),
149
            ],
150
            [
151
                'name',
152
                'table',
153
                ['column1', 'column2'],
154
                '',
155
                '',
156
                DbHelper::replaceQuotes(
157
                    <<<SQL
158
                    CREATE INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
159
                    SQL,
160
                    $db->getName(),
161
                ),
162
            ],
163
            [
164
                'name',
165
                'table',
166
                ['column1', 'column2'],
167
                QueryBuilder::INDEX_UNIQUE,
168
                '',
169
                DbHelper::replaceQuotes(
170
                    <<<SQL
171
                    CREATE UNIQUE INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
172
                    SQL,
173
                    $db->getName(),
174
                ),
175
            ],
176
            [
177
                'name',
178
                'table',
179
                ['column1', 'column2'],
180
                'FULLTEXT',
181
                '',
182
                DbHelper::replaceQuotes(
183
                    <<<SQL
184
                    CREATE FULLTEXT INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
185
                    SQL,
186
                    $db->getName(),
187
                ),
188
            ],
189
            [
190
                'name',
191
                'table',
192
                ['column1', 'column2'],
193
                'SPATIAL',
194
                '',
195
                DbHelper::replaceQuotes(
196
                    <<<SQL
197
                    CREATE SPATIAL INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
198
                    SQL,
199
                    $db->getName(),
200
                ),
201
            ],
202
            [
203
                'name',
204
                'table',
205
                ['column1', 'column2'],
206
                'BITMAP',
207
                '',
208
                DbHelper::replaceQuotes(
209
                    <<<SQL
210
                    CREATE BITMAP INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
211
                    SQL,
212
                    $db->getName(),
213
                ),
214
            ],
215
        ];
216
    }
217
218
    public function invalidSelectColumns(): array
219
    {
220
        return [[[]], ['*'], [['*']]];
221
    }
222
223
    public function rawSql(): array
224
    {
225
        return [
226
            [
227
                <<<SQL
228
                SELECT * FROM customer WHERE id = :id
229
                SQL,
230
                [':id' => 1],
231
                <<<SQL
232
                SELECT * FROM customer WHERE id = 1
233
                SQL,
234
            ],
235
            [
236
                <<<SQL
237
                SELECT * FROM customer WHERE id = :id
238
                SQL,
239
                ['id' => 1],
240
                <<<SQL
241
                SELECT * FROM customer WHERE id = 1
242
                SQL,
243
            ],
244
            [
245
                <<<SQL
246
                SELECT * FROM customer WHERE id = :id
247
                SQL,
248
                ['id' => null],
249
                <<<SQL
250
                SELECT * FROM customer WHERE id = NULL
251
                SQL,
252
            ],
253
            [
254
                <<<SQL
255
                SELECT * FROM customer WHERE id = :base OR id = :basePrefix
256
                SQL,
257
                ['base' => 1, 'basePrefix' => 2],
258
                <<<SQL
259
                SELECT * FROM customer WHERE id = 1 OR id = 2
260
                SQL,
261
            ],
262
            /**
263
             * {@see https://github.com/yiisoft/yii2/issues/9268}
264
             */
265
            [
266
                <<<SQL
267
                SELECT * FROM customer WHERE active = :active
268
                SQL,
269
                [':active' => false],
270
                <<<SQL
271
                SELECT * FROM customer WHERE active = FALSE
272
                SQL,
273
            ],
274
            /**
275
             * {@see https://github.com/yiisoft/yii2/issues/15122}
276
             */
277
            [
278
                <<<SQL
279
                SELECT * FROM customer WHERE id IN (:ids)
280
                SQL,
281
                [':ids' => new Expression(implode(', ', [1, 2]))],
282
                <<<SQL
283
                SELECT * FROM customer WHERE id IN (1, 2)
284
                SQL,
285
            ],
286
        ];
287
    }
288
289
    public function update(ConnectionPDOInterface $db): array
290
    {
291
        return [
292
            [
293
                'table',
294
                ['name' => 'test'],
295
                [],
296
                [],
297
                DbHelper::replaceQuotes(
298
                    <<<SQL
299
                    UPDATE [[table]] SET [[name]]=:qp0
300
                    SQL,
301
                    $db->getName(),
302
                ),
303
            ],
304
            [
305
                'table',
306
                ['name' => 'test'],
307
                ['id' => 1],
308
                [],
309
                DbHelper::replaceQuotes(
310
                    <<<SQL
311
                    UPDATE [[table]] SET [[name]]=:qp0 WHERE [[id]]=:qp1
312
                    SQL,
313
                    $db->getName(),
314
                ),
315
            ],
316
            [
317
                'table',
318
                ['name' => 'test'],
319
                ['id' => 1],
320
                ['id' => 'integer'],
321
                DbHelper::replaceQuotes(
322
                    <<<SQL
323
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
324
                    SQL,
325
                    $db->getName(),
326
                ),
327
            ],
328
            [
329
                'table',
330
                ['name' => 'test'],
331
                ['id' => 1],
332
                ['id' => 'string'],
333
                DbHelper::replaceQuotes(
334
                    <<<SQL
335
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
336
                    SQL,
337
                    $db->getName(),
338
                ),
339
            ],
340
            [
341
                'table',
342
                ['name' => 'test'],
343
                ['id' => 1],
344
                ['id' => 'boolean'],
345
                DbHelper::replaceQuotes(
346
                    <<<SQL
347
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
348
                    SQL,
349
                    $db->getName(),
350
                ),
351
            ],
352
            [
353
                'table',
354
                ['name' => 'test'],
355
                ['id' => 1],
356
                ['id' => 'float'],
357
                DbHelper::replaceQuotes(
358
                    <<<SQL
359
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
360
                    SQL,
361
                    $db->getName(),
362
                ),
363
            ],
364
        ];
365
    }
366
367
    public function upsert(ConnectionPDOInterface $db): array
368
    {
369
        return [
370
            'regular values' => [
371
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3]]],
372
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1]]],
373
            ],
374
            'regular values with update part' => [
375
                ['params' => [
376
                    'T_upsert',
377
                    ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3],
378
                    ['address' => 'Moon', 'status' => 2],
379
                ],
380
                ],
381
                [
382
                    'params' => [
383
                        'T_upsert',
384
                        ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1],
385
                        ['address' => 'Moon', 'status' => 2],
386
                    ],
387
                    'expected' => ['email' => '[email protected]', 'address' => 'Moon', 'status' => 2],
388
                ],
389
            ],
390
            'regular values without update part' => [
391
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3], false]],
392
                [
393
                    'params' => [
394
                        'T_upsert',
395
                        ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1],
396
                        false,
397
                    ],
398
                    'expected' => ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3],
399
                ],
400
            ],
401
            'query' => [
402
                [
403
                    'params' => [
404
                        'T_upsert',
405
                        (new query($db))
406
                            ->select(['email', 'address', 'status' => new Expression('1')])
407
                            ->from('customer')
408
                            ->where(['name' => 'user1'])
409
                            ->limit(1),
410
                    ],
411
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
412
                ],
413
                [
414
                    'params' => [
415
                        'T_upsert',
416
                        (new query($db))
417
                            ->select(['email', 'address', 'status' => new Expression('2')])
418
                            ->from('customer')
419
                            ->where(['name' => 'user1'])
420
                            ->limit(1),
421
                    ],
422
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 2],
423
                ],
424
            ],
425
            'query with update part' => [
426
                [
427
                    'params' => [
428
                        'T_upsert',
429
                        (new query($db))
430
                            ->select(['email', 'address', 'status' => new Expression('1')])
431
                            ->from('customer')
432
                            ->where(['name' => 'user1'])
433
                            ->limit(1),
434
                        ['address' => 'Moon', 'status' => 2],
435
                    ],
436
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
437
                ],
438
                [
439
                    'params' => [
440
                        'T_upsert',
441
                        (new query($db))
442
                            ->select(['email', 'address', 'status' => new Expression('3')])
443
                            ->from('customer')
444
                            ->where(['name' => 'user1'])
445
                            ->limit(1),
446
                        ['address' => 'Moon', 'status' => 2],
447
                    ],
448
                    'expected' => ['email' => '[email protected]', 'address' => 'Moon', 'status' => 2],
449
                ],
450
            ],
451
            'query without update part' => [
452
                [
453
                    'params' => [
454
                        'T_upsert',
455
                        (new query($db))
456
                            ->select(['email', 'address', 'status' => new Expression('1')])
457
                            ->from('customer')
458
                            ->where(['name' => 'user1'])
459
                            ->limit(1),
460
                        false,
461
                    ],
462
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
463
                ],
464
                [
465
                    'params' => [
466
                        'T_upsert',
467
                        (new query($db))
468
                            ->select(['email', 'address', 'status' => new Expression('2')])
469
                            ->from('customer')
470
                            ->where(['name' => 'user1'])
471
                            ->limit(1),
472
                        false,
473
                    ],
474
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
475
                ],
476
            ],
477
        ];
478
    }
479
}
480