Passed
Push — master ( e9fa3d...22a26f )
by William
03:07
created

CreateStatementTest::testBuilderCompressed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Builder;
6
7
use PhpMyAdmin\SqlParser\Components\CreateDefinition;
8
use PhpMyAdmin\SqlParser\Components\DataType;
9
use PhpMyAdmin\SqlParser\Components\Expression;
10
use PhpMyAdmin\SqlParser\Components\Key;
11
use PhpMyAdmin\SqlParser\Components\OptionsArray;
12
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
13
use PhpMyAdmin\SqlParser\Parser;
14
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
15
use PhpMyAdmin\SqlParser\Tests\TestCase;
16
use PhpMyAdmin\SqlParser\TokensList;
17
18
class CreateStatementTest extends TestCase
19
{
20
    public function testBuilder(): void
21
    {
22
        $parser = new Parser('CREATE USER "jeffrey"@"localhost" IDENTIFIED BY "mypass"');
23
        $stmt = $parser->statements[0];
24
        $this->assertEquals(
25
            'CREATE USER "jeffrey"@"localhost" IDENTIFIED BY "mypass"',
26
            $stmt->build()
27
        );
28
    }
29
30
    public function testBuilderDatabase(): void
31
    {
32
        // CREATE DATABASE ...
33
        $parser = new Parser(
34
            'CREATE DATABASE `mydb` ' .
35
            'DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8_general_ci'
36
        );
37
        $stmt = $parser->statements[0];
38
39
        $this->assertEquals(
40
            'CREATE DATABASE `mydb` ' .
41
            'DEFAULT CHARACTER SET=utf8 DEFAULT COLLATE=utf8_general_ci',
42
            $stmt->build()
43
        );
44
45
        // CREATE SCHEMA ...
46
        $parser = new Parser('CREATE SCHEMA `mydb` DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8_general_ci');
47
        $stmt = $parser->statements[0];
48
49
        $this->assertEquals(
50
            'CREATE SCHEMA `mydb` ' .
51
            'DEFAULT CHARACTER SET=utf8 DEFAULT COLLATE=utf8_general_ci',
52
            $stmt->build()
53
        );
54
    }
55
56
    public function testBuilderDefaultInt(): void
57
    {
58
        $parser = new Parser(
59
            'CREATE TABLE IF NOT EXISTS t1 (' .
60
            " c1 int(11) NOT NULL DEFAULT '0' COMMENT 'xxx'" .
61
            ') ENGINE=MyISAM'
62
        );
63
        $stmt = $parser->statements[0];
64
65
        $this->assertEquals(
66
            "CREATE TABLE IF NOT EXISTS t1 (\n" .
67
            "  `c1` int(11) NOT NULL DEFAULT '0' COMMENT 'xxx'\n" .
68
            ') ENGINE=MyISAM',
69
            $stmt->build()
70
        );
71
    }
72
73
    public function testBuilderCompressed(): void
74
    {
75
        $parser = new Parser('CREATE TABLE users ( user_id int ) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=9;');
76
        $stmt = $parser->statements[0];
77
        $this->assertEquals(
78
            "CREATE TABLE users (\n  `user_id` int\n) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=9",
79
            $stmt->build()
80
        );
81
    }
82
83
    public function testBuilderCollate(): void
84
    {
85
        $parser = new Parser(
86
            'CREATE TABLE IF NOT EXISTS t1 (' .
87
            " c1 varchar(11) NOT NULL DEFAULT '0' COLLATE 'utf8_czech_ci' COMMENT 'xxx'" .
88
            ') ENGINE=MyISAM'
89
        );
90
        $stmt = $parser->statements[0];
91
92
        $this->assertEquals(
93
            "CREATE TABLE IF NOT EXISTS t1 (\n" .
94
            "  `c1` varchar(11) NOT NULL DEFAULT '0' COLLATE 'utf8_czech_ci' COMMENT 'xxx'\n" .
95
            ') ENGINE=MyISAM',
96
            $stmt->build()
97
        );
98
    }
99
100
    public function testBuilderDefaultComment(): void
101
    {
102
        $parser = new Parser(
103
            'CREATE TABLE `wp_audio` (' .
104
            " `somedata` int(11) DEFAULT NULL COMMENT 'ma data', " .
105
            " `someinfo` int(11) DEFAULT NULL COMMENT 'ma info' " .
106
            ' )'
107
        );
108
        $stmt = $parser->statements[0];
109
110
        $this->assertEquals(
111
            "CREATE TABLE `wp_audio` (\n" .
112
            "  `somedata` int(11) DEFAULT NULL COMMENT 'ma data',\n" .
113
            "  `someinfo` int(11) DEFAULT NULL COMMENT 'ma info'\n" .
114
            ') ',
115
            $stmt->build()
116
        );
117
    }
118
119
    public function testBuilderTable(): void
120
    {
121
        /* Assertion 1 */
122
        $stmt = new CreateStatement();
123
124
        $stmt->name = new Expression('', 'test', '');
125
        $stmt->options = new OptionsArray(['TABLE']);
126
        $stmt->fields = [
127
            new CreateDefinition(
128
                'id',
129
                new OptionsArray(['NOT NULL', 'AUTO_INCREMENT']),
130
                new DataType('INT', [11], new OptionsArray(['UNSIGNED']))
131
            ),
132
            new CreateDefinition(
133
                '',
134
                null,
135
                new Key('', [['name' => 'id']], 'PRIMARY KEY')
136
            ),
137
        ];
138
139
        $this->assertEquals(
140
            "CREATE TABLE `test` (\n" .
141
            "  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n" .
142
            "  PRIMARY KEY (`id`)\n" .
143
            ') ',
144
            $stmt->build()
145
        );
146
147
        /* Assertion 2 */
148
        $query =
149
            "CREATE TABLE `jos_core_acl_aro` (\n" .
150
            "  `id` int(11) NOT NULL,\n" .
151
            "  `section_value` varchar(240) NOT NULL DEFAULT '0',\n" .
152
            "  `value` varchar(240) NOT NULL DEFAULT '',\n" .
153
            "  `order_value` int(11) NOT NULL DEFAULT '0',\n" .
154
            "  `name` varchar(255) NOT NULL DEFAULT '',\n" .
155
            "  `hidden` int(11) NOT NULL DEFAULT '0',\n" .
156
            "  PRIMARY KEY (`id`),\n" .
157
            "  UNIQUE KEY `jos_section_value_value_aro` (`section_value`(100),`value`(15)) USING BTREE,\n" .
158
            "  KEY `jos_gacl_hidden_aro` (`hidden`)\n" .
159
            ') ENGINE=InnoDB DEFAULT CHARSET=latin1';
160
        $parser = new Parser($query);
161
        $this->assertEquals($query, $parser->statements[0]->build());
162
163
        /* Assertion 3 */
164
        $query = 'CREATE TABLE `table_copy` LIKE `old_table`';
165
        $parser = new Parser($query);
166
        $this->assertEquals($query, $parser->statements[0]->build());
167
168
        /* Assertion 4 */
169
        $query =
170
            "CREATE TABLE `aa` (\n" .
171
            "  `id` int(11) NOT NULL,\n" .
172
            "  `rTime` timestamp(3) NOT NULL DEFAULT '0000-00-00 00:00:00.000' ON UPDATE CURRENT_TIMESTAMP(3),\n" .
173
            "  PRIMARY KEY (`id`)\n" .
174
            ') ENGINE=InnoDB DEFAULT CHARSET=latin1';
175
        $parser = new Parser($query);
176
        $this->assertEquals($query, $parser->statements[0]->build());
177
178
        /* Assertion 5 */
179
        $parser = new Parser(
180
            'CREATE table table_name WITH' .
181
            ' cte (col1) AS ( SELECT 1 UNION ALL SELECT 2 )' .
182
            ' SELECT col1 FROM cte'
183
        );
184
        $stmt = $parser->statements[0];
185
186
        $this->assertEquals(
187
            'CREATE TABLE table_name WITH' .
188
            ' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)' .
189
            ' SELECT col1 FROM cte',
190
            $stmt->build()
191
        );
192
    }
193
194
    public function testBuilderPartitions(): void
195
    {
196
        /* Assertion 1 */
197
        $query = 'CREATE TABLE ts (' . "\n"
198
            . '  `id` int,' . "\n"
199
            . '  `purchased` date' . "\n"
200
            . ') ' . "\n"
201
            . 'PARTITION BY RANGE(YEAR(purchased))' . "\n"
202
            . 'PARTITIONS 3' . "\n"
203
            . 'SUBPARTITION BY HASH(TO_DAYS(purchased))' . "\n"
204
            . 'SUBPARTITIONS 2' . "\n"
205
            . '(' . "\n"
206
            . 'PARTITION p0 VALUES LESS THAN (1990)  (' . "\n"
207
            . 'SUBPARTITION s0,' . "\n"
208
            . 'SUBPARTITION s1' . "\n"
209
            . '),' . "\n"
210
            . 'PARTITION p1 VALUES LESS THAN (2000)  (' . "\n"
211
            . 'SUBPARTITION s2,' . "\n"
212
            . 'SUBPARTITION s3' . "\n"
213
            . '),' . "\n"
214
            . 'PARTITION p2 VALUES LESS THAN MAXVALUE  (' . "\n"
215
            . 'SUBPARTITION s4,' . "\n"
216
            . 'SUBPARTITION s5' . "\n"
217
            . ')' . "\n"
218
            . ')';
219
        $parser = new Parser($query);
220
        $this->assertEquals($query, $parser->statements[0]->build());
221
222
        /* Assertion 2 */
223
        $query = 'CREATE TABLE `pma_test` (' . "\n"
224
            . '  `test_id` int(32) NOT NULL,' . "\n"
225
            . '  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP' . "\n"
226
            . ') ENGINE=InnoDB DEFAULT CHARSET=utf8' . "\n"
227
            . 'PARTITION BY RANGE (test_id)' . "\n"
228
            . '(' . "\n"
229
            . 'PARTITION p0 VALUES LESS THAN (250000) ENGINE=InnoDB,' . "\n"
230
            . 'PARTITION p1 VALUES LESS THAN (500000) ENGINE=InnoDB,' . "\n"
231
            . 'PARTITION p2 VALUES LESS THAN (750000) ENGINE=InnoDB,' . "\n"
232
            . 'PARTITION p3 VALUES LESS THAN (1000000) ENGINE=InnoDB,' . "\n"
233
            . 'PARTITION p4 VALUES LESS THAN (1250000) ENGINE=InnoDB,' . "\n"
234
            . 'PARTITION p5 VALUES LESS THAN (1500000) ENGINE=InnoDB,' . "\n"
235
            . 'PARTITION p6 VALUES LESS THAN MAXVALUE ENGINE=InnoDB' . "\n"
236
            . ')';
237
        $parser = new Parser($query);
238
        $this->assertEquals($query, $parser->statements[0]->build());
239
    }
240
241
    /**
242
     * @return string[][]
243
     */
244
    public function partitionQueriesProvider(): array
245
    {
246
        return [
247
            [
248
                'subparts' => <<<EOT
249
CREATE TABLE `ts` (
250
  `id` int(11) DEFAULT NULL,
251
  `purchased` date DEFAULT NULL
252
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
253
PARTITION BY RANGE (YEAR(purchased))
254
SUBPARTITION BY HASH (TO_DAYS(purchased))
255
(
256
PARTITION p0 VALUES LESS THAN (1990)  (
257
SUBPARTITION s0 ENGINE=InnoDB,
258
SUBPARTITION s1 ENGINE=InnoDB
259
),
260
PARTITION p1 VALUES LESS THAN (2000)  (
261
SUBPARTITION s2 ENGINE=InnoDB,
262
SUBPARTITION s3 ENGINE=InnoDB
263
),
264
PARTITION p2 VALUES LESS THAN MAXVALUE  (
265
SUBPARTITION s4 ENGINE=InnoDB,
266
SUBPARTITION s5 ENGINE=InnoDB
267
)
268
)
269
EOT
270
            ,
271
            ],
272
            [
273
                'parts' => <<<EOT
274
CREATE TABLE ptest (
275
  `event_date` date NOT NULL
276
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
277
PARTITION BY HASH (TO_DAYS(event_date))
278
(
279
PARTITION p0 ENGINE=InnoDB,
280
PARTITION p1 ENGINE=InnoDB,
281
PARTITION p2 ENGINE=InnoDB,
282
PARTITION p3 ENGINE=InnoDB,
283
PARTITION p4 ENGINE=InnoDB
284
)
285
EOT
286
            ,
287
            ],
288
        ];
289
    }
290
291
    /**
292
     * @dataProvider partitionQueriesProvider
293
     */
294
    public function testBuilderPartitionsEngine(string $query): void
295
    {
296
        $parser = new Parser($query);
297
        $stmt = $parser->statements[0];
298
299
        $this->assertEquals($query, $stmt->build());
300
    }
301
302
    public function testBuilderView(): void
303
    {
304
        $parser = new Parser(
305
            'CREATE VIEW myView (vid, vfirstname) AS ' .
306
            'SELECT id, first_name FROM employee WHERE id = 1'
307
        );
308
        $stmt = $parser->statements[0];
309
310
        $this->assertEquals(
311
            'CREATE VIEW myView (vid, vfirstname) AS  ' .
312
            'SELECT id, first_name FROM employee WHERE id = 1 ',
313
            $stmt->build()
314
        );
315
316
        $parser = new Parser(
317
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS ' .
318
            'SELECT id, first_name FROM employee WHERE id = 1'
319
        );
320
        $stmt = $parser->statements[0];
321
322
        $this->assertEquals(
323
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS  ' .
324
            'SELECT id, first_name FROM employee WHERE id = 1 ',
325
            $stmt->build()
326
        );
327
328
        // Assert the builder can build wrong syntax select expressions
329
        $parser = new Parser(
330
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS ' .
331
            'SELECT id, first_name, FROMzz employee WHERE id = 1'
332
        );
333
        $stmt = $parser->statements[0];
334
        $this->assertEquals(
335
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS  ' .
336
            'SELECT id, first_name, FROMzz employee WHERE id = 1 ',
337
            $stmt->build()
338
        );
339
340
        $parser = new Parser(
341
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS ' .
342
            'SELECT id, first_name, FROMzz employee WHERE id = 1 ' .
343
            'UNION ' .
344
            'SELECT id, first_name, FROMzz employee WHERE id = 2 '
345
        );
346
        $stmt = $parser->statements[0];
347
348
        $this->assertEquals(
349
            'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS  ' .
350
            'SELECT id, first_name, FROMzz employee WHERE id = 1 ' .
351
            'UNION ' .
352
            'SELECT id, first_name, FROMzz employee WHERE id = 2  ',
353
            $stmt->build()
354
        );
355
356
        $parser = new Parser('CREATE VIEW `view_programlevelpartner`  AS SELECT `p`.`country_id`'
357
        . 'AS `country_id` FROM `program_level_partner` `p` ORDER BY `p`.`id` asc');
358
        $stmt = $parser->statements[0];
359
        $this->assertEquals(
360
            'CREATE VIEW `view_programlevelpartner`  AS SELECT `p`.`country_id`'
361
            . ' AS `country_id` FROM `program_level_partner` AS `p` ORDER BY `p`.`id` ASC ',
362
            $stmt->build()
363
        );
364
365
        $parser = new Parser('CREATE VIEW `view_zg_bycountry`  AS '
366
        . 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id`;');
367
        $stmt = $parser->statements[0];
368
        $this->assertEquals(
369
            'CREATE VIEW `view_zg_bycountry`  AS '
370
            . 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id` ',
371
            $stmt->build()
372
        );
373
374
        $parser = new Parser('CREATE view view_name AS WITH  aa(col1)'
375
        . ' AS ( SELECT 1 UNION ALL SELECT 2 ) SELECT col1 FROM cte AS `d` ');
376
        $stmt = $parser->statements[0];
377
        $this->assertEquals(
378
            'CREATE view view_name  AS WITH aa(col1)'
379
            . ' AS (SELECT 1 UNION ALL SELECT 2) SELECT col1 FROM cte AS `d`  ',
380
            $stmt->build()
381
        );
382
    }
383
384
    public function testBuilderViewComplex(): void
385
    {
386
        $parser = new Parser(
387
            'CREATE VIEW withclause AS' . "\n"
388
            . "\n"
389
            . 'WITH cte AS (' . "\n"
390
                . 'SELECT p.name, p.shape' . "\n"
391
                . 'FROM gis_all as p' . "\n"
392
            . ')' . "\n"
393
            . "\n"
394
            . 'SELECT cte.*' . "\n"
395
            . 'FROM cte' . "\n"
396
            . 'CROSS JOIN gis_all;'
397
        );
398
        $stmt = $parser->statements[0];
399
400
        $this->assertEquals(
401
            'CREATE VIEW withclause  AS '
402
            . 'WITH cte AS ('
403
                . 'SELECT p.name, p.shape '
404
                . 'FROM gis_all AS `p`'
405
            . ') '
406
            . 'SELECT cte.* '
407
            . 'FROM cte '
408
            . 'CROSS JOIN gis_all ',
409
            $stmt->build()
410
        );
411
        $parser = new Parser(
412
            'CREATE VIEW withclause2 AS' . "\n"
413
            . "\n"
414
            . 'WITH cte AS (' . "\n"
415
                . "\t" . 'SELECT p.name, p.shape' . "\n"
416
                . "\t" . 'FROM gis_all as p' . "\n"
417
            . '), cte2 AS (' . "\n"
418
                . "\t" . 'SELECT p.name as n2, p.shape as sh2' . "\n"
419
                . "\t" . 'FROM gis_all as p' . "\n"
420
            . ')' . "\n"
421
            . "\n"
422
            . 'SELECT cte.*,cte2.*' . "\n"
423
            . 'FROM cte,cte2' . "\n"
424
            . 'CROSS JOIN gis_all;'
425
        );
426
        $stmt = $parser->statements[0];
427
428
        $this->assertEquals(
429
            'CREATE VIEW withclause2  AS '
430
            . 'WITH cte AS ('
431
                . 'SELECT p.name, p.shape'
432
                . ' FROM gis_all AS `p`'
433
            . '), cte2 AS ('
434
                . 'SELECT p.name AS `n2`, p.shape AS `sh2`'
435
                . ' FROM gis_all AS `p`'
436
            . ')'
437
            . ' SELECT cte.*, cte2.* '
438
            . 'FROM cte, cte2'
439
            . ' CROSS JOIN gis_all ',
440
            $stmt->build()
441
        );
442
    }
443
444
    public function testBuilderCreateProcedure(): void
445
    {
446
        $parser = new Parser(
447
            'CREATE DEFINER=`root`@`%`'
448
            . ' PROCEDURE `test2`(IN `_var` INT) NOT DETERMINISTIC NO SQL'
449
            . ' SQL SECURITY INVOKER NO SQL SQL SECURITY INVOKER SELECT _var'
450
        );
451
452
        /** @var CreateStatement $stmt */
453
        $stmt = $parser->statements[0];
454
455
        $this->assertSame(
456
            'CREATE DEFINER=`root`@`%`'
457
            . ' PROCEDURE `test2` (IN `_var` INT)  NOT DETERMINISTIC NO SQL'
458
            . ' SQL SECURITY INVOKER NO SQL SQL SECURITY INVOKER SELECT _var',
459
            $stmt->build()
460
        );
461
462
        $this->assertFalse($stmt->entityOptions->isEmpty());
0 ignored issues
show
Bug introduced by
The method isEmpty() does not exist on null. ( Ignorable by Annotation )

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

462
        $this->assertFalse($stmt->entityOptions->/** @scrutinizer ignore-call */ isEmpty());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
463
        $this->assertFalse($stmt->options->isEmpty());
0 ignored issues
show
Bug introduced by
The method isEmpty() does not exist on null. ( Ignorable by Annotation )

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

463
        $this->assertFalse($stmt->options->/** @scrutinizer ignore-call */ isEmpty());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
464
        $this->assertSame(
465
            'DEFINER=`root`@`%` PROCEDURE',
466
            $stmt->options->__toString()
467
        );
468
469
        $this->assertSame(
470
            '`test2`',
471
            $stmt->name->__toString()
472
        );
473
474
        $this->assertSame(
475
            '(IN `_var` INT)',
476
            ParameterDefinition::build($stmt->parameters)
477
        );
478
479
        $this->assertSame(
480
            'NOT DETERMINISTIC NO SQL SQL SECURITY INVOKER NO SQL SQL SECURITY INVOKER',
481
            $stmt->entityOptions->__toString()
482
        );
483
484
        $this->assertSame(
485
            'SELECT _var',
486
            TokensList::build($stmt->body)
487
        );
488
    }
489
490
    public function testBuilderCreateFunction(): void
491
    {
492
        $parser = new Parser(
493
            'CREATE DEFINER=`root`@`localhost`'
494
            . ' FUNCTION `inventory_in_stock`(`p_inventory_id` INT) RETURNS tinyint(1)'
495
            . ' READS SQL DATA'
496
            . ' COMMENT \'My best function written by a friend\'\'s friend\''
497
            . ' BEGIN' . "\n"
498
            . '    DECLARE v_rentals INT;' . "\n"
499
            . '    DECLARE v_out     INT;' . "\n"
500
            . "\n"
501
            . '    ' . "\n"
502
            . '    ' . "\n"
503
            . "\n"
504
            . '    SELECT COUNT(*) INTO v_rentals' . "\n"
505
            . '    FROM rental' . "\n"
506
            . '    WHERE inventory_id = p_inventory_id;' . "\n"
507
            . "\n"
508
            . '    IF v_rentals = 0 THEN' . "\n"
509
            . '      RETURN TRUE;' . "\n"
510
            . '    END IF;' . "\n"
511
            . "\n"
512
            . '    SELECT COUNT(rental_id) INTO v_out' . "\n"
513
            . '    FROM inventory LEFT JOIN rental USING(inventory_id)' . "\n"
514
            . '    WHERE inventory.inventory_id = p_inventory_id' . "\n"
515
            . '    AND rental.return_date IS NULL;' . "\n"
516
            . "\n"
517
            . '    IF v_out > 0 THEN' . "\n"
518
            . '      RETURN FALSE;' . "\n"
519
            . '    ELSE' . "\n"
520
            . '      RETURN TRUE;' . "\n"
521
            . '    END IF;' . "\n"
522
            . 'END'
523
        );
524
525
        /** @var CreateStatement $stmt */
526
        $stmt = $parser->statements[0];
527
528
        $this->assertSame(
529
            'CREATE DEFINER=`root`@`localhost`'
530
            . ' FUNCTION `inventory_in_stock` (`p_inventory_id` INT) RETURNS TINYINT(1)'
531
            . ' READS SQL DATA'
532
            . ' COMMENT \'My best function written by a friend\'\'s friend\''
533
            . ' BEGIN' . "\n"
534
            . '    DECLARE v_rentals INT;' . "\n"
535
            . '    DECLARE v_out     INT;' . "\n"
536
            . "\n"
537
            . '    ' . "\n"
538
            . '    ' . "\n"
539
            . "\n"
540
            . '    SELECT COUNT(*) INTO v_rentals' . "\n"
541
            . '    FROM rental' . "\n"
542
            . '    WHERE inventory_id = p_inventory_id;' . "\n"
543
            . "\n"
544
            . '    IF v_rentals = 0 THEN' . "\n"
545
            . '      RETURN TRUE;' . "\n"
546
            . '    END IF;' . "\n"
547
            . "\n"
548
            . '    SELECT COUNT(rental_id) INTO v_out' . "\n"
549
            . '    FROM inventory LEFT JOIN rental USING(inventory_id)' . "\n"
550
            . '    WHERE inventory.inventory_id = p_inventory_id' . "\n"
551
            . '    AND rental.return_date IS NULL;' . "\n"
552
            . "\n"
553
            . '    IF v_out > 0 THEN' . "\n"
554
            . '      RETURN FALSE;' . "\n"
555
            . '    ELSE' . "\n"
556
            . '      RETURN TRUE;' . "\n"
557
            . '    END IF;' . "\n"
558
            . 'END',
559
            $stmt->build()
560
        );
561
562
        $this->assertFalse($stmt->entityOptions->isEmpty());
563
        $this->assertFalse($stmt->options->isEmpty());
564
565
        $this->assertSame(
566
            'DEFINER=`root`@`localhost` FUNCTION',
567
            $stmt->options->__toString()
568
        );
569
570
        $this->assertSame(
571
            '`inventory_in_stock`',
572
            $stmt->name->__toString()
573
        );
574
575
        $this->assertSame(
576
            '(`p_inventory_id` INT)',
577
            ParameterDefinition::build($stmt->parameters)
578
        );
579
580
        $this->assertSame(
581
            'READS SQL DATA COMMENT \'My best function written by a friend\'\'s friend\'',
582
            $stmt->entityOptions->__toString()
583
        );
584
585
        $this->assertSame(
586
            'BEGIN' . "\n"
587
            . '    DECLARE v_rentals INT;' . "\n"
588
            . '    DECLARE v_out     INT;' . "\n"
589
            . "\n"
590
            . '    ' . "\n"
591
            . '    ' . "\n"
592
            . "\n"
593
            . '    SELECT COUNT(*) INTO v_rentals' . "\n"
594
            . '    FROM rental' . "\n"
595
            . '    WHERE inventory_id = p_inventory_id;' . "\n"
596
            . "\n"
597
            . '    IF v_rentals = 0 THEN' . "\n"
598
            . '      RETURN TRUE;' . "\n"
599
            . '    END IF;' . "\n"
600
            . "\n"
601
            . '    SELECT COUNT(rental_id) INTO v_out' . "\n"
602
            . '    FROM inventory LEFT JOIN rental USING(inventory_id)' . "\n"
603
            . '    WHERE inventory.inventory_id = p_inventory_id' . "\n"
604
            . '    AND rental.return_date IS NULL;' . "\n"
605
            . "\n"
606
            . '    IF v_out > 0 THEN' . "\n"
607
            . '      RETURN FALSE;' . "\n"
608
            . '    ELSE' . "\n"
609
            . '      RETURN TRUE;' . "\n"
610
            . '    END IF;' . "\n"
611
            . 'END',
612
            TokensList::build($stmt->body)
613
        );
614
    }
615
616
    public function testBuilderTrigger(): void
617
    {
618
        $stmt = new CreateStatement();
619
620
        $stmt->options = new OptionsArray(['TRIGGER']);
621
        $stmt->name = new Expression('ins_sum');
622
        $stmt->entityOptions = new OptionsArray(['BEFORE', 'INSERT']);
623
        $stmt->table = new Expression('account');
624
        $stmt->body = 'SET @sum = @sum + NEW.amount';
625
626
        $this->assertEquals(
627
            'CREATE TRIGGER ins_sum BEFORE INSERT ON account ' .
628
            'FOR EACH ROW SET @sum = @sum + NEW.amount',
629
            $stmt->build()
630
        );
631
    }
632
633
    public function testBuilderRoutine(): void
634
    {
635
        $parser = new Parser(
636
            'CREATE FUNCTION test (IN `i` INT) RETURNS VARCHAR ' .
637
            'BEGIN ' .
638
            'DECLARE name VARCHAR DEFAULT ""; ' .
639
            'SELECT name INTO name FROM employees WHERE id = i; ' .
640
            'RETURN name; ' .
641
            'END'
642
        );
643
        $stmt = $parser->statements[0];
644
645
        $this->assertEquals(
646
            'CREATE FUNCTION test (IN `i` INT) RETURNS VARCHAR ' .
647
            ' BEGIN ' .
648
            'DECLARE name VARCHAR DEFAULT ""; ' .
649
            'SELECT name INTO name FROM employees WHERE id = i; ' .
650
            'RETURN name; ' .
651
            'END',
652
            $stmt->build()
653
        );
654
    }
655
656
    public function testBuildSelect(): void
657
    {
658
        $parser = new Parser('CREATE TABLE new_tbl SELECT * FROM orig_tbl');
659
        $this->assertEquals(
660
            'CREATE TABLE new_tbl SELECT * FROM orig_tbl',
661
            $parser->statements[0]->build()
662
        );
663
    }
664
665
    public function testBuildCreateTableSortedIndex(): void
666
    {
667
        $parser = new Parser(
668
            <<<'SQL'
669
CREATE TABLE `entries` (
670
    `id` int(11) NOT NULL AUTO_INCREMENT,
671
    `fk_ug_id` int(11) DEFAULT NULL,
672
    `amount` decimal(10,2) DEFAULT NULL,
673
    PRIMARY KEY (`id`),
674
    KEY `entries__ug` (`fk_ug_id` DESC),
675
    KEY `entries__ug2` (`fk_ug_id` ASC),
676
    KEY `33` (`id` ASC, `fk_ug_id` DESC)
677
) /*!50100 TABLESPACE `innodb_system` */ ENGINE=InnoDB AUTO_INCREMENT=4465 DEFAULT CHARSET=utf8
678
SQL
679
        );
680
681
        /** @var CreateStatement $stmt */
682
        $stmt = $parser->statements[0];
683
684
        $tableBody = <<<'SQL'
685
(
686
  `id` int(11) NOT NULL AUTO_INCREMENT,
687
  `fk_ug_id` int(11) DEFAULT NULL,
688
  `amount` decimal(10,2) DEFAULT NULL,
689
  PRIMARY KEY (`id`),
690
  KEY `entries__ug` (`fk_ug_id` DESC),
691
  KEY `entries__ug2` (`fk_ug_id` ASC),
692
  KEY `33` (`id` ASC,`fk_ug_id` DESC)
693
)
694
SQL;
695
696
        $this->assertEquals(
697
            $tableBody,
698
            CreateDefinition::build($stmt->fields)
0 ignored issues
show
Bug introduced by
It seems like $stmt->fields can also be of type PhpMyAdmin\SqlParser\Components\ArrayObj; however, parameter $component of PhpMyAdmin\SqlParser\Com...eateDefinition::build() does only seem to accept PhpMyAdmin\SqlParser\Com...onents\CreateDefinition, maybe add an additional type check? ( Ignorable by Annotation )

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

698
            CreateDefinition::build(/** @scrutinizer ignore-type */ $stmt->fields)
Loading history...
699
        );
700
701
        $this->assertEquals(
702
            'CREATE TABLE `entries` '
703
            . $tableBody
704
            . ' ENGINE=InnoDB AUTO_INCREMENT=4465 DEFAULT CHARSET=utf8 TABLESPACE `innodb_system`',
705
            $stmt->build()
706
        );
707
    }
708
709
    public function testBuildCreateTableComplexIndexes(): void
710
    {
711
        // phpcs:disable Generic.Files.LineLength.TooLong
712
        $parser = new Parser(
713
            <<<'SQL'
714
CREATE TABLE `page_rebuild_control` (
715
    `proc_row_number` int DEFAULT NULL,
716
    `place_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
717
    `place_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
718
    `place_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
719
    `waterway_id` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
720
    `cache_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
721
    `place_active` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
722
    `alias_type` int NOT NULL DEFAULT '0',
723
    `status` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
724
    `time_taken` float DEFAULT NULL,
725
    PRIMARY KEY (`place_id`,`place_type`) USING BTREE,
726
    KEY `place_type_idx` (`place_type`(10)),
727
    KEY `cached_time_idx` (`cache_updated`),
728
    KEY `active_idx` (`place_active`),
729
    KEY `status_idx` (`status`),
730
    KEY `waterway_idx` (`waterway_id`),
731
    KEY `time_taken_idx` (`time_taken`),
732
    KEY `updated_tz_ind3` (
733
        -- my expression
734
		(convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB'))
735
    ) COMMENT 'foo\'s',
736
    KEY `updated_tz_ind_two_indexes_commented` (
737
		-- first expression
738
		(
739
			convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')
740
		)
741
		,
742
		-- second expression
743
		(
744
			convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'FR')
745
		)
746
	)
747
	-- and now some options
748
	COMMENT 'haha, this is a complex and indented case',
749
    KEY `alias_type_idx` (`alias_type`),
750
    KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB'))) COMMENT 'foo\'s',
751
    KEY `updated_tz_ind_two_indexes` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')), (convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'FR'))) COMMENT 'bar\'s',
752
    KEY `updated_tz_ind` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')))
753
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
754
SQL
755
        );
756
        // phpcs:enable
757
758
        /** @var CreateStatement $stmt */
759
        $stmt = $parser->statements[0];
760
761
        // phpcs:disable Generic.Files.LineLength.TooLong
762
        $tableBody = <<<'SQL'
763
(
764
  `proc_row_number` int DEFAULT NULL,
765
  `place_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
766
  `place_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
767
  `place_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
768
  `waterway_id` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
769
  `cache_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
770
  `place_active` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
771
  `alias_type` int NOT NULL DEFAULT '0',
772
  `status` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
773
  `time_taken` float DEFAULT NULL,
774
  PRIMARY KEY (`place_id`,`place_type`) USING BTREE,
775
  KEY `place_type_idx` (`place_type`(10)),
776
  KEY `cached_time_idx` (`cache_updated`),
777
  KEY `active_idx` (`place_active`),
778
  KEY `status_idx` (`status`),
779
  KEY `waterway_idx` (`waterway_id`),
780
  KEY `time_taken_idx` (`time_taken`),
781
  KEY `updated_tz_ind3` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB'))) COMMENT 'foo\'s',
782
  KEY `updated_tz_ind_two_indexes_commented` ((
783
			convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')
784
		), (
785
			convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'FR')
786
		)) COMMENT 'haha, this is a complex and indented case',
787
  KEY `alias_type_idx` (`alias_type`),
788
  KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB'))) COMMENT 'foo\'s',
789
  KEY `updated_tz_ind_two_indexes` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')), (convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'FR'))) COMMENT 'bar\'s',
790
  KEY `updated_tz_ind` ((convert_tz(`cache_updated`,_utf8mb4'GMT',_utf8mb4'GB')))
791
)
792
SQL;
793
        // phpcs:enable
794
795
        $this->assertEquals(
796
            $tableBody,
797
            CreateDefinition::build($stmt->fields)
0 ignored issues
show
Bug introduced by
It seems like $stmt->fields can also be of type PhpMyAdmin\SqlParser\Components\ArrayObj; however, parameter $component of PhpMyAdmin\SqlParser\Com...eateDefinition::build() does only seem to accept PhpMyAdmin\SqlParser\Com...onents\CreateDefinition, maybe add an additional type check? ( Ignorable by Annotation )

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

797
            CreateDefinition::build(/** @scrutinizer ignore-type */ $stmt->fields)
Loading history...
798
        );
799
800
        $this->assertEquals(
801
            'CREATE TABLE `page_rebuild_control` '
802
            . $tableBody
803
            . ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci',
804
            $stmt->build()
805
        );
806
    }
807
}
808