-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from kamil-tekiela/Refactor-Parser
Refactor Parser and Statement
- Loading branch information
Showing
11 changed files
with
92 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,18 +48,13 @@ abstract class Statement implements Stringable | |
*/ | ||
public static array $statementOptions = []; | ||
|
||
protected const ADD_CLAUSE = 1; | ||
protected const ADD_KEYWORD = 2; | ||
|
||
/** | ||
* The clauses of this statement, in order. | ||
* | ||
* The value attributed to each clause is used by the builder and it may | ||
* have one of the following values: | ||
* | ||
* - 1 = 01 - add the clause only | ||
* - 2 = 10 - add the keyword | ||
* - 3 = 11 - add both the keyword and the clause | ||
* | ||
* @var array<string, array<int, int|string>> | ||
* @psalm-var array<string, array{non-empty-string, (1|2|3)}> | ||
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}> | ||
*/ | ||
public static array $clauses = []; | ||
Check failure on line 59 in src/Statement.php GitHub Actions / lint-docs
|
||
|
||
|
@@ -115,29 +110,7 @@ public function build(): string | |
*/ | ||
$built = []; | ||
|
||
/** | ||
* Statement's clauses. | ||
*/ | ||
$clauses = $this->getClauses(); | ||
|
||
foreach ($clauses as $clause) { | ||
/** | ||
* The name of the clause. | ||
*/ | ||
$name = $clause[0]; | ||
|
||
/** | ||
* The type of the clause. | ||
* | ||
* @see Statement::$clauses | ||
*/ | ||
$type = $clause[1]; | ||
|
||
/** | ||
* The builder (parser) of this clause. | ||
*/ | ||
$class = Parser::KEYWORD_PARSERS[$name]['class']; | ||
|
||
foreach ($this->getClauses() as [$name, $type]) { | ||
/** | ||
* The name of the field that is used as source for the builder. | ||
* Same field is used to store the result of parsing. | ||
|
@@ -150,7 +123,7 @@ public function build(): string | |
} | ||
|
||
// Checking if this field was already built. | ||
if ($type & 1) { | ||
if ($type & self::ADD_CLAUSE) { | ||
if (! empty($built[$field])) { | ||
continue; | ||
} | ||
|
@@ -159,16 +132,17 @@ public function build(): string | |
} | ||
|
||
// Checking if the name of the clause should be added. | ||
if ($type & 2) { | ||
if ($type & self::ADD_KEYWORD) { | ||
$query = trim($query) . ' ' . $name; | ||
} | ||
|
||
// Checking if the result of the builder should be added. | ||
if (! ($type & 1)) { | ||
if (! ($type & self::ADD_CLAUSE)) { | ||
continue; | ||
} | ||
|
||
if (is_array($this->$field)) { | ||
$class = Parser::KEYWORD_PARSERS[$name]['class']; | ||
$query = trim($query) . ' ' . $class::buildAll($this->$field); | ||
} else { | ||
$query = trim($query) . ' ' . $this->$field->build(); | ||
|
@@ -286,7 +260,7 @@ public function parse(Parser $parser, TokensList $list): void | |
$options = []; | ||
|
||
// Looking for duplicated clauses. | ||
if (! empty(Parser::KEYWORD_PARSERS[$token->value]) || ! empty(Parser::STATEMENT_PARSERS[$token->value])) { | ||
if (isset(Parser::KEYWORD_PARSERS[$token->value]) || ! empty(Parser::STATEMENT_PARSERS[$token->value])) { | ||
if (! empty($parsedClauses[$token->value])) { | ||
$parser->error('This type of clause was previously parsed.', $token); | ||
break; | ||
|
@@ -300,7 +274,7 @@ public function parse(Parser $parser, TokensList $list): void | |
// but it might be the beginning of a statement of truncate, | ||
// so let the value use the keyword field for truncate type. | ||
$tokenValue = in_array($token->keyword, ['TRUNCATE']) ? $token->keyword : $token->value; | ||
if (! empty(Parser::KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) { | ||
if (isset(Parser::KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) { | ||
$class = Parser::KEYWORD_PARSERS[$tokenValue]['class']; | ||
$field = Parser::KEYWORD_PARSERS[$tokenValue]['field']; | ||
if (! empty(Parser::KEYWORD_PARSERS[$tokenValue]['options'])) { | ||
|
@@ -419,8 +393,7 @@ public function after(Parser $parser, TokensList $list, Token $token): void | |
/** | ||
* Gets the clauses of this statement. | ||
* | ||
* @return array<string, array<int, int|string>> | ||
* @psalm-return array<string, array{non-empty-string, (1|2|3)}> | ||
* @return array<string, array{non-empty-string, int-mask-of<Statement::ADD_*>}> | ||
*/ | ||
public function getClauses(): array | ||
Check failure on line 398 in src/Statement.php GitHub Actions / lint-docs
|
||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,42 +62,41 @@ class DeleteStatement extends Statement | |
* | ||
* @see Statement::$clauses | ||
* | ||
* @var array<string, array<int, int|string>> | ||
* @psalm-var array<string, array{non-empty-string, (1|2|3)}> | ||
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}> | ||
*/ | ||
public static array $clauses = [ | ||
Check failure on line 67 in src/Statements/DeleteStatement.php GitHub Actions / lint-docs
|
||
'DELETE' => [ | ||
'DELETE', | ||
2, | ||
Statement::ADD_KEYWORD, | ||
], | ||
// Used for options. | ||
'_OPTIONS' => [ | ||
'_OPTIONS', | ||
1, | ||
Statement::ADD_CLAUSE, | ||
], | ||
'FROM' => [ | ||
'FROM', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
'PARTITION' => [ | ||
'PARTITION', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
'USING' => [ | ||
'USING', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
'WHERE' => [ | ||
'WHERE', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
'ORDER BY' => [ | ||
'ORDER BY', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
'LIMIT' => [ | ||
'LIMIT', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
]; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,27 +42,26 @@ class DropStatement extends Statement | |
* | ||
* @see Statement::$clauses | ||
* | ||
* @var array<string, array<int, int|string>> | ||
* @psalm-var array<string, array{non-empty-string, (1|2|3)}> | ||
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}> | ||
*/ | ||
public static array $clauses = [ | ||
Check failure on line 47 in src/Statements/DropStatement.php GitHub Actions / lint-docs
|
||
'DROP' => [ | ||
'DROP', | ||
2, | ||
Statement::ADD_KEYWORD, | ||
], | ||
// Used for options. | ||
'_OPTIONS' => [ | ||
'_OPTIONS', | ||
1, | ||
Statement::ADD_CLAUSE, | ||
], | ||
// Used for select expressions. | ||
'DROP_' => [ | ||
'DROP', | ||
1, | ||
Statement::ADD_CLAUSE, | ||
], | ||
'ON' => [ | ||
'ON', | ||
3, | ||
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD, | ||
], | ||
]; | ||
|
||
|
Oops, something went wrong.