diff --git a/README.md b/README.md index 46c6cbb5..af54459a 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ $query = 'OPTIMIZE TABLE tbl'; $parser = new Parser($query); $flags = Query::getFlags($parser->statements[0]); -echo $flags['querytype']; +echo $flags->queryType?->value; ``` ### Parsing and building SQL query diff --git a/src/Utils/Query.php b/src/Utils/Query.php index 6ad1daa2..e80b17bf 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -39,36 +39,6 @@ /** * Statement utilities. - * - * @psalm-type QueryFlagsType = array{ - * distinct?: bool, - * drop_database?: bool, - * group?: bool, - * having?: bool, - * is_affected?: bool, - * is_analyse?: bool, - * is_count?: bool, - * is_delete?: bool, - * is_explain?: bool, - * is_export?: bool, - * is_func?: bool, - * is_group?: bool, - * is_insert?: bool, - * is_maint?: bool, - * is_procedure?: bool, - * is_replace?: bool, - * is_select?: bool, - * is_show?: bool, - * is_subquery?: bool, - * join?: bool, - * limit?: bool, - * offset?: bool, - * order?: bool, - * querytype: ('ALTER'|'ANALYZE'|'CALL'|'CHECK'|'CHECKSUM'|'CREATE'|'DELETE'|'DROP'|'EXPLAIN'|'INSERT'|'LOAD'|'OPTIMIZE'|'REPAIR'|'REPLACE'|'SELECT'|'SET'|'SHOW'|'UPDATE'|false), - * reload?: bool, - * select_from?: bool, - * union?: bool - * } */ class Query { @@ -88,223 +58,32 @@ class Query 'BIT_AND', ]; - /** - * @var array - * @psalm-var array{ - * distinct: false, - * drop_database: false, - * group: false, - * having: false, - * is_affected: false, - * is_analyse: false, - * is_count: false, - * is_delete: false, - * is_explain: false, - * is_export: false, - * is_func: false, - * is_group: false, - * is_insert: false, - * is_maint: false, - * is_procedure: false, - * is_replace: false, - * is_select: false, - * is_show: false, - * is_subquery: false, - * join: false, - * limit: false, - * offset: false, - * order: false, - * querytype: false, - * reload: false, - * select_from: false, - * union: false - * } - */ - public static array $allFlags = [ - /* - * select ... DISTINCT ... - */ - 'distinct' => false, - - /* - * drop ... DATABASE ... - */ - 'drop_database' => false, - - /* - * ... GROUP BY ... - */ - 'group' => false, - - /* - * ... HAVING ... - */ - 'having' => false, - - /* - * INSERT ... - * or - * REPLACE ... - * or - * DELETE ... - */ - 'is_affected' => false, - - /* - * select ... PROCEDURE ANALYSE( ... ) ... - */ - 'is_analyse' => false, - - /* - * select COUNT( ... ) ... - */ - 'is_count' => false, - - /* - * DELETE ... - */ - 'is_delete' => false, // @deprecated; use `querytype` - - /* - * EXPLAIN ... - */ - 'is_explain' => false, // @deprecated; use `querytype` - - /* - * select ... INTO OUTFILE ... - */ - 'is_export' => false, - - /* - * select FUNC( ... ) ... - */ - 'is_func' => false, - - /* - * select ... GROUP BY ... - * or - * select ... HAVING ... - */ - 'is_group' => false, - - /* - * INSERT ... - * or - * REPLACE ... - * or - * LOAD DATA ... - */ - 'is_insert' => false, - - /* - * ANALYZE ... - * or - * CHECK ... - * or - * CHECKSUM ... - * or - * OPTIMIZE ... - * or - * REPAIR ... - */ - 'is_maint' => false, - - /* - * CALL ... - */ - 'is_procedure' => false, - - /* - * REPLACE ... - */ - 'is_replace' => false, // @deprecated; use `querytype` - - /* - * SELECT ... - */ - 'is_select' => false, // @deprecated; use `querytype` - - /* - * SHOW ... - */ - 'is_show' => false, // @deprecated; use `querytype` - - /* - * Contains a subquery. - */ - 'is_subquery' => false, - - /* - * ... JOIN ... - */ - 'join' => false, - - /* - * ... LIMIT ... - */ - 'limit' => false, - - /* - * TODO - */ - 'offset' => false, - - /* - * ... ORDER ... - */ - 'order' => false, - - /* - * The type of the query (which is usually the first keyword of - * the statement). - */ - 'querytype' => false, - - /* - * Whether a page reload is required. - */ - 'reload' => false, - - /* - * SELECT ... FROM ... - */ - 'select_from' => false, - - /* - * ... UNION ... - */ - 'union' => false, - ]; - /** * Gets an array with flags select statement has. * - * @param SelectStatement $statement the statement to be processed - * @param array $flags flags set so far - * @psalm-param QueryFlagsType $flags - * - * @return array - * @psalm-return QueryFlagsType + * @param SelectStatement $statement the statement to be processed + * @param StatementFlags $flags flags set so far */ - private static function getFlagsSelect(SelectStatement $statement, array $flags): array + private static function getFlagsSelect(SelectStatement $statement, StatementFlags $flags): void { - $flags['querytype'] = 'SELECT'; - $flags['is_select'] = true; + $flags->queryType = StatementType::Select; + /** @psalm-suppress DeprecatedProperty */ + $flags->isSelect = true; if ($statement->from !== []) { - $flags['select_from'] = true; + $flags->selectFrom = true; } if ($statement->options->has('DISTINCT')) { - $flags['distinct'] = true; + $flags->distinct = true; } if (! empty($statement->group) || ! empty($statement->having)) { - $flags['is_group'] = true; + $flags->isGroup = true; } if (! empty($statement->into) && ($statement->into->type === 'OUTFILE')) { - $flags['is_export'] = true; + $flags->isExport = true; } $expressions = $statement->expr; @@ -317,9 +96,9 @@ private static function getFlagsSelect(SelectStatement $statement, array $flags) foreach ($expressions as $expr) { if (! empty($expr->function)) { if ($expr->function === 'COUNT') { - $flags['is_count'] = true; + $flags->isCount = true; } elseif (in_array($expr->function, static::$functions)) { - $flags['is_func'] = true; + $flags->isFunc = true; } } @@ -327,109 +106,106 @@ private static function getFlagsSelect(SelectStatement $statement, array $flags) continue; } - $flags['is_subquery'] = true; + $flags->isSubQuery = true; } if (! empty($statement->procedure) && ($statement->procedure->name === 'ANALYSE')) { - $flags['is_analyse'] = true; + $flags->isAnalyse = true; } if (! empty($statement->group)) { - $flags['group'] = true; + $flags->group = true; } if (! empty($statement->having)) { - $flags['having'] = true; + $flags->having = true; } if ($statement->union !== []) { - $flags['union'] = true; + $flags->union = true; } - if (! empty($statement->join)) { - $flags['join'] = true; + if (empty($statement->join)) { + return; } - return $flags; + $flags->join = true; } /** * Gets an array with flags this statement has. * * @param Statement|null $statement the statement to be processed - * @param bool $all if `false`, false values will not be included - * - * @return array - * @psalm-return QueryFlagsType */ - public static function getFlags(Statement|null $statement, bool $all = false): array + public static function getFlags(Statement|null $statement): StatementFlags { - $flags = ['querytype' => false]; - if ($all) { - $flags = self::$allFlags; - } + $flags = new StatementFlags(); if ($statement instanceof AlterStatement) { - $flags['querytype'] = 'ALTER'; - $flags['reload'] = true; + $flags->queryType = StatementType::Alter; + $flags->reload = true; } elseif ($statement instanceof CreateStatement) { - $flags['querytype'] = 'CREATE'; - $flags['reload'] = true; + $flags->queryType = StatementType::Create; + $flags->reload = true; } elseif ($statement instanceof AnalyzeStatement) { - $flags['querytype'] = 'ANALYZE'; - $flags['is_maint'] = true; + $flags->queryType = StatementType::Analyze; + $flags->isMaint = true; } elseif ($statement instanceof CheckStatement) { - $flags['querytype'] = 'CHECK'; - $flags['is_maint'] = true; + $flags->queryType = StatementType::Check; + $flags->isMaint = true; } elseif ($statement instanceof ChecksumStatement) { - $flags['querytype'] = 'CHECKSUM'; - $flags['is_maint'] = true; + $flags->queryType = StatementType::Checksum; + $flags->isMaint = true; } elseif ($statement instanceof OptimizeStatement) { - $flags['querytype'] = 'OPTIMIZE'; - $flags['is_maint'] = true; + $flags->queryType = StatementType::Optimize; + $flags->isMaint = true; } elseif ($statement instanceof RepairStatement) { - $flags['querytype'] = 'REPAIR'; - $flags['is_maint'] = true; + $flags->queryType = StatementType::Repair; + $flags->isMaint = true; } elseif ($statement instanceof CallStatement) { - $flags['querytype'] = 'CALL'; - $flags['is_procedure'] = true; + $flags->queryType = StatementType::Call; + $flags->isProcedure = true; } elseif ($statement instanceof DeleteStatement) { - $flags['querytype'] = 'DELETE'; - $flags['is_delete'] = true; - $flags['is_affected'] = true; + $flags->queryType = StatementType::Delete; + /** @psalm-suppress DeprecatedProperty */ + $flags->isDelete = true; + $flags->isAffected = true; } elseif ($statement instanceof DropStatement) { - $flags['querytype'] = 'DROP'; - $flags['reload'] = true; + $flags->queryType = StatementType::Drop; + $flags->reload = true; if ($statement->options->has('DATABASE') || $statement->options->has('SCHEMA')) { - $flags['drop_database'] = true; + $flags->dropDatabase = true; } } elseif ($statement instanceof ExplainStatement) { - $flags['querytype'] = 'EXPLAIN'; - $flags['is_explain'] = true; + $flags->queryType = StatementType::Explain; + /** @psalm-suppress DeprecatedProperty */ + $flags->isExplain = true; } elseif ($statement instanceof InsertStatement) { - $flags['querytype'] = 'INSERT'; - $flags['is_affected'] = true; - $flags['is_insert'] = true; + $flags->queryType = StatementType::Insert; + $flags->isAffected = true; + $flags->isInsert = true; } elseif ($statement instanceof LoadStatement) { - $flags['querytype'] = 'LOAD'; - $flags['is_affected'] = true; - $flags['is_insert'] = true; + $flags->queryType = StatementType::Load; + $flags->isAffected = true; + $flags->isInsert = true; } elseif ($statement instanceof ReplaceStatement) { - $flags['querytype'] = 'REPLACE'; - $flags['is_affected'] = true; - $flags['is_replace'] = true; - $flags['is_insert'] = true; + $flags->queryType = StatementType::Replace; + $flags->isAffected = true; + /** @psalm-suppress DeprecatedProperty */ + $flags->isReplace = true; + $flags->isInsert = true; } elseif ($statement instanceof SelectStatement) { - $flags = self::getFlagsSelect($statement, $flags); + self::getFlagsSelect($statement, $flags); } elseif ($statement instanceof ShowStatement) { - $flags['querytype'] = 'SHOW'; - $flags['is_show'] = true; + $flags->queryType = StatementType::Show; + /** @psalm-suppress DeprecatedProperty */ + $flags->isShow = true; } elseif ($statement instanceof UpdateStatement) { - $flags['querytype'] = 'UPDATE'; - $flags['is_affected'] = true; + $flags->queryType = StatementType::Update; + $flags->isAffected = true; } elseif ($statement instanceof SetStatement) { - $flags['querytype'] = 'SET'; + $flags->queryType = StatementType::Set; } if ( @@ -438,11 +214,11 @@ public static function getFlags(Statement|null $statement, bool $all = false): a || ($statement instanceof DeleteStatement) ) { if (! empty($statement->limit)) { - $flags['limit'] = true; + $flags->limit = true; } if (! empty($statement->order)) { - $flags['order'] = true; + $flags->order = true; } } @@ -453,41 +229,21 @@ public static function getFlags(Statement|null $statement, bool $all = false): a * Parses a query and gets all information about it. * * @param string $query the query to be parsed - * - * @return array The array returned is the one returned by - * `static::getFlags()`, with the following keys added: - * - parser - the parser used to analyze the query; - * - statement - the first statement resulted from parsing; - * - select_tables - the real name of the tables selected; - * if there are no table names in the `SELECT` - * expressions, the table names are fetched from the - * `FROM` expressions - * - select_expr - selected expressions - * @psalm-return QueryFlagsType&array{ - * select_expr?: (string|null)[], - * select_tables?: array{string, string|null}[], - * statement?: Statement|null, parser?: Parser - * } */ - public static function getAll(string $query): array + public static function getAll(string $query): StatementInfo { $parser = new Parser($query); if ($parser->statements === []) { - return static::getFlags(null, true); + return new StatementInfo($parser, null, static::getFlags(null), [], []); } $statement = $parser->statements[0]; - - $ret = static::getFlags($statement, true); - - $ret['parser'] = $parser; - $ret['statement'] = $statement; + $flags = static::getFlags($statement); + $selectTables = []; + $selectExpressions = []; if ($statement instanceof SelectStatement) { - $ret['select_tables'] = []; - $ret['select_expr'] = []; - // Finding tables' aliases and their associated real names. $tableAliases = []; foreach ($statement->from as $expr) { @@ -516,18 +272,18 @@ public static function getAll(string $query): array ]; } - if (! in_array($arr, $ret['select_tables'])) { - $ret['select_tables'][] = $arr; + if (! in_array($arr, $selectTables)) { + $selectTables[] = $arr; } } else { - $ret['select_expr'][] = $expr->expr; + $selectExpressions[] = $expr->expr; } } // If no tables names were found in the SELECT clause or if there // are expressions like * or COUNT(*), etc. tables names should be // extracted from the FROM clause. - if ($ret['select_tables'] === []) { + if ($selectTables === []) { foreach ($statement->from as $expr) { if (! isset($expr->table) || ($expr->table === '')) { continue; @@ -538,16 +294,16 @@ public static function getAll(string $query): array isset($expr->database) && ($expr->database !== '') ? $expr->database : null, ]; - if (in_array($arr, $ret['select_tables'])) { + if (in_array($arr, $selectTables)) { continue; } - $ret['select_tables'][] = $arr; + $selectTables[] = $arr; } } } - return $ret; + return new StatementInfo($parser, $statement, $flags, $selectTables, $selectExpressions); } /** diff --git a/src/Utils/StatementFlags.php b/src/Utils/StatementFlags.php new file mode 100644 index 00000000..f135adad --- /dev/null +++ b/src/Utils/StatementFlags.php @@ -0,0 +1,153 @@ + $selectTables + * @psalm-param list $selectExpressions + */ + public function __construct( + public readonly Parser $parser, + public readonly Statement|null $statement, + public readonly StatementFlags $flags, + public readonly array $selectTables, + public readonly array $selectExpressions, + ) { + } +} diff --git a/src/Utils/StatementType.php b/src/Utils/StatementType.php new file mode 100644 index 00000000..a2e03ea7 --- /dev/null +++ b/src/Utils/StatementType.php @@ -0,0 +1,27 @@ + $expected - * @psalm-param QueryFlagsType $expected + * @psalm-param non-empty-string $query + * @psalm-param array>, bool|StatementType|null> $expected */ #[DataProvider('getFlagsProvider')] public function testGetFlags(string $query, array $expected): void { $parser = new Parser($query); - $this->assertEquals($expected, Query::getFlags($parser->statements[0])); + $flags = new StatementFlags(); + foreach ($expected as $property => $expectedValue) { + $flags->$property = $expectedValue; + } + + $this->assertEquals($flags, Query::getFlags($parser->statements[0])); } - /** - * @return array>> - * @psalm-return list - */ + /** @psalm-return list>, bool|StatementType|null>}> */ public static function getFlagsProvider(): array { return [ @@ -36,216 +37,216 @@ public static function getFlagsProvider(): array 'ALTER TABLE DROP col', [ 'reload' => true, - 'querytype' => 'ALTER', + 'queryType' => StatementType::Alter, ], ], [ 'CALL test()', [ - 'is_procedure' => true, - 'querytype' => 'CALL', + 'isProcedure' => true, + 'queryType' => StatementType::Call, ], ], [ 'CREATE TABLE tbl (id INT)', [ 'reload' => true, - 'querytype' => 'CREATE', + 'queryType' => StatementType::Create, ], ], [ 'CHECK TABLE tbl', [ - 'is_maint' => true, - 'querytype' => 'CHECK', + 'isMaint' => true, + 'queryType' => StatementType::Check, ], ], [ 'DELETE FROM tbl', [ - 'is_affected' => true, - 'is_delete' => true, - 'querytype' => 'DELETE', + 'isAffected' => true, + 'isDelete' => true, + 'queryType' => StatementType::Delete, ], ], [ 'DROP VIEW v', [ 'reload' => true, - 'querytype' => 'DROP', + 'queryType' => StatementType::Drop, ], ], [ 'DROP DATABASE db', [ - 'drop_database' => true, + 'dropDatabase' => true, 'reload' => true, - 'querytype' => 'DROP', + 'queryType' => StatementType::Drop, ], ], [ 'EXPLAIN tbl', [ - 'is_explain' => true, - 'querytype' => 'EXPLAIN', + 'isExplain' => true, + 'queryType' => StatementType::Explain, ], ], [ 'LOAD DATA INFILE \'/tmp/test.txt\' INTO TABLE test', [ - 'is_affected' => true, - 'is_insert' => true, - 'querytype' => 'LOAD', + 'isAffected' => true, + 'isInsert' => true, + 'queryType' => StatementType::Load, ], ], [ 'INSERT INTO tbl VALUES (1)', [ - 'is_affected' => true, - 'is_insert' => true, - 'querytype' => 'INSERT', + 'isAffected' => true, + 'isInsert' => true, + 'queryType' => StatementType::Insert, ], ], [ 'REPLACE INTO tbl VALUES (2)', [ - 'is_affected' => true, - 'is_replace' => true, - 'is_insert' => true, - 'querytype' => 'REPLACE', + 'isAffected' => true, + 'isReplace' => true, + 'isInsert' => true, + 'queryType' => StatementType::Replace, ], ], [ 'SELECT 1', [ - 'is_select' => true, - 'querytype' => 'SELECT', + 'isSelect' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM tbl', [ - 'is_select' => true, - 'select_from' => true, - 'querytype' => 'SELECT', + 'isSelect' => true, + 'selectFrom' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT DISTINCT * FROM tbl LIMIT 0, 10 ORDER BY id', [ 'distinct' => true, - 'is_select' => true, - 'select_from' => true, + 'isSelect' => true, + 'selectFrom' => true, 'limit' => true, 'order' => true, - 'querytype' => 'SELECT', + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM actor GROUP BY actor_id', [ - 'is_group' => true, - 'is_select' => true, - 'select_from' => true, + 'isGroup' => true, + 'isSelect' => true, + 'selectFrom' => true, 'group' => true, - 'querytype' => 'SELECT', + 'queryType' => StatementType::Select, ], ], [ 'SELECT col1, col2 FROM table1 PROCEDURE ANALYSE(10, 2000);', [ - 'is_analyse' => true, - 'is_select' => true, - 'select_from' => true, - 'querytype' => 'SELECT', + 'isAnalyse' => true, + 'isSelect' => true, + 'selectFrom' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM tbl INTO OUTFILE "/tmp/export.txt"', [ - 'is_export' => true, - 'is_select' => true, - 'select_from' => true, - 'querytype' => 'SELECT', + 'isExport' => true, + 'isSelect' => true, + 'selectFrom' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT COUNT(id), SUM(id) FROM tbl', [ - 'is_count' => true, - 'is_func' => true, - 'is_select' => true, - 'select_from' => true, - 'querytype' => 'SELECT', + 'isCount' => true, + 'isFunc' => true, + 'isSelect' => true, + 'selectFrom' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT (SELECT "foo")', [ - 'is_select' => true, - 'is_subquery' => true, - 'querytype' => 'SELECT', + 'isSelect' => true, + 'isSubQuery' => true, + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM customer HAVING store_id = 2;', [ - 'is_select' => true, - 'select_from' => true, - 'is_group' => true, + 'isSelect' => true, + 'selectFrom' => true, + 'isGroup' => true, 'having' => true, - 'querytype' => 'SELECT', + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;', [ - 'is_select' => true, - 'select_from' => true, + 'isSelect' => true, + 'selectFrom' => true, 'join' => true, - 'querytype' => 'SELECT', + 'queryType' => StatementType::Select, ], ], [ 'SHOW CREATE TABLE tbl', [ - 'is_show' => true, - 'querytype' => 'SHOW', + 'isShow' => true, + 'queryType' => StatementType::Show, ], ], [ 'UPDATE tbl SET id = 1', [ - 'is_affected' => true, - 'querytype' => 'UPDATE', + 'isAffected' => true, + 'queryType' => StatementType::Update, ], ], [ 'ANALYZE TABLE tbl', [ - 'is_maint' => true, - 'querytype' => 'ANALYZE', + 'isMaint' => true, + 'queryType' => StatementType::Analyze, ], ], [ 'CHECKSUM TABLE tbl', [ - 'is_maint' => true, - 'querytype' => 'CHECKSUM', + 'isMaint' => true, + 'queryType' => StatementType::Checksum, ], ], [ 'OPTIMIZE TABLE tbl', [ - 'is_maint' => true, - 'querytype' => 'OPTIMIZE', + 'isMaint' => true, + 'queryType' => StatementType::Optimize, ], ], [ 'REPAIR TABLE tbl', [ - 'is_maint' => true, - 'querytype' => 'REPAIR', + 'isMaint' => true, + 'queryType' => StatementType::Repair, ], ], [ @@ -253,146 +254,110 @@ public static function getFlagsProvider(): array 'UNION ' . '(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);', [ - 'is_select' => true, - 'select_from' => true, + 'isSelect' => true, + 'selectFrom' => true, 'limit' => true, 'order' => true, 'union' => true, - 'querytype' => 'SELECT', + 'queryType' => StatementType::Select, ], ], [ 'SELECT * FROM orders AS ord WHERE 1', [ - 'querytype' => 'SELECT', - 'is_select' => true, - 'select_from' => true, + 'queryType' => StatementType::Select, + 'isSelect' => true, + 'selectFrom' => true, ], ], [ 'SET NAMES \'latin\'', - ['querytype' => 'SET'], + ['queryType' => StatementType::Set], ], ]; } - public function testGetAll(): void + public function testGetFlagsWithEmptyString(): void { - $this->assertEquals( - [ - 'distinct' => false, - 'drop_database' => false, - 'group' => false, - 'having' => false, - 'is_affected' => false, - 'is_analyse' => false, - 'is_count' => false, - 'is_delete' => false, - 'is_explain' => false, - 'is_export' => false, - 'is_func' => false, - 'is_group' => false, - 'is_insert' => false, - 'is_maint' => false, - 'is_procedure' => false, - 'is_replace' => false, - 'is_select' => false, - 'is_show' => false, - 'is_subquery' => false, - 'join' => false, - 'limit' => false, - 'offset' => false, - 'order' => false, - 'querytype' => false, - 'reload' => false, - 'select_from' => false, - 'union' => false, - ], - Query::getAll(''), - ); + $statementInfo = Query::getAll(''); + self::assertEquals(new Parser(''), $statementInfo->parser); + self::assertNull($statementInfo->statement); + self::assertSame([], $statementInfo->selectTables); + self::assertSame([], $statementInfo->selectExpressions); + $flags = $statementInfo->flags; + self::assertFalse($flags->distinct); + self::assertFalse($flags->dropDatabase); + self::assertFalse($flags->group); + self::assertFalse($flags->having); + self::assertFalse($flags->isAffected); + self::assertFalse($flags->isAnalyse); + self::assertFalse($flags->isCount); + /** @psalm-suppress DeprecatedProperty */ + self::assertFalse($flags->isDelete); + /** @psalm-suppress DeprecatedProperty */ + self::assertFalse($flags->isExplain); + self::assertFalse($flags->isExport); + self::assertFalse($flags->isFunc); + self::assertFalse($flags->isGroup); + self::assertFalse($flags->isInsert); + self::assertFalse($flags->isMaint); + self::assertFalse($flags->isProcedure); + /** @psalm-suppress DeprecatedProperty */ + self::assertFalse($flags->isReplace); + /** @psalm-suppress DeprecatedProperty */ + self::assertFalse($flags->isSelect); + /** @psalm-suppress DeprecatedProperty */ + self::assertFalse($flags->isShow); + self::assertFalse($flags->isSubQuery); + self::assertFalse($flags->join); + self::assertFalse($flags->limit); + self::assertFalse($flags->offset); + self::assertFalse($flags->order); + self::assertNull($flags->queryType); + self::assertFalse($flags->reload); + self::assertFalse($flags->selectFrom); + self::assertFalse($flags->union); + } - $query = 'SELECT *, actor.actor_id, sakila2.film.* - FROM sakila2.city, sakila2.film, actor'; + public function testGetAll(): void + { + $query = 'SELECT *, actor.actor_id, sakila2.film.* FROM sakila2.city, sakila2.film, actor'; $parser = new Parser($query); - $this->assertEquals( - array_merge( - Query::getFlags($parser->statements[0], true), - [ - 'parser' => $parser, - 'statement' => $parser->statements[0], - 'select_expr' => ['*'], - 'select_tables' => [ - [ - 'actor', - null, - ], - [ - 'film', - 'sakila2', - ], - ], - ], - ), - Query::getAll($query), - ); + $statementInfo = Query::getAll($query); + self::assertEquals($parser, $statementInfo->parser); + self::assertEquals($parser->statements[0], $statementInfo->statement); + self::assertEquals(Query::getFlags($parser->statements[0]), $statementInfo->flags); + self::assertSame([['actor', null], ['film', 'sakila2']], $statementInfo->selectTables); + self::assertSame(['*'], $statementInfo->selectExpressions); $query = 'SELECT * FROM sakila.actor, film'; $parser = new Parser($query); - $this->assertEquals( - array_merge( - Query::getFlags($parser->statements[0], true), - [ - 'parser' => $parser, - 'statement' => $parser->statements[0], - 'select_expr' => ['*'], - 'select_tables' => [ - [ - 'actor', - 'sakila', - ], - [ - 'film', - null, - ], - ], - ], - ), - Query::getAll($query), - ); + $statementInfo = Query::getAll($query); + self::assertEquals($parser, $statementInfo->parser); + self::assertEquals($parser->statements[0], $statementInfo->statement); + self::assertEquals(Query::getFlags($parser->statements[0]), $statementInfo->flags); + self::assertSame([['actor', 'sakila'], ['film', null]], $statementInfo->selectTables); + self::assertSame(['*'], $statementInfo->selectExpressions); $query = 'SELECT a.actor_id FROM sakila.actor AS a, film'; $parser = new Parser($query); - $this->assertEquals( - array_merge( - Query::getFlags($parser->statements[0], true), - [ - 'parser' => $parser, - 'statement' => $parser->statements[0], - 'select_expr' => [], - 'select_tables' => [ - [ - 'actor', - 'sakila', - ], - ], - ], - ), - Query::getAll($query), - ); + $statementInfo = Query::getAll($query); + self::assertEquals($parser, $statementInfo->parser); + self::assertEquals($parser->statements[0], $statementInfo->statement); + self::assertEquals(Query::getFlags($parser->statements[0]), $statementInfo->flags); + self::assertSame([['actor', 'sakila']], $statementInfo->selectTables); + self::assertSame([], $statementInfo->selectExpressions); $query = 'SELECT CASE WHEN 2 IS NULL THEN "this is true" ELSE "this is false" END'; $parser = new Parser($query); - $this->assertEquals( - array_merge( - Query::getFlags($parser->statements[0], true), - [ - 'parser' => $parser, - 'statement' => $parser->statements[0], - 'select_expr' => ['CASE WHEN 2 IS NULL THEN "this is true" ELSE "this is false" END'], - 'select_tables' => [], - ], - ), - Query::getAll($query), + $statementInfo = Query::getAll($query); + self::assertEquals($parser, $statementInfo->parser); + self::assertEquals($parser->statements[0], $statementInfo->statement); + self::assertEquals(Query::getFlags($parser->statements[0]), $statementInfo->flags); + self::assertSame([], $statementInfo->selectTables); + self::assertSame( + ['CASE WHEN 2 IS NULL THEN "this is true" ELSE "this is false" END'], + $statementInfo->selectExpressions, ); }