Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
*/
public function __construct(Parser|null $parser = null, TokensList|null $list = null)
{
if (($parser === null) || ($list === null)) {

Check warning on line 96 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalOr": @@ @@ */ public function __construct(Parser|null $parser = null, TokensList|null $list = null) { - if (($parser === null) || ($list === null)) { + if ($parser === null && $list === null) { return; }
return;
}

Expand Down Expand Up @@ -145,7 +145,7 @@

// Checking if the name of the clause should be added.
if ($type & self::ADD_KEYWORD) {
$query = trim($query) . ' ' . $name;

Check warning on line 148 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "UnwrapTrim": @@ @@ // Checking if the name of the clause should be added. if ($type & self::ADD_KEYWORD) { - $query = trim($query) . ' ' . $name; + $query = $query . ' ' . $name; } // Checking if the result of the builder should be added.
}

// Checking if the result of the builder should be added.
Expand All @@ -157,7 +157,7 @@
$class = Parser::KEYWORD_PARSERS[$name]['class'];
$query = trim($query) . ' ' . $class::buildAll($this->$field);
} else {
$query = trim($query) . ' ' . $this->$field->build();

Check warning on line 160 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "UnwrapTrim": @@ @@ $class = Parser::KEYWORD_PARSERS[$name]['class']; $query = trim($query) . ' ' . $class::buildAll($this->$field); } else { - $query = trim($query) . ' ' . $this->$field->build(); + $query = $query . ' ' . $this->$field->build(); } }
}
}

Expand All @@ -172,7 +172,7 @@
*
* @throws ParserException
*/
public function parse(Parser $parser, TokensList $list): void

