* Update sqlparser to fit php8.

This commit is contained in:
songchenxuan
2024-03-19 14:52:48 +08:00
parent 39f5d2905b
commit ea516fd9b3
663 changed files with 140347 additions and 16174 deletions
@@ -1,13 +1,16 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use Generator;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;
class AlterStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'ALTER TABLE `actor` ' .
'ADD PRIMARY KEY (`actor_id`), ' .
@@ -18,4 +21,176 @@ class AlterStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
public function testBuilderWithExpression(): void
{
$query = 'ALTER TABLE `table` '
. 'ADD UNIQUE KEY `functional_index`'
. ' (`field1`,`field2`, (IFNULL(`field3`,0)))';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
public function testBuilderWithComments(): void
{
$query = 'ALTER /* comment */ TABLE `actor` ' .
'ADD PRIMARY KEY (`actor_id`), -- comment at the end of the line' . "\n" .
'ADD KEY `idx_actor_last_name` (`last_name`) -- and that is the last comment.';
$expectedQuery = 'ALTER TABLE `actor` ' .
'ADD PRIMARY KEY (`actor_id`), ' .
'ADD KEY `idx_actor_last_name` (`last_name`)';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($expectedQuery, $stmt->build());
}
public function testBuilderWithCommentsOnOptions(): void
{
$query = 'ALTER EVENT `myEvent` /* comment */ ' .
'ON SCHEDULE -- Comment at the end of the line' . "\n" .
'AT "2023-01-01 01:23:45"';
$expectedQuery = 'ALTER EVENT `myEvent` ' .
'ON SCHEDULE AT "2023-01-01 01:23:45"';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($expectedQuery, $stmt->build());
}
public function testBuilderCompressed(): void
{
$query = 'ALTER TABLE `user` CHANGE `message` `message` TEXT COMPRESSED';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
public function testBuilderPartitions(): void
{
$parser = new Parser('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8');
$stmt = $parser->statements[0];
$this->assertEquals('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8', $stmt->build());
$parser = new Parser('ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002))');
$stmt = $parser->statements[0];
$this->assertEquals(
"ALTER TABLE t1 ADD PARTITION (\n" .
"PARTITION p3 VALUES LESS THAN (2002)\n" .
')',
$stmt->build()
);
$parser = new Parser('ALTER TABLE p PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32;');
$stmt = $parser->statements[0];
$this->assertEquals(
'ALTER TABLE p PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32',
$stmt->build()
);
$parser = new Parser('ALTER TABLE t1 DROP PARTITION p0, p1;');
$stmt = $parser->statements[0];
$this->assertEquals(
'ALTER TABLE t1 DROP PARTITION p0, p1',
$stmt->build()
);
$parser = new Parser(
'ALTER TABLE trips PARTITION BY RANGE (MONTH(trip_date))'
. ' (' . "\n"
. ' PARTITION p01 VALUES LESS THAN (02),' . "\n"
. ' PARTITION p02 VALUES LESS THAN (03),' . "\n"
. ' PARTITION p03 VALUES LESS THAN (04),' . "\n"
. ' PARTITION p04 VALUES LESS THAN (05),' . "\n"
. ' PARTITION p05 VALUES LESS THAN (06),' . "\n"
. ' PARTITION p06 VALUES LESS THAN (07),' . "\n"
. ' PARTITION p07 VALUES LESS THAN (08),' . "\n"
. ' PARTITION p08 VALUES LESS THAN (09),' . "\n"
. ' PARTITION p09 VALUES LESS THAN (10),' . "\n"
. ' PARTITION p10 VALUES LESS THAN (11),' . "\n"
. ' PARTITION p11 VALUES LESS THAN (12),' . "\n"
. ' PARTITION p12 VALUES LESS THAN (13),' . "\n"
. ' PARTITION pmaxval VALUES LESS THAN MAXVALUE' . "\n"
. ');'
);
$stmt = $parser->statements[0];
$this->assertEquals(
'ALTER TABLE trips PARTITION BY RANGE (MONTH(trip_date)) (' . "\n"
. 'PARTITION p01 VALUES LESS THAN (02),' . "\n"
. 'PARTITION p02 VALUES LESS THAN (03),' . "\n"
. 'PARTITION p03 VALUES LESS THAN (04),' . "\n"
. 'PARTITION p04 VALUES LESS THAN (05),' . "\n"
. 'PARTITION p05 VALUES LESS THAN (06),' . "\n"
. 'PARTITION p06 VALUES LESS THAN (07),' . "\n"
. 'PARTITION p07 VALUES LESS THAN (08),' . "\n"
. 'PARTITION p08 VALUES LESS THAN (09),' . "\n"
. 'PARTITION p09 VALUES LESS THAN (10),' . "\n"
. 'PARTITION p10 VALUES LESS THAN (11),' . "\n"
. 'PARTITION p11 VALUES LESS THAN (12),' . "\n"
. 'PARTITION p12 VALUES LESS THAN (13),' . "\n"
. 'PARTITION pmaxval VALUES LESS THAN MAXVALUE' . "\n"
. ')',
$stmt->build()
);
}
public function testBuilderEventWithDefiner(): void
{
$query = 'ALTER DEFINER=user EVENT myEvent ENABLE';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
/**
* @return Generator<string, array{string}>
*/
public static function provideBuilderForRenameColumn(): Generator
{
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b';
yield 'Single RENAME COLUMN' => [$query];
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b, RENAME COLUMN b TO a';
yield 'Multiple RENAME COLUMN' => [$query];
$query = 'ALTER TABLE myTable ' .
'RENAME COLUMN a TO b, ' .
'RENAME COLUMN b TO a, ' .
'RENAME INDEX oldIndex TO newIndex, ' .
'RENAME TO newTable';
yield 'Mixed RENAME COLUMN + RENAME INDEX + RENAME table' => [$query];
$query = 'ALTER TABLE myTable ' .
'RENAME TO newTable, ' .
'RENAME INDEX oldIndex TO newIndex, ' .
'RENAME COLUMN b TO a, ' .
'RENAME COLUMN a TO b';
yield 'Mixed RENAME table + RENAME INDEX + RENAME COLUMNS' => [$query];
}
/**
* @dataProvider provideBuilderForRenameColumn
*/
public function testBuilderRenameColumn(string $query): void
{
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class CallStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'CALL foo()';
@@ -16,4 +18,83 @@ class CallStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
public function testBuilderShort(): void
{
$query = 'CALL foo';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query . '()', $stmt->build());
}
public function testBuilderWithDbName(): void
{
$query = 'CALL mydb.foo()';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
public function testBuilderWithDbNameShort(): void
{
$query = 'CALL mydb.foo';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query . '()', $stmt->build());
}
public function testBuilderWithDbNameAndParams(): void
{
$query = 'CALL mydb.foo(@bar, @baz);';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('CALL mydb.foo(@bar,@baz)', $stmt->build());
}
public function testBuilderMultiCallsShort(): void
{
$query = 'call e;call f';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('CALL e()', $stmt->build());
$stmt = $parser->statements[1];
$this->assertEquals('CALL f()', $stmt->build());
}
public function testBuilderMultiCalls(): void
{
$query = 'call e();call f';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('CALL e()', $stmt->build());
$stmt = $parser->statements[1];
$this->assertEquals('CALL f()', $stmt->build());
}
public function testBuilderMultiCallsArgs(): void
{
$query = 'call e("foo");call f';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('CALL e("foo")', $stmt->build());
$stmt = $parser->statements[1];
$this->assertEquals('CALL f()', $stmt->build());
}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Components\CreateDefinition;
@@ -15,11 +17,9 @@ use PhpMyAdmin\SqlParser\TokensList;
class CreateStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$parser = new Parser(
'CREATE USER "jeffrey"@"localhost" IDENTIFIED BY "mypass"'
);
$parser = new Parser('CREATE USER "jeffrey"@"localhost" IDENTIFIED BY "mypass"');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE USER "jeffrey"@"localhost" IDENTIFIED BY "mypass"',
@@ -27,7 +27,7 @@ class CreateStatementTest extends TestCase
);
}
public function testBuilderDatabase()
public function testBuilderDatabase(): void
{
// CREATE DATABASE ...
$parser = new Parser(
@@ -42,12 +42,8 @@ class CreateStatementTest extends TestCase
$stmt->build()
);
// CREATE SCHEMA ...
$parser = new Parser(
'CREATE SCHEMA `mydb` ' .
'DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8_general_ci'
);
$parser = new Parser('CREATE SCHEMA `mydb` DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8_general_ci');
$stmt = $parser->statements[0];
$this->assertEquals(
@@ -57,7 +53,7 @@ class CreateStatementTest extends TestCase
);
}
public function testBuilderDefaultInt()
public function testBuilderDefaultInt(): void
{
$parser = new Parser(
'CREATE TABLE IF NOT EXISTS t1 (' .
@@ -74,7 +70,28 @@ class CreateStatementTest extends TestCase
);
}
public function testBuilderCollate()
public function testBuilderWithComments(): void
{
$parser = new Parser('CREATE TABLE tab1 (`col1` TIMESTAMP /*!40100 DEFAULT NULL */)');
$stmt = $parser->statements[0];
$this->assertEquals(
// TODO: fix with https://github.com/phpmyadmin/sql-parser/issues/256
"CREATE TABLE tab1 (\n `col1` timestamp DEFAULT NULL\n) ",
$stmt->build()
);
}
public function testBuilderCompressed(): void
{
$parser = new Parser('CREATE TABLE users ( user_id int ) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=9;');
$stmt = $parser->statements[0];
$this->assertEquals(
"CREATE TABLE users (\n `user_id` int\n) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=9",
$stmt->build()
);
}
public function testBuilderCollate(): void
{
$parser = new Parser(
'CREATE TABLE IF NOT EXISTS t1 (' .
@@ -91,7 +108,7 @@ class CreateStatementTest extends TestCase
);
}
public function testBuilderDefaultComment()
public function testBuilderDefaultComment(): void
{
$parser = new Parser(
'CREATE TABLE `wp_audio` (' .
@@ -110,25 +127,25 @@ class CreateStatementTest extends TestCase
);
}
public function testBuilderTable()
public function testBuilderTable(): void
{
/* Assertion 1 */
$stmt = new CreateStatement();
$stmt->name = new Expression('', 'test', '');
$stmt->options = new OptionsArray(array('TABLE'));
$stmt->fields = array(
$stmt->options = new OptionsArray(['TABLE']);
$stmt->fields = [
new CreateDefinition(
'id',
new OptionsArray(array('NOT NULL', 'AUTO_INCREMENT')),
new DataType('INT', array(11), new OptionsArray(array('UNSIGNED')))
new OptionsArray(['NOT NULL', 'AUTO_INCREMENT']),
new DataType('INT', [11], new OptionsArray(['UNSIGNED']))
),
new CreateDefinition(
'',
null,
new Key('', array(array('name' => 'id')), 'PRIMARY KEY')
)
);
new Key('', [['name' => 'id']], 'PRIMARY KEY')
),
];
$this->assertEquals(
"CREATE TABLE `test` (\n" .
@@ -168,9 +185,24 @@ class CreateStatementTest extends TestCase
') ENGINE=InnoDB DEFAULT CHARSET=latin1';
$parser = new Parser($query);
$this->assertEquals($query, $parser->statements[0]->build());
/* Assertion 5 */
$parser = new Parser(
'CREATE table table_name WITH' .
' cte (col1) AS ( SELECT 1 UNION ALL SELECT 2 )' .
' SELECT col1 FROM cte'
);
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE TABLE table_name WITH' .
' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)' .
' SELECT col1 FROM cte',
$stmt->build()
);
}
public function testBuilderPartitions()
public function testBuilderPartitions(): void
{
/* Assertion 1 */
$query = 'CREATE TABLE ts (' . "\n"
@@ -217,10 +249,13 @@ class CreateStatementTest extends TestCase
$this->assertEquals($query, $parser->statements[0]->build());
}
public function partitionQueries()
/**
* @return string[][]
*/
public function partitionQueriesProvider(): array
{
return array(
array(
return [
[
'subparts' => <<<EOT
CREATE TABLE `ts` (
`id` int(11) DEFAULT NULL,
@@ -243,8 +278,9 @@ SUBPARTITION s5 ENGINE=InnoDB
)
)
EOT
),
array(
,
],
[
'parts' => <<<EOT
CREATE TABLE ptest (
`event_date` date NOT NULL
@@ -258,16 +294,15 @@ PARTITION p3 ENGINE=InnoDB,
PARTITION p4 ENGINE=InnoDB
)
EOT
)
);
,
],
];
}
/**
* @dataProvider partitionQueries
*
* @param string $query
* @dataProvider partitionQueriesProvider
*/
public function testBuilderPartitionsEngine($query)
public function testBuilderPartitionsEngine(string $query): void
{
$parser = new Parser($query);
$stmt = $parser->statements[0];
@@ -275,8 +310,22 @@ EOT
$this->assertEquals($query, $stmt->build());
}
public function testBuilderView()
public function testBuilderView(): void
{
$parser = new Parser(
'CREATE OR REPLACE VIEW xviewmytable AS SELECT mytable.id '
. 'AS id, mytable.personid AS personid FROM mytable '
. 'WHERE (mytable.birth > \'1990-01-19\') GROUP BY mytable.personid ;'
);
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE OR REPLACE VIEW xviewmytable AS SELECT mytable.id '
. 'AS `id`, mytable.personid AS `personid` FROM mytable '
. 'WHERE (mytable.birth > \'1990-01-19\') GROUP BY mytable.personid ',
$stmt->build()
);
$parser = new Parser(
'CREATE VIEW myView (vid, vfirstname) AS ' .
'SELECT id, first_name FROM employee WHERE id = 1'
@@ -328,9 +377,36 @@ EOT
'SELECT id, first_name, FROMzz employee WHERE id = 2 ',
$stmt->build()
);
$parser = new Parser('CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
. 'AS `country_id` FROM `program_level_partner` `p` ORDER BY `p`.`id` asc');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
. ' AS `country_id` FROM `program_level_partner` AS `p` ORDER BY `p`.`id` ASC ',
$stmt->build()
);
$parser = new Parser('CREATE VIEW `view_zg_bycountry` AS '
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id`;');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW `view_zg_bycountry` AS '
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id` ',
$stmt->build()
);
$parser = new Parser('CREATE view view_name AS WITH aa(col1)'
. ' AS ( SELECT 1 UNION ALL SELECT 2 ) SELECT col1 FROM cte AS `d` ');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE view view_name AS WITH aa(col1)'
. ' AS (SELECT 1 UNION ALL SELECT 2) SELECT col1 FROM cte AS `d` ',
$stmt->build()
);
}
public function testBuilderViewComplex()
public function testBuilderViewComplex(): void
{
$parser = new Parser(
'CREATE VIEW withclause AS' . "\n"
@@ -347,15 +423,13 @@ EOT
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW withclause AS ' . "\n"
. "\n"
. 'WITH cte AS (' . "\n"
. 'SELECT p.name, p.shape' . "\n"
. 'FROM gis_all as p' . "\n"
. ')' . "\n"
. "\n"
. 'SELECT cte.*' . "\n"
. 'FROM cte' . "\n"
'CREATE VIEW withclause AS '
. 'WITH cte AS ('
. 'SELECT p.name, p.shape '
. 'FROM gis_all AS `p`'
. ') '
. 'SELECT cte.* '
. 'FROM cte '
. 'CROSS JOIN gis_all ',
$stmt->build()
);
@@ -377,25 +451,39 @@ EOT
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW withclause2 AS ' . "\n"
. "\n"
. 'WITH cte AS (' . "\n"
. "\t" . 'SELECT p.name, p.shape' . "\n"
. "\t" . 'FROM gis_all as p' . "\n"
. '), cte2 AS (' . "\n"
. "\t" . 'SELECT p.name as n2, p.shape as sh2' . "\n"
. "\t" . 'FROM gis_all as p' . "\n"
. ')' . "\n"
. "\n"
. 'SELECT cte.*,cte2.*' . "\n"
. 'FROM cte,cte2' . "\n"
. 'CROSS JOIN gis_all ',
'CREATE VIEW withclause2 AS '
. 'WITH cte AS ('
. 'SELECT p.name, p.shape'
. ' FROM gis_all AS `p`'
. '), cte2 AS ('
. 'SELECT p.name AS `n2`, p.shape AS `sh2`'
. ' FROM gis_all AS `p`'
. ')'
. ' SELECT cte.*, cte2.* '
. 'FROM cte, cte2'
. ' CROSS JOIN gis_all ',
$stmt->build()
);
}
public function testBuilderCreateProcedure()
public function testBuilderCreateProcedure(): void
{
$parser = new Parser(
'CREATE DEFINER=`root`@`%`'
. ' PROCEDURE `test2`(IN `_var` INT) DETERMINISTIC'
. ' MODIFIES SQL DATA SELECT _var'
);
/** @var CreateStatement $stmt */
$stmt = $parser->statements[0];
$this->assertSame(
'CREATE DEFINER=`root`@`%`'
. ' PROCEDURE `test2` (IN `_var` INT) DETERMINISTIC'
. ' MODIFIES SQL DATA SELECT _var',
$stmt->build()
);
$parser = new Parser(
'CREATE DEFINER=`root`@`%`'
. ' PROCEDURE `test2`(IN `_var` INT) NOT DETERMINISTIC NO SQL'
@@ -440,10 +528,11 @@ EOT
);
}
public function testBuilderCreateFunction()
public function testBuilderCreateFunction(): void
{
$parser = new Parser(
'CREATE DEFINER=`root`@`localhost`'
'DELIMITER $$' . "\n"
. 'CREATE DEFINER=`root`@`localhost`'
. ' FUNCTION `inventory_in_stock`(`p_inventory_id` INT) RETURNS tinyint(1)'
. ' READS SQL DATA'
. ' COMMENT \'My best function written by a friend\'\'s friend\''
@@ -566,13 +655,13 @@ EOT
);
}
public function testBuilderTrigger()
public function testBuilderTrigger(): void
{
$stmt = new CreateStatement();
$stmt->options = new OptionsArray(array('TRIGGER'));
$stmt->options = new OptionsArray(['TRIGGER']);
$stmt->name = new Expression('ins_sum');
$stmt->entityOptions = new OptionsArray(array('BEFORE', 'INSERT'));
$stmt->entityOptions = new OptionsArray(['BEFORE', 'INSERT']);
$stmt->table = new Expression('account');
$stmt->body = 'SET @sum = @sum + NEW.amount';
@@ -583,9 +672,10 @@ EOT
);
}
public function testBuilderRoutine()
public function testBuilderRoutine(): void
{
$parser = new Parser(
'DELIMITER $$' . "\n" .
'CREATE FUNCTION test (IN `i` INT) RETURNS VARCHAR ' .
'BEGIN ' .
'DECLARE name VARCHAR DEFAULT ""; ' .
@@ -606,18 +696,16 @@ EOT
);
}
public function testBuildSelect()
public function testBuildSelect(): void
{
$parser = new Parser(
'CREATE TABLE new_tbl SELECT * FROM orig_tbl'
);
$parser = new Parser('CREATE TABLE new_tbl SELECT * FROM orig_tbl');
$this->assertEquals(
'CREATE TABLE new_tbl SELECT * FROM orig_tbl',
$parser->statements[0]->build()
);
}
public function testBuildCreateTableSortedIndex()
public function testBuildCreateTableSortedIndex(): void
{
$parser = new Parser(
<<<'SQL'
@@ -659,10 +747,9 @@ SQL;
. ' ENGINE=InnoDB AUTO_INCREMENT=4465 DEFAULT CHARSET=utf8 TABLESPACE `innodb_system`',
$stmt->build()
);
}
public function testBuildCreateTableComplexIndexes()
public function testBuildCreateTableComplexIndexes(): void
{
// phpcs:disable Generic.Files.LineLength.TooLong
$parser = new Parser(
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class DeleteStatementTest extends TestCase
{
public function testBuilderSingleTable()
public function testBuilderSingleTable(): void
{
/* Assertion 1 */
$query = 'DELETE IGNORE FROM t1';
@@ -59,7 +61,7 @@ class DeleteStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
public function testBuilderMultiTable()
public function testBuilderMultiTable(): void
{
/* Assertion 1 */
$query = 'DELETE QUICK table1, table2.* FROM table1 AS `t1`, table2 AS `t2`';
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,15 +9,95 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class ExplainStatementTest extends TestCase
{
public function testBuilderView()
public function testBuilder(): void
{
/* Assertion 1 */
$query = 'EXPLAIN SELECT * FROM test;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
' EXPLAIN SELECT * FROM test',
'EXPLAIN SELECT * FROM test',
$stmt->build()
);
/* Assertion 2 */
$query = 'EXPLAIN ANALYZE SELECT * FROM tablename;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'EXPLAIN ANALYZE SELECT * FROM tablename',
$stmt->build()
);
/* Assertion 3 */
$query = 'DESC ANALYZE SELECT * FROM tablename;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESC ANALYZE SELECT * FROM tablename',
$stmt->build()
);
/* Assertion 4 */
$query = 'ANALYZE SELECT * FROM tablename;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'ANALYZE SELECT * FROM tablename',
$stmt->build()
);
/* Assertion 5 */
$query = 'DESCRIBE tablename;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESCRIBE `tablename`',
$stmt->build()
);
/* Assertion 6 */
$query = 'DESC FOR CONNECTION 458';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESC FOR CONNECTION 458',
$stmt->build()
);
/* Assertion 7 */
$query = 'EXPLAIN FORMAT=TREE SELECT * FROM db;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'EXPLAIN FORMAT=TREE SELECT * FROM db',
$stmt->build()
);
/* Assertion 8 */
$query = 'DESCRIBE tablename colname;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESCRIBE `tablename` `colname`',
$stmt->build()
);
/* Assertion 9 */
$query = 'DESCRIBE tablename \'col%me\';';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESCRIBE `tablename` `col%me`',
$stmt->build()
);
/* Assertion 9 */
$query = 'DESCRIBE db.tablename \'col%me\';';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
'DESCRIBE `db`.`tablename` `col%me`',
$stmt->build()
);
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,12 +9,10 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class InsertStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
/* Assertion 1 */
$parser = new Parser(
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)'
);
$parser = new Parser('INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
@@ -20,10 +20,8 @@ class InsertStatementTest extends TestCase
);
/* Assertion 2 */
/* Reserved keywords (with backqoutes as field name) */
$parser = new Parser(
'INSERT INTO tbl(`order`) VALUES (1)'
);
/* Reserved keywords (with backquotes as field name) */
$parser = new Parser('INSERT INTO tbl(`order`) VALUES (1)');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl(`order`) VALUES (1)',
@@ -32,9 +30,7 @@ class InsertStatementTest extends TestCase
/* Assertion 3 */
/* INSERT ... SET ... */
$parser = new Parser(
'INSERT INTO tbl SET FOO = 1'
);
$parser = new Parser('INSERT INTO tbl SET FOO = 1');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SET FOO = 1',
@@ -43,9 +39,7 @@ class InsertStatementTest extends TestCase
/* Assertion 4 */
/* INSERT ... SELECT ... */
$parser = new Parser(
'INSERT INTO tbl SELECT * FROM bar'
);
$parser = new Parser('INSERT INTO tbl SELECT * FROM bar');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SELECT * FROM bar',
@@ -54,9 +48,7 @@ class InsertStatementTest extends TestCase
/* Assertion 5 */
/* INSERT ... ON DUPLICATE KEY UPDATE ... */
$parser = new Parser(
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1'
);
$parser = new Parser('INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1',
@@ -64,10 +56,8 @@ class InsertStatementTest extends TestCase
);
/* Assertion 6 */
/* INSERT array(OPTIONS] INTO ... */
$parser = new Parser(
'INSERT DELAYED IGNORE INTO tbl SELECT * FROM bar'
);
/* INSERT [OPTIONS] INTO ... */
$parser = new Parser('INSERT DELAYED IGNORE INTO tbl SELECT * FROM bar');
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT DELAYED IGNORE INTO tbl SELECT * FROM bar',
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class LoadStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
/* Assertion 1 */
$query = 'LOAD DATA CONCURRENT INFILE '
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class LockStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
/* Assertion 1 */
$query = 'LOCK TABLES table1 AS `t1` READ LOCAL';
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class PurgeStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'PURGE BINARY LOGS TO \'mysql-bin.010\'';
$parser = new Parser($query);
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,12 +9,10 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class RenameStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'RENAME TABLE old_table TO new_table';
$parser = new Parser(
$query
);
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
@@ -20,9 +20,7 @@ class RenameStatementTest extends TestCase
);
$query = 'RENAME TABLE current_db.tbl_name TO other_db.tbl_name';
$parser = new Parser(
$query
);
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
@@ -30,9 +28,7 @@ class RenameStatementTest extends TestCase
);
$query = 'RENAME TABLE old_table1 TO new_table1, old_table2 TO new_table2, old_table3 TO new_table3';
$parser = new Parser(
$query
);
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,11 +9,9 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class ReplaceStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$parser = new Parser(
'REPLACE INTO tbl(col1, col2, col3) VALUES (1, "str", 3.14)'
);
$parser = new Parser('REPLACE INTO tbl(col1, col2, col3) VALUES (1, "str", 3.14)');
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
@@ -19,11 +19,9 @@ class ReplaceStatementTest extends TestCase
);
}
public function testBuilderSet()
public function testBuilderSet(): void
{
$parser = new Parser(
'REPLACE INTO tbl(col1, col2, col3) SET col1=1, col2="str", col3=3.14'
);
$parser = new Parser('REPLACE INTO tbl(col1, col2, col3) SET col1=1, col2="str", col3=3.14');
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SET col1 = 1, col2 = "str", col3 = 3.14',
@@ -31,11 +29,9 @@ class ReplaceStatementTest extends TestCase
);
}
public function testBuilderSelect()
public function testBuilderSelect(): void
{
$parser = new Parser(
'REPLACE INTO tbl(col1, col2, col3) SELECT col1, col2, col3 FROM tbl2'
);
$parser = new Parser('REPLACE INTO tbl(col1, col2, col3) SELECT col1, col2, col3 FROM tbl2');
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2',
@@ -43,11 +39,9 @@ class ReplaceStatementTest extends TestCase
);
}
public function testBuilderSelectDelayed()
public function testBuilderSelectDelayed(): void
{
$parser = new Parser(
'REPLACE DELAYED INTO tbl(col1, col2, col3) SELECT col1, col2, col3 FROM tbl2'
);
$parser = new Parser('REPLACE DELAYED INTO tbl(col1, col2, col3) SELECT col1, col2, col3 FROM tbl2');
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE DELAYED INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2',
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class SelectStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
. 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)';
@@ -20,9 +22,34 @@ class SelectStatementTest extends TestCase
. 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)',
$stmt->build()
);
$parser = new Parser('SELECT NULL IS NULL');
$stmt = $parser->statements[0];
$this->assertEquals('SELECT NULL IS NULL', $stmt->build());
$parser = new Parser('SELECT NOT 1');
$stmt = $parser->statements[0];
$this->assertEquals('SELECT NOT 1', $stmt->build());
$parser = new Parser('SELECT 1 BETWEEN 0 AND 2');
$stmt = $parser->statements[0];
$this->assertEquals('SELECT 1 BETWEEN 0 AND 2', $stmt->build());
$parser = new Parser("SELECT 'a' NOT REGEXP '^[a-d]'");
$stmt = $parser->statements[0];
$this->assertEquals("SELECT 'a' NOT REGEXP '^[a-d]'", $stmt->build());
$parser = new Parser("SELECT 'a' RLIKE 'a'");
$stmt = $parser->statements[0];
$this->assertEquals("SELECT 'a' RLIKE 'a'", $stmt->build());
}
public function testBuilderUnion()
public function testBuilderUnion(): void
{
$parser = new Parser('SELECT 1 UNION SELECT 2');
$stmt = $parser->statements[0];
@@ -33,7 +60,35 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAlias()
public function testBuilderWithIsNull(): void
{
$parser = new Parser('SELECT `test3`.`t1` is not null AS `is_not_null` FROM `test3` ;');
$stmt = $parser->statements[0];
$this->assertEquals('SELECT `test3`.`t1` is not null AS `is_not_null` FROM `test3`', $stmt->build());
$parser = new Parser('SELECT test3.t1 is null AS `col1` FROM test3');
$stmt = $parser->statements[0];
$this->assertEquals('SELECT test3.t1 is null AS `col1` FROM test3', $stmt->build());
}
public function testBuilderOrderByNull(): void
{
$query = 'SELECT * FROM some_table ORDER BY some_col IS NULL DESC;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('SELECT * FROM some_table ORDER BY some_col IS NULL DESC', $stmt->build());
$query = 'SELECT * FROM some_table ORDER BY some_col IS NOT NULL;';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals('SELECT * FROM some_table ORDER BY some_col IS NOT NULL ASC', $stmt->build());
}
public function testBuilderAlias(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -52,7 +107,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasOrder()
public function testBuilderAliasOrder(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -71,7 +126,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasOrderMultiple()
public function testBuilderAliasOrderMultiple(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -90,7 +145,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasOrderMultipleFunctions()
public function testBuilderAliasOrderMultipleFunctions(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -109,7 +164,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasGroupByMultipleFunctions()
public function testBuilderAliasGroupByMultipleFunctions(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -128,7 +183,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved()
public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved(): void
{
$parser = new Parser(
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
@@ -150,7 +205,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasOrderCase()
public function testBuilderAliasOrderCase(): void
{
$parser = new Parser(
'SELECT * FROM `world_borders` ORDER BY CASE '
@@ -169,7 +224,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderAliasGroupByCase()
public function testBuilderAliasGroupByCase(): void
{
$parser = new Parser(
'SELECT * FROM `world_borders` GROUP BY CASE '
@@ -188,7 +243,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderEndOptions()
public function testBuilderEndOptions(): void
{
/* Assertion 1 */
$query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 FOR UPDATE';
@@ -211,7 +266,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderIntoOptions()
public function testBuilderIntoOptions(): void
{
/* Assertion 1 */
$query = 'SELECT a, b, a+b INTO OUTFILE "/tmp/result.txt"'
@@ -227,7 +282,7 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderGroupBy()
public function testBuilderGroupBy(): void
{
$query = 'SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country';
$parser = new Parser($query);
@@ -239,7 +294,43 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderIndexHint()
public function testBuilderGroupByWithRollup(): void
{
$query = 'SELECT year FROM movies GROUP BY year WITH ROLLUP';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
$stmt->build()
);
}
public function testBuilderGroupByMultipleColumnsWithRollup(): void
{
$query = 'SELECT title, year FROM movies GROUP BY title, year WITH ROLLUP';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
$stmt->build()
);
}
public function testBuilderGroupByWithRollupWithOtherClauses(): void
{
$query = 'SELECT year FROM movies GROUP BY year WITH ROLLUP ORDER BY year ASC LIMIT 0, 5';
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals(
$query,
$stmt->build()
);
}
public function testBuilderIndexHint(): void
{
$query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0';
$parser = new Parser($query);
@@ -251,7 +342,8 @@ class SelectStatementTest extends TestCase
);
}
public function testBuilderSurroundedByParanthesisWithLimit() {
public function testBuilderSurroundedByParanthesisWithLimit(): void
{
$query = '(SELECT first_name FROM `actor` LIMIT 1, 2)';
$parser = new Parser($query);
$stmt = $parser->statements[0];
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class SetStatementTest extends TestCase
{
public function testBuilderView()
public function testBuilderView(): void
{
/* Assertion 1 */
$query = 'SET CHARACTER SET \'utf8\'';
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Components\Condition;
@@ -11,11 +13,11 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class StatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$stmt = new SelectStatement();
$stmt->options = new OptionsArray(array('DISTINCT'));
$stmt->options = new OptionsArray(['DISTINCT']);
$stmt->expr[] = new Expression('sakila', 'film', 'film_id', 'fid');
$stmt->expr[] = new Expression('COUNT(film_id)');
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class TransactionStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'START TRANSACTION;' .
'SELECT @A:=SUM(salary) FROM table1 WHERE type=1;' .
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Builder;
use PhpMyAdmin\SqlParser\Parser;
@@ -7,7 +9,7 @@ use PhpMyAdmin\SqlParser\Tests\TestCase;
class TruncateStatementTest extends TestCase
{
public function testBuilder()
public function testBuilder(): void
{
$query = 'TRUNCATE TABLE mytable;';
@@ -17,7 +19,7 @@ class TruncateStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
public function testBuilderDbtable()
public function testBuilderDbtable(): void
{
$query = 'TRUNCATE TABLE mydb.mytable;';
@@ -27,7 +29,7 @@ class TruncateStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
public function testBuilderDbtableBackQuotes()
public function testBuilderDbtableBackQuotes(): void
{
$query = 'TRUNCATE TABLE `mydb`.`mytable`;';
@@ -36,5 +38,4 @@ class TruncateStatementTest extends TestCase
$this->assertEquals($query, $stmt->build());
}
}