Passed
Push — master ( 8807b4...37e483 )
by Wilmer
03:08
created

BaseCommandProvider::upsert()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 108
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 76
nc 1
nop 1
dl 0
loc 108
rs 8.5236
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 createIndex(ConnectionPDOInterface $db): array
114
    {
115
        return [
116
            [
117
                'name',
118
                'table',
119
                'column',
120
                '',
121
                '',
122
                DbHelper::replaceQuotes(
123
                    <<<SQL
124
                    CREATE INDEX [[name]] ON [[table]] ([[column]])
125
                    SQL,
126
                    $db->getName(),
127
                ),
128
            ],
129
            [
130
                'name',
131
                'table',
132
                ['column1', 'column2'],
133
                '',
134
                '',
135
                DbHelper::replaceQuotes(
136
                    <<<SQL
137
                    CREATE INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
138
                    SQL,
139
                    $db->getName(),
140
                ),
141
            ],
142
            [
143
                'name',
144
                'table',
145
                ['column1', 'column2'],
146
                QueryBuilder::INDEX_UNIQUE,
147
                '',
148
                DbHelper::replaceQuotes(
149
                    <<<SQL
150
                    CREATE UNIQUE INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
151
                    SQL,
152
                    $db->getName(),
153
                ),
154
            ],
155
            [
156
                'name',
157
                'table',
158
                ['column1', 'column2'],
159
                'FULLTEXT',
160
                '',
161
                DbHelper::replaceQuotes(
162
                    <<<SQL
163
                    CREATE FULLTEXT INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
164
                    SQL,
165
                    $db->getName(),
166
                ),
167
            ],
168
            [
169
                'name',
170
                'table',
171
                ['column1', 'column2'],
172
                'SPATIAL',
173
                '',
174
                DbHelper::replaceQuotes(
175
                    <<<SQL
176
                    CREATE SPATIAL INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
177
                    SQL,
178
                    $db->getName(),
179
                ),
180
            ],
181
            [
182
                'name',
183
                'table',
184
                ['column1', 'column2'],
185
                'BITMAP',
186
                '',
187
                DbHelper::replaceQuotes(
188
                    <<<SQL
189
                    CREATE BITMAP INDEX [[name]] ON [[table]] ([[column1]], [[column2]])
190
                    SQL,
191
                    $db->getName(),
192
                ),
193
            ],
194
        ];
195
    }
196
197
    public function rawSql(): array
198
    {
199
        return [
200
            [
201
                <<<SQL
202
                SELECT * FROM customer WHERE id = :id
203
                SQL,
204
                [':id' => 1],
205
                <<<SQL
206
                SELECT * FROM customer WHERE id = 1
207
                SQL,
208
            ],
209
            [
210
                <<<SQL
211
                SELECT * FROM customer WHERE id = :id
212
                SQL,
213
                ['id' => 1],
214
                <<<SQL
215
                SELECT * FROM customer WHERE id = 1
216
                SQL,
217
            ],
218
            [
219
                <<<SQL
220
                SELECT * FROM customer WHERE id = :id
221
                SQL,
222
                ['id' => null],
223
                <<<SQL
224
                SELECT * FROM customer WHERE id = NULL
225
                SQL,
226
            ],
227
            [
228
                <<<SQL
229
                SELECT * FROM customer WHERE id = :base OR id = :basePrefix
230
                SQL,
231
                ['base' => 1, 'basePrefix' => 2],
232
                <<<SQL
233
                SELECT * FROM customer WHERE id = 1 OR id = 2
234
                SQL,
235
            ],
236
            /**
237
             * {@see https://github.com/yiisoft/yii2/issues/9268}
238
             */
239
            [
240
                <<<SQL
241
                SELECT * FROM customer WHERE active = :active
242
                SQL,
243
                [':active' => false],
244
                <<<SQL
245
                SELECT * FROM customer WHERE active = FALSE
246
                SQL,
247
            ],
248
            /**
249
             * {@see https://github.com/yiisoft/yii2/issues/15122}
250
             */
251
            [
252
                <<<SQL
253
                SELECT * FROM customer WHERE id IN (:ids)
254
                SQL,
255
                [':ids' => new Expression(implode(', ', [1, 2]))],
256
                <<<SQL
257
                SELECT * FROM customer WHERE id IN (1, 2)
258
                SQL,
259
            ],
260
        ];
261
    }
262
263
    public function update(ConnectionPDOInterface $db): array