Check warning on line 175 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "PublicVisibility": @@ @@ * * @throws ParserException */ - public function parse(Parser $parser, TokensList $list): void + protected function parse(Parser $parser, TokensList $list): void { /** * Array containing all list of clauses parsed.
{
/**
* Array containing all list of clauses parsed.
Expand All @@ -190,7 +190,7 @@
*/
$parsedOptions = static::$statementOptions === [];

for (; $list->idx < $list->count; ++$list->idx) {

Check warning on line 193 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThan": @@ @@ */ $parsedOptions = static::$statementOptions === []; - for (; $list->idx < $list->count; ++$list->idx) { + for (; $list->idx <= $list->count; ++$list->idx) { /** * Token parsed at this moment. */
/**
* Token parsed at this moment.
*/
Expand All @@ -203,7 +203,7 @@

// Checking if this closing bracket is the pair for a bracket
// outside the statement.
if (($token->value === ')') && ($parser->brackets > 0)) {

Check warning on line 206 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalAnd": @@ @@ // Checking if this closing bracket is the pair for a bracket // outside the statement. - if (($token->value === ')') && ($parser->brackets > 0)) { + if ($token->value === ')' || $parser->brackets > 0) { --$parser->brackets; continue; }

Check warning on line 206 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "GreaterThanNegotiation": @@ @@ // Checking if this closing bracket is the pair for a bracket // outside the statement. - if (($token->value === ')') && ($parser->brackets > 0)) { + if (($token->value === ')') && ($parser->brackets <= 0)) { --$parser->brackets; continue; }

Check warning on line 206 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Identical": @@ @@ // Checking if this closing bracket is the pair for a bracket // outside the statement. - if (($token->value === ')') && ($parser->brackets > 0)) { + if (($token->value !== ')') && ($parser->brackets > 0)) { --$parser->brackets; continue; }

Check warning on line 206 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "GreaterThan": @@ @@ // Checking if this closing bracket is the pair for a bracket // outside the statement. - if (($token->value === ')') && ($parser->brackets > 0)) { + if (($token->value === ')') && ($parser->brackets >= 0)) { --$parser->brackets; continue; }
--$parser->brackets;
continue;
}
Expand All @@ -221,7 +221,7 @@
// Unions are parsed by the parser because they represent more than
// one statement.
if (
($token->keyword === 'UNION') ||

Check warning on line 224 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalOr": @@ @@ // Unions are parsed by the parser because they represent more than // one statement. if ( - ($token->keyword === 'UNION') || - ($token->keyword === 'UNION ALL') || + $token->keyword === 'UNION' && $token->keyword === 'UNION ALL' || ($token->keyword === 'UNION DISTINCT') || ($token->keyword === 'EXCEPT') || ($token->keyword === 'INTERSECT')
($token->keyword === 'UNION ALL') ||
($token->keyword === 'UNION DISTINCT') ||
($token->keyword === 'EXCEPT') ||
Expand All @@ -241,9 +241,9 @@
$field = null;

/**
* Parser's options.
* The keyword of the clause, a key of `Parser::KEYWORD_PARSERS`.
*/
$options = [];
$keyword = '';

// Looking for duplicated clauses.
if (
Expand All @@ -270,11 +270,9 @@
// so let the value use the keyword field for truncate type.
$tokenValue = $token->keyword === 'TRUNCATE' ? $token->keyword : $token->value;
if (is_string($tokenValue) && isset(Parser::KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) {
$keyword = $tokenValue;
$class = Parser::KEYWORD_PARSERS[$tokenValue]['class'];
$field = Parser::KEYWORD_PARSERS[$tokenValue]['field'];
if (isset(Parser::KEYWORD_PARSERS[$tokenValue]['options'])) {
$options = Parser::KEYWORD_PARSERS[$tokenValue]['options'];
}
}

// Checking if this is the beginning of the statement.
Expand Down Expand Up @@ -327,7 +325,7 @@
}

++$list->idx; // Skipping keyword or last option.
$this->$field = $class::parse($parser, $list, $options);
$this->$field = $class::parse($parser, $list, $this->getKeywordParserOptions($keyword));
}

$this->after($parser, $list, $token);
Expand Down Expand Up @@ -359,6 +357,18 @@
{
}

/**
* Gets the options for the parser of a clause. The options of the statement are already parsed.
*
* @param string $keyword the keyword of the clause, a key of `Parser::KEYWORD_PARSERS`
*
* @return array<string, mixed>
*/
protected function getKeywordParserOptions(string $keyword): array
{
return Parser::KEYWORD_PARSERS[$keyword]['options'] ?? [];
}

/**
* Gets the clauses of this statement.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Statements/AlterStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class AlterStatement extends Statement
{
/**
* Table affected.
* The altered object. A database name is in `database`, the name of other objects in `table`.
*/
public Expression|null $table = null;

Expand Down Expand Up @@ -77,12 +77,12 @@ public function parse(Parser $parser, TokensList $list): void
$this->options = $parsedOptions;
++$list->idx;

// Parsing affected table.
// Parsing the name of the altered object.
$this->table = Expressions::parse(
$parser,
$list,
[
'parseField' => 'table',
'parseField' => $parsedOptions->has('DATABASE') ? 'database' : 'table',
'breakOnAlias' => true,
],
);
Expand Down
14 changes: 14 additions & 0 deletions src/Statements/DropStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@ class DropStatement extends Statement
* Table of the dropped index.
*/
public Expression|null $table = null;

/** @return array<string, mixed> */
protected function getKeywordParserOptions(string $keyword): array
{
if (
$keyword === 'DROP'
&& $this->options !== null
&& ($this->options->has('DATABASE') || $this->options->has('SCHEMA'))
) {
return ['parseField' => 'database'];
}

return parent::getKeywordParserOptions($keyword);
}
}
2 changes: 2 additions & 0 deletions tests/Parser/DropStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static function dropProvider(): array
return [
['parser/parseDrop'],
['parser/parseDrop2'],
['parser/parseDropDatabase'],
['parser/parseDropSchema'],
];
}
}
4 changes: 4 additions & 0 deletions tests/Utils/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ public static function getTablesProvider(): array
'TRUNCATE tbl',
['`tbl`'],
],
[
'ALTER DATABASE db CHARACTER SET utf8mb4',
[],
],
[
'DROP VIEW v',
[],
Expand Down
4 changes: 2 additions & 2 deletions tests/data/parser/parseAlter5.out
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@
"last": 11,
"table": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": null,
"table": "abc",
"database": "abc",
"table": null,
"column": null,
"expr": "`abc`",
"alias": null,
Expand Down
1 change: 1 addition & 0 deletions tests/data/parser/parseDropDatabase.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP DATABASE `db`
219 changes: 219 additions & 0 deletions tests/data/parser/parseDropDatabase.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"query": "DROP DATABASE `db`",
"lexer": {
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
"strict": false,
"errors": [],
"str": "DROP DATABASE `db`",
"len": 18,
"last": 18,
"list": {
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
"count": 6,
"idx": 6,
"tokens": [
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "DROP",
"value": "DROP",
"keyword": "DROP",
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Keyword",
"value": 1
},
"flags": 3,
"position": 0
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Whitespace",
"value": 3
},
"flags": 0,
"position": 4
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "DATABASE",
"value": "DATABASE",
"keyword": "DATABASE",
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Keyword",
"value": 1
},
"flags": 35,
"position": 5
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Whitespace",
"value": 3
},
"flags": 0,
"position": 13
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "`db`",
"value": "db",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Symbol",
"value": 8
},
"flags": 2,
"position": 14
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "",
"value": "",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Delimiter",
"value": 9
},
"flags": 0,
"position": null
}
]
},
"delimiter": ";",
"delimiterLen": 1
},
"parser": {
"@type": "PhpMyAdmin\\SqlParser\\Parser",
"strict": false,
"errors": [],
"list": {
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
"count": 6,
"idx": 6,
"tokens": [
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "DROP",
"value": "DROP",
"keyword": "DROP",
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Keyword",
"value": 1
},
"flags": 3,
"position": 0
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Whitespace",
"value": 3
},
"flags": 0,
"position": 4
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "DATABASE",
"value": "DATABASE",
"keyword": "DATABASE",
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Keyword",
"value": 1
},
"flags": 35,
"position": 5
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Whitespace",
"value": 3
},
"flags": 0,
"position": 13
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "`db`",
"value": "db",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Symbol",
"value": 8
},
"flags": 2,
"position": 14
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "",
"value": "",
"keyword": null,
"type": {
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
"name": "Delimiter",
"value": 9
},
"flags": 0,
"position": null
}
]
},
"statements": [
{
"@type": "PhpMyAdmin\\SqlParser\\Statements\\DropStatement",
"options": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
"options": {
"1": "DATABASE"
}
},
"first": 0,
"last": 4,
"fields": [
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": "db",
"table": null,
"column": null,
"expr": "`db`",
"alias": null,
"function": null,
"subquery": null
}
],
"table": null
}
],
"brackets": 0
},
"errors": {
"lexer": [],
"parser": []
}
}
1 change: 1 addition & 0 deletions tests/data/parser/parseDropSchema.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP SCHEMA IF EXISTS db
Loading
Loading