diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-06-26 12:23:47 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-06-26 12:25:07 +0300 |
commit | 13a0881dc9591b3fc3eb2386eb6fde0bfb194ea6 (patch) | |
tree | f080396925e5cd311dc2180457fce7e8eb4b9903 /src | |
parent | 4ab1342a38db10cc4d58ba8952c5f245fcc484b9 (diff) | |
download | sql-parser-13a0881dc9591b3fc3eb2386eb6fde0bfb194ea6.zip sql-parser-13a0881dc9591b3fc3eb2386eb6fde0bfb194ea6.tar.gz sql-parser-13a0881dc9591b3fc3eb2386eb6fde0bfb194ea6.tar.bz2 |
Refactoring.
Diffstat (limited to 'src')
22 files changed, 244 insertions, 271 deletions
diff --git a/src/Fragments/FieldFragment.php b/src/Fragments/FieldFragment.php index a45c0fe..4a0c2ca 100644 --- a/src/Fragments/FieldFragment.php +++ b/src/Fragments/FieldFragment.php @@ -57,7 +57,7 @@ class FieldFragment extends Fragment /** * The name of the function. * - * @var string + * @var mixed */ public $function; diff --git a/src/Statement.php b/src/Statement.php index 0fa1fc2..1b7e590 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -5,21 +5,7 @@ namespace SqlParser; use SqlParser\Parser; use SqlParser\Statement; use SqlParser\Token; -use SqlParser\Fragments\CreateDefFragment; -use SqlParser\Fragments\DataTypeFragment; -use SqlParser\Fragments\FieldDefFragment; use SqlParser\Fragments\OptionsFragment; -use SqlParser\Fragments\ParamDefFragment; -use SqlParser\Statements\AlterStatement; -use SqlParser\Statements\BackupStatement; -use SqlParser\Statements\CheckStatement; -use SqlParser\Statements\ChecksumStatement; -use SqlParser\Statements\CreateStatement; -use SqlParser\Statements\ExplainStatement; -use SqlParser\Statements\RenameStatement; -use SqlParser\Statements\RepairStatement; -use SqlParser\Statements\RestoreStatement; -use SqlParser\Statements\ShowStatement; /** * Abstract statement definition. @@ -33,6 +19,15 @@ abstract class Statement { /** + * The options of this query. + * + * @var OptionsFragment + * + * @see static::$OPTIONS + */ + public $options; + + /** * The index of the first token used in this statement. * * @var int @@ -120,61 +115,7 @@ abstract class Statement continue; } - // Special cases: before parsing this keyword. - if ($this instanceof CreateStatement) { - ++$list->idx; - $this->name = CreateDefFragment::parse($parser, $list); - if ($this->options->has('TABLE')) { - ++$list->idx; - $this->fields = FieldDefFragment::parse($parser, $list); - ++$list->idx; - $this->entityOptions = OptionsFragment::parse( - $parser, - $list, - CreateDefFragment::$TABLE_OPTIONS - ); - } elseif (($this->options->has('PROCEDURE')) - || ($this->options->has('FUNCTION')) - ) { - ++$list->idx; - $this->parameters = ParamDefFragment::parse($parser, $list); - if ($this->options->has('FUNCTION')) { - $token = $list->getNextOfType(Token::TYPE_KEYWORD); - if ($token->value !== 'RETURNS') { - $parser->error( - '\'RETURNS\' keyword was expected.', - $token - ); - } else { - ++$list->idx; - $this->return = DataTypeFragment::parse( - $parser, - $list - ); - } - } - ++$list->idx; - $this->entityOptions = OptionsFragment::parse( - $parser, - $list, - CreateDefFragment::$FUNC_OPTIONS - ); - ++$list->idx; - $this->body = array(); - for (; $list->idx < $list->count; ++$list->idx) { - $token = $list->tokens[$list->idx]; - $this->body[] = $token; - if (($token->type === Token::TYPE_KEYWORD) - && ($token->value === 'END') - ) { - break; - } - } - $class = null; // The statement has been processed here. - } - } else if ($this instanceof RenameStatement) { - $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'TABLE'); - } + $this->before($parser, $list, $token); // Parsing this keyword. if ($class !== null) { @@ -182,42 +123,38 @@ abstract class Statement $this->$field = $class::parse($parser, $list, array()); } - // Special cases: after parsing this keyword. - if (($this instanceof BackupStatement) - || ($this instanceof CheckStatement) - || ($this instanceof ChecksumStatement) - || ($this instanceof RepairStatement) - || ($this instanceof RestoreStatement) - ) { - - // The statements mentioned above follow this template: - // `STMT` <some options> <tables> <some more options> - // - // First of all, because static::$OPTIONS is set for all of the - // statements above, <some options> is going to be parsed first. - // - // There is a parser specified in `Parser::$KEYWORD_PARSERS` - // which parses <tables>. - // - // Finally, we pares <some more options> here and that's all. - ++$list->idx; - $this->options->merge( - OptionsFragment::parse( - $parser, - $list, - static::$OPTIONS - ) - ); - } else if (($this instanceof AlterStatement) - || ($this instanceof ExplainStatement) - || ($this instanceof ShowStatement) - ) { - // TODO: Implement the statements above. - $list->getNextOfType(Token::TYPE_DELIMITER); - ++$list->idx; - } + $this->after($parser, $list, $token); } $this->last = --$list->idx; // Go back to last used token. } + + /** + * Function called before the token was processed. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return void + */ + public function before(Parser $parser, TokensList $list, Token $token) + { + + } + + /** + * Function called after the token was processed. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return + */ + public function after(Parser $parser, TokensList $list, Token $token) + { + + } + } diff --git a/src/Statements/AlterStatement.php b/src/Statements/AlterStatement.php index 47d34bb..4cc29cd 100644 --- a/src/Statements/AlterStatement.php +++ b/src/Statements/AlterStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\NotImplementedStatement; /** * `ALTER` statement. @@ -13,7 +13,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class AlterStatement extends Statement +class AlterStatement extends NotImplementedStatement { /** @@ -36,13 +36,4 @@ class AlterStatement extends Statement 'TRIGGER' => 1, ); - /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - } diff --git a/src/Statements/AnalyzeStatement.php b/src/Statements/AnalyzeStatement.php index abe872c..78783f2 100644 --- a/src/Statements/AnalyzeStatement.php +++ b/src/Statements/AnalyzeStatement.php @@ -33,15 +33,6 @@ class AnalyzeStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Analyzed tables. * * @var FieldFragment[] diff --git a/src/Statements/BackupStatement.php b/src/Statements/BackupStatement.php index bca4be7..cc2e07e 100644 --- a/src/Statements/BackupStatement.php +++ b/src/Statements/BackupStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\MaintenanceStatement; /** * `BACKUP` statement. @@ -15,7 +15,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class BackupStatement extends Statement +class BackupStatement extends MaintenanceStatement { /** @@ -32,20 +32,4 @@ class BackupStatement extends Statement 'TO' => array(4, 'var'), ); - - /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** - * Backup tables. - * - * @var FieldFragment[] - */ - public $tables; } diff --git a/src/Statements/CheckStatement.php b/src/Statements/CheckStatement.php index fa54591..4619613 100644 --- a/src/Statements/CheckStatement.php +++ b/src/Statements/CheckStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\MaintenanceStatement; /** * `CHECK` statement. @@ -15,7 +15,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class CheckStatement extends Statement +class CheckStatement extends MaintenanceStatement { /** @@ -36,15 +36,6 @@ class CheckStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Checked tables. * * @var FieldFragment[] diff --git a/src/Statements/ChecsumStatement.php b/src/Statements/ChecsumStatement.php index ff879c2..36426d2 100644 --- a/src/Statements/ChecsumStatement.php +++ b/src/Statements/ChecsumStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\MaintenanceStatement; /** * `CHECKSUM` statement. @@ -15,7 +15,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class ChecksumStatement extends Statement +class ChecksumStatement extends MaintenanceStatement { /** @@ -32,15 +32,6 @@ class ChecksumStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Checked tables. * * @var FieldFragment[] diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 69e3f7f..b5d56fe 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -2,7 +2,15 @@ namespace SqlParser\Statements; +use SqlParser\Parser; use SqlParser\Statement; +use sqlParser\Token; +use SqlParser\TokensList; +use SqlParser\Fragments\CreateDefFragment; +use SqlParser\Fragments\DataTypeFragment; +use SqlParser\Fragments\FieldDefFragment; +use SqlParser\Fragments\OptionsFragment; +use SqlParser\Fragments\ParamDefFragment; /** * `CREATE` statement. @@ -48,15 +56,6 @@ class CreateStatement extends Statement public $name; /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * The options of the entity (table, procedure, function, etc.). * * @var OptionsFragment @@ -93,4 +92,66 @@ class CreateStatement extends Statement * @var Token[] */ public $body; + + /** + * Parsing the `CREATE` statement. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return void + */ + public function before(Parser $parser, TokensList $list, Token $token) + { + ++$list->idx; + $this->name = CreateDefFragment::parse($parser, $list); + if ($this->options->has('TABLE')) { + ++$list->idx; + $this->fields = FieldDefFragment::parse($parser, $list); + ++$list->idx; + $this->entityOptions = OptionsFragment::parse( + $parser, + $list, + CreateDefFragment::$TABLE_OPTIONS + ); + } elseif (($this->options->has('PROCEDURE')) + || ($this->options->has('FUNCTION')) + ) { + ++$list->idx; + $this->parameters = ParamDefFragment::parse($parser, $list); + if ($this->options->has('FUNCTION')) { + $token = $list->getNextOfType(Token::TYPE_KEYWORD); + if ($token->value !== 'RETURNS') { + $parser->error( + '\'RETURNS\' keyword was expected.', + $token + ); + } else { + ++$list->idx; + $this->return = DataTypeFragment::parse( + $parser, + $list + ); + } + } + ++$list->idx; + $this->entityOptions = OptionsFragment::parse( + $parser, + $list, + CreateDefFragment::$FUNC_OPTIONS + ); + ++$list->idx; + $this->body = array(); + for (; $list->idx < $list->count; ++$list->idx) { + $token = $list->tokens[$list->idx]; + $this->body[] = $token; + if (($token->type === Token::TYPE_KEYWORD) + && ($token->value === 'END') + ) { + break; + } + } + } + } } diff --git a/src/Statements/DeleteStatement.php b/src/Statements/DeleteStatement.php index 0e88fed..e7f3a69 100644 --- a/src/Statements/DeleteStatement.php +++ b/src/Statements/DeleteStatement.php @@ -34,15 +34,6 @@ class DeleteStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Tables used as sources for this statement. * * @var FieldFragment[] diff --git a/src/Statements/DropStatement.php b/src/Statements/DropStatement.php index 00d1591..de96de7 100644 --- a/src/Statements/DropStatement.php +++ b/src/Statements/DropStatement.php @@ -40,15 +40,6 @@ class DropStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Dropped elements. * * @var FromKeyworrd[] diff --git a/src/Statements/ExplainStatement.php b/src/Statements/ExplainStatement.php index 1983413..8670b08 100644 --- a/src/Statements/ExplainStatement.php +++ b/src/Statements/ExplainStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\NotImplementedStatement; /** * `EXPLAIN` statement. @@ -13,7 +13,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class ExplainStatement extends Statement +class ExplainStatement extends NotImplementedStatement { } diff --git a/src/Statements/InsertStatement.php b/src/Statements/InsertStatement.php index db88b42..c80e194 100644 --- a/src/Statements/InsertStatement.php +++ b/src/Statements/InsertStatement.php @@ -59,15 +59,6 @@ class InsertStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Tables used as target for this statement. * * @var IntoKeyword diff --git a/src/Statements/MaintenanceStatement.php b/src/Statements/MaintenanceStatement.php new file mode 100644 index 0000000..b26e1c0 --- /dev/null +++ b/src/Statements/MaintenanceStatement.php @@ -0,0 +1,59 @@ +<?php + +namespace SqlParser\Statements; + +use SqlParser\Parser; +use SqlParser\Statement; +use sqlParser\Token; +use SqlParser\TokensList; +use SqlParser\Fragments\OptionsFragment; + +/** + * Maintenance statements. + * + * They follow the syntax: + * STMT [some options] tbl_name [, tbl_name] ... [some more options] + * + * @category Statements + * @package SqlParser + * @subpackage Statements + * @author Dan Ungureanu <udan1107@gmail.com> + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class MaintenanceStatement extends Statement +{ + + /** + * Tables maintained. + * + * @var FieldFragment[] + */ + public $tables; + + /** + * Parses the additional options fragment at the end. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return + */ + public function after(Parser $parser, TokensList $list, Token $token) + { + // [some options] is going to be parsed first. + // + // There is a parser specified in `Parser::$KEYWORD_PARSERS` + // which parses the name of the tables. + // + // Finally, we parse here [some more options] and that's all. + ++$list->idx; + $this->options->merge( + OptionsFragment::parse( + $parser, + $list, + static::$OPTIONS + ) + ); + } +} diff --git a/src/Statements/NotImplementedStatement.php b/src/Statements/NotImplementedStatement.php new file mode 100644 index 0000000..9f9f61c --- /dev/null +++ b/src/Statements/NotImplementedStatement.php @@ -0,0 +1,38 @@ +<?php + +namespace SqlParser\Statements; + +use SqlParser\Parser; +use SqlParser\Statement; +use sqlParser\Token; +use SqlParser\TokensList; + +/** + * Not implemented (yet) statements. + * + * The `before` function makes the parser jump straight to the first delimiter. + * + * @category Statements + * @package SqlParser + * @subpackage Statements + * @author Dan Ungureanu <udan1107@gmail.com> + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class NotImplementedStatement extends Statement +{ + + /** + * Jump to the end of the delimiter. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return + */ + public function before(Parser $parser, TokensList $list, Token $token) + { + $list->getNextOfType(Token::TYPE_DELIMITER); + --$list->idx; + } +} diff --git a/src/Statements/OptimizeStatement.php b/src/Statements/OptimizeStatement.php index cb90f08..0b7b38a 100644 --- a/src/Statements/OptimizeStatement.php +++ b/src/Statements/OptimizeStatement.php @@ -33,15 +33,6 @@ class OptimizeStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Optimized tables. * * @var FieldFragment[] diff --git a/src/Statements/RenameStatement.php b/src/Statements/RenameStatement.php index 88607ea..0f5a1c7 100644 --- a/src/Statements/RenameStatement.php +++ b/src/Statements/RenameStatement.php @@ -2,7 +2,10 @@ namespace SqlParser\Statements; +use SqlParser\Parser; use SqlParser\Statement; +use sqlParser\Token; +use SqlParser\TokensList; /** * `RENAME` statement. @@ -25,4 +28,21 @@ class RenameStatement extends Statement * @var RenameKeyword[] */ public $renames; + + /** + * Skips the `TABLE` keyword after `RENAME`. + * + * @param Parser $parser The instance that requests parsing. + * @param TokensList $list The list of tokens to be parsed. + * @param Token $token The token that is being parsed. + * + * @return void + */ + public function before(Parser $parser, TokensList $list, Token $token) + { + if (($token->type === Token::TYPE_KEYWORD) && ($token->value === 'RENAME')) { + // Checking if it is the beginning of the query. + $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'TABLE'); + } + } } diff --git a/src/Statements/RepairStatement.php b/src/Statements/RepairStatement.php index a3f1afe..d4053ee 100644 --- a/src/Statements/RepairStatement.php +++ b/src/Statements/RepairStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\MaintenanceStatement; /** * `REPAIR` statement. @@ -17,7 +17,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class RepairStatement extends Statement +class RepairStatement extends MaintenanceStatement { /** @@ -38,15 +38,6 @@ class RepairStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Repaired tables. * * @var FieldFragment[] diff --git a/src/Statements/ReplaceStatement.php b/src/Statements/ReplaceStatement.php index 577f72c..0411f45 100644 --- a/src/Statements/ReplaceStatement.php +++ b/src/Statements/ReplaceStatement.php @@ -37,15 +37,6 @@ class ReplaceStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Tables used as target for this statement. * * @var IntoKeyword diff --git a/src/Statements/RestoreStatement.php b/src/Statements/RestoreStatement.php index e8c46f8..89e2ed5 100644 --- a/src/Statements/RestoreStatement.php +++ b/src/Statements/RestoreStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\MaintenanceStatement; /** * `RESTORE` statement. @@ -15,7 +15,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class RestoreStatement extends Statement +class RestoreStatement extends MaintenanceStatement { /** @@ -31,15 +31,6 @@ class RestoreStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Restored tables. * * @var FieldFragment[] diff --git a/src/Statements/SelectStatement.php b/src/Statements/SelectStatement.php index d8c9baa..4b9584f 100644 --- a/src/Statements/SelectStatement.php +++ b/src/Statements/SelectStatement.php @@ -62,15 +62,6 @@ class SelectStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Expressions that are being selected by this statement. * * @var FieldFragment[] diff --git a/src/Statements/ShowStatement.php b/src/Statements/ShowStatement.php index 962a7a3..57a4110 100644 --- a/src/Statements/ShowStatement.php +++ b/src/Statements/ShowStatement.php @@ -2,7 +2,7 @@ namespace SqlParser\Statements; -use SqlParser\Statement; +use SqlParser\Statements\NotImplementedStatement; /** * `SHOW` statement. @@ -13,7 +13,7 @@ use SqlParser\Statement; * @author Dan Ungureanu <udan1107@gmail.com> * @license http://opensource.org/licenses/GPL-2.0 GNU Public License */ -class ShowStatement extends Statement +class ShowStatement extends NotImplementedStatement { /** @@ -64,13 +64,4 @@ class ShowStatement extends Statement 'VIEW' => 2, 'WARNINGS' => 2, ); - - /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; } diff --git a/src/Statements/UpdateStatement.php b/src/Statements/UpdateStatement.php index 8dcc6f0..39a0f1c 100644 --- a/src/Statements/UpdateStatement.php +++ b/src/Statements/UpdateStatement.php @@ -39,15 +39,6 @@ class UpdateStatement extends Statement ); /** - * The options of this query. - * - * @var OptionsFragment - * - * @see static::$OPTIONS - */ - public $options; - - /** * Tables used as sources for this statement. * * @var FieldFragment[] |