264
    {
265
        return [
266
            [
267
                'table',
268
                ['name' => 'test'],
269
                [],
270
                [],
271
                DbHelper::replaceQuotes(
272
                    <<<SQL
273
                    UPDATE [[table]] SET [[name]]=:qp0
274
                    SQL,
275
                    $db->getName(),
276
                ),
277
            ],
278
            [
279
                'table',
280
                ['name' => 'test'],
281
                ['id' => 1],
282
                [],
283
                DbHelper::replaceQuotes(
284
                    <<<SQL
285
                    UPDATE [[table]] SET [[name]]=:qp0 WHERE [[id]]=:qp1
286
                    SQL,
287
                    $db->getName(),
288
                ),
289
            ],
290
            [
291
                'table',
292
                ['name' => 'test'],
293
                ['id' => 1],
294
                ['id' => 'integer'],
295
                DbHelper::replaceQuotes(
296
                    <<<SQL
297
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
298
                    SQL,
299
                    $db->getName(),
300
                ),
301
            ],
302
            [
303
                'table',
304
                ['name' => 'test'],
305
                ['id' => 1],
306
                ['id' => 'string'],
307
                DbHelper::replaceQuotes(
308
                    <<<SQL
309
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
310
                    SQL,
311
                    $db->getName(),
312
                ),
313
            ],
314
            [
315
                'table',
316
                ['name' => 'test'],
317
                ['id' => 1],
318
                ['id' => 'boolean'],
319
                DbHelper::replaceQuotes(
320
                    <<<SQL
321
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
322
                    SQL,
323
                    $db->getName(),
324
                ),
325
            ],
326
            [
327
                'table',
328
                ['name' => 'test'],
329
                ['id' => 1],
330
                ['id' => 'float'],
331
                DbHelper::replaceQuotes(
332
                    <<<SQL
333
                    UPDATE [[table]] SET [[name]]=:qp1 WHERE [[id]]=:qp2
334
                    SQL,
335
                    $db->getName(),
336
                ),
337
            ],
338
        ];
339
    }
340
341
    public function upsert(ConnectionPDOInterface $db): array
342
    {
343
        return [
344
            'regular values' => [
345
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3]]],
346
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1]]],
347
            ],
348
            'regular values with update part' => [
349
                ['params' => [
350
                    'T_upsert',
351
                    ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3],
352
                    ['address' => 'Moon', 'status' => 2],
353
                ],
354
                ],
355
                [
356
                    'params' => [
357
                        'T_upsert',
358
                        ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1],
359
                        ['address' => 'Moon', 'status' => 2],
360
                    ],
361
                    'expected' => ['email' => '[email protected]', 'address' => 'Moon', 'status' => 2],
362
                ],
363
            ],
364
            'regular values without update part' => [
365
                ['params' => ['T_upsert', ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3], false]],
366
                [
367
                    'params' => [
368
                        'T_upsert',
369
                        ['email' => '[email protected]', 'address' => 'Universe', 'status' => 1],
370
                        false,
371
                    ],
372
                    'expected' => ['email' => '[email protected]', 'address' => 'Earth', 'status' => 3],
373
                ],
374
            ],
375
            'query' => [
376
                [
377
                    'params' => [
378
                        'T_upsert',
379
                        (new query($db))
380
                            ->select(['email', 'address', 'status' => new Expression('1')])
381
                            ->from('customer')
382
                            ->where(['name' => 'user1'])
383
                            ->limit(1),
384
                    ],
385
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
386
                ],
387
                [
388
                    'params' => [
389
                        'T_upsert',
390
                        (new query($db))
391
                            ->select(['email', 'address', 'status' => new Expression('2')])
392
                            ->from('customer')
393
                            ->where(['name' => 'user1'])
394
                            ->limit(1),
395
                    ],
396
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 2],
397
                ],
398
            ],
399
            'query with update part' => [
400
                [
401
                    'params' => [
402
                        'T_upsert',
403
                        (new query($db))
404
                            ->select(['email', 'address', 'status' => new Expression('1')])
405
                            ->from('customer')
406
                            ->where(['name' => 'user1'])
407
                            ->limit(1),
408
                        ['address' => 'Moon', 'status' => 2],
409
                    ],
410
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
411
                ],
412
                [
413
                    'params' => [
414
                        'T_upsert',
415
                        (new query($db))
416
                            ->select(['email', 'address', 'status' => new Expression('3')])
417
                            ->from('customer')
418
                            ->where(['name' => 'user1'])
419
                            ->limit(1),
420
                        ['address' => 'Moon', 'status' => 2],
421
                    ],
422
                    'expected' => ['email' => '[email protected]', 'address' => 'Moon', 'status' => 2],
423
                ],
424
            ],
425
            'query without 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
                        false,
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('2')])
443
                            ->from('customer')
444
                            ->where(['name' => 'user1'])
445
                            ->limit(1),
446
                        false,
447
                    ],
448
                    'expected' => ['email' => '[email protected]', 'address' => 'address1', 'status' => 1],
449
                ],
450
            ],
451
        ];
452
    }
453
}
454