diff options
author | Damian Dlugosz <bigfootdd@gmail.com> | 2017-01-03 22:06:01 +0100 |
---|---|---|
committer | Damian Dlugosz <bigfootdd@gmail.com> | 2017-01-06 00:11:32 +0100 |
commit | a6e45c5e2e006bbd90306e1e947e25e601965657 (patch) | |
tree | 07fbbdbe95d08721e97b6683226ad3a26a6e9886 /src | |
parent | 082a1fad6c64f455ac42698cb9c7a08f0347a2c0 (diff) | |
download | sql-parser-a6e45c5e2e006bbd90306e1e947e25e601965657.zip sql-parser-a6e45c5e2e006bbd90306e1e947e25e601965657.tar.gz sql-parser-a6e45c5e2e006bbd90306e1e947e25e601965657.tar.bz2 |
Apply php-cs-fixer
Diffstat (limited to 'src')
66 files changed, 1578 insertions, 1839 deletions
diff --git a/src/Component.php b/src/Component.php index 08720cf..107756c 100644 --- a/src/Component.php +++ b/src/Component.php @@ -7,9 +7,8 @@ * There is a small difference between *Component and *Keyword classes: usually, * *Component parsers can be reused in multiple situations and *Keyword parsers * count on the *Component classes to do their job. - * - * @package SqlParser */ + namespace SqlParser; require_once 'common.php'; @@ -19,21 +18,20 @@ require_once 'common.php'; * multiple query types. * * @category Components - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ abstract class Component { - /** * Parses the tokens contained in the given list in the context of the given * parser. * - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * - * @throws \Exception Not implemented yet. + * @throws \Exception not implemented yet * * @return mixed */ @@ -53,10 +51,10 @@ abstract class Component * In other words, this function represents the inverse function of * `static::parse`. * - * @param mixed $component The component to be built. - * @param array $options Parameters for building. + * @param mixed $component the component to be built + * @param array $options parameters for building * - * @throws \Exception Not implemented yet. + * @throws \Exception not implemented yet * * @return string */ diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index c2344c6..43bd14a 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -2,10 +2,8 @@ /** * Parses an alter operation. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,86 +15,84 @@ use SqlParser\TokensList; * Parses an alter operation. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class AlterOperation extends Component { - /** - * All database options + * All database options. * * @var array */ public static $DB_OPTIONS = array( - 'CHARACTER SET' => array(1, 'var'), - 'CHARSET' => array(1, 'var'), - 'DEFAULT CHARACTER SET' => array(1, 'var'), - 'DEFAULT CHARSET' => array(1, 'var'), - 'UPGRADE' => array(1, 'var'), - 'COLLATE' => array(2, 'var'), - 'DEFAULT COLLATE' => array(2, 'var'), + 'CHARACTER SET' => array(1, 'var'), + 'CHARSET' => array(1, 'var'), + 'DEFAULT CHARACTER SET' => array(1, 'var'), + 'DEFAULT CHARSET' => array(1, 'var'), + 'UPGRADE' => array(1, 'var'), + 'COLLATE' => array(2, 'var'), + 'DEFAULT COLLATE' => array(2, 'var'), ); /** - * All table options + * All table options. * * @var array */ public static $TABLE_OPTIONS = array( - 'ENGINE' => array(1, 'var='), - 'AUTO_INCREMENT' => array(1, 'var='), - 'AVG_ROW_LENGTH' => array(1, 'var'), - 'MAX_ROWS' => array(1, 'var'), - 'ROW_FORMAT' => array(1, 'var'), - 'COMMENT' => array(1, 'var'), - 'ADD' => 1, - 'ALTER' => 1, - 'ANALYZE' => 1, - 'CHANGE' => 1, - 'CHECK' => 1, - 'COALESCE' => 1, - 'CONVERT' => 1, - 'DISABLE' => 1, - 'DISCARD' => 1, - 'DROP' => 1, - 'ENABLE' => 1, - 'IMPORT' => 1, - 'MODIFY' => 1, - 'OPTIMIZE' => 1, - 'ORDER' => 1, - 'PARTITION' => 1, - 'REBUILD' => 1, - 'REMOVE' => 1, - 'RENAME' => 1, - 'REORGANIZE' => 1, - 'REPAIR' => 1, - 'UPGRADE' => 1, + 'ENGINE' => array(1, 'var='), + 'AUTO_INCREMENT' => array(1, 'var='), + 'AVG_ROW_LENGTH' => array(1, 'var'), + 'MAX_ROWS' => array(1, 'var'), + 'ROW_FORMAT' => array(1, 'var'), + 'COMMENT' => array(1, 'var'), + 'ADD' => 1, + 'ALTER' => 1, + 'ANALYZE' => 1, + 'CHANGE' => 1, + 'CHECK' => 1, + 'COALESCE' => 1, + 'CONVERT' => 1, + 'DISABLE' => 1, + 'DISCARD' => 1, + 'DROP' => 1, + 'ENABLE' => 1, + 'IMPORT' => 1, + 'MODIFY' => 1, + 'OPTIMIZE' => 1, + 'ORDER' => 1, + 'PARTITION' => 1, + 'REBUILD' => 1, + 'REMOVE' => 1, + 'RENAME' => 1, + 'REORGANIZE' => 1, + 'REPAIR' => 1, + 'UPGRADE' => 1, - 'COLUMN' => 2, - 'CONSTRAINT' => 2, - 'DEFAULT' => 2, - 'TO' => 2, - 'BY' => 2, - 'FOREIGN' => 2, - 'FULLTEXT' => 2, - 'KEY' => 2, - 'KEYS' => 2, - 'PARTITIONING' => 2, - 'PRIMARY KEY' => 2, - 'SPATIAL' => 2, - 'TABLESPACE' => 2, - 'INDEX' => 2, + 'COLUMN' => 2, + 'CONSTRAINT' => 2, + 'DEFAULT' => 2, + 'TO' => 2, + 'BY' => 2, + 'FOREIGN' => 2, + 'FULLTEXT' => 2, + 'KEY' => 2, + 'KEYS' => 2, + 'PARTITIONING' => 2, + 'PRIMARY KEY' => 2, + 'SPATIAL' => 2, + 'TABLESPACE' => 2, + 'INDEX' => 2, ); /** - * All view options + * All view options. * * @var array */ public static $VIEW_OPTIONS = array( - 'AS' => 1, + 'AS' => 1, ); /** @@ -121,20 +117,20 @@ class AlterOperation extends Component public $unknown = array(); /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return AlterOperation */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new AlterOperation(); + $ret = new self(); /** * Counts brackets. * - * @var int $brackets + * @var int */ $brackets = 0; @@ -149,7 +145,7 @@ class AlterOperation extends Component * * 2 -------------------------[ , ]-----------------------> 0 * - * @var int $state + * @var int */ $state = 0; @@ -157,7 +153,7 @@ class AlterOperation extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -223,7 +219,7 @@ class AlterOperation extends Component // We have reached the end of ALTER operation and suddenly found // a start to new statement, but have not find a delimiter between them - if (! ($token->value == 'SET' && $list->tokens[$list->idx - 1]->value == 'CHARACTER')) { + if (!($token->value == 'SET' && $list->tokens[$list->idx - 1]->value == 'CHARACTER')) { $parser->error( __('A new statement was found, but no delimiter between it and the previous one.'), $token @@ -248,8 +244,8 @@ class AlterOperation extends Component } /** - * @param AlterOperation $component The component to be built. - * @param array $options Parameters for building. + * @param AlterOperation $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -260,6 +256,7 @@ class AlterOperation extends Component $ret .= $component->field . ' '; } $ret .= TokensList::build($component->unknown); + return $ret; } } diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index 2199efe..e8da84b 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -2,10 +2,8 @@ /** * `VALUES` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,17 +15,15 @@ use SqlParser\TokensList; * `VALUES` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Array2d extends Component { - /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return ArrayObj[] */ @@ -38,7 +34,7 @@ class Array2d extends Component /** * The number of values in each set. * - * @var int $count + * @var int */ $count = -1; @@ -52,7 +48,7 @@ class Array2d extends Component * 1 ------------------------[ , ]------------------------> 0 * 1 -----------------------[ else ]----------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -60,7 +56,7 @@ class Array2d extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -117,12 +113,13 @@ class Array2d extends Component } --$list->idx; + return $ret; } /** - * @param ArrayObj[] $component The component to be built. - * @param array $options Parameters for building. + * @param ArrayObj[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/ArrayObj.php b/src/Components/ArrayObj.php index bed7fc7..8a046a8 100644 --- a/src/Components/ArrayObj.php +++ b/src/Components/ArrayObj.php @@ -2,10 +2,8 @@ /** * Parses an array. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * Parses an array. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ArrayObj extends Component { - /** * The array that contains the unprocessed value of each token. * @@ -41,8 +37,8 @@ class ArrayObj extends Component /** * Constructor. * - * @param array $raw The unprocessed values. - * @param array $values The processed values. + * @param array $raw the unprocessed values + * @param array $values the processed values */ public function __construct(array $raw = array(), array $values = array()) { @@ -51,41 +47,41 @@ class ArrayObj extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return ArrayObj|Component[] */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = empty($options['type']) ? new ArrayObj() : array(); + $ret = empty($options['type']) ? new self() : array(); /** * The last raw expression. * - * @var string $lastRaw + * @var string */ $lastRaw = ''; /** * The last value. * - * @var string $lastValue + * @var string */ $lastValue = ''; /** * Counts brackets. * - * @var int $brackets + * @var int */ $brackets = 0; /** * Last separator (bracket or comma). * - * @var boolean $isCommaLast + * @var bool */ $isCommaLast = false; @@ -93,7 +89,7 @@ class ArrayObj extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -175,8 +171,8 @@ class ArrayObj extends Component } /** - * @param ArrayObj|ArrayObj[] $component The component to be built. - * @param array $options Parameters for building. + * @param ArrayObj|ArrayObj[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/CaseExpression.php b/src/Components/CaseExpression.php index df5a1bd..0971ed8 100644 --- a/src/Components/CaseExpression.php +++ b/src/Components/CaseExpression.php @@ -1,11 +1,9 @@ <?php /** - * Parses a reference to a CASE expression - * - * @package SqlParser - * @subpackage Components + * Parses a reference to a CASE expression. */ + namespace SqlParser\Components; use SqlParser\Component; @@ -13,48 +11,45 @@ use SqlParser\Parser; use SqlParser\Token; use SqlParser\TokensList; - /** - * Parses a reference to a CASE expression + * Parses a reference to a CASE expression. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CaseExpression extends Component { - /** - * The value to be compared + * The value to be compared. * * @var Expression */ public $value; /** - * The conditions in WHEN clauses + * The conditions in WHEN clauses. * * @var array */ public $conditions; /** - * The results matching with the WHEN clauses + * The results matching with the WHEN clauses. * * @var array */ public $results; /** - * The values to be compared against + * The values to be compared against. * * @var array */ public $compare_values; /** - * The result in ELSE section of expr + * The result in ELSE section of expr. * * @var array */ @@ -62,45 +57,42 @@ class CaseExpression extends Component /** * Constructor. - * */ public function __construct() { } /** - * - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed * * @return Expression */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new CaseExpression(); + $ret = new self(); /** - * State of parser + * State of parser. * - * @var int $parser + * @var int */ $state = 0; /** - * Syntax type (type 0 or type 1) + * Syntax type (type 0 or type 1). * - * @var int $type + * @var int */ $type = 0; ++$list->idx; // Skip 'CASE' for (; $list->idx < $list->count; ++$list->idx) { - /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -205,12 +197,13 @@ class CaseExpression extends Component } --$list->idx; + return $ret; } /** - * @param Expression $component The component to be built. - * @param array $options Parameters for building. + * @param Expression $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/Condition.php b/src/Components/Condition.php index c69441a..1971c09 100644 --- a/src/Components/Condition.php +++ b/src/Components/Condition.php @@ -2,10 +2,8 @@ /** * `WHERE` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * `WHERE` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Condition extends Component { - /** * Logical operators that can be used to delimit expressions. * @@ -37,24 +33,24 @@ class Condition extends Component * @var array */ public static $ALLOWED_KEYWORDS = array( - 'ALL' => 1, - 'AND' => 1, - 'BETWEEN' => 1, - 'EXISTS' => 1, - 'IF' => 1, - 'IN' => 1, - 'INTERVAL' => 1, - 'IS' => 1, - 'LIKE' => 1, - 'MATCH' => 1, - 'NOT IN' => 1, - 'NOT NULL' => 1, - 'NOT' => 1, - 'NULL' => 1, - 'OR' => 1, - 'REGEXP' => 1, - 'RLIKE' => 1, - 'XOR' => 1, + 'ALL' => 1, + 'AND' => 1, + 'BETWEEN' => 1, + 'EXISTS' => 1, + 'IF' => 1, + 'IN' => 1, + 'INTERVAL' => 1, + 'IS' => 1, + 'LIKE' => 1, + 'MATCH' => 1, + 'NOT IN' => 1, + 'NOT NULL' => 1, + 'NOT' => 1, + 'NULL' => 1, + 'OR' => 1, + 'REGEXP' => 1, + 'RLIKE' => 1, + 'XOR' => 1, ); /** @@ -81,7 +77,7 @@ class Condition extends Component /** * Constructor. * - * @param string $expr The condition or the operator. + * @param string $expr the condition or the operator */ public function __construct($expr = null) { @@ -89,9 +85,9 @@ class Condition extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Condition[] */ @@ -99,12 +95,12 @@ class Condition extends Component { $ret = array(); - $expr = new Condition(); + $expr = new self(); /** * Counts brackets. * - * @var int $brackets + * @var int */ $brackets = 0; @@ -115,16 +111,15 @@ class Condition extends Component * the keyword `AND`, which is also an operator that delimits * expressions. * - * @var bool $betweenBefore + * @var bool */ $betweenBefore = false; for (; $list->idx < $list->count; ++$list->idx) { - /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -158,12 +153,12 @@ class Condition extends Component } // Adding the operator. - $expr = new Condition($token->value); + $expr = new self($token->value); $expr->isOperator = true; $ret[] = $expr; // Preparing to parse another condition. - $expr = new Condition(); + $expr = new self(); continue; } } @@ -211,12 +206,13 @@ class Condition extends Component } --$list->idx; + return $ret; } /** - * @param Condition[] $component The component to be built. - * @param array $options Parameters for building. + * @param Condition[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/CreateDefinition.php b/src/Components/CreateDefinition.php index 86bb315..567602a 100644 --- a/src/Components/CreateDefinition.php +++ b/src/Components/CreateDefinition.php @@ -4,10 +4,8 @@ * Parses the create definition of a column or a key. * * Used for parsing `CREATE TABLE` statement. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Context; @@ -22,42 +20,39 @@ use SqlParser\TokensList; * Used for parsing `CREATE TABLE` statement. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CreateDefinition extends Component { - /** * All field options. * * @var array */ public static $FIELD_OPTIONS = array( - // Tells the `OptionsArray` to not sort the options. // See the note below. - '_UNSORTED' => true, - - 'NOT NULL' => 1, - 'NULL' => 1, - 'DEFAULT' => array(2, 'expr', array('breakOnAlias' => true)), - 'AUTO_INCREMENT' => 3, - 'PRIMARY' => 4, - 'PRIMARY KEY' => 4, - 'UNIQUE' => 4, - 'UNIQUE KEY' => 4, - 'COMMENT' => array(5, 'var'), - 'COLUMN_FORMAT' => array(6, 'var'), - 'ON UPDATE' => array(7, 'expr'), + '_UNSORTED' => true, + + 'NOT NULL' => 1, + 'NULL' => 1, + 'DEFAULT' => array(2, 'expr', array('breakOnAlias' => true)), + 'AUTO_INCREMENT' => 3, + 'PRIMARY' => 4, + 'PRIMARY KEY' => 4, + 'UNIQUE' => 4, + 'UNIQUE KEY' => 4, + 'COMMENT' => array(5, 'var'), + 'COLUMN_FORMAT' => array(6, 'var'), + 'ON UPDATE' => array(7, 'expr'), // Generated columns options. - 'GENERATED ALWAYS' => 8, - 'AS' => array(9, 'expr', array('parenthesesDelimited' => true)), - 'VIRTUAL' => 10, - 'PERSISTENT' => 11, - 'STORED' => 11, + 'GENERATED ALWAYS' => 8, + 'AS' => array(9, 'expr', array('parenthesesDelimited' => true)), + 'VIRTUAL' => 10, + 'PERSISTENT' => 11, + 'STORED' => 11, // Common entries. // // NOTE: Some of the common options are not in the same order which @@ -119,11 +114,11 @@ class CreateDefinition extends Component /** * Constructor. * - * @param string $name The name of the field. - * @param OptionsArray $options The options of this field. - * @param DataType|Key $type The data type of this field or the key. - * @param bool $isConstraint Whether this field is a constraint or not. - * @param Reference $references References. + * @param string $name the name of the field + * @param OptionsArray $options the options of this field + * @param DataType|Key $type the data type of this field or the key + * @param bool $isConstraint whether this field is a constraint or not + * @param Reference $references references */ public function __construct( $name = null, @@ -144,9 +139,9 @@ class CreateDefinition extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return CreateDefinition[] */ @@ -154,7 +149,7 @@ class CreateDefinition extends Component { $ret = array(); - $expr = new CreateDefinition(); + $expr = new self(); /** * The state of the parser. @@ -176,7 +171,7 @@ class CreateDefinition extends Component * 5 ------------------------[ , ]-----------------------> 1 * 5 ------------------------[ ) ]-----------------------> 6 (-1) * - * @var int $state + * @var int */ $state = 0; @@ -184,7 +179,7 @@ class CreateDefinition extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -219,7 +214,7 @@ class CreateDefinition extends Component if (!$expr->isConstraint) { $state = 2; } - } else if ($token->type === Token::TYPE_KEYWORD) { + } elseif ($token->type === Token::TYPE_KEYWORD) { if ($token->flags & Token::FLAG_KEYWORD_RESERVED) { // Reserved keywords can't be used // as field names without backquotes @@ -230,6 +225,7 @@ class CreateDefinition extends Component ), $token ); + return $ret; } else { // Non-reserved keywords are allowed without backquotes @@ -241,6 +237,7 @@ class CreateDefinition extends Component __('A symbol name was expected!'), $token ); + return $ret; } } elseif ($state === 2) { @@ -261,7 +258,7 @@ class CreateDefinition extends Component if ((!empty($expr->type)) || (!empty($expr->key))) { $ret[] = $expr; } - $expr = new CreateDefinition(); + $expr = new self(); if ($token->value === ',') { $state = 1; } elseif ($token->value === ')') { @@ -292,12 +289,13 @@ class CreateDefinition extends Component } --$list->idx; + return $ret; } /** - * @param CreateDefinition|CreateDefinition[] $component The component to be built. - * @param array $options Parameters for building. + * @param CreateDefinition|CreateDefinition[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/DataType.php b/src/Components/DataType.php index 378ec9d..04ec79d 100644 --- a/src/Components/DataType.php +++ b/src/Components/DataType.php @@ -2,10 +2,8 @@ /** * Parses a data type. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,25 +15,23 @@ use SqlParser\TokensList; * Parses a data type. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class DataType extends Component { - /** * All data type options. * * @var array */ public static $DATA_TYPE_OPTIONS = array( - 'BINARY' => 1, - 'CHARACTER SET' => array(2, 'var'), - 'CHARSET' => array(2, 'var'), - 'COLLATE' => array(3, 'var'), - 'UNSIGNED' => 4, - 'ZEROFILL' => 5, + 'BINARY' => 1, + 'CHARACTER SET' => array(2, 'var'), + 'CHARSET' => array(2, 'var'), + 'COLLATE' => array(3, 'var'), + 'UNSIGNED' => 4, + 'ZEROFILL' => 5, ); /** @@ -70,9 +66,9 @@ class DataType extends Component /** * Constructor. * - * @param string $name The name of this data type. - * @param array $parameters The parameters (size or possible values). - * @param OptionsArray $options The options of this data type. + * @param string $name the name of this data type + * @param array $parameters the parameters (size or possible values) + * @param OptionsArray $options the options of this data type */ public function __construct( $name = null, @@ -85,15 +81,15 @@ class DataType extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return DataType */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new DataType(); + $ret = new self(); /** * The state of the parser. @@ -104,7 +100,7 @@ class DataType extends Component * * 1 ----------------[ size and options ]----------------> 2 * - * @var int $state + * @var int */ $state = 0; @@ -112,7 +108,7 @@ class DataType extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -138,7 +134,6 @@ class DataType extends Component ++$list->idx; break; } - } if (empty($ret->name)) { @@ -146,12 +141,13 @@ class DataType extends Component } --$list->idx; + return $ret; } /** - * @param DataType $component The component to be built. - * @param array $options Parameters for building. + * @param DataType $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/Expression.php b/src/Components/Expression.php index 7a7a333..b0c45df 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -3,10 +3,8 @@ /** * Parses a reference to an expression (column, table or database name, function * call, mathematical expression, etc.). - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Context; @@ -20,20 +18,18 @@ use SqlParser\TokensList; * call, mathematical expression, etc.). * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Expression extends Component { - /** * List of allowed reserved keywords in expressions. * * @var array */ private static $ALLOWED_KEYWORDS = array( - 'AS' => 1, 'DUAL' => 1, 'NULL' => 1, 'REGEXP' => 1, 'CASE' => 1 + 'AS' => 1, 'DUAL' => 1, 'NULL' => 1, 'REGEXP' => 1, 'CASE' => 1, ); /** @@ -98,11 +94,11 @@ class Expression extends Component * string. * * @param string $database The name of the database or the the expression. - * the the expression. + * the the expression. * @param string $table The name of the table or the alias of the expression. - * the alias of the expression. - * @param string $column The name of the column. - * @param string $alias The name of the alias. + * the alias of the expression. + * @param string $column the name of the column + * @param string $alias the name of the alias */ public function __construct($database = null, $table = null, $column = null, $alias = null) { @@ -118,7 +114,7 @@ class Expression extends Component } /** - * Possible options: + * Possible options:. * * `field` * @@ -143,48 +139,48 @@ class Expression extends Component * * If not empty, breaks after last parentheses occurred. * - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Expression */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new Expression(); + $ret = new self(); /** * Whether current tokens make an expression or a table reference. * - * @var bool $isExpr + * @var bool */ $isExpr = false; /** * Whether a period was previously found. * - * @var bool $dot + * @var bool */ $dot = false; /** * Whether an alias is expected. Is 2 if `AS` keyword was found. * - * @var bool $alias + * @var bool */ $alias = false; /** * Counts brackets. * - * @var int $brackets + * @var int */ $brackets = 0; /** * Keeps track of the last two previous tokens. * - * @var Token[] $prev + * @var Token[] */ $prev = array(null, null); @@ -195,11 +191,10 @@ class Expression extends Component } for (; $list->idx < $list->count; ++$list->idx) { - /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -227,7 +222,7 @@ class Expression extends Component $ret->subquery = $token->value; } elseif (($token->flags & Token::FLAG_KEYWORD_FUNCTION) && (empty($options['parseField']) - && ! $alias) + && !$alias) ) { $isExpr = true; } elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED) @@ -261,7 +256,7 @@ class Expression extends Component continue; } $isExpr = true; - } elseif ($brackets === 0 && strlen($ret->expr) > 0 && ! $alias) { + } elseif ($brackets === 0 && strlen($ret->expr) > 0 && !$alias) { /* End of expression */ break; } @@ -408,12 +403,13 @@ class Expression extends Component } --$list->idx; + return $ret; } /** - * @param Expression|Expression[] $component The component to be built. - * @param array $options Parameters for building. + * @param Expression|Expression[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/ExpressionArray.php b/src/Components/ExpressionArray.php index e2aa86e..1901048 100644 --- a/src/Components/ExpressionArray.php +++ b/src/Components/ExpressionArray.php @@ -2,10 +2,8 @@ /** * Parses a list of expressions delimited by a comma. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,17 +15,15 @@ use SqlParser\TokensList; * Parses a list of expressions delimited by a comma. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ExpressionArray extends Component { - /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Expression[] */ @@ -45,7 +41,7 @@ class ExpressionArray extends Component * 1 ------------------------[ , ]------------------------> 0 * 1 -----------------------[ else ]----------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -53,7 +49,7 @@ class ExpressionArray extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -109,12 +105,13 @@ class ExpressionArray extends Component } --$list->idx; + return $ret; } /** - * @param Expression[] $component The component to be built. - * @param array $options Parameters for building. + * @param Expression[] $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -124,6 +121,7 @@ class ExpressionArray extends Component foreach ($component as $frag) { $ret[] = $frag::build($frag); } + return implode($ret, ', '); } } diff --git a/src/Components/FunctionCall.php b/src/Components/FunctionCall.php index e260532..3d9b33b 100644 --- a/src/Components/FunctionCall.php +++ b/src/Components/FunctionCall.php @@ -2,10 +2,8 @@ /** * Parses a function call. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * Parses a function call. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class FunctionCall extends Component { - /** * The name of this function. * @@ -32,7 +28,7 @@ class FunctionCall extends Component public $name; /** - * The list of parameters + * The list of parameters. * * @var ArrayObj */ @@ -41,8 +37,8 @@ class FunctionCall extends Component /** * Constructor. * - * @param string $name The name of the function to be called. - * @param array|ArrayObj $parameters The parameters of this function. + * @param string $name the name of the function to be called + * @param array|ArrayObj $parameters the parameters of this function */ public function __construct($name = null, $parameters = null) { @@ -55,15 +51,15 @@ class FunctionCall extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return FunctionCall */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new FunctionCall(); + $ret = new self(); /** * The state of the parser. @@ -74,7 +70,7 @@ class FunctionCall extends Component * * 1 --------------------[ parameters ]-------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -82,7 +78,7 @@ class FunctionCall extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -105,15 +101,14 @@ class FunctionCall extends Component } break; } - } return $ret; } /** - * @param FunctionCall $component The component to be built. - * @param array $options Parameters for building. + * @param FunctionCall $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/IntoKeyword.php b/src/Components/IntoKeyword.php index 1a182ff..c7d0d40 100644 --- a/src/Components/IntoKeyword.php +++ b/src/Components/IntoKeyword.php @@ -2,10 +2,8 @@ /** * `INTO` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,24 +15,21 @@ use SqlParser\TokensList; * `INTO` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class IntoKeyword extends Component { - /** * FIELDS/COLUMNS Options for `SELECT...INTO` statements. * * @var array */ public static $FIELDS_OPTIONS = array( - - 'TERMINATED BY' => array(1, 'expr'), - 'OPTIONALLY' => 2, - 'ENCLOSED BY' => array(3, 'expr'), - 'ESCAPED BY' => array(4, 'expr'), + 'TERMINATED BY' => array(1, 'expr'), + 'OPTIONALLY' => 2, + 'ENCLOSED BY' => array(3, 'expr'), + 'ESCAPED BY' => array(4, 'expr'), ); /** @@ -43,9 +38,8 @@ class IntoKeyword extends Component * @var array */ public static $LINES_OPTIONS = array( - - 'STARTING BY' => array(1, 'expr'), - 'TERMINATED BY' => array(2, 'expr'), + 'STARTING BY' => array(1, 'expr'), + 'TERMINATED BY' => array(2, 'expr'), ); /** @@ -70,45 +64,47 @@ class IntoKeyword extends Component public $columns; /** - * The values to be selected into (SELECT .. INTO @var1) + * The values to be selected into (SELECT .. INTO @var1). * * @var ExpressionArray */ public $values; /** - * Options for FIELDS/COLUMNS keyword + * Options for FIELDS/COLUMNS keyword. * * @var OptionsArray + * * @see static::$FIELDS_OPTIONS */ public $fields_options; /** - * Whether to use `FIELDS` or `COLUMNS` while building + * Whether to use `FIELDS` or `COLUMNS` while building. * - * @var boolean + * @var bool */ public $fields_keyword; /** - * Options for OPTIONS keyword + * Options for OPTIONS keyword. * * @var OptionsArray + * * @see static::$LINES_OPTIONS */ public $lines_options; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return IntoKeyword */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new IntoKeyword(); + $ret = new self(); /** * The state of the parser. @@ -122,7 +118,7 @@ class IntoKeyword extends Component * * 2 ---------------------[ filename ]--------------------> 1 * - * @var int $state + * @var int */ $state = 0; @@ -130,7 +126,7 @@ class IntoKeyword extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -199,10 +195,12 @@ class IntoKeyword extends Component } --$list->idx; + return $ret; } - public function parseFileOptions(Parser $parser, TokensList $list, $keyword='FIELDS') { + public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'FIELDS') + { ++$list->idx; if ($keyword === 'FIELDS' || $keyword === 'COLUMNS') { @@ -229,8 +227,8 @@ class IntoKeyword extends Component } /** - * @param IntoKeyword $component The component to be built. - * @param array $options Parameters for building. + * @param IntoKeyword $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -239,6 +237,7 @@ class IntoKeyword extends Component if ($component->dest instanceof Expression) { $columns = !empty($component->columns) ? '(`' . implode('`, `', $component->columns) . '`)' : ''; + return $component->dest . $columns; } elseif (isset($component->values)) { return ExpressionArray::build($component->values); @@ -259,4 +258,4 @@ class IntoKeyword extends Component return $ret; } } -}
\ No newline at end of file +} diff --git a/src/Components/JoinKeyword.php b/src/Components/JoinKeyword.php index 60711ce..8954482 100644 --- a/src/Components/JoinKeyword.php +++ b/src/Components/JoinKeyword.php @@ -2,10 +2,8 @@ /** * `JOIN` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,40 +15,39 @@ use SqlParser\TokensList; * `JOIN` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class JoinKeyword extends Component { - /** * Types of join. * * @var array */ public static $JOINS = array( - 'CROSS JOIN' => 'CROSS', - 'FULL JOIN' => 'FULL', - 'FULL OUTER JOIN' => 'FULL', - 'INNER JOIN' => 'INNER', - 'JOIN' => 'JOIN', - 'LEFT JOIN' => 'LEFT', - 'LEFT OUTER JOIN' => 'LEFT', - 'RIGHT JOIN' => 'RIGHT', - 'RIGHT OUTER JOIN' => 'RIGHT', - 'NATURAL JOIN' => 'NATURAL', - 'NATURAL LEFT JOIN' => 'NATURAL LEFT', - 'NATURAL RIGHT JOIN' => 'NATURAL RIGHT', - 'NATURAL LEFT OUTER JOIN' => 'NATURAL LEFT OUTER', - 'NATURAL RIGHT OUTER JOIN' => 'NATURAL RIGHT OUTER', - 'STRAIGHT_JOIN' => 'STRAIGHT', + 'CROSS JOIN' => 'CROSS', + 'FULL JOIN' => 'FULL', + 'FULL OUTER JOIN' => 'FULL', + 'INNER JOIN' => 'INNER', + 'JOIN' => 'JOIN', + 'LEFT JOIN' => 'LEFT', + 'LEFT OUTER JOIN' => 'LEFT', + 'RIGHT JOIN' => 'RIGHT', + 'RIGHT OUTER JOIN' => 'RIGHT', + 'NATURAL JOIN' => 'NATURAL', + 'NATURAL LEFT JOIN' => 'NATURAL LEFT', + 'NATURAL RIGHT JOIN' => 'NATURAL RIGHT', + 'NATURAL LEFT OUTER JOIN' => 'NATURAL LEFT OUTER', + 'NATURAL RIGHT OUTER JOIN' => 'NATURAL RIGHT OUTER', + 'STRAIGHT_JOIN' => 'STRAIGHT', ); /** * Type of this join. * * @see static::$JOINS + * * @var string */ public $type; @@ -70,16 +67,16 @@ class JoinKeyword extends Component public $on; /** - * Columns in Using clause + * Columns in Using clause. * * @var ArrayObj */ public $using; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return JoinKeyword[] */ @@ -87,7 +84,7 @@ class JoinKeyword extends Component { $ret = array(); - $expr = new JoinKeyword(); + $expr = new self(); /** * The state of the parser. @@ -105,7 +102,7 @@ class JoinKeyword extends Component * * 4 ----------------------[ columns ]--------------------> 0 * - * @var int $state + * @var int */ $state = 0; @@ -120,7 +117,7 @@ class JoinKeyword extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -157,7 +154,7 @@ class JoinKeyword extends Component && (!empty(static::$JOINS[$token->value])) ) { $ret[] = $expr; - $expr = new JoinKeyword(); + $expr = new self(); $expr->type = static::$JOINS[$token->value]; $state = 1; } else { @@ -169,15 +166,14 @@ class JoinKeyword extends Component } elseif ($state === 3) { $expr->on = Condition::parse($parser, $list); $ret[] = $expr; - $expr = new JoinKeyword(); + $expr = new self(); $state = 0; } elseif ($state === 4) { $expr->using = ArrayObj::parse($parser, $list); $ret[] = $expr; - $expr = new JoinKeyword(); + $expr = new self(); $state = 0; } - } if (!empty($expr->type)) { @@ -185,12 +181,13 @@ class JoinKeyword extends Component } --$list->idx; + return $ret; } /** - * @param JoinKeyword[] $component The component to be built. - * @param array $options Parameters for building. + * @param JoinKeyword[] $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -204,6 +201,7 @@ class JoinKeyword extends Component . (!empty($c->using) ? ' USING ' . ArrayObj::build($c->using) : ''); } + return implode(' ', $ret); } } diff --git a/src/Components/Key.php b/src/Components/Key.php index 24cc363..a33122a 100644 --- a/src/Components/Key.php +++ b/src/Components/Key.php @@ -2,10 +2,8 @@ /** * Parses the definition of a key. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Context; @@ -20,23 +18,21 @@ use SqlParser\TokensList; * Used for parsing `CREATE TABLE` statement. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Key extends Component { - /** * All key options. * * @var array */ public static $KEY_OPTIONS = array( - 'KEY_BLOCK_SIZE' => array(1, 'var'), - 'USING' => array(2, 'var'), - 'WITH PARSER' => array(3, 'var'), - 'COMMENT' => array(4, 'var='), + 'KEY_BLOCK_SIZE' => array(1, 'var'), + 'USING' => array(2, 'var'), + 'WITH PARSER' => array(3, 'var'), + 'COMMENT' => array(4, 'var='), ); /** @@ -70,10 +66,10 @@ class Key extends Component /** * Constructor. * - * @param string $name The name of the key. - * @param array $columns The columns covered by this key. - * @param string $type The type of this key. - * @param OptionsArray $options The options of this key. + * @param string $name the name of the key + * @param array $columns the columns covered by this key + * @param string $type the type of this key + * @param OptionsArray $options the options of this key */ public function __construct( $name = null, @@ -88,15 +84,15 @@ class Key extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Key */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new Key(); + $ret = new self(); /** * Last parsed column. @@ -117,7 +113,7 @@ class Key extends Component * * 2 ---------------------[ options ]---------------------> 3 * - * @var int $state + * @var int */ $state = 0; @@ -125,7 +121,7 @@ class Key extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -176,12 +172,13 @@ class Key extends Component } --$list->idx; + return $ret; } /** - * @param Key $component The component to be built. - * @param array $options Parameters for building. + * @param Key $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -202,6 +199,7 @@ class Key extends Component } $ret .= '(' . implode(',', $columns) . ') ' . $component->options; + return trim($ret); } } diff --git a/src/Components/Limit.php b/src/Components/Limit.php index 13a1127..d192949 100644 --- a/src/Components/Limit.php +++ b/src/Components/Limit.php @@ -2,10 +2,8 @@ /** * `LIMIT` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * `LIMIT` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Limit extends Component { - /** * The number of rows skipped. * @@ -41,8 +37,8 @@ class Limit extends Component /** * Constructor. * - * @param int $rowCount The row count. - * @param int $offset The offset. + * @param int $rowCount the row count + * @param int $offset the offset */ public function __construct($rowCount = 0, $offset = 0) { @@ -51,15 +47,15 @@ class Limit extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Limit */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new Limit(); + $ret = new self(); $offset = false; @@ -67,7 +63,7 @@ class Limit extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -115,12 +111,13 @@ class Limit extends Component } --$list->idx; + return $ret; } /** - * @param Limit $component The component to be built. - * @param array $options Parameters for building. + * @param Limit $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index 315237f..e7694b3 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -2,10 +2,8 @@ /** * Parses a list of options. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * Parses a list of options. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class OptionsArray extends Component { - /** * ArrayObj of selected options. * @@ -44,41 +40,41 @@ class OptionsArray extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return OptionsArray */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new OptionsArray(); + $ret = new self(); /** * The ID that will be assigned to duplicate options. * - * @var int $lastAssignedId + * @var int */ $lastAssignedId = count($options) + 1; /** * The option that was processed last time. * - * @var array $lastOption + * @var array */ $lastOption = null; /** * The index of the option that was processed last time. * - * @var int $lastOptionId + * @var int */ $lastOptionId = 0; /** * Counts brackets. * - * @var int $brackets + * @var int */ $brackets = 0; @@ -93,7 +89,7 @@ class OptionsArray extends Component * * 2 ----------------------[ value ]----------------------> 0 * - * @var int $state + * @var int */ $state = 0; @@ -101,7 +97,7 @@ class OptionsArray extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -182,7 +178,7 @@ class OptionsArray extends Component 'value' => '', ); $state = 1; - } elseif ($lastOption[1] === 'expr' || $lastOption[1] === 'expr=') { + } elseif ($lastOption[1] === 'expr' || $lastOption[1] === 'expr=') { // This is a keyword that is followed by an expression. // The expression is used by the specialized parser. @@ -269,12 +265,13 @@ class OptionsArray extends Component } --$list->idx; + return $ret; } /** - * @param OptionsArray $component The component to be built. - * @param array $options Parameters for building. + * @param OptionsArray $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -294,13 +291,14 @@ class OptionsArray extends Component . (!empty($option['expr']) ? $option['expr'] : $option['value']); } } + return implode(' ', $options); } /** * Checks if it has the specified option and returns it value or true. * - * @param string $key The key to be checked. + * @param string $key the key to be checked * @param bool $getExpr Gets the expression instead of the value. * The value is the processed form of the expression. * @@ -317,15 +315,16 @@ class OptionsArray extends Component return true; } } + return false; } /** * Removes the option from the array. * - * @param string $key The key to be removed. + * @param string $key the key to be removed * - * @return bool Whether the key was found and deleted or not. + * @return bool whether the key was found and deleted or not */ public function remove($key) { @@ -333,13 +332,16 @@ class OptionsArray extends Component if (is_array($option)) { if (!strcasecmp($key, $option['name'])) { unset($this->options[$idx]); + return true; } } elseif (!strcasecmp($key, $option)) { unset($this->options[$idx]); + return true; } } + return false; } @@ -347,15 +349,13 @@ class OptionsArray extends Component * Merges the specified options with these ones. Values with same ID will be * replaced. * - * @param array|OptionsArray $options The options to be merged. - * - * @return void + * @param array|OptionsArray $options the options to be merged */ public function merge($options) { if (is_array($options)) { $this->options = array_merge_recursive($this->options, $options); - } elseif ($options instanceof OptionsArray) { + } elseif ($options instanceof self) { $this->options = array_merge_recursive($this->options, $options->options); } } diff --git a/src/Components/OrderKeyword.php b/src/Components/OrderKeyword.php index ba773a8..1a7a5f2 100644 --- a/src/Components/OrderKeyword.php +++ b/src/Components/OrderKeyword.php @@ -2,10 +2,8 @@ /** * `ORDER BY` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * `ORDER BY` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class OrderKeyword extends Component { - /** * The expression that is used for ordering. * @@ -41,8 +37,8 @@ class OrderKeyword extends Component /** * Constructor. * - * @param Expression $expr The expression that we are sorting by. - * @param string $type The sorting type. + * @param Expression $expr the expression that we are sorting by + * @param string $type the sorting type */ public function __construct($expr = null, $type = 'ASC') { @@ -51,9 +47,9 @@ class OrderKeyword extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return OrderKeyword[] */ @@ -61,7 +57,7 @@ class OrderKeyword extends Component { $ret = array(); - $expr = new OrderKeyword(); + $expr = new self(); /** * The state of the parser. @@ -73,7 +69,7 @@ class OrderKeyword extends Component * 1 ------------------------[ , ]------------------------> 0 * 1 -------------------[ ASC / DESC ]--------------------> 1 * - * @var int $state + * @var int */ $state = 0; @@ -81,7 +77,7 @@ class OrderKeyword extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -109,13 +105,12 @@ class OrderKeyword extends Component if (!empty($expr->expr)) { $ret[] = $expr; } - $expr = new OrderKeyword(); + $expr = new self(); $state = 0; } else { break; } } - } // Last iteration was not processed. @@ -124,12 +119,13 @@ class OrderKeyword extends Component } --$list->idx; + return $ret; } /** - * @param OrderKeyword|OrderKeyword[] $component The component to be built. - * @param array $options Parameters for building. + * @param OrderKeyword|OrderKeyword[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/ParameterDefinition.php b/src/Components/ParameterDefinition.php index 274555e..bc74776 100644 --- a/src/Components/ParameterDefinition.php +++ b/src/Components/ParameterDefinition.php @@ -2,10 +2,8 @@ /** * The definition of a parameter of a function or procedure. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Context; @@ -18,13 +16,11 @@ use SqlParser\TokensList; * The definition of a parameter of a function or procedure. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ParameterDefinition extends Component { - /** * The name of the new column. * @@ -47,9 +43,9 @@ class ParameterDefinition extends Component public $type; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return ParameterDefinition[] */ @@ -57,7 +53,7 @@ class ParameterDefinition extends Component { $ret = array(); - $expr = new ParameterDefinition(); + $expr = new self(); /** * The state of the parser. @@ -74,7 +70,7 @@ class ParameterDefinition extends Component * 3 ------------------------[ , ]-----------------------> 1 * 3 ------------------------[ ) ]-----------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -82,7 +78,7 @@ class ParameterDefinition extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -117,7 +113,7 @@ class ParameterDefinition extends Component $state = 3; } elseif ($state === 3) { $ret[] = $expr; - $expr = new ParameterDefinition(); + $expr = new self(); if ($token->value === ',') { $state = 1; } elseif ($token->value === ')') { @@ -133,12 +129,13 @@ class ParameterDefinition extends Component } --$list->idx; + return $ret; } /** - * @param ParameterDefinition[] $component The component to be built. - * @param array $options Parameters for building. + * @param ParameterDefinition[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/PartitionDefinition.php b/src/Components/PartitionDefinition.php index 33c3692..c688f55 100644 --- a/src/Components/PartitionDefinition.php +++ b/src/Components/PartitionDefinition.php @@ -4,10 +4,8 @@ * Parses the create definition of a partition. * * Used for parsing `CREATE TABLE` statement. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -21,28 +19,26 @@ use SqlParser\TokensList; * Used for parsing `CREATE TABLE` statement. * * @category Components - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class PartitionDefinition extends Component { - /** * All field options. * * @var array */ public static $OPTIONS = array( - 'STORAGE ENGINE' => array(1, 'var'), - 'ENGINE' => array(1, 'var'), - 'COMMENT' => array(2, 'var'), - 'DATA DIRECTORY' => array(3, 'var'), - 'INDEX DIRECTORY' => array(4, 'var'), - 'MAX_ROWS' => array(5, 'var'), - 'MIN_ROWS' => array(6, 'var'), - 'TABLESPACE' => array(7, 'var'), - 'NODEGROUP' => array(8, 'var'), + 'STORAGE ENGINE' => array(1, 'var'), + 'ENGINE' => array(1, 'var'), + 'COMMENT' => array(2, 'var'), + 'DATA DIRECTORY' => array(3, 'var'), + 'INDEX DIRECTORY' => array(4, 'var'), + 'MAX_ROWS' => array(5, 'var'), + 'MIN_ROWS' => array(6, 'var'), + 'TABLESPACE' => array(7, 'var'), + 'NODEGROUP' => array(8, 'var'), ); /** @@ -88,15 +84,15 @@ class PartitionDefinition extends Component public $options; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return PartitionDefinition */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new PartitionDefinition(); + $ret = new self(); /** * The state of the parser. @@ -118,7 +114,7 @@ class PartitionDefinition extends Component * * 6 ------------------[ subpartitions ]------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -126,7 +122,7 @@ class PartitionDefinition extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -183,7 +179,7 @@ class PartitionDefinition extends Component $parser, $list, array( - 'type' => 'SqlParser\\Components\\PartitionDefinition' + 'type' => 'SqlParser\\Components\\PartitionDefinition', ) ); ++$list->idx; @@ -193,12 +189,13 @@ class PartitionDefinition extends Component } --$list->idx; + return $ret; } /** - * @param PartitionDefinition|PartitionDefinition[] $component The component to be built. - * @param array $options Parameters for building. + * @param PartitionDefinition|PartitionDefinition[] $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -211,7 +208,8 @@ class PartitionDefinition extends Component return trim('SUBPARTITION ' . $component->name . ' ' . $component->options); } else { $subpartitions = empty($component->subpartitions) - ? '' : ' ' . PartitionDefinition::build($component->subpartitions); + ? '' : ' ' . self::build($component->subpartitions); + return trim( 'PARTITION ' . $component->name . (empty($component->type) ? '' : ' VALUES ' . $component->type . ' ' . $component->expr . ' ') diff --git a/src/Components/Reference.php b/src/Components/Reference.php index 24d633c..447e3cf 100644 --- a/src/Components/Reference.php +++ b/src/Components/Reference.php @@ -2,10 +2,8 @@ /** * `REFERENCES` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Context; @@ -18,22 +16,20 @@ use SqlParser\TokensList; * `REFERENCES` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Reference extends Component { - /** * All references options. * * @var array */ public static $REFERENCES_OPTIONS = array( - 'MATCH' => array(1, 'var'), - 'ON DELETE' => array(2, 'var'), - 'ON UPDATE' => array(3, 'var'), + 'MATCH' => array(1, 'var'), + 'ON DELETE' => array(2, 'var'), + 'ON UPDATE' => array(3, 'var'), ); /** @@ -60,9 +56,9 @@ class Reference extends Component /** * Constructor. * - * @param Expression $table The name of the table referenced. - * @param array $columns The columns referenced. - * @param OptionsArray $options The options. + * @param Expression $table the name of the table referenced + * @param array $columns the columns referenced + * @param OptionsArray $options the options */ public function __construct($table = null, array $columns = array(), $options = null) { @@ -72,15 +68,15 @@ class Reference extends Component } /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return Reference */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { - $ret = new Reference(); + $ret = new self(); /** * The state of the parser. @@ -93,7 +89,7 @@ class Reference extends Component * * 2 ---------------------[ options ]--------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -101,7 +97,7 @@ class Reference extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -133,16 +129,16 @@ class Reference extends Component ++$list->idx; break; } - } --$list->idx; + return $ret; } /** - * @param Reference $component The component to be built. - * @param array $options Parameters for building. + * @param Reference $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/RenameOperation.php b/src/Components/RenameOperation.php index 291bd43..84c4aed 100644 --- a/src/Components/RenameOperation.php +++ b/src/Components/RenameOperation.php @@ -2,10 +2,8 @@ /** * `RENAME TABLE` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * `RENAME TABLE` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class RenameOperation extends Component { - /** * The old table name. * @@ -39,9 +35,9 @@ class RenameOperation extends Component public $new; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return RenameOperation[] */ @@ -49,7 +45,7 @@ class RenameOperation extends Component { $ret = array(); - $expr = new RenameOperation(); + $expr = new self(); /** * The state of the parser. @@ -65,7 +61,7 @@ class RenameOperation extends Component * 3 ------------------------[ , ]------------------------> 0 * 3 -----------------------[ else ]----------------------> (END) * - * @var int $state + * @var int */ $state = 0; @@ -73,7 +69,7 @@ class RenameOperation extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -132,7 +128,7 @@ class RenameOperation extends Component } elseif ($state === 3) { if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) { $ret[] = $expr; - $expr = new RenameOperation(); + $expr = new self(); $state = 0; } else { break; @@ -153,12 +149,13 @@ class RenameOperation extends Component } --$list->idx; + return $ret; } /** - * @param RenameOperation $component The component to be built. - * @param array $options Parameters for building. + * @param RenameOperation $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/SetOperation.php b/src/Components/SetOperation.php index cd8b854..2bd600b 100644 --- a/src/Components/SetOperation.php +++ b/src/Components/SetOperation.php @@ -2,10 +2,8 @@ /** * `SET` keyword parser. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * `SET` keyword parser. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class SetOperation extends Component { - /** * The name of the column that is being updated. * @@ -39,9 +35,9 @@ class SetOperation extends Component public $value; /** - * @param Parser $parser The parser that serves as context. - * @param TokensList $list The list of tokens that are being parsed. - * @param array $options Parameters for parsing. + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing * * @return SetOperation[] */ @@ -49,7 +45,7 @@ class SetOperation extends Component { $ret = array(); - $expr = new SetOperation(); + $expr = new self(); /** * The state of the parser. @@ -61,7 +57,7 @@ class SetOperation extends Component * 1 ------------------------[ , ]------------------------> 0 * 1 ----------------------[ value ]----------------------> 1 * - * @var int $state + * @var int */ $state = 0; @@ -69,7 +65,7 @@ class SetOperation extends Component /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -112,18 +108,19 @@ class SetOperation extends Component $expr->column = trim($expr->column); $expr->value = $tmp->expr; $ret[] = $expr; - $expr = new SetOperation(); + $expr = new self(); $state = 0; } } --$list->idx; + return $ret; } /** - * @param SetOperation|SetOperation[] $component The component to be built. - * @param array $options Parameters for building. + * @param SetOperation|SetOperation[] $component the component to be built + * @param array $options parameters for building * * @return string */ diff --git a/src/Components/UnionKeyword.php b/src/Components/UnionKeyword.php index 138a79e..8e4923a 100644 --- a/src/Components/UnionKeyword.php +++ b/src/Components/UnionKeyword.php @@ -2,10 +2,8 @@ /** * `UNION` keyword builder. - * - * @package SqlParser - * @subpackage Components */ + namespace SqlParser\Components; use SqlParser\Component; @@ -15,16 +13,14 @@ use SqlParser\Statements\SelectStatement; * `UNION` keyword builder. * * @category Keywords - * @package SqlParser - * @subpackage Components + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class UnionKeyword extends Component { - /** - * @param SelectStatement[] $component The component to be built. - * @param array $options Parameters for building. + * @param SelectStatement[] $component the component to be built + * @param array $options parameters for building * * @return string */ @@ -34,6 +30,7 @@ class UnionKeyword extends Component foreach ($component as $component) { $tmp[] = $component[0] . ' ' . $component[1]; } + return implode(' ', $tmp); } } diff --git a/src/Context.php b/src/Context.php index 3685857..3ea42de 100644 --- a/src/Context.php +++ b/src/Context.php @@ -5,21 +5,19 @@ * * A context is a collection of keywords, operators and functions used for * parsing. - * - * @package SqlParser */ + namespace SqlParser; /** * Holds the configuration of the context that is currently used. * * @category Contexts - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ abstract class Context { - /** * The maximum length of a keyword. * @@ -92,7 +90,6 @@ abstract class Context * @var array */ public static $OPERATORS = array( - // Some operators (*, =) may have ambiguous flags, because they depend on // the context they are being used in. // For example: 1. SELECT * FROM table; # SQL specific (wildcard) @@ -101,22 +98,22 @@ abstract class Context // SET @i = 0; // @see Token::FLAG_OPERATOR_ARITHMETIC - '%' => 1, '*' => 1, '+' => 1, '-' => 1, '/' => 1, + '%' => 1, '*' => 1, '+' => 1, '-' => 1, '/' => 1, // @see Token::FLAG_OPERATOR_LOGICAL - '!' => 2, '!=' => 2, '&&' => 2, '<' => 2, '<=' => 2, - '<=>' => 2, '<>' => 2, '=' => 2, '>' => 2, '>=' => 2, - '||' => 2, + '!' => 2, '!=' => 2, '&&' => 2, '<' => 2, '<=' => 2, + '<=>' => 2, '<>' => 2, '=' => 2, '>' => 2, '>=' => 2, + '||' => 2, // @see Token::FLAG_OPERATOR_BITWISE - '&' => 4, '<<' => 4, '>>' => 4, '^' => 4, '|' => 4, - '~' => 4, + '&' => 4, '<<' => 4, '>>' => 4, '^' => 4, '|' => 4, + '~' => 4, // @see Token::FLAG_OPERATOR_ASSIGNMENT - ':=' => 8, + ':=' => 8, // @see Token::FLAG_OPERATOR_SQL - '(' => 16, ')' => 16, '.' => 16, ',' => 16, ';' => 16, + '(' => 16, ')' => 16, '.' => 16, ',' => 16, ';' => 16, ); /** @@ -134,77 +131,77 @@ abstract class Context // Compatibility mode for Microsoft's SQL server. // This is the equivalent of ANSI_QUOTES. - const COMPAT_MYSQL = 2; + const COMPAT_MYSQL = 2; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates - const ALLOW_INVALID_DATES = 1; + const ALLOW_INVALID_DATES = 1; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes - const ANSI_QUOTES = 2; + const ANSI_QUOTES = 2; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_error_for_division_by_zero - const ERROR_FOR_DIVISION_BY_ZERO = 4; + const ERROR_FOR_DIVISION_BY_ZERO = 4; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_high_not_precedence - const HIGH_NOT_PRECEDENCE = 8; + const HIGH_NOT_PRECEDENCE = 8; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ignore_space - const IGNORE_SPACE = 16; + const IGNORE_SPACE = 16; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_create_user - const NO_AUTO_CREATE_USER = 32; + const NO_AUTO_CREATE_USER = 32; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_value_on_zero - const NO_AUTO_VALUE_ON_ZERO = 64; + const NO_AUTO_VALUE_ON_ZERO = 64; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_backslash_escapes - const NO_BACKSLASH_ESCAPES = 128; + const NO_BACKSLASH_ESCAPES = 128; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create - const NO_DIR_IN_CREATE = 256; + const NO_DIR_IN_CREATE = 256; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create - const NO_ENGINE_SUBSTITUTION = 512; + const NO_ENGINE_SUBSTITUTION = 512; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_field_options - const NO_FIELD_OPTIONS = 1024; + const NO_FIELD_OPTIONS = 1024; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_key_options - const NO_KEY_OPTIONS = 2048; + const NO_KEY_OPTIONS = 2048; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_table_options - const NO_TABLE_OPTIONS = 4096; + const NO_TABLE_OPTIONS = 4096; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_unsigned_subtraction - const NO_UNSIGNED_SUBTRACTION = 8192; + const NO_UNSIGNED_SUBTRACTION = 8192; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_date - const NO_ZERO_DATE = 16384; + const NO_ZERO_DATE = 16384; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_in_date - const NO_ZERO_IN_DATE = 32768; + const NO_ZERO_IN_DATE = 32768; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by - const ONLY_FULL_GROUP_BY = 65536; + const ONLY_FULL_GROUP_BY = 65536; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_pipes_as_concat - const PIPES_AS_CONCAT = 131072; + const PIPES_AS_CONCAT = 131072; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_real_as_float - const REAL_AS_FLOAT = 262144; + const REAL_AS_FLOAT = 262144; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_all_tables - const STRICT_ALL_TABLES = 524288; + const STRICT_ALL_TABLES = 524288; // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_trans_tables - const STRICT_TRANS_TABLES = 1048576; + const STRICT_TRANS_TABLES = 1048576; // Custom modes. // The table and column names and any other field that must be escaped will // not be. // Reserved keywords are being escaped regardless this mode is used or not. - const NO_ENCLOSING_QUOTES = 1073741824; + const NO_ENCLOSING_QUOTES = 1073741824; /* * Combination SQL Modes @@ -212,31 +209,31 @@ abstract class Context */ // REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE - const SQL_MODE_ANSI = 393234; + const SQL_MODE_ANSI = 393234; // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, - const SQL_MODE_DB2 = 138258; + const SQL_MODE_DB2 = 138258; // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER - const SQL_MODE_MAXDB = 138290; + const SQL_MODE_MAXDB = 138290; // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS - const SQL_MODE_MSSQL = 138258; + const SQL_MODE_MSSQL = 138258; // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER - const SQL_MODE_ORACLE = 138290; + const SQL_MODE_ORACLE = 138290; // PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, // NO_TABLE_OPTIONS, NO_FIELD_OPTIONS - const SQL_MODE_POSTGRESQL = 138258; + const SQL_MODE_POSTGRESQL = 138258; // STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, // ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER - const SQL_MODE_TRADITIONAL = 1622052; + const SQL_MODE_TRADITIONAL = 1622052; // ------------------------------------------------------------------------- // Keyword. @@ -244,8 +241,8 @@ abstract class Context /** * Checks if the given string is a keyword. * - * @param string $str String to be checked. - * @param bool $isReserved Checks if the keyword is reserved. + * @param string $str string to be checked + * @param bool $isReserved checks if the keyword is reserved * * @return int */ @@ -259,6 +256,7 @@ abstract class Context return null; } } + return static::$KEYWORDS[$str]; } @@ -271,15 +269,16 @@ abstract class Context /** * Checks if the given string is an operator. * - * @param string $str String to be checked. + * @param string $str string to be checked * - * @return int The appropriate flag for the operator. + * @return int the appropriate flag for the operator */ public static function isOperator($str) { if (!isset(static::$OPERATORS[$str])) { return null; } + return static::$OPERATORS[$str]; } @@ -289,7 +288,7 @@ abstract class Context /** * Checks if the given character is a whitespace. * - * @param string $str String to be checked. + * @param string $str string to be checked * * @return bool */ @@ -304,9 +303,9 @@ abstract class Context /** * Checks if the given string is the beginning of a whitespace. * - * @param string $str String to be checked. + * @param string $str string to be checked * - * @return int The appropriate flag for the comment type. + * @return int the appropriate flag for the comment type */ public static function isComment($str) { @@ -323,6 +322,7 @@ abstract class Context ) { return Token::FLAG_COMMENT_SQL; } + return null; } @@ -334,13 +334,14 @@ abstract class Context * This actually check only for `TRUE` and `FALSE` because `1` or `0` are * actually numbers and are parsed by specific methods. * - * @param string $str String to be checked. + * @param string $str string to be checked * * @return bool */ public static function isBool($str) { $str = strtoupper($str); + return ($str === 'TRUE') || ($str === 'FALSE'); } @@ -350,7 +351,7 @@ abstract class Context /** * Checks if the given character can be a part of a number. * - * @param string $str String to be checked. + * @param string $str string to be checked * * @return bool */ @@ -367,9 +368,9 @@ abstract class Context * Checks if the given character is the beginning of a symbol. A symbol * can be either a variable or a field name. * - * @param string $str String to be checked. + * @param string $str string to be checked * - * @return int The appropriate flag for the symbol type. + * @return int the appropriate flag for the symbol type */ public static function isSymbol($str) { @@ -378,6 +379,7 @@ abstract class Context } elseif ($str[0] === '`') { return Token::FLAG_SYMBOL_BACKTICK; } + return null; } @@ -387,9 +389,9 @@ abstract class Context /** * Checks if the given character is the beginning of a string. * - * @param string $str String to be checked. + * @param string $str string to be checked * - * @return int The appropriate flag for the string type. + * @return int the appropriate flag for the string type */ public static function isString($str) { @@ -398,6 +400,7 @@ abstract class Context } elseif ($str[0] === '"') { return Token::FLAG_STRING_DOUBLE_QUOTES; } + return null; } @@ -407,7 +410,7 @@ abstract class Context /** * Checks if the given character can be a separator for two lexeme. * - * @param string $str String to be checked. + * @param string $str string to be checked * * @return bool */ @@ -426,12 +429,10 @@ abstract class Context * * Contexts may be used by accessing the context directly. * - * @param string $context Name of the context or full class name that - * defines the context. + * @param string $context name of the context or full class name that + * defines the context * - * @throws \Exception If the specified context doesn't exist. - * - * @return void + * @throws \Exception if the specified context doesn't exist */ public static function load($context = '') { @@ -459,8 +460,8 @@ abstract class Context * * @see Context::load() * - * @param string $context Name of the context or full class name that - * defines the context. + * @param string $context name of the context or full class name that + * defines the context * * @return string The loaded context. `null` if no context was loaded. */ @@ -470,7 +471,7 @@ abstract class Context * The number of replaces done by `preg_replace`. * This actually represents whether a new context was generated or not. * - * @var int $count + * @var int */ $count = 0; @@ -504,8 +505,6 @@ abstract class Context * Sets the SQL mode. * * @param string $mode The list of modes. If empty, the mode is reset. - * - * @return void */ public static function setMode($mode = '') { @@ -522,8 +521,8 @@ abstract class Context /** * Escapes the symbol by adding surrounding backticks. * - * @param array|string $str The string to be escaped. - * @param string $quote Quote to be used when escaping. + * @param array|string $str the string to be escaped + * @param string $quote quote to be used when escaping * * @return string */ @@ -533,16 +532,17 @@ abstract class Context foreach ($str as $key => $value) { $str[$key] = static::escape($value); } + return $str; } - if ((static::$MODE & Context::NO_ENCLOSING_QUOTES) + if ((static::$MODE & self::NO_ENCLOSING_QUOTES) && (!static::isKeyword($str, true)) ) { return $str; } - if (static::$MODE & Context::ANSI_QUOTES) { + if (static::$MODE & self::ANSI_QUOTES) { $quote = '"'; } diff --git a/src/Exceptions/LexerException.php b/src/Exceptions/LexerException.php index 2dd3041..e60c332 100644 --- a/src/Exceptions/LexerException.php +++ b/src/Exceptions/LexerException.php @@ -2,23 +2,19 @@ /** * Exception thrown by the lexer. - * - * @package SqlParser - * @subpackage Exceptions */ + namespace SqlParser\Exceptions; /** * Exception thrown by the lexer. * * @category Exceptions - * @package SqlParser - * @subpackage Exceptions + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class LexerException extends \Exception { - /** * The character that produced this error. * @@ -36,10 +32,10 @@ class LexerException extends \Exception /** * Constructor. * - * @param string $msg The message of this exception. - * @param string $ch The character that produced this exception. - * @param int $pos The position of the character. - * @param int $code The code of this error. + * @param string $msg the message of this exception + * @param string $ch the character that produced this exception + * @param int $pos the position of the character + * @param int $code the code of this error */ public function __construct($msg = '', $ch = '', $pos = 0, $code = 0) { diff --git a/src/Exceptions/ParserException.php b/src/Exceptions/ParserException.php index 81f6259..07f30c7 100644 --- a/src/Exceptions/ParserException.php +++ b/src/Exceptions/ParserException.php @@ -2,10 +2,8 @@ /** * Exception thrown by the parser. - * - * @package SqlParser - * @subpackage Exceptions */ + namespace SqlParser\Exceptions; use SqlParser\Token; @@ -14,13 +12,11 @@ use SqlParser\Token; * Exception thrown by the parser. * * @category Exceptions - * @package SqlParser - * @subpackage Exceptions + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ParserException extends \Exception { - /** * The token that produced this error. * @@ -31,9 +27,9 @@ class ParserException extends \Exception /** * Constructor. * - * @param string $msg The message of this exception. - * @param Token $token The token that produced this exception. - * @param int $code The code of this error. + * @param string $msg the message of this exception + * @param Token $token the token that produced this exception + * @param int $code the code of this error */ public function __construct($msg = '', Token $token = null, $code = 0) { diff --git a/src/Lexer.php b/src/Lexer.php index 9de2a7a..73c4100 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -6,9 +6,8 @@ * This is one of the most important components, along with the parser. * * Depends on context to extract lexemes. - * - * @package SqlParser */ + namespace SqlParser; require_once 'common.php'; @@ -21,7 +20,7 @@ if (!defined('USE_UTF_STRINGS')) { // All `mb_` functions must specify the correct encoding, which is // 'UTF-8' in order to work properly. - /** + /* * Forces usage of `UtfString` if the string is multibyte. * `UtfString` may be slower, but it gives better results. * @@ -37,20 +36,19 @@ if (!defined('USE_UTF_STRINGS')) { * The output of the lexer is affected by the context of the SQL statement. * * @category Lexer - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ + * * @see Context */ class Lexer { - /** * A list of methods that are used in lexing the SQL query. * * @var array */ public static $PARSER_METHODS = array( - // It is best to put the parsers in order of their complexity // (ascending) and their occurrence rate (descending). // @@ -76,7 +74,7 @@ class Lexer 'parseDelimiter', 'parseWhitespace', 'parseNumber', 'parseComment', 'parseOperator', 'parseBool', 'parseString', - 'parseSymbol', 'parseKeyword', 'parseLabel', 'parseUnknown' + 'parseSymbol', 'parseKeyword', 'parseLabel', 'parseUnknown', ); /** @@ -161,26 +159,27 @@ class Lexer /** * Gets the tokens list parsed by a new instance of a lexer. * - * @param string|UtfString $str The query to be lexed. - * @param bool $strict Whether strict mode should be - * enabled or not. - * @param string $delimiter The delimiter to be used. + * @param string|UtfString $str the query to be lexed + * @param bool $strict whether strict mode should be + * enabled or not + * @param string $delimiter the delimiter to be used * * @return TokensList */ public static function getTokens($str, $strict = false, $delimiter = null) { - $lexer = new Lexer($str, $strict, $delimiter); + $lexer = new self($str, $strict, $delimiter); + return $lexer->list; } /** * Constructor. * - * @param string|UtfString $str The query to be lexed. - * @param bool $strict Whether strict mode should be - * enabled or not. - * @param string $delimiter The delimiter to be used. + * @param string|UtfString $str the query to be lexed + * @param bool $strict whether strict mode should be + * enabled or not + * @param string $delimiter the delimiter to be used */ public function __construct($str, $strict = false, $delimiter = null) { @@ -212,7 +211,7 @@ class Lexer /** * Sets the delimiter. * - * @param string $delimiter The new delimiter. + * @param string $delimiter the new delimiter */ public function setDelimiter($delimiter) { @@ -222,8 +221,6 @@ class Lexer /** * Parses the string and extracts lexemes. - * - * @return void */ public function lex() { @@ -241,7 +238,7 @@ class Lexer /** * Last processed token. * - * @var Token $lastToken + * @var Token */ $lastToken = null; @@ -249,7 +246,7 @@ class Lexer /** * The new token. * - * @var Token $token + * @var Token */ $token = null; @@ -363,14 +360,12 @@ class Lexer /** * Creates a new error log. * - * @param string $msg The error message. - * @param string $str The character that produced the error. - * @param int $pos The position of the character. - * @param int $code The code of the error. + * @param string $msg the error message + * @param string $str the character that produced the error + * @param int $pos the position of the character + * @param int $code the code of the error * - * @throws LexerException Throws the exception, if strict mode is enabled. - * - * @return void + * @throws LexerException throws the exception, if strict mode is enabled */ public function error($msg = '', $str = '', $pos = 0, $code = 0) { @@ -393,21 +388,21 @@ class Lexer /** * Value to be returned. * - * @var Token $ret + * @var Token */ $ret = null; /** * The value of `$this->last` where `$token` ends in `$this->str`. * - * @var int $iEnd + * @var int */ $iEnd = $this->last; /** * Whether last parsed character is a whitespace. * - * @var bool $lastSpace + * @var bool */ $lastSpace = false; @@ -438,6 +433,7 @@ class Lexer } $this->last = $iEnd; + return $ret; } @@ -453,21 +449,21 @@ class Lexer /** * Value to be returned. * - * @var Token $ret + * @var Token */ $ret = null; /** * The value of `$this->last` where `$token` ends in `$this->str`. * - * @var int $iEnd + * @var int */ $iEnd = $this->last; /** * Whether last parsed character is a whitespace. * - * @var bool $lastSpace + * @var bool */ $lastSpace = false; @@ -493,6 +489,7 @@ class Lexer } $this->last = $iEnd; + return $ret; } @@ -508,14 +505,14 @@ class Lexer /** * Value to be returned. * - * @var Token $ret + * @var Token */ $ret = null; /** * The value of `$this->last` where `$token` ends in `$this->str`. * - * @var int $iEnd + * @var int */ $iEnd = $this->last; @@ -528,6 +525,7 @@ class Lexer } $this->last = $iEnd; + return $ret; } @@ -549,6 +547,7 @@ class Lexer } --$this->last; + return new Token($token, Token::TYPE_WHITESPACE); } @@ -612,6 +611,7 @@ class Lexer if ($this->last < $this->len) { $token .= $this->str[$this->last]; } + return new Token($token, Token::TYPE_COMMENT, $flags); } } @@ -627,11 +627,13 @@ class Lexer } $token .= "\n"; // Adding the line ending. } + return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL); } } $this->last = $iBak; + return null; } @@ -662,6 +664,7 @@ class Lexer } $this->last = $iBak; + return null; } @@ -797,16 +800,18 @@ class Lexer || ($state === 6) || ($state === 9) ) { --$this->last; + return new Token($token, Token::TYPE_NUMBER, $flags); } $this->last = $iBak; + return null; } /** * Parses a string. * - * @param string $quote Additional starting symbol. + * @param string $quote additional starting symbol * * @return Token */ @@ -844,6 +849,7 @@ class Lexer } else { $token .= $this->str[$this->last]; } + return new Token($token, Token::TYPE_STRING, $flags); } @@ -905,6 +911,7 @@ class Lexer $token .= $this->str[$this->last]; } --$this->last; + return new Token($token); } @@ -925,6 +932,7 @@ class Lexer } $this->last += $this->delimiterLen - 1; + return new Token($this->delimiter, Token::TYPE_DELIMITER); } } diff --git a/src/Parser.php b/src/Parser.php index ddefc7e..1ad5bb8 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -4,9 +4,8 @@ * Defines the parser of the library. * * This is one of the most important components, along with the lexer. - * - * @package SqlParser */ + namespace SqlParser; require_once 'common.php'; @@ -20,76 +19,74 @@ use SqlParser\Statements\TransactionStatement; * parse tree. * * @category Parser - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Parser { - /** * Array of classes that are used in parsing the SQL statements. * * @var array */ public static $STATEMENT_PARSERS = array( - // MySQL Utility Statements - 'DESCRIBE' => 'SqlParser\\Statements\\ExplainStatement', - 'DESC' => 'SqlParser\\Statements\\ExplainStatement', - 'EXPLAIN' => 'SqlParser\\Statements\\ExplainStatement', - 'FLUSH' => '', - 'GRANT' => '', - 'HELP' => '', - 'SET PASSWORD' => '', - 'STATUS' => '', - 'USE' => '', + 'DESCRIBE' => 'SqlParser\\Statements\\ExplainStatement', + 'DESC' => 'SqlParser\\Statements\\ExplainStatement', + 'EXPLAIN' => 'SqlParser\\Statements\\ExplainStatement', + 'FLUSH' => '', + 'GRANT' => '', + 'HELP' => '', + 'SET PASSWORD' => '', + 'STATUS' => '', + 'USE' => '', // Table Maintenance Statements // https://dev.mysql.com/doc/refman/5.7/en/table-maintenance-sql.html - 'ANALYZE' => 'SqlParser\\Statements\\AnalyzeStatement', - 'BACKUP' => 'SqlParser\\Statements\\BackupStatement', - 'CHECK' => 'SqlParser\\Statements\\CheckStatement', - 'CHECKSUM' => 'SqlParser\\Statements\\ChecksumStatement', - 'OPTIMIZE' => 'SqlParser\\Statements\\OptimizeStatement', - 'REPAIR' => 'SqlParser\\Statements\\RepairStatement', - 'RESTORE' => 'SqlParser\\Statements\\RestoreStatement', + 'ANALYZE' => 'SqlParser\\Statements\\AnalyzeStatement', + 'BACKUP' => 'SqlParser\\Statements\\BackupStatement', + 'CHECK' => 'SqlParser\\Statements\\CheckStatement', + 'CHECKSUM' => 'SqlParser\\Statements\\ChecksumStatement', + 'OPTIMIZE' => 'SqlParser\\Statements\\OptimizeStatement', + 'REPAIR' => 'SqlParser\\Statements\\RepairStatement', + 'RESTORE' => 'SqlParser\\Statements\\RestoreStatement', // Database Administration Statements // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-server-administration.html - 'SET' => 'SqlParser\\Statements\\SetStatement', - 'SHOW' => 'SqlParser\\Statements\\ShowStatement', + 'SET' => 'SqlParser\\Statements\\SetStatement', + 'SHOW' => 'SqlParser\\Statements\\ShowStatement', // Data Definition Statements. // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-definition.html - 'ALTER' => 'SqlParser\\Statements\\AlterStatement', - 'CREATE' => 'SqlParser\\Statements\\CreateStatement', - 'DROP' => 'SqlParser\\Statements\\DropStatement', - 'RENAME' => 'SqlParser\\Statements\\RenameStatement', - 'TRUNCATE' => 'SqlParser\\Statements\\TruncateStatement', + 'ALTER' => 'SqlParser\\Statements\\AlterStatement', + 'CREATE' => 'SqlParser\\Statements\\CreateStatement', + 'DROP' => 'SqlParser\\Statements\\DropStatement', + 'RENAME' => 'SqlParser\\Statements\\RenameStatement', + 'TRUNCATE' => 'SqlParser\\Statements\\TruncateStatement', // Data Manipulation Statements. // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-manipulation.html - 'CALL' => 'SqlParser\\Statements\\CallStatement', - 'DELETE' => 'SqlParser\\Statements\\DeleteStatement', - 'DO' => '', - 'HANDLER' => '', - 'INSERT' => 'SqlParser\\Statements\\InsertStatement', - 'LOAD' => '', - 'REPLACE' => 'SqlParser\\Statements\\ReplaceStatement', - 'SELECT' => 'SqlParser\\Statements\\SelectStatement', - 'UPDATE' => 'SqlParser\\Statements\\UpdateStatement', + 'CALL' => 'SqlParser\\Statements\\CallStatement', + 'DELETE' => 'SqlParser\\Statements\\DeleteStatement', + 'DO' => '', + 'HANDLER' => '', + 'INSERT' => 'SqlParser\\Statements\\InsertStatement', + 'LOAD' => '', + 'REPLACE' => 'SqlParser\\Statements\\ReplaceStatement', + 'SELECT' => 'SqlParser\\Statements\\SelectStatement', + 'UPDATE' => 'SqlParser\\Statements\\UpdateStatement', // Prepared Statements. // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html - 'DEALLOCATE' => '', - 'EXECUTE' => '', - 'PREPARE' => '', + 'DEALLOCATE' => '', + 'EXECUTE' => '', + 'PREPARE' => '', // Transactional and Locking Statements // https://dev.mysql.com/doc/refman/5.7/en/commit.html - 'BEGIN' => 'SqlParser\\Statements\\TransactionStatement', - 'COMMIT' => 'SqlParser\\Statements\\TransactionStatement', - 'ROLLBACK' => 'SqlParser\\Statements\\TransactionStatement', + 'BEGIN' => 'SqlParser\\Statements\\TransactionStatement', + 'COMMIT' => 'SqlParser\\Statements\\TransactionStatement', + 'ROLLBACK' => 'SqlParser\\Statements\\TransactionStatement', 'START TRANSACTION' => 'SqlParser\\Statements\\TransactionStatement', ); @@ -99,216 +96,213 @@ class Parser * @var array */ public static $KEYWORD_PARSERS = array( - // This is not a proper keyword and was added here to help the // formatter. - 'PARTITION BY' => array(), - 'SUBPARTITION BY' => array(), + 'PARTITION BY' => array(), + 'SUBPARTITION BY' => array(), // This is not a proper keyword and was added here to help the // builder. - '_OPTIONS' => array( - 'class' => 'SqlParser\\Components\\OptionsArray', - 'field' => 'options', + '_OPTIONS' => array( + 'class' => 'SqlParser\\Components\\OptionsArray', + 'field' => 'options', ), - '_END_OPTIONS' => array( - 'class' => 'SqlParser\\Components\\OptionsArray', - 'field' => 'end_options', + '_END_OPTIONS' => array( + 'class' => 'SqlParser\\Components\\OptionsArray', + 'field' => 'end_options', ), - - 'UNION' => array( - 'class' => 'SqlParser\\Components\\UnionKeyword', - 'field' => 'union', + 'UNION' => array( + 'class' => 'SqlParser\\Components\\UnionKeyword', + 'field' => 'union', ), - 'UNION ALL' => array( - 'class' => 'SqlParser\\Components\\UnionKeyword', - 'field' => 'union', + 'UNION ALL' => array( + 'class' => 'SqlParser\\Components\\UnionKeyword', + 'field' => 'union', ), - 'UNION DISTINCT' => array( - 'class' => 'SqlParser\\Components\\UnionKeyword', - 'field' => 'union', + 'UNION DISTINCT' => array( + 'class' => 'SqlParser\\Components\\UnionKeyword', + 'field' => 'union', ), // Actual clause parsers. - 'ALTER' => array( - 'class' => 'SqlParser\\Components\\Expression', - 'field' => 'table', - 'options' => array('parseField' => 'table'), - ), - 'ANALYZE' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'BACKUP' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'CALL' => array( - 'class' => 'SqlParser\\Components\\FunctionCall', - 'field' => 'call', - ), - 'CHECK' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'CHECKSUM' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'CROSS JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'DROP' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'fields', - 'options' => array('parseField' => 'table'), - ), - 'FROM' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'from', - 'options' => array('field' => 'table'), - ), - 'GROUP BY' => array( - 'class' => 'SqlParser\\Components\\OrderKeyword', - 'field' => 'group', - ), - 'HAVING' => array( - 'class' => 'SqlParser\\Components\\Condition', - 'field' => 'having', - ), - 'INTO' => array( - 'class' => 'SqlParser\\Components\\IntoKeyword', - 'field' => 'into', - ), - 'JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'LEFT JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'LEFT OUTER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'ON' => array( - 'class' => 'SqlParser\\Components\\Expression', - 'field' => 'table', - 'options' => array('parseField' => 'table'), - ), - 'RIGHT JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'RIGHT OUTER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'INNER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'FULL JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'FULL OUTER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'NATURAL JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'NATURAL LEFT JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'NATURAL RIGHT JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'NATURAL LEFT OUTER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'NATURAL RIGHT OUTER JOIN' => array( - 'class' => 'SqlParser\\Components\\JoinKeyword', - 'field' => 'join', - ), - 'LIMIT' => array( - 'class' => 'SqlParser\\Components\\Limit', - 'field' => 'limit', - ), - 'OPTIMIZE' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'ORDER BY' => array( - 'class' => 'SqlParser\\Components\\OrderKeyword', - 'field' => 'order', - ), - 'PARTITION' => array( - 'class' => 'SqlParser\\Components\\ArrayObj', - 'field' => 'partition', - ), - 'PROCEDURE' => array( - 'class' => 'SqlParser\\Components\\FunctionCall', - 'field' => 'procedure', - ), - 'RENAME' => array( - 'class' => 'SqlParser\\Components\\RenameOperation', - 'field' => 'renames', - ), - 'REPAIR' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), + 'ALTER' => array( + 'class' => 'SqlParser\\Components\\Expression', + 'field' => 'table', + 'options' => array('parseField' => 'table'), + ), + 'ANALYZE' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'BACKUP' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'CALL' => array( + 'class' => 'SqlParser\\Components\\FunctionCall', + 'field' => 'call', + ), + 'CHECK' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'CHECKSUM' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'CROSS JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'DROP' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'fields', + 'options' => array('parseField' => 'table'), + ), + 'FROM' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'from', + 'options' => array('field' => 'table'), + ), + 'GROUP BY' => array( + 'class' => 'SqlParser\\Components\\OrderKeyword', + 'field' => 'group', + ), + 'HAVING' => array( + 'class' => 'SqlParser\\Components\\Condition', + 'field' => 'having', + ), + 'INTO' => array( + 'class' => 'SqlParser\\Components\\IntoKeyword', + 'field' => 'into', + ), + 'JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'LEFT JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'LEFT OUTER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'ON' => array( + 'class' => 'SqlParser\\Components\\Expression', + 'field' => 'table', + 'options' => array('parseField' => 'table'), + ), + 'RIGHT JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'RIGHT OUTER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'INNER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'FULL JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'FULL OUTER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'NATURAL JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'NATURAL LEFT JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'NATURAL RIGHT JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'NATURAL LEFT OUTER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'NATURAL RIGHT OUTER JOIN' => array( + 'class' => 'SqlParser\\Components\\JoinKeyword', + 'field' => 'join', + ), + 'LIMIT' => array( + 'class' => 'SqlParser\\Components\\Limit', + 'field' => 'limit', + ), + 'OPTIMIZE' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'ORDER BY' => array( + 'class' => 'SqlParser\\Components\\OrderKeyword', + 'field' => 'order', + ), + 'PARTITION' => array( + 'class' => 'SqlParser\\Components\\ArrayObj', + 'field' => 'partition', + ), + 'PROCEDURE' => array( + 'class' => 'SqlParser\\Components\\FunctionCall', + 'field' => 'procedure', + ), + 'RENAME' => array( + 'class' => 'SqlParser\\Components\\RenameOperation', + 'field' => 'renames', + ), + 'REPAIR' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'RESTORE' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), ), - 'RESTORE' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), + 'SET' => array( + 'class' => 'SqlParser\\Components\\SetOperation', + 'field' => 'set', ), - 'SET' => array( - 'class' => 'SqlParser\\Components\\SetOperation', - 'field' => 'set', + 'SELECT' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'expr', ), - 'SELECT' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'expr', + 'TRUNCATE' => array( + 'class' => 'SqlParser\\Components\\Expression', + 'field' => 'table', + 'options' => array('parseField' => 'table'), + ), + 'UPDATE' => array( + 'class' => 'SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => array('parseField' => 'table'), + ), + 'VALUE' => array( + 'class' => 'SqlParser\\Components\\Array2d', + 'field' => 'values', + ), + 'VALUES' => array( + 'class' => 'SqlParser\\Components\\Array2d', + 'field' => 'values', + ), + 'WHERE' => array( + 'class' => 'SqlParser\\Components\\Condition', + 'field' => 'where', ), - 'TRUNCATE' => array( - 'class' => 'SqlParser\\Components\\Expression', - 'field' => 'table', - 'options' => array('parseField' => 'table'), - ), - 'UPDATE' => array( - 'class' => 'SqlParser\\Components\\ExpressionArray', - 'field' => 'tables', - 'options' => array('parseField' => 'table'), - ), - 'VALUE' => array( - 'class' => 'SqlParser\\Components\\Array2d', - 'field' => 'values', - ), - 'VALUES' => array( - 'class' => 'SqlParser\\Components\\Array2d', - 'field' => 'values', - ), - 'WHERE' => array( - 'class' => 'SqlParser\\Components\\Condition', - 'field' => 'where', - ), - ); /** @@ -357,8 +351,8 @@ class Parser /** * Constructor. * - * @param string|UtfString|TokensList $list The list of tokens to be parsed. - * @param bool $strict Whether strict mode should be enabled or not. + * @param string|UtfString|TokensList $list the list of tokens to be parsed + * @param bool $strict whether strict mode should be enabled or not */ public function __construct($list = null, $strict = false) { @@ -378,44 +372,41 @@ class Parser /** * Builds the parse trees. - * - * @return void */ public function parse() { - /** * Last transaction. * - * @var TransactionStatement $lastTransaction + * @var TransactionStatement */ $lastTransaction = null; /** * Last parsed statement. * - * @var Statement $lastStatement + * @var Statement */ $lastStatement = null; /** * Union's type or false for no union. * - * @var bool|string $unionType + * @var bool|string */ $unionType = false; /** * The index of the last token from the last statement. * - * @var int $prevLastIdx + * @var int */ $prevLastIdx = -1; /** * The list of tokens. * - * @var TokensList $list + * @var TokensList */ $list = &$this->list; @@ -423,7 +414,7 @@ class Parser /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -485,14 +476,14 @@ class Parser /** * The name of the class that is used for parsing. * - * @var string $class + * @var string */ $class = static::$STATEMENT_PARSERS[$token->value]; /** * Processed statement. * - * @var Statement $statement + * @var Statement */ $statement = new $class($this, $this->list); @@ -512,13 +503,13 @@ class Parser && ($lastStatement instanceof SelectStatement) && ($statement instanceof SelectStatement) ) { - /** + /* * This SELECT statement. * * @var SelectStatement $statement */ - /** + /* * Last SELECT statement. * * @var SelectStatement $lastStatement @@ -545,8 +536,8 @@ class Parser // Handles transactions. if ($statement instanceof TransactionStatement) { - /** - * @var TransactionStatement $statement + /* + * @var TransactionStatement */ if ($statement->type === TransactionStatement::TYPE_BEGIN) { $lastTransaction = $statement; @@ -581,20 +572,17 @@ class Parser $this->statements[] = $statement; } $lastStatement = $statement; - } } /** * Creates a new error log. * - * @param string $msg The error message. - * @param Token $token The token that produced the error. - * @param int $code The code of the error. - * - * @throws ParserException Throws the exception, if strict mode is enabled. + * @param string $msg the error message + * @param Token $token the token that produced the error + * @param int $code the code of the error * - * @return void + * @throws ParserException throws the exception, if strict mode is enabled */ public function error($msg = '', Token $token = null, $code = 0) { diff --git a/src/Statement.php b/src/Statement.php index b7c4da0..a907b99 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -5,9 +5,8 @@ * class defined here. * * A statement represents the result of parsing the lexemes. - * - * @package SqlParser */ + namespace SqlParser; use SqlParser\Components\OptionsArray; @@ -16,12 +15,11 @@ use SqlParser\Components\OptionsArray; * Abstract statement definition. * * @category Statements - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ abstract class Statement { - /** * Options for this statement. * @@ -83,8 +81,8 @@ abstract class Statement /** * Constructor. * - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function __construct(Parser $parser = null, TokensList $list = null) { @@ -103,7 +101,7 @@ abstract class Statement /** * Query to be returned. * - * @var string $query + * @var string */ $query = ''; @@ -132,7 +130,7 @@ abstract class Statement /** * The name of the clause. * - * @var string $name + * @var string */ $name = $clause[0]; @@ -140,14 +138,15 @@ abstract class Statement * The type of the clause. * * @see self::$CLAUSES - * @var int $type + * + * @var int */ $type = $clause[1]; /** * The builder (parser) of this clause. * - * @var Component $class + * @var Component */ $class = Parser::$KEYWORD_PARSERS[$name]['class']; @@ -155,7 +154,7 @@ abstract class Statement * The name of the field that is used as source for the builder. * Same field is used to store the result of parsing. * - * @var string $field + * @var string */ $field = Parser::$KEYWORD_PARSERS[$name]['field']; @@ -189,10 +188,8 @@ abstract class Statement /** * Parses the statements defined by the tokens list. * - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -200,7 +197,7 @@ abstract class Statement * Array containing all list of clauses parsed. * This is used to check for duplicates. * - * @var array $parsedClauses + * @var array */ $parsedClauses = array(); @@ -212,7 +209,7 @@ abstract class Statement * For statements that do not have any options this is set to `true` by * default. * - * @var bool $parsedOptions + * @var bool */ $parsedOptions = empty(static::$OPTIONS); @@ -220,7 +217,7 @@ abstract class Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -282,21 +279,21 @@ abstract class Statement /** * The name of the class that is used for parsing. * - * @var Component $class + * @var Component */ $class = null; /** * The name of the field where the result of the parsing is stored. * - * @var string $field + * @var string */ $field = null; /** * Parser's options. * - * @var array $options + * @var array */ $options = array(); @@ -389,29 +386,23 @@ abstract class Statement /** * Function called before the token is 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 + * @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 */ 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 void + * @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 */ public function after(Parser $parser, TokensList $list, Token $token) { - } /** @@ -439,12 +430,12 @@ abstract class Statement /** * Validates the order of the clauses in parsed statement * Ideally this should be called after successfully - * completing the parsing of each statement + * completing the parsing of each statement. * - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed * - * @return boolean + * @return bool */ public function validateClauseOrder($parser, $list) { @@ -463,9 +454,9 @@ abstract class Statement * 0 - JOIN not found till now * 1 - JOIN has been found * 2 - A Non-JOIN clause has been found - * after a previously found JOIN clause + * after a previously found JOIN clause. * - * @var int $joinStart + * @var int */ $joinStart = 0; @@ -495,6 +486,7 @@ abstract class Statement __('Unexpected ordering of clauses.'), $token ); + return false; } else { $minIdx = $clauseStartIdx; diff --git a/src/Statements/AlterStatement.php b/src/Statements/AlterStatement.php index abad5da..487ad43 100644 --- a/src/Statements/AlterStatement.php +++ b/src/Statements/AlterStatement.php @@ -2,10 +2,8 @@ /** * `ALTER` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -20,13 +18,11 @@ use SqlParser\Components\OptionsArray; * `ALTER` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class AlterStatement extends Statement { - /** * Table affected. * @@ -47,25 +43,23 @@ class AlterStatement extends Statement * @var array */ public static $OPTIONS = array( - 'ONLINE' => 1, - 'OFFLINE' => 1, - 'IGNORE' => 2, - - 'DATABASE' => 3, - 'EVENT' => 3, - 'FUNCTION' => 3, - 'PROCEDURE' => 3, - 'SERVER' => 3, - 'TABLE' => 3, - 'TABLESPACE' => 3, - 'VIEW' => 3, + 'ONLINE' => 1, + 'OFFLINE' => 1, + 'IGNORE' => 2, + + 'DATABASE' => 3, + 'EVENT' => 3, + 'FUNCTION' => 3, + 'PROCEDURE' => 3, + 'SERVER' => 3, + 'TABLE' => 3, + 'TABLESPACE' => 3, + 'VIEW' => 3, ); /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -97,7 +91,7 @@ class AlterStatement extends Statement * * 1 -------------------------[ , ]-----------------------> 0 * - * @var int $state + * @var int */ $state = 0; @@ -105,7 +99,7 @@ class AlterStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; diff --git a/src/Statements/AnalyzeStatement.php b/src/Statements/AnalyzeStatement.php index f7e8f81..aaa5850 100644 --- a/src/Statements/AnalyzeStatement.php +++ b/src/Statements/AnalyzeStatement.php @@ -2,10 +2,8 @@ /** * `ANALYZE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -18,24 +16,21 @@ use SqlParser\Components\Expression; * tbl_name [, tbl_name] ... * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class AnalyzeStatement extends Statement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'NO_WRITE_TO_BINLOG' => 2, - 'LOCAL' => 3, + 'NO_WRITE_TO_BINLOG' => 2, + 'LOCAL' => 3, ); /** diff --git a/src/Statements/BackupStatement.php b/src/Statements/BackupStatement.php index df75939..b207a42 100644 --- a/src/Statements/BackupStatement.php +++ b/src/Statements/BackupStatement.php @@ -2,10 +2,8 @@ /** * `BACKUP` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** @@ -14,25 +12,22 @@ namespace SqlParser\Statements; * BACKUP TABLE tbl_name [, tbl_name] ... TO '/path/to/backup/directory' * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class BackupStatement extends MaintenanceStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'NO_WRITE_TO_BINLOG' => 2, - 'LOCAL' => 3, + 'NO_WRITE_TO_BINLOG' => 2, + 'LOCAL' => 3, - 'TO' => array(4, 'var'), + 'TO' => array(4, 'var'), ); } diff --git a/src/Statements/CallStatement.php b/src/Statements/CallStatement.php index 42a21d7..80e49d3 100644 --- a/src/Statements/CallStatement.php +++ b/src/Statements/CallStatement.php @@ -2,10 +2,8 @@ /** * `CALL` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -21,13 +19,11 @@ use SqlParser\Components\FunctionCall; * CALL sp_name[()] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CallStatement extends Statement { - /** * The name of the function and its parameters. * diff --git a/src/Statements/CheckStatement.php b/src/Statements/CheckStatement.php index ef99ed3..6d852cc 100644 --- a/src/Statements/CheckStatement.php +++ b/src/Statements/CheckStatement.php @@ -2,10 +2,8 @@ /** * `CHECK` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** @@ -14,27 +12,24 @@ namespace SqlParser\Statements; * CHECK TABLE tbl_name [, tbl_name] ... [option] ... * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CheckStatement extends MaintenanceStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'FOR UPGRADE' => 2, - 'QUICK' => 3, - 'FAST' => 4, - 'MEDIUM' => 5, - 'EXTENDED' => 6, - 'CHANGED' => 7, + 'FOR UPGRADE' => 2, + 'QUICK' => 3, + 'FAST' => 4, + 'MEDIUM' => 5, + 'EXTENDED' => 6, + 'CHANGED' => 7, ); } diff --git a/src/Statements/ChecksumStatement.php b/src/Statements/ChecksumStatement.php index 8b62335..d8001e6 100644 --- a/src/Statements/ChecksumStatement.php +++ b/src/Statements/ChecksumStatement.php @@ -2,10 +2,8 @@ /** * `CHECKSUM` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** @@ -14,23 +12,20 @@ namespace SqlParser\Statements; * CHECKSUM TABLE tbl_name [, tbl_name] ... [ QUICK | EXTENDED ] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ChecksumStatement extends MaintenanceStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'QUICK' => 2, - 'EXTENDED' => 3, + 'QUICK' => 2, + 'EXTENDED' => 3, ); } diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index ed13952..891fb50 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -2,10 +2,8 @@ /** * `CREATE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -19,53 +17,49 @@ use SqlParser\Components\PartitionDefinition; use SqlParser\Components\Expression; use SqlParser\Components\OptionsArray; use SqlParser\Components\ParameterDefinition; -use SqlParser\Statements\SelectStatement; /** * `CREATE` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CreateStatement extends Statement { - /** * Options for `CREATE` statements. * * @var array */ public static $OPTIONS = array( - // CREATE TABLE - 'TEMPORARY' => 1, + 'TEMPORARY' => 1, // CREATE VIEW - 'OR REPLACE' => array(2, 'var='), - 'ALGORITHM' => array(3, 'var='), + 'OR REPLACE' => array(2, 'var='), + 'ALGORITHM' => array(3, 'var='), // `DEFINER` is also used for `CREATE FUNCTION / PROCEDURE` - 'DEFINER' => array(4, 'expr='), - 'SQL SECURITY' => array(5, 'var'), - - 'DATABASE' => 6, - 'EVENT' => 6, - 'FUNCTION' => 6, - 'INDEX' => 6, - 'UNIQUE INDEX' => 6, - 'FULLTEXT INDEX' => 6, - 'SPATIAL INDEX' => 6, - 'PROCEDURE' => 6, - 'SERVER' => 6, - 'TABLE' => 6, - 'TABLESPACE' => 6, - 'TRIGGER' => 6, - 'USER' => 6, - 'VIEW' => 6, + 'DEFINER' => array(4, 'expr='), + 'SQL SECURITY' => array(5, 'var'), + + 'DATABASE' => 6, + 'EVENT' => 6, + 'FUNCTION' => 6, + 'INDEX' => 6, + 'UNIQUE INDEX' => 6, + 'FULLTEXT INDEX' => 6, + 'SPATIAL INDEX' => 6, + 'PROCEDURE' => 6, + 'SERVER' => 6, + 'TABLE' => 6, + 'TABLESPACE' => 6, + 'TRIGGER' => 6, + 'USER' => 6, + 'VIEW' => 6, // CREATE TABLE - 'IF NOT EXISTS' => 7, + 'IF NOT EXISTS' => 7, ); /** @@ -74,12 +68,12 @@ class CreateStatement extends Statement * @var array */ public static $DB_OPTIONS = array( - 'CHARACTER SET' => array(1, 'var='), - 'CHARSET' => array(1, 'var='), - 'DEFAULT CHARACTER SET' => array(1, 'var='), - 'DEFAULT CHARSET' => array(1, 'var='), - 'DEFAULT COLLATE' => array(2, 'var='), - 'COLLATE' => array(2, 'var='), + 'CHARACTER SET' => array(1, 'var='), + 'CHARSET' => array(1, 'var='), + 'DEFAULT CHARACTER SET' => array(1, 'var='), + 'DEFAULT CHARSET' => array(1, 'var='), + 'DEFAULT COLLATE' => array(2, 'var='), + 'COLLATE' => array(2, 'var='), ); /** @@ -88,31 +82,31 @@ class CreateStatement extends Statement * @var array */ public static $TABLE_OPTIONS = array( - 'ENGINE' => array(1, 'var='), - 'AUTO_INCREMENT' => array(2, 'var='), - 'AVG_ROW_LENGTH' => array(3, 'var'), - 'CHARACTER SET' => array(4, 'var='), - 'CHARSET' => array(4, 'var='), - 'DEFAULT CHARACTER SET' => array(4, 'var='), - 'DEFAULT CHARSET' => array(4, 'var='), - 'CHECKSUM' => array(5, 'var'), - 'DEFAULT COLLATE' => array(6, 'var='), - 'COLLATE' => array(6, 'var='), - 'COMMENT' => array(7, 'var='), - 'CONNECTION' => array(8, 'var'), - 'DATA DIRECTORY' => array(9, 'var'), - 'DELAY_KEY_WRITE' => array(10, 'var'), - 'INDEX DIRECTORY' => array(11, 'var'), - 'INSERT_METHOD' => array(12, 'var'), - 'KEY_BLOCK_SIZE' => array(13, 'var'), - 'MAX_ROWS' => array(14, 'var'), - 'MIN_ROWS' => array(15, 'var'), - 'PACK_KEYS' => array(16, 'var'), - 'PASSWORD' => array(17, 'var'), - 'ROW_FORMAT' => array(18, 'var'), - 'TABLESPACE' => array(19, 'var'), - 'STORAGE' => array(20, 'var'), - 'UNION' => array(21, 'var'), + 'ENGINE' => array(1, 'var='), + 'AUTO_INCREMENT' => array(2, 'var='), + 'AVG_ROW_LENGTH' => array(3, 'var'), + 'CHARACTER SET' => array(4, 'var='), + 'CHARSET' => array(4, 'var='), + 'DEFAULT CHARACTER SET' => array(4, 'var='), + 'DEFAULT CHARSET' => array(4, 'var='), + 'CHECKSUM' => array(5, 'var'), + 'DEFAULT COLLATE' => array(6, 'var='), + 'COLLATE' => array(6, 'var='), + 'COMMENT' => array(7, 'var='), + 'CONNECTION' => array(8, 'var'), + 'DATA DIRECTORY' => array(9, 'var'), + 'DELAY_KEY_WRITE' => array(10, 'var'), + 'INDEX DIRECTORY' => array(11, 'var'), + 'INSERT_METHOD' => array(12, 'var'), + 'KEY_BLOCK_SIZE' => array(13, 'var'), + 'MAX_ROWS' => array(14, 'var'), + 'MIN_ROWS' => array(15, 'var'), + 'PACK_KEYS' => array(16, 'var'), + 'PASSWORD' => array(17, 'var'), + 'ROW_FORMAT' => array(18, 'var'), + 'TABLESPACE' => array(19, 'var'), + 'STORAGE' => array(20, 'var'), + 'UNION' => array(21, 'var'), ); /** @@ -121,15 +115,15 @@ class CreateStatement extends Statement * @var array */ public static $FUNC_OPTIONS = array( - 'COMMENT' => array(1, 'var='), - 'LANGUAGE SQL' => 2, - 'DETERMINISTIC' => 3, - 'NOT DETERMINISTIC' => 3, - 'CONTAINS SQL' => 4, - 'NO SQL' => 4, - 'READS SQL DATA' => 4, - 'MODIFIES SQL DATA' => 4, - 'SQL SECURITY DEFINER' => array(5, 'var'), + 'COMMENT' => array(1, 'var='), + 'LANGUAGE SQL' => 2, + 'DETERMINISTIC' => 3, + 'NOT DETERMINISTIC' => 3, + 'CONTAINS SQL' => 4, + 'NO SQL' => 4, + 'READS SQL DATA' => 4, + 'MODIFIES SQL DATA' => 4, + 'SQL SECURITY DEFINER' => array(5, 'var'), ); /** @@ -138,11 +132,11 @@ class CreateStatement extends Statement * @var array */ public static $TRIGGER_OPTIONS = array( - 'BEFORE' => 1, - 'AFTER' => 1, - 'INSERT' => 2, - 'UPDATE' => 2, - 'DELETE' => 2, + 'BEFORE' => 1, + 'AFTER' => 1, + 'INSERT' => 2, + 'UPDATE' => 2, + 'DELETE' => 2, ); /** @@ -178,7 +172,7 @@ class CreateStatement extends Statement public $fields; /** - * If `CREATE TABLE ... SELECT` + * If `CREATE TABLE ... SELECT`. * * Used by `CREATE TABLE` * @@ -187,7 +181,7 @@ class CreateStatement extends Statement public $select; /** - * If `CREATE TABLE ... LIKE` + * If `CREATE TABLE ... LIKE`. * * Used by `CREATE TABLE` * @@ -259,7 +253,7 @@ class CreateStatement extends Statement /** * The body of this function or procedure. For views, it is the select - * statement that gets the + * statement that gets the. * * Used by `CREATE FUNCTION`, `CREATE PROCEDURE` and `CREATE VIEW`. * @@ -340,12 +334,14 @@ class CreateStatement extends Statement if ($this->options->has('FUNCTION')) { $tmp = 'RETURNS ' . DataType::build($this->return); } + return 'CREATE ' . OptionsArray::build($this->options) . ' ' . Expression::build($this->name) . ' ' . ParameterDefinition::build($this->parameters) . ' ' . $tmp . ' ' . TokensList::build($this->body); } + return 'CREATE ' . OptionsArray::build($this->options) . ' ' . Expression::build($this->name) . ' ' @@ -353,10 +349,8 @@ class CreateStatement extends Statement } /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -388,12 +382,12 @@ class CreateStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; $nextidx = $list->idx + 1; while ($nextidx < $list->count && $list->tokens[$nextidx]->type == Token::TYPE_WHITESPACE) { - $nextidx++; + ++$nextidx; } if ($this->options->has('DATABASE')) { @@ -457,7 +451,7 @@ class CreateStatement extends Statement * The field that is being filled (`partitionBy` or * `subpartitionBy`). * - * @var string $field + * @var string */ $field = null; @@ -466,7 +460,7 @@ class CreateStatement extends Statement * previously. At least one bracket is required to validate the * expression. * - * @var int|bool $brackets + * @var int|bool */ $brackets = false; @@ -477,7 +471,7 @@ class CreateStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -537,7 +531,7 @@ class CreateStatement extends Statement $parser, $list, array( - 'type' => 'SqlParser\\Components\\PartitionDefinition' + 'type' => 'SqlParser\\Components\\PartitionDefinition', ) ); } diff --git a/src/Statements/DeleteStatement.php b/src/Statements/DeleteStatement.php index d83d0e9..621d032 100644 --- a/src/Statements/DeleteStatement.php +++ b/src/Statements/DeleteStatement.php @@ -2,10 +2,8 @@ /** * `DELETE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -45,22 +43,20 @@ use SqlParser\Components\OptionsArray; * * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class DeleteStatement extends Statement { - /** * Options for `DELETE` statements. * * @var array */ public static $OPTIONS = array( - 'LOW_PRIORITY' => 1, - 'QUICK' => 2, - 'IGNORE' => 3, + 'LOW_PRIORITY' => 1, + 'QUICK' => 2, + 'IGNORE' => 3, ); /** @@ -71,15 +67,15 @@ class DeleteStatement extends Statement * @var array */ public static $CLAUSES = array( - 'DELETE' => array('DELETE', 2), + 'DELETE' => array('DELETE', 2), // Used for options. - '_OPTIONS' => array('_OPTIONS', 1), - 'FROM' => array('FROM', 3), - 'PARTITION' => array('PARTITION', 3), - 'USING' => array('USING', 3), - 'WHERE' => array('WHERE', 3), - 'ORDER BY' => array('ORDER BY', 3), - 'LIMIT' => array('LIMIT', 3), + '_OPTIONS' => array('_OPTIONS', 1), + 'FROM' => array('FROM', 3), + 'PARTITION' => array('PARTITION', 3), + 'USING' => array('USING', 3), + 'WHERE' => array('WHERE', 3), + 'ORDER BY' => array('ORDER BY', 3), + 'LIMIT' => array('LIMIT', 3), ); /** @@ -90,14 +86,14 @@ class DeleteStatement extends Statement public $from; /** - * Tables used as sources for this statement + * Tables used as sources for this statement. * * @var Expression[] */ public $using; /** - * Columns used in this statement + * Columns used in this statement. * * @var Expression[] */ @@ -131,7 +127,6 @@ class DeleteStatement extends Statement */ public $limit; - /** * @return string */ @@ -139,35 +134,31 @@ class DeleteStatement extends Statement { $ret = 'DELETE ' . OptionsArray::build($this->options); - if ($this->columns != NULL && count($this->columns) > 0) { + if ($this->columns != null && count($this->columns) > 0) { $ret .= ' ' . ExpressionArray::build($this->columns); } - if ($this->from != NULL && count($this->from) > 0) { + if ($this->from != null && count($this->from) > 0) { $ret .= ' FROM ' . ExpressionArray::build($this->from); } - if ($this->using != NULL && count($this->using) > 0) { + if ($this->using != null && count($this->using) > 0) { $ret .= ' USING ' . ExpressionArray::build($this->using); } - if ($this->where != NULL && count($this->where) > 0) { + if ($this->where != null && count($this->where) > 0) { $ret .= ' WHERE ' . Condition::build($this->where); } - if ($this->order != NULL && count($this->order) > 0) { + if ($this->order != null && count($this->order) > 0) { $ret .= ' ORDER BY ' . ExpressionArray::build($this->order); } - if ($this->limit != NULL && strlen($this->limit) > 0) { + if ($this->limit != null && strlen($this->limit) > 0) { $ret .= ' LIMIT ' . Limit::build($this->limit); } return $ret; - } - /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -194,14 +185,14 @@ class DeleteStatement extends Statement * 2 --------------------------------[ ORDER ]----------------------------------> 5 * 2 --------------------------------[ LIMIT ]----------------------------------> 6 * - * @var int $state + * @var int */ $state = 0; /** - * If the query is multi-table or not + * If the query is multi-table or not. * - * @var bool $multiTable + * @var bool */ $multiTable = false; @@ -209,7 +200,7 @@ class DeleteStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -312,7 +303,7 @@ class DeleteStatement extends Statement ++$list->idx; // Skip 'ORDER BY' $this->order = OrderKeyword::parse($parser, $list); $state = 5; - } elseif ($token->type === Token::TYPE_KEYWORD + } elseif ($token->type === Token::TYPE_KEYWORD && $token->value === 'LIMIT' ) { ++$list->idx; // Skip 'LIMIT' diff --git a/src/Statements/DropStatement.php b/src/Statements/DropStatement.php index b75ab0e..ebcae4a 100644 --- a/src/Statements/DropStatement.php +++ b/src/Statements/DropStatement.php @@ -2,10 +2,8 @@ /** * `DROP` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -15,35 +13,32 @@ use SqlParser\Components\Expression; * `DROP` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class DropStatement extends Statement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'DATABASE' => 1, + 'EVENT' => 1, + 'FUNCTION' => 1, + 'INDEX' => 1, + 'LOGFILE' => 1, + 'PROCEDURE' => 1, + 'SCHEMA' => 1, + 'SERVER' => 1, + 'TABLE' => 1, + 'VIEW' => 1, + 'TABLESPACE' => 1, + 'TRIGGER' => 1, - 'DATABASE' => 1, - 'EVENT' => 1, - 'FUNCTION' => 1, - 'INDEX' => 1, - 'LOGFILE' => 1, - 'PROCEDURE' => 1, - 'SCHEMA' => 1, - 'SERVER' => 1, - 'TABLE' => 1, - 'VIEW' => 1, - 'TABLESPACE' => 1, - 'TRIGGER' => 1, - - 'TEMPORARY' => 2, - 'IF EXISTS' => 3, + 'TEMPORARY' => 2, + 'IF EXISTS' => 3, ); /** @@ -54,12 +49,12 @@ class DropStatement extends Statement * @var array */ public static $CLAUSES = array( - 'DROP' => array('DROP', 2), + 'DROP' => array('DROP', 2), // Used for options. - '_OPTIONS' => array('_OPTIONS', 1), + '_OPTIONS' => array('_OPTIONS', 1), // Used for select expressions. - 'DROP_' => array('DROP', 1), - 'ON' => array('ON', 3), + 'DROP_' => array('DROP', 1), + 'ON' => array('ON', 3), ); /** diff --git a/src/Statements/ExplainStatement.php b/src/Statements/ExplainStatement.php index 78c7acd..482f53f 100644 --- a/src/Statements/ExplainStatement.php +++ b/src/Statements/ExplainStatement.php @@ -2,21 +2,17 @@ /** * `EXPLAIN` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** * `EXPLAIN` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ExplainStatement extends NotImplementedStatement { - } diff --git a/src/Statements/InsertStatement.php b/src/Statements/InsertStatement.php index 9db1a44..6c76df6 100644 --- a/src/Statements/InsertStatement.php +++ b/src/Statements/InsertStatement.php @@ -2,17 +2,14 @@ /** * `INSERT` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; use SqlParser\Token; use SqlParser\TokensList; use SqlParser\Statement; -use SqlParser\Statements\SelectStatement; use SqlParser\Components\IntoKeyword; use SqlParser\Components\Array2d; use SqlParser\Components\OptionsArray; @@ -52,23 +49,21 @@ use SqlParser\Components\SetOperation; * [, col_name=expr] ... ] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class InsertStatement extends Statement { - /** * Options for `INSERT` statements. * * @var array */ public static $OPTIONS = array( - 'LOW_PRIORITY' => 1, - 'DELAYED' => 2, - 'HIGH_PRIORITY' => 3, - 'IGNORE' => 4, + 'LOW_PRIORITY' => 1, + 'DELAYED' => 2, + 'HIGH_PRIORITY' => 3, + 'IGNORE' => 4, ); /** @@ -87,7 +82,7 @@ class InsertStatement extends Statement /** * If SET clause is present - * holds the SetOperation + * holds the SetOperation. * * @var SetOperation[] */ @@ -95,7 +90,7 @@ class InsertStatement extends Statement /** * If SELECT clause is present - * holds the SelectStatement + * holds the SelectStatement. * * @var SelectStatement */ @@ -103,7 +98,7 @@ class InsertStatement extends Statement /** * If ON DUPLICATE KEY UPDATE clause is present - * holds the SetOperation + * holds the SetOperation. * * @var SetOperation[] */ @@ -117,28 +112,24 @@ class InsertStatement extends Statement $ret = 'INSERT ' . $this->options . ' INTO ' . $this->into; - if ($this->values != NULL && count($this->values) > 0) { + if ($this->values != null && count($this->values) > 0) { $ret .= ' VALUES ' . Array2d::build($this->values); - } elseif ($this->set != NULL && count($this->set) > 0) { + } elseif ($this->set != null && count($this->set) > 0) { $ret .= ' SET ' . SetOperation::build($this->set); - } elseif ($this->select != NULL && strlen($this->select) > 0) { + } elseif ($this->select != null && strlen($this->select) > 0) { $ret .= ' ' . $this->select->build(); } - if ($this->onDuplicateSet != NULL && count($this->onDuplicateSet) > 0) { + if ($this->onDuplicateSet != null && count($this->onDuplicateSet) > 0) { $ret .= ' ON DUPLICATE KEY UPDATE ' . SetOperation::build($this->onDuplicateSet); } return $ret; - } - /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -163,14 +154,13 @@ class InsertStatement extends Statement * * 2 -------------------------[ ON DUPLICATE KEY UPDATE ]-----------------------> 3 * - * @var int $state + * @var int */ $state = 0; /** * For keeping track of semi-states on encountering * ON DUPLICATE KEY UPDATE ... - * */ $miniState = 0; @@ -178,7 +168,7 @@ class InsertStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; @@ -242,13 +232,13 @@ class InsertStatement extends Statement $lastCount = $miniState; if ($miniState === 1 && $token->value === 'ON') { - $miniState++; + ++$miniState; } elseif ($miniState === 2 && $token->value === 'DUPLICATE') { - $miniState++; + ++$miniState; } elseif ($miniState === 3 && $token->value === 'KEY') { - $miniState++; + ++$miniState; } elseif ($miniState === 4 && $token->value === 'UPDATE') { - $miniState++; + ++$miniState; } if ($lastCount === $miniState) { diff --git a/src/Statements/MaintenanceStatement.php b/src/Statements/MaintenanceStatement.php index 9097527..5ce55d6 100644 --- a/src/Statements/MaintenanceStatement.php +++ b/src/Statements/MaintenanceStatement.php @@ -2,10 +2,8 @@ /** * Maintenance statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -22,13 +20,11 @@ use SqlParser\Components\OptionsArray; * STMT [some options] tbl_name [, tbl_name] ... [some more options] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class MaintenanceStatement extends Statement { - /** * Tables maintained. * @@ -41,11 +37,9 @@ class MaintenanceStatement extends Statement * * Parses the additional options from 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 void + * @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 */ public function after(Parser $parser, TokensList $list, Token $token) { diff --git a/src/Statements/NotImplementedStatement.php b/src/Statements/NotImplementedStatement.php index 8bdff2d..6796af7 100644 --- a/src/Statements/NotImplementedStatement.php +++ b/src/Statements/NotImplementedStatement.php @@ -2,10 +2,8 @@ /** * Not implemented (yet) statements. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -19,13 +17,11 @@ use SqlParser\TokensList; * The `after` function makes the parser jump straight to the first delimiter. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class NotImplementedStatement extends Statement { - /** * The part of the statement that can't be parsed. * @@ -50,10 +46,8 @@ class NotImplementedStatement extends Statement } /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { diff --git a/src/Statements/OptimizeStatement.php b/src/Statements/OptimizeStatement.php index f688333..7fd7045 100644 --- a/src/Statements/OptimizeStatement.php +++ b/src/Statements/OptimizeStatement.php @@ -2,10 +2,8 @@ /** * `OPTIMIZE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -18,24 +16,21 @@ use SqlParser\Components\Expression; * tbl_name [, tbl_name] ... * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class OptimizeStatement extends Statement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'NO_WRITE_TO_BINLOG' => 2, - 'LOCAL' => 3, + 'NO_WRITE_TO_BINLOG' => 2, + 'LOCAL' => 3, ); /** diff --git a/src/Statements/RenameStatement.php b/src/Statements/RenameStatement.php index 0205f84..e14223c 100644 --- a/src/Statements/RenameStatement.php +++ b/src/Statements/RenameStatement.php @@ -2,10 +2,8 @@ /** * `RENAME` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -21,13 +19,11 @@ use SqlParser\Components\RenameOperation; * [, tbl_name2 TO new_tbl_name2] ... * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class RenameStatement extends Statement { - /** * The old and new names of the tables. * @@ -40,11 +36,9 @@ class RenameStatement extends Statement * * 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 + * @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 */ public function before(Parser $parser, TokensList $list, Token $token) { diff --git a/src/Statements/RepairStatement.php b/src/Statements/RepairStatement.php index f98fcc6..f4ce53d 100644 --- a/src/Statements/RepairStatement.php +++ b/src/Statements/RepairStatement.php @@ -2,10 +2,8 @@ /** * `REPAIR` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** @@ -16,27 +14,24 @@ namespace SqlParser\Statements; * [QUICK] [EXTENDED] [USE_FRM] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class RepairStatement extends MaintenanceStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'NO_WRITE_TO_BINLOG' => 2, - 'LOCAL' => 3, + 'NO_WRITE_TO_BINLOG' => 2, + 'LOCAL' => 3, - 'QUICK' => 4, - 'EXTENDED' => 5, - 'USE_FRM' => 6, + 'QUICK' => 4, + 'EXTENDED' => 5, + 'USE_FRM' => 6, ); } diff --git a/src/Statements/ReplaceStatement.php b/src/Statements/ReplaceStatement.php index 0e85507..0b4af13 100644 --- a/src/Statements/ReplaceStatement.php +++ b/src/Statements/ReplaceStatement.php @@ -2,17 +2,14 @@ /** * `REPLACE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; use SqlParser\Token; use SqlParser\TokensList; use SqlParser\Statement; -use SqlParser\Statements\SelectStatement; use SqlParser\Components\Array2d; use SqlParser\Components\IntoKeyword; use SqlParser\Components\OptionsArray; @@ -40,21 +37,19 @@ use SqlParser\Components\SetOperation; * SELECT ... * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ReplaceStatement extends Statement { - /** * Options for `REPLACE` statements and their slot ID. * * @var array */ public static $OPTIONS = array( - 'LOW_PRIORITY' => 1, - 'DELAYED' => 1, + 'LOW_PRIORITY' => 1, + 'DELAYED' => 1, ); /** @@ -73,7 +68,7 @@ class ReplaceStatement extends Statement /** * If SET clause is present - * holds the SetOperation + * holds the SetOperation. * * @var SetOperation[] */ @@ -81,7 +76,7 @@ class ReplaceStatement extends Statement /** * If SELECT clause is present - * holds the SelectStatement + * holds the SelectStatement. * * @var SelectStatement */ @@ -95,23 +90,20 @@ class ReplaceStatement extends Statement $ret = 'REPLACE ' . $this->options . ' INTO ' . $this->into; - if ($this->values != NULL && count($this->values) > 0) { + if ($this->values != null && count($this->values) > 0) { $ret .= ' VALUES ' . Array2d::build($this->values); - } elseif ($this->set != NULL && count($this->set) > 0) { + } elseif ($this->set != null && count($this->set) > 0) { $ret .= ' SET ' . SetOperation::build($this->set); - } elseif ($this->select != NULL && strlen($this->select) > 0) { + } elseif ($this->select != null && strlen($this->select) > 0) { $ret .= ' ' . $this->select->build(); } return $ret; } - /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -136,7 +128,7 @@ class ReplaceStatement extends Statement * * 1 -------------------------[ VALUES/VALUE/SET/SELECT ]-----------------------> 2 * - * @var int $state + * @var int */ $state = 0; @@ -144,7 +136,7 @@ class ReplaceStatement extends Statement /** * Token parsed at this moment. * - * @var Token $token + * @var Token */ $token = $list->tokens[$list->idx]; diff --git a/src/Statements/RestoreStatement.php b/src/Statements/RestoreStatement.php index 8500479..720f907 100644 --- a/src/Statements/RestoreStatement.php +++ b/src/Statements/RestoreStatement.php @@ -2,10 +2,8 @@ /** * `RESTORE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** @@ -14,22 +12,19 @@ namespace SqlParser\Statements; * RESTORE TABLE tbl_name [, tbl_name] ... FROM '/path/to/backup/directory' * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class RestoreStatement extends MaintenanceStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( + 'TABLE' => 1, - 'TABLE' => 1, - - 'FROM' => array(2, 'var'), + 'FROM' => array(2, 'var'), ); } diff --git a/src/Statements/SelectStatement.php b/src/Statements/SelectStatement.php index 49ed393..511334c 100644 --- a/src/Statements/SelectStatement.php +++ b/src/Statements/SelectStatement.php @@ -2,10 +2,8 @@ /** * `SELECT` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -47,36 +45,34 @@ use SqlParser\Components\Condition; * [FOR UPDATE | LOCK IN SHARE MODE]] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class SelectStatement extends Statement { - /** * Options for `SELECT` statements and their slot ID. * * @var array */ public static $OPTIONS = array( - 'ALL' => 1, - 'DISTINCT' => 1, - 'DISTINCTROW' => 1, - 'HIGH_PRIORITY' => 2, - 'MAX_STATEMENT_TIME' => array(3, 'var='), - 'STRAIGHT_JOIN' => 4, - 'SQL_SMALL_RESULT' => 5, - 'SQL_BIG_RESULT' => 6, - 'SQL_BUFFER_RESULT' => 7, - 'SQL_CACHE' => 8, - 'SQL_NO_CACHE' => 8, - 'SQL_CALC_FOUND_ROWS' => 9, + 'ALL' => 1, + 'DISTINCT' => 1, + 'DISTINCTROW' => 1, + 'HIGH_PRIORITY' => 2, + 'MAX_STATEMENT_TIME' => array(3, 'var='), + 'STRAIGHT_JOIN' => 4, + 'SQL_SMALL_RESULT' => 5, + 'SQL_BIG_RESULT' => 6, + 'SQL_BUFFER_RESULT' => 7, + 'SQL_CACHE' => 8, + 'SQL_NO_CACHE' => 8, + 'SQL_CALC_FOUND_ROWS' => 9, ); public static $END_OPTIONS = array( - 'FOR UPDATE' => 1, - 'LOCK IN SHARE MODE' => 1 + 'FOR UPDATE' => 1, + 'LOCK IN SHARE MODE' => 1, ); /** @@ -87,36 +83,36 @@ class SelectStatement extends Statement * @var array */ public static $CLAUSES = array( - 'SELECT' => array('SELECT', 2), + 'SELECT' => array('SELECT', 2), // Used for options. - '_OPTIONS' => array('_OPTIONS', 1), + '_OPTIONS' => array('_OPTIONS', 1), // Used for selected expressions. - '_SELECT' => array('SELECT', 1), - 'INTO' => array('INTO', 3), - 'FROM' => array('FROM', 3), - 'PARTITION' => array('PARTITION', 3), - - 'JOIN' => array('JOIN', 1), - 'FULL JOIN' => array('FULL JOIN', 1), - 'INNER JOIN' => array('INNER JOIN', 1), - 'LEFT JOIN' => array('LEFT JOIN', 1), - 'LEFT OUTER JOIN' => array('LEFT OUTER JOIN', 1), - 'RIGHT JOIN' => array('RIGHT JOIN', 1), - 'RIGHT OUTER JOIN' => array('RIGHT OUTER JOIN', 1), - 'NATURAL JOIN' => array('NATURAL JOIN', 1), - 'NATURAL LEFT JOIN' => array('NATURAL LEFT JOIN', 1), - 'NATURAL RIGHT JOIN' => array('NATURAL RIGHT JOIN', 1), - 'NATURAL LEFT OUTER JOIN' => array('NATURAL LEFT OUTER JOIN', 1), - 'NATURAL RIGHT OUTER JOIN' => array('NATURAL RIGHT JOIN', 1), - - 'WHERE' => array('WHERE', 3), - 'GROUP BY' => array('GROUP BY', 3), - 'HAVING' => array('HAVING', 3), - 'ORDER BY' => array('ORDER BY', 3), - 'LIMIT' => array('LIMIT', 3), - 'PROCEDURE' => array('PROCEDURE', 3), - 'UNION' => array('UNION', 1), - '_END_OPTIONS' => array('_END_OPTIONS', 1) + '_SELECT' => array('SELECT', 1), + 'INTO' => array('INTO', 3), + 'FROM' => array('FROM', 3), + 'PARTITION' => array('PARTITION', 3), + + 'JOIN' => array('JOIN', 1), + 'FULL JOIN' => array('FULL JOIN', 1), + 'INNER JOIN' => array('INNER JOIN', 1), + 'LEFT JOIN' => array('LEFT JOIN', 1), + 'LEFT OUTER JOIN' => array('LEFT OUTER JOIN', 1), + 'RIGHT JOIN' => array('RIGHT JOIN', 1), + 'RIGHT OUTER JOIN' => array('RIGHT OUTER JOIN', 1), + 'NATURAL JOIN' => array('NATURAL JOIN', 1), + 'NATURAL LEFT JOIN' => array('NATURAL LEFT JOIN', 1), + 'NATURAL RIGHT JOIN' => array('NATURAL RIGHT JOIN', 1), + 'NATURAL LEFT OUTER JOIN' => array('NATURAL LEFT OUTER JOIN', 1), + 'NATURAL RIGHT OUTER JOIN' => array('NATURAL RIGHT JOIN', 1), + + 'WHERE' => array('WHERE', 3), + 'GROUP BY' => array('GROUP BY', 3), + 'HAVING' => array('HAVING', 3), + 'ORDER BY' => array('ORDER BY', 3), + 'LIMIT' => array('LIMIT', 3), + 'PROCEDURE' => array('PROCEDURE', 3), + 'UNION' => array('UNION', 1), + '_END_OPTIONS' => array('_END_OPTIONS', 1), // These are available only when `UNION` is present. // 'ORDER BY' => array('ORDER BY', 3), // 'LIMIT' => array('LIMIT', 3), @@ -231,8 +227,10 @@ class SelectStatement extends Statement unset($clauses['LIMIT']); $clauses['ORDER BY'] = array('ORDER BY', 3); $clauses['LIMIT'] = array('LIMIT', 3); + return $clauses; } + return static::$CLAUSES; } } diff --git a/src/Statements/SetStatement.php b/src/Statements/SetStatement.php index a9ff498..5329b07 100644 --- a/src/Statements/SetStatement.php +++ b/src/Statements/SetStatement.php @@ -2,10 +2,8 @@ /** * `SET` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -16,13 +14,11 @@ use SqlParser\Components\OptionsArray; * `SET` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class SetStatement extends Statement { - /** * The clauses of this statement, in order. * @@ -31,23 +27,23 @@ class SetStatement extends Statement * @var array */ public static $CLAUSES = array( - 'SET' => array('SET', 3), + 'SET' => array('SET', 3), ); /** - * Possible exceptions in SET statment + * Possible exceptions in SET statment. * * @var array */ public static $OPTIONS = array( - 'CHARSET' => array(3, 'var'), - 'CHARACTER SET' => array(3, 'var'), - 'NAMES' => array(3, 'var'), - 'PASSWORD' => array(3, 'expr'), + 'CHARSET' => array(3, 'var'), + 'CHARACTER SET' => array(3, 'var'), + 'NAMES' => array(3, 'var'), + 'PASSWORD' => array(3, 'expr'), ); /** - * Options used in current statement + * Options used in current statement. * * @var OptionsArray[] */ diff --git a/src/Statements/ShowStatement.php b/src/Statements/ShowStatement.php index ecca348..be233a7 100644 --- a/src/Statements/ShowStatement.php +++ b/src/Statements/ShowStatement.php @@ -2,69 +2,65 @@ /** * `SHOW` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; /** * `SHOW` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ShowStatement extends NotImplementedStatement { - /** * Options of this statement. * * @var array */ public static $OPTIONS = array( - 'CREATE' => 1, - 'AUTHORS' => 2, - 'BINARY' => 2, - 'BINLOG' => 2, - 'CHARACTER' => 2, - 'CODE' => 2, - 'COLLATION' => 2, - 'COLUMNS' => 2, - 'CONTRIBUTORS' => 2, - 'DATABASE' => 2, - 'DATABASES' => 2, - 'ENGINE' => 2, - 'ENGINES' => 2, - 'ERRORS' => 2, - 'EVENT' => 2, - 'EVENTS' => 2, - 'FUNCTION' => 2, - 'GRANTS' => 2, - 'HOSTS' => 2, - 'INDEX' => 2, - 'INNODB' => 2, - 'LOGS' => 2, - 'MASTER' => 2, - 'OPEN' => 2, - 'PLUGINS' => 2, - 'PRIVILEGES' => 2, - 'PROCEDURE' => 2, - 'PROCESSLIST' => 2, - 'PROFILE' => 2, - 'PROFILES' => 2, - 'SCHEDULER' => 2, - 'SET' => 2, - 'SLAVE' => 2, - 'STATUS' => 2, - 'TABLE' => 2, - 'TABLES' => 2, - 'TRIGGER' => 2, - 'TRIGGERS' => 2, - 'VARIABLES' => 2, - 'VIEW' => 2, - 'WARNINGS' => 2, + 'CREATE' => 1, + 'AUTHORS' => 2, + 'BINARY' => 2, + 'BINLOG' => 2, + 'CHARACTER' => 2, + 'CODE' => 2, + 'COLLATION' => 2, + 'COLUMNS' => 2, + 'CONTRIBUTORS' => 2, + 'DATABASE' => 2, + 'DATABASES' => 2, + 'ENGINE' => 2, + 'ENGINES' => 2, + 'ERRORS' => 2, + 'EVENT' => 2, + 'EVENTS' => 2, + 'FUNCTION' => 2, + 'GRANTS' => 2, + 'HOSTS' => 2, + 'INDEX' => 2, + 'INNODB' => 2, + 'LOGS' => 2, + 'MASTER' => 2, + 'OPEN' => 2, + 'PLUGINS' => 2, + 'PRIVILEGES' => 2, + 'PROCEDURE' => 2, + 'PROCESSLIST' => 2, + 'PROFILE' => 2, + 'PROFILES' => 2, + 'SCHEDULER' => 2, + 'SET' => 2, + 'SLAVE' => 2, + 'STATUS' => 2, + 'TABLE' => 2, + 'TABLES' => 2, + 'TRIGGER' => 2, + 'TRIGGERS' => 2, + 'VARIABLES' => 2, + 'VIEW' => 2, + 'WARNINGS' => 2, ); } diff --git a/src/Statements/TransactionStatement.php b/src/Statements/TransactionStatement.php index 2d355ea..9e11daa 100644 --- a/src/Statements/TransactionStatement.php +++ b/src/Statements/TransactionStatement.php @@ -2,10 +2,8 @@ /** * Transaction statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Parser; @@ -17,26 +15,24 @@ use SqlParser\Components\OptionsArray; * Transaction statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class TransactionStatement extends Statement { - /** - * START TRANSACTION and BEGIN + * START TRANSACTION and BEGIN. * * @var int */ - const TYPE_BEGIN = 1; + const TYPE_BEGIN = 1; /** - * COMMIT and ROLLBACK + * COMMIT and ROLLBACK. * * @var int */ - const TYPE_END = 2; + const TYPE_END = 2; /** * The type of this query. @@ -65,23 +61,21 @@ class TransactionStatement extends Statement * @var array */ public static $OPTIONS = array( - 'START TRANSACTION' => 1, - 'BEGIN' => 1, - 'COMMIT' => 1, - 'ROLLBACK' => 1, - 'WITH CONSISTENT SNAPSHOT' => 2, - 'WORK' => 2, - 'AND NO CHAIN' => 3, - 'AND CHAIN' => 3, - 'RELEASE' => 4, - 'NO RELEASE' => 4, + 'START TRANSACTION' => 1, + 'BEGIN' => 1, + 'COMMIT' => 1, + 'ROLLBACK' => 1, + 'WITH CONSISTENT SNAPSHOT' => 2, + 'WORK' => 2, + 'AND NO CHAIN' => 3, + 'AND CHAIN' => 3, + 'RELEASE' => 4, + 'NO RELEASE' => 4, ); /** - * @param Parser $parser The instance that requests parsing. - * @param TokensList $list The list of tokens to be parsed. - * - * @return void + * @param Parser $parser the instance that requests parsing + * @param TokensList $list the list of tokens to be parsed */ public function parse(Parser $parser, TokensList $list) { @@ -91,11 +85,11 @@ class TransactionStatement extends Statement if (($this->options->has('START TRANSACTION')) || ($this->options->has('BEGIN')) ) { - $this->type = TransactionStatement::TYPE_BEGIN; + $this->type = self::TYPE_BEGIN; } elseif (($this->options->has('COMMIT')) || ($this->options->has('ROLLBACK')) ) { - $this->type = TransactionStatement::TYPE_END; + $this->type = self::TYPE_END; } } @@ -105,15 +99,16 @@ class TransactionStatement extends Statement public function build() { $ret = OptionsArray::build($this->options); - if ($this->type === TransactionStatement::TYPE_BEGIN) { + if ($this->type === self::TYPE_BEGIN) { foreach ($this->statements as $statement) { - /** + /* * @var SelectStatement $statement */ $ret .= ';' . $statement->build(); } $ret .= ';' . $this->end->build(); } + return $ret; } } diff --git a/src/Statements/TruncateStatement.php b/src/Statements/TruncateStatement.php index 6f9d303..20ad4b8 100644 --- a/src/Statements/TruncateStatement.php +++ b/src/Statements/TruncateStatement.php @@ -2,10 +2,8 @@ /** * `TRUNCATE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -15,20 +13,18 @@ use SqlParser\Components\Expression; * `TRUNCATE` statement. * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class TruncateStatement extends Statement { - /** * Options for `TRUNCATE` statements. * * @var array */ public static $OPTIONS = array( - 'TABLE' => 1, + 'TABLE' => 1, ); /** diff --git a/src/Statements/UpdateStatement.php b/src/Statements/UpdateStatement.php index 011b955..796a722 100644 --- a/src/Statements/UpdateStatement.php +++ b/src/Statements/UpdateStatement.php @@ -2,10 +2,8 @@ /** * `UPDATE` statement. - * - * @package SqlParser - * @subpackage Statements */ + namespace SqlParser\Statements; use SqlParser\Statement; @@ -31,21 +29,19 @@ use SqlParser\Components\Condition; * [WHERE where_condition] * * @category Statements - * @package SqlParser - * @subpackage Statements + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class UpdateStatement extends Statement { - /** * Options for `UPDATE` statements and their slot ID. * * @var array */ public static $OPTIONS = array( - 'LOW_PRIORITY' => 1, - 'IGNORE' => 2, + 'LOW_PRIORITY' => 1, + 'IGNORE' => 2, ); /** @@ -56,15 +52,15 @@ class UpdateStatement extends Statement * @var array */ public static $CLAUSES = array( - 'UPDATE' => array('UPDATE', 2), + 'UPDATE' => array('UPDATE', 2), // Used for options. - '_OPTIONS' => array('_OPTIONS', 1), + '_OPTIONS' => array('_OPTIONS', 1), // Used for updated tables. - '_UPDATE' => array('UPDATE', 1), - 'SET' => array('SET', 3), - 'WHERE' => array('WHERE', 3), - 'ORDER BY' => array('ORDER BY', 3), - 'LIMIT' => array('LIMIT', 3), + '_UPDATE' => array('UPDATE', 1), + 'SET' => array('SET', 3), + 'WHERE' => array('WHERE', 3), + 'ORDER BY' => array('ORDER BY', 3), + 'LIMIT' => array('LIMIT', 3), ); /** diff --git a/src/Token.php b/src/Token.php index e5001ef..d847224 100644 --- a/src/Token.php +++ b/src/Token.php @@ -4,9 +4,8 @@ * Defines a token along with a set of types and flags and utility functions. * * An array of tokens will result after parsing the query. - * - * @package SqlParser */ + namespace SqlParser; /** @@ -14,12 +13,11 @@ namespace SqlParser; * categorization for the purpose of parsing. * * @category Tokens - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Token { - // Types of tokens (a vague description of a token's purpose). /** @@ -29,14 +27,14 @@ class Token * * @var int */ - const TYPE_NONE = 0; + const TYPE_NONE = 0; /** * SQL specific keywords: SELECT, UPDATE, INSERT, etc. * * @var int */ - const TYPE_KEYWORD = 1; + const TYPE_KEYWORD = 1; /** * Any type of legal operator. @@ -50,14 +48,14 @@ class Token * * @var int */ - const TYPE_OPERATOR = 2; + const TYPE_OPERATOR = 2; /** * Spaces, tabs, new lines, etc. * * @var int */ - const TYPE_WHITESPACE = 3; + const TYPE_WHITESPACE = 3; /** * Any type of legal comment. @@ -79,21 +77,21 @@ class Token * * @var int */ - const TYPE_COMMENT = 4; + const TYPE_COMMENT = 4; /** * Boolean values: true or false. * * @var int */ - const TYPE_BOOL = 5; + const TYPE_BOOL = 5; /** * Numbers: 4, 0x8, 15.16, 23e42, etc. * * @var int */ - const TYPE_NUMBER = 6; + const TYPE_NUMBER = 6; /** * Literal strings: 'string', "test". @@ -101,15 +99,15 @@ class Token * * @var int */ - const TYPE_STRING = 7; + const TYPE_STRING = 7; /** * Database, table names, variables, etc. - * For example: ```SELECT `foo`, `bar` FROM `database`.`table`;``` + * For example: ```SELECT `foo`, `bar` FROM `database`.`table`;```. * * @var int */ - const TYPE_SYMBOL = 8; + const TYPE_SYMBOL = 8; /** * Delimits an unknown string. @@ -117,7 +115,7 @@ class Token * * @var int */ - const TYPE_DELIMITER = 9; + const TYPE_DELIMITER = 9; /** * Labels in LOOP statement, ITERATE statement etc. @@ -125,50 +123,50 @@ class Token * begin_label: BEGIN [statement_list] END [end_label] * begin_label: LOOP [statement_list] END LOOP [end_label] * begin_label: REPEAT [statement_list] ... END REPEAT [end_label] - * begin_label: WHILE ... DO [statement_list] END WHILE [end_label] + * begin_label: WHILE ... DO [statement_list] END WHILE [end_label]. * * @var int */ - const TYPE_LABEL = 10; + const TYPE_LABEL = 10; // Flags that describe the tokens in more detail. // All keywords must have flag 1 so `Context::isKeyword` method doesn't // require strict comparison. - const FLAG_KEYWORD_RESERVED = 2; - const FLAG_KEYWORD_COMPOSED = 4; - const FLAG_KEYWORD_DATA_TYPE = 8; - const FLAG_KEYWORD_KEY = 16; - const FLAG_KEYWORD_FUNCTION = 32; + const FLAG_KEYWORD_RESERVED = 2; + const FLAG_KEYWORD_COMPOSED = 4; + const FLAG_KEYWORD_DATA_TYPE = 8; + const FLAG_KEYWORD_KEY = 16; + const FLAG_KEYWORD_FUNCTION = 32; // Numbers related flags. - const FLAG_NUMBER_HEX = 1; - const FLAG_NUMBER_FLOAT = 2; - const FLAG_NUMBER_APPROXIMATE = 4; - const FLAG_NUMBER_NEGATIVE = 8; - const FLAG_NUMBER_BINARY = 16; + const FLAG_NUMBER_HEX = 1; + const FLAG_NUMBER_FLOAT = 2; + const FLAG_NUMBER_APPROXIMATE = 4; + const FLAG_NUMBER_NEGATIVE = 8; + const FLAG_NUMBER_BINARY = 16; // Strings related flags. - const FLAG_STRING_SINGLE_QUOTES = 1; - const FLAG_STRING_DOUBLE_QUOTES = 2; + const FLAG_STRING_SINGLE_QUOTES = 1; + const FLAG_STRING_DOUBLE_QUOTES = 2; // Comments related flags. - const FLAG_COMMENT_BASH = 1; - const FLAG_COMMENT_C = 2; - const FLAG_COMMENT_SQL = 4; - const FLAG_COMMENT_MYSQL_CMD = 8; + const FLAG_COMMENT_BASH = 1; + const FLAG_COMMENT_C = 2; + const FLAG_COMMENT_SQL = 4; + const FLAG_COMMENT_MYSQL_CMD = 8; // Operators related flags. - const FLAG_OPERATOR_ARITHMETIC = 1; - const FLAG_OPERATOR_LOGICAL = 2; - const FLAG_OPERATOR_BITWISE = 4; - const FLAG_OPERATOR_ASSIGNMENT = 8; - const FLAG_OPERATOR_SQL = 16; + const FLAG_OPERATOR_ARITHMETIC = 1; + const FLAG_OPERATOR_LOGICAL = 2; + const FLAG_OPERATOR_BITWISE = 4; + const FLAG_OPERATOR_ASSIGNMENT = 8; + const FLAG_OPERATOR_SQL = 16; // Symbols related flags. - const FLAG_SYMBOL_VARIABLE = 1; - const FLAG_SYMBOL_BACKTICK = 2; - const FLAG_SYMBOL_USER = 4; - const FLAG_SYMBOL_SYSTEM = 8; + const FLAG_SYMBOL_VARIABLE = 1; + const FLAG_SYMBOL_BACKTICK = 2; + const FLAG_SYMBOL_USER = 4; + const FLAG_SYMBOL_SYSTEM = 8; /** * The token it its raw string representation. @@ -178,7 +176,7 @@ class Token public $token; /** - * The value this token contains (i.e. token after some evaluation) + * The value this token contains (i.e. token after some evaluation). * * @var mixed */ @@ -208,9 +206,9 @@ class Token /** * Constructor. * - * @param string $token The value of the token. - * @param int $type The type of the token. - * @param int $flags The flags of the token. + * @param string $token the value of the token + * @param int $type the type of the token + * @param int $flags the flags of the token */ public function __construct($token, $type = 0, $flags = 0) { @@ -230,40 +228,43 @@ class Token public function extract() { switch ($this->type) { - case Token::TYPE_KEYWORD: - if (!($this->flags & Token::FLAG_KEYWORD_RESERVED)) { + case self::TYPE_KEYWORD: + if (!($this->flags & self::FLAG_KEYWORD_RESERVED)) { // Unreserved keywords should stay the way they are because they // might represent field names. return $this->token; } + return strtoupper($this->token); - case Token::TYPE_WHITESPACE: + case self::TYPE_WHITESPACE: return ' '; - case Token::TYPE_BOOL: + case self::TYPE_BOOL: return strtoupper($this->token) === 'TRUE'; - case Token::TYPE_NUMBER: + case self::TYPE_NUMBER: $ret = str_replace('--', '', $this->token); // e.g. ---42 === -42 - if ($this->flags & Token::FLAG_NUMBER_HEX) { - if ($this->flags & Token::FLAG_NUMBER_NEGATIVE) { + if ($this->flags & self::FLAG_NUMBER_HEX) { + if ($this->flags & self::FLAG_NUMBER_NEGATIVE) { $ret = str_replace('-', '', $this->token); sscanf($ret, '%x', $ret); $ret = -$ret; } else { sscanf($ret, '%x', $ret); } - } elseif (($this->flags & Token::FLAG_NUMBER_APPROXIMATE) - || ($this->flags & Token::FLAG_NUMBER_FLOAT) + } elseif (($this->flags & self::FLAG_NUMBER_APPROXIMATE) + || ($this->flags & self::FLAG_NUMBER_FLOAT) ) { sscanf($ret, '%f', $ret); } else { sscanf($ret, '%d', $ret); } + return $ret; - case Token::TYPE_STRING: + case self::TYPE_STRING: $quote = $this->token[0]; $str = str_replace($quote . $quote, $quote, $this->token); + return mb_substr($str, 1, -1, 'UTF-8'); // trims quotes - case Token::TYPE_SYMBOL: + case self::TYPE_SYMBOL: $str = $this->token; if ((isset($str[0])) && ($str[0] === '@')) { // `mb_strlen($str)` must be used instead of `null` because @@ -282,8 +283,10 @@ class Token $str = str_replace($quote . $quote, $quote, $str); $str = mb_substr($str, 1, -1, 'UTF-8'); } + return $str; } + return $this->token; } diff --git a/src/TokensList.php b/src/TokensList.php index c0c2c89..512fcb5 100644 --- a/src/TokensList.php +++ b/src/TokensList.php @@ -2,21 +2,19 @@ /** * Defines an array of tokens and utility functions to iterate through it. - * - * @package SqlParser */ + namespace SqlParser; /** * A structure representing a list of tokens. * * @category Tokens - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class TokensList implements \ArrayAccess { - /** * The array of tokens. * @@ -41,8 +39,8 @@ class TokensList implements \ArrayAccess /** * Constructor. * - * @param array $tokens The initial array of tokens. - * @param int $count The count of tokens in the initial array. + * @param array $tokens the initial array of tokens + * @param int $count the count of tokens in the initial array */ public function __construct(array $tokens = array(), $count = -1) { @@ -57,7 +55,7 @@ class TokensList implements \ArrayAccess /** * Builds an array of tokens by merging their raw value. * - * @param string|Token[]|TokensList $list The tokens to be built. + * @param string|Token[]|TokensList $list the tokens to be built * * @return string */ @@ -67,7 +65,7 @@ class TokensList implements \ArrayAccess return $list; } - if ($list instanceof TokensList) { + if ($list instanceof self) { $list = $list->tokens; } @@ -77,15 +75,14 @@ class TokensList implements \ArrayAccess $ret .= $tok->token; } } + return $ret; } /** * Adds a new token. * - * @param Token $token Token to be added in list. - * - * @return void + * @param Token $token token to be added in list */ public function add(Token $token) { @@ -107,13 +104,14 @@ class TokensList implements \ArrayAccess return $this->tokens[$this->idx++]; } } + return null; } /** * Gets the next token. * - * @param int $type The type. + * @param int $type the type * * @return Token */ @@ -124,14 +122,15 @@ class TokensList implements \ArrayAccess return $this->tokens[$this->idx++]; } } + return null; } /** * Gets the next token. * - * @param int $type The type of the token. - * @param string $value The value of the token. + * @param int $type the type of the token + * @param string $value the value of the token * * @return Token */ @@ -144,16 +143,15 @@ class TokensList implements \ArrayAccess return $this->tokens[$this->idx++]; } } + return null; } /** * Sets an value inside the container. * - * @param int $offset The offset to be set. - * @param Token $value The token to be saved. - * - * @return void + * @param int $offset the offset to be set + * @param Token $value the token to be saved */ public function offsetSet($offset, $value) { @@ -167,7 +165,7 @@ class TokensList implements \ArrayAccess /** * Gets a value from the container. * - * @param int $offset The offset to be returned. + * @param int $offset the offset to be returned * * @return Token */ @@ -179,7 +177,7 @@ class TokensList implements \ArrayAccess /** * Checks if an offset was previously set. * - * @param int $offset The offset to be checked. + * @param int $offset the offset to be checked * * @return bool */ @@ -191,9 +189,7 @@ class TokensList implements \ArrayAccess /** * Unsets the value of an offset. * - * @param int $offset The offset to be unset. - * - * @return void + * @param int $offset the offset to be unset */ public function offsetUnset($offset) { diff --git a/src/UtfString.php b/src/UtfString.php index 66ec4e4..91aa19e 100644 --- a/src/UtfString.php +++ b/src/UtfString.php @@ -9,9 +9,8 @@ * * Because the lexer relies on the subscript operator this class had to be * implemented. - * - * @package SqlParser */ + namespace SqlParser; /** @@ -20,12 +19,11 @@ namespace SqlParser; * In this library, this class should be used to parse UTF-8 queries. * * @category Misc - * @package SqlParser + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class UtfString implements \ArrayAccess { - /** * The raw, multi-byte string. * @@ -69,7 +67,7 @@ class UtfString implements \ArrayAccess /** * Constructor. * - * @param string $str The string. + * @param string $str the string */ public function __construct($str) { @@ -83,7 +81,7 @@ class UtfString implements \ArrayAccess /** * Checks if the given offset exists. * - * @param int $offset The offset to be checked. + * @param int $offset the offset to be checked * * @return bool */ @@ -95,7 +93,7 @@ class UtfString implements \ArrayAccess /** * Gets the character at given offset. * - * @param int $offset The offset to be returned. + * @param int $offset the offset to be returned * * @return string */ @@ -136,12 +134,10 @@ class UtfString implements \ArrayAccess /** * Sets the value of a character. * - * @param int $offset The offset to be set. - * @param string $value The value to be set. + * @param int $offset the offset to be set + * @param string $value the value to be set * - * @throws \Exception Not implemented. - * - * @return void + * @throws \Exception not implemented */ public function offsetSet($offset, $value) { @@ -151,11 +147,9 @@ class UtfString implements \ArrayAccess /** * Unsets an index. * - * @param int $offset The value to be unset. - * - * @throws \Exception Not implemented. + * @param int $offset the value to be unset * - * @return void + * @throws \Exception not implemented */ public function offsetUnset($offset) { @@ -169,7 +163,7 @@ class UtfString implements \ArrayAccess * However, this implementation supports UTF-8 characters containing up to 6 * bytes. * - * @param string $byte The byte to be analyzed. + * @param string $byte the byte to be analyzed * * @see https://tools.ietf.org/html/rfc3629 * @@ -189,6 +183,7 @@ class UtfString implements \ArrayAccess } elseif ($byte < 252) { return 5; // unofficial } + return 6; // unofficial } diff --git a/src/Utils/BufferedQuery.php b/src/Utils/BufferedQuery.php index b1ef46d..045513d 100644 --- a/src/Utils/BufferedQuery.php +++ b/src/Utils/BufferedQuery.php @@ -2,10 +2,8 @@ /** * Buffered query utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Context; @@ -20,26 +18,24 @@ use SqlParser\Context; * All comments are skipped, with one exception: MySQL commands inside `/*!`. * * @category Lexer - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class BufferedQuery { - // Constants that describe the current status of the parser. // A string is being parsed. - const STATUS_STRING = 16; // 0001 0000 - const STATUS_STRING_SINGLE_QUOTES = 17; // 0001 0001 - const STATUS_STRING_DOUBLE_QUOTES = 18; // 0001 0010 - const STATUS_STRING_BACKTICK = 20; // 0001 0100 + const STATUS_STRING = 16; // 0001 0000 + const STATUS_STRING_SINGLE_QUOTES = 17; // 0001 0001 + const STATUS_STRING_DOUBLE_QUOTES = 18; // 0001 0010 + const STATUS_STRING_BACKTICK = 20; // 0001 0100 // A comment is being parsed. - const STATUS_COMMENT = 32; // 0010 0000 - const STATUS_COMMENT_BASH = 33; // 0010 0001 - const STATUS_COMMENT_C = 34; // 0010 0010 - const STATUS_COMMENT_SQL = 36; // 0010 0100 + const STATUS_COMMENT = 32; // 0010 0000 + const STATUS_COMMENT_BASH = 33; // 0010 0001 + const STATUS_COMMENT_C = 34; // 0010 0010 + const STATUS_COMMENT_SQL = 36; // 0010 0100 /** * The query that is being processed. @@ -88,30 +84,29 @@ class BufferedQuery /** * Constructor. * - * @param string $query The query to be parsed. - * @param array $options The options of this parser. + * @param string $query the query to be parsed + * @param array $options the options of this parser */ public function __construct($query = '', array $options = array()) { // Merges specified options with defaults. $this->options = array_merge( array( - - /** + /* * The starting delimiter. * * @var string */ 'delimiter' => ';', - /** + /* * Whether `DELIMITER` statements should be parsed. * * @var bool */ 'parse_delimiter' => false, - /** + /* * Whether a delimiter should be added at the end of the * statement. * @@ -142,7 +137,7 @@ class BufferedQuery /** * Extracts a statement from the buffer. * - * @param bool $end Whether the end of the buffer was reached. + * @param bool $end whether the end of the buffer was reached * * @return string */ @@ -166,7 +161,7 @@ class BufferedQuery /** * The length of the buffer. * - * @var int $len + * @var int */ $len = strlen($this->query); @@ -186,12 +181,12 @@ class BufferedQuery * Those extra characters are required only if there is more data * expected (the end of the buffer was not reached). * - * @var int $loopLen + * @var int */ $loopLen = $end ? $len : $len - 16; for (; $i < $loopLen; ++$i) { - /** + /* * Handling backslash. * * Even if the next character is a special character that should be @@ -294,7 +289,7 @@ class BufferedQuery * statement. This is the reason for the last condition. */ if (($i + 9 < $len) - && (($this->query[$i ] === 'D') || ($this->query[$i ] === 'd')) + && (($this->query[$i] === 'D') || ($this->query[$i] === 'd')) && (($this->query[$i + 1] === 'E') || ($this->query[$i + 1] === 'e')) && (($this->query[$i + 2] === 'L') || ($this->query[$i + 2] === 'l')) && (($this->query[$i + 3] === 'I') || ($this->query[$i + 3] === 'i')) @@ -353,6 +348,7 @@ class BufferedQuery // Incomplete statement. Reverting $i = $iBak; + return false; } diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php index 9ec7b85..a5ed0be 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -1,22 +1,19 @@ <?php /** - * CLI interface - * - * @package SqlParser - * @subpackage Utils + * CLI interface. */ + namespace SqlParser\Utils; use SqlParser\Parser; use SqlParser\Lexer; /** - * CLI interface + * CLI interface. * * @category Exceptions - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class CLI @@ -51,13 +48,15 @@ class CLI return false; } $this->mergeLongOpts($params, $longopts); - if (! isset($params['f'])) { + if (!isset($params['f'])) { $params['f'] = 'cli'; } - if (! in_array($params['f'], array('html', 'cli', 'text'))) { + if (!in_array($params['f'], array('html', 'cli', 'text'))) { echo "ERROR: Invalid value for format!\n"; + return false; } + return $params; } @@ -69,6 +68,7 @@ class CLI } if (isset($params['h'])) { $this->usageHighlight(); + return 0; } if (isset($params['q'])) { @@ -76,10 +76,12 @@ class CLI $params['q'], array('type' => $params['f']) ); echo "\n"; + return 0; } echo "ERROR: Missing parameters!\n"; $this->usageHighlight(); + return 1; } @@ -95,6 +97,7 @@ class CLI 'hq:', $longopts ); $this->mergeLongOpts($params, $longopts); + return $params; } @@ -106,6 +109,7 @@ class CLI } if (isset($params['h'])) { $this->usageLint(); + return 0; } if (isset($params['q'])) { @@ -118,10 +122,12 @@ class CLI $output = Error::format($errors); echo implode("\n", $output); echo "\n"; + return 10; } echo "ERROR: Missing parameters!\n"; $this->usageLint(); + return 1; } } diff --git a/src/Utils/Error.php b/src/Utils/Error.php index 2e942c4..568af0a 100644 --- a/src/Utils/Error.php +++ b/src/Utils/Error.php @@ -2,10 +2,8 @@ /** * Error related utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Lexer; @@ -15,24 +13,22 @@ use SqlParser\Parser; * Error related utilities. * * @category Exceptions - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Error { - /** * Gets the errors of a lexer and a parser. * - * @param array $objs Objects from where the errors will be extracted. + * @param array $objs objects from where the errors will be extracted * * @return array Each element of the array represents an error. - * `$err[0]` holds the error message. - * `$err[1]` holds the error code. - * `$err[2]` holds the string that caused the issue. - * `$err[3]` holds the position of the string. - * (i.e. `array($msg, $code, $str, $pos)`) + * `$err[0]` holds the error message. + * `$err[1]` holds the error code. + * `$err[2]` holds the string that caused the issue. + * `$err[3]` holds the position of the string. + * (i.e. `array($msg, $code, $str, $pos)`) */ public static function get($objs) { @@ -45,7 +41,7 @@ class Error $err->getMessage(), $err->getCode(), $err->ch, - $err->pos + $err->pos, ); } } elseif ($obj instanceof Parser) { @@ -54,7 +50,7 @@ class Error $err->getMessage(), $err->getCode(), $err->token->token, - $err->token->position + $err->token->position, ); } } @@ -64,16 +60,17 @@ class Error } /** - * Formats the specified errors + * Formats the specified errors. + * + * @param array $errors the errors to be formatted + * @param string $format The format of an error. + * '$1$d' is replaced by the position of this error. + * '$2$s' is replaced by the error message. + * '$3$d' is replaced by the error code. + * '$4$s' is replaced by the string that caused the + * issue. + * '$5$d' is replaced by the position of the string. * - * @param array $errors The errors to be formatted. - * @param string $format The format of an error. - * '$1$d' is replaced by the position of this error. - * '$2$s' is replaced by the error message. - * '$3$d' is replaced by the error code. - * '$4$s' is replaced by the string that caused the - * issue. - * '$5$d' is replaced by the position of the string. * @return array */ public static function format( diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index e459b60..ec23ba9 100644 --- a/src/Utils/Formatter.php +++ b/src/Utils/Formatter.php @@ -2,10 +2,8 @@ /** * Utilities that are used for formatting queries. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Lexer; @@ -17,13 +15,11 @@ use SqlParser\TokensList; * Utilities that are used for formatting queries. * * @category Misc - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Formatter { - /** * The formatting options. * @@ -39,19 +35,19 @@ class Formatter * @var array */ public static $INLINE_CLAUSES = array( - 'CREATE' => true, - 'LIMIT' => true, - 'PARTITION BY' => true, - 'PARTITION' => true, - 'PROCEDURE' => true, - 'SUBPARTITION BY' => true, - 'VALUES' => true, + 'CREATE' => true, + 'LIMIT' => true, + 'PARTITION BY' => true, + 'PARTITION' => true, + 'PROCEDURE' => true, + 'SUBPARTITION BY' => true, + 'VALUES' => true, ); /** * Constructor. * - * @param array $options The formatting options. + * @param array $options the formatting options */ public function __construct(array $options = array()) { @@ -62,6 +58,7 @@ class Formatter * The specified formatting options are merged with the default values. * * @param array $options + * * @return array */ private function getMergedOptions(array $options) @@ -95,43 +92,43 @@ class Formatter protected function getDefaultOptions() { return array( - /** + /* * The format of the result. * * @var string The type ('text', 'cli' or 'html') */ 'type' => php_sapi_name() === 'cli' ? 'cli' : 'text', - /** + /* * The line ending used. * By default, for text this is "\n" and for HTML this is "<br/>". * * @var string */ - 'line_ending' => NULL, + 'line_ending' => null, - /** + /* * The string used for indentation. * * @var string */ 'indentation' => ' ', - /** + /* * Whether comments should be removed or not. * * @var bool */ 'remove_comments' => false, - /** + /* * Whether each clause should be on a new line. * * @var bool */ 'clause_newline' => true, - /** + /* * Whether each part should be on a new line. * Parts are delimited by brackets and commas. * @@ -139,7 +136,7 @@ class Formatter */ 'parts_newline' => true, - /** + /* * Whether each part of each clause should be indented. * * @var bool @@ -150,7 +147,7 @@ class Formatter /** * The styles used for HTML formatting. - * array($type, $flags, $span, $callback) + * array($type, $flags, $span, $callback). * * @return array */ @@ -218,12 +215,12 @@ class Formatter /* Sanitize the array so that we do not have to care later */ foreach ($newFormats as $j => $new) { foreach ($integers as $name) { - if (! isset($new[$name])) { + if (!isset($new[$name])) { $newFormats[$j][$name] = 0; } } foreach ($strings as $name) { - if (! isset($new[$name])) { + if (!isset($new[$name])) { $newFormats[$j][$name] = ''; } } @@ -243,7 +240,7 @@ class Formatter /* Add not already handled formats */ foreach ($newFormats as $j => $new) { - if (! in_array($j, $added)) { + if (!in_array($j, $added)) { $formats[] = $new; } } @@ -254,45 +251,44 @@ class Formatter /** * Formats the given list of tokens. * - * @param TokensList $list The list of tokens. + * @param TokensList $list the list of tokens * * @return string */ public function formatList($list) { - /** * The query to be returned. * - * @var string $ret + * @var string */ $ret = ''; /** * The indentation level. * - * @var int $indent + * @var int */ $indent = 0; /** * Whether the line ended. * - * @var bool $lineEnded + * @var bool */ $lineEnded = false; /** - * Whether current group is short (no linebreaks) + * Whether current group is short (no linebreaks). * - * @var bool $shortGroup + * @var bool */ $shortGroup = false; /** * The name of the last clause. * - * @var string $lastClause + * @var string */ $lastClause = ''; @@ -300,7 +296,7 @@ class Formatter * A stack that keeps track of the indentation level every time a new * block is found. * - * @var array $blocksIndentation + * @var array */ $blocksIndentation = array(); @@ -308,21 +304,21 @@ class Formatter * A stack that keeps track of the line endings every time a new block * is found. * - * @var array $blocksLineEndings + * @var array */ $blocksLineEndings = array(); /** * Whether clause's options were formatted. * - * @var bool $formattedOptions + * @var bool */ $formattedOptions = false; /** * Previously parsed token. * - * @var Token $prev + * @var Token */ $prev = null; @@ -330,7 +326,7 @@ class Formatter * Comments are being formatted separately to maintain the whitespaces * before and after them. * - * @var string $comment + * @var string */ $comment = ''; @@ -342,7 +338,7 @@ class Formatter /** * Token parsed at this moment. * - * @var Token $curr + * @var Token */ $curr = $list->tokens[$list->idx]; @@ -421,7 +417,7 @@ class Formatter // pieces only if the clause is not inlined or this fragment // is between brackets that are on new line. if (((empty(self::$INLINE_CLAUSES[$lastClause])) - && ! $shortGroup + && !$shortGroup && ($this->options['parts_newline'])) || (end($blocksLineEndings) === true) ) { @@ -505,18 +501,12 @@ class Formatter { return str_replace( array( - "\x00", "\x01", "\x02", "\x03", "\x04", - "\x05", "\x06", "\x07", "\x08", "\x09", "\x0A", - "\x0B","\x0C","\x0D", "\x0E", "\x0F", "\x10", "\x11", - "\x12","\x13","\x14","\x15", "\x16", "\x17", "\x18", - "\x19","\x1A","\x1B","\x1C","\x1D", "\x1E", "\x1F" + "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D", "\x0E", "\x0F", + "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", "\x1E", "\x1F", ), array( - '\x00', '\x01', '\x02', '\x03', '\x04', - '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', - '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', - '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', - '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' + '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', + '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F', ), $string ); @@ -525,7 +515,7 @@ class Formatter /** * Tries to print the query and returns the result. * - * @param Token $token The token to be printed. + * @param Token $token the token to be printed * * @return string */ @@ -559,6 +549,7 @@ class Formatter } elseif ($this->options['type'] === 'html') { return htmlspecialchars($text, ENT_NOQUOTES); } + return $text; } @@ -566,14 +557,15 @@ class Formatter * Formats a query. * * @param string $query The query to be formatted - * @param array $options The formatting options. + * @param array $options the formatting options * - * @return string The formatted string. + * @return string the formatted string */ public static function format($query, array $options = array()) { $lexer = new Lexer($query); - $formatter = new Formatter($options); + $formatter = new self($options); + return $formatter->formatList($lexer->list); } @@ -582,7 +574,7 @@ class Formatter * * A group is delimited by a pair of brackets. * - * @param TokensList $list The list of tokens. + * @param TokensList $list the list of tokens * * @return int */ @@ -594,14 +586,14 @@ class Formatter * the list already advanced one position and the opening bracket was * already parsed. * - * @var int $count + * @var int */ $count = 1; /** * The length of this group. * - * @var int $length + * @var int */ $length = 0; @@ -628,7 +620,7 @@ class Formatter /** * Checks if a token is a statement or a clause inside a statement. * - * @param Token $token The token to be checked. + * @param Token $token the token to be checked * * @return int|bool */ @@ -641,6 +633,7 @@ class Formatter } elseif (($token->type === Token::TYPE_KEYWORD) && (isset(Parser::$KEYWORD_PARSERS[$token->value]))) { return 1; } + return false; } } diff --git a/src/Utils/Misc.php b/src/Utils/Misc.php index 8166c45..84ec3d1 100644 --- a/src/Utils/Misc.php +++ b/src/Utils/Misc.php @@ -2,10 +2,8 @@ /** * Miscellaneous utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Components\Expression; @@ -15,18 +13,16 @@ use SqlParser\Statements\SelectStatement; * Miscellaneous utilities. * * @category Misc - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Misc { - /** * Gets a list of all aliases and their original names. * - * @param SelectStatement $statement The statement to be processed. - * @param string $database The name of the database. + * @param SelectStatement $statement the statement to be processed + * @param string $database the name of the database * * @return array */ @@ -47,7 +43,7 @@ class Misc * Expressions that may contain aliases. * These are extracted from `FROM` and `JOIN` keywords. * - * @var Expression[] $expressions + * @var Expression[] */ $expressions = $statement->from; diff --git a/src/Utils/Query.php b/src/Utils/Query.php index bfd10a9..1a5162d 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -2,10 +2,8 @@ /** * Statement utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Lexer; @@ -37,27 +35,25 @@ use SqlParser\Statements\UpdateStatement; * Statement utilities. * * @category Statement - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Query { - /** * Functions that set the flag `is_func`. * * @var array */ public static $FUNCTIONS = array( - 'SUM', 'AVG', 'STD', 'STDDEV', 'MIN', 'MAX', 'BIT_OR', 'BIT_AND' + 'SUM', 'AVG', 'STD', 'STDDEV', 'MIN', 'MAX', 'BIT_OR', 'BIT_AND', ); /** * 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. + * @param Statement|null $statement the statement to be processed + * @param bool $all if `false`, false values will not be included * * @return array */ @@ -66,83 +62,82 @@ class Query $flags = array(); if ($all) { $flags = array( - - /** + /* * select ... DISTINCT ... */ - 'distinct' => false, + 'distinct' => false, - /** + /* * drop ... DATABASE ... */ 'drop_database' => false, - /** + /* * ... GROUP BY ... */ - 'group' => false, + 'group' => false, - /** + /* * ... HAVING ... */ - 'having' => false, + 'having' => false, - /** + /* * INSERT ... * or * REPLACE ... * or * DELETE ... */ - 'is_affected' => false, + 'is_affected' => false, - /** + /* * select ... PROCEDURE ANALYSE( ... ) ... */ - 'is_analyse' => false, + 'is_analyse' => false, - /** + /* * select COUNT( ... ) ... */ - 'is_count' => false, + 'is_count' => false, - /** + /* * DELETE ... */ - 'is_delete' => false, // @deprecated; use `querytype` + 'is_delete' => false, // @deprecated; use `querytype` - /** + /* * EXPLAIN ... */ - 'is_explain' => false, // @deprecated; use `querytype` + 'is_explain' => false, // @deprecated; use `querytype` - /** + /* * select ... INTO OUTFILE ... */ - 'is_export' => false, + 'is_export' => false, - /** + /* * select FUNC( ... ) ... */ - 'is_func' => false, + 'is_func' => false, - /** + /* * select ... GROUP BY ... * or * select ... HAVING ... */ - 'is_group' => false, + 'is_group' => false, - /** + /* * INSERT ... * or * REPLACE ... * or * TODO: LOAD DATA ... */ - 'is_insert' => false, + 'is_insert' => false, - /** + /* * ANALYZE ... * or * CHECK ... @@ -153,73 +148,73 @@ class Query * or * REPAIR ... */ - 'is_maint' => false, + 'is_maint' => false, - /** + /* * CALL ... */ - 'is_procedure' => false, + 'is_procedure' => false, - /** + /* * REPLACE ... */ - 'is_replace' => false, // @deprecated; use `querytype` + 'is_replace' => false, // @deprecated; use `querytype` - /** + /* * SELECT ... */ - 'is_select' => false, // @deprecated; use `querytype` + 'is_select' => false, // @deprecated; use `querytype` - /** + /* * SHOW ... */ - 'is_show' => false, // @deprecated; use `querytype` + 'is_show' => false, // @deprecated; use `querytype` - /** + /* * Contains a subquery. */ - 'is_subquery' => false, + 'is_subquery' => false, - /** + /* * ... JOIN ... */ - 'join' => false, + 'join' => false, - /** + /* * ... LIMIT ... */ - 'limit' => false, + 'limit' => false, - /** + /* * TODO */ - 'offset' => false, + 'offset' => false, - /** + /* * ... ORDER ... */ - 'order' => false, + 'order' => false, - /** + /* * The type of the query (which is usually the first keyword of * the statement). */ - 'querytype' => false, + 'querytype' => false, - /** + /* * Whether a page reload is required. */ - 'reload' => false, + 'reload' => false, - /** + /* * SELECT ... FROM ... */ - 'select_from' => false, + 'select_from' => false, - /** + /* * ... UNION ... */ - 'union' => false + 'union' => false, ); } @@ -335,7 +330,6 @@ class Query if (!empty($statement->join)) { $flags['join'] = true; } - } elseif ($statement instanceof ShowStatement) { $flags['querytype'] = 'SHOW'; $flags['is_show'] = true; @@ -362,17 +356,17 @@ class Query /** * Parses a query and gets all information about it. * - * @param string $query The query to be parsed. + * @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 + * - 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 */ public static function getAll($query) { @@ -401,7 +395,7 @@ class Query ) { $tableAliases[$expr->alias] = array( $expr->table, - isset($expr->database) ? $expr->database : null + isset($expr->database) ? $expr->database : null, ); } } @@ -417,7 +411,7 @@ class Query $arr = array( $expr->table, ((isset($expr->database)) && ($expr->database !== '')) ? - $expr->database : null + $expr->database : null, ); } if (!in_array($arr, $ret['select_tables'])) { @@ -437,7 +431,7 @@ class Query $arr = array( $expr->table, ((isset($expr->database)) && ($expr->database !== '')) ? - $expr->database : null + $expr->database : null, ); if (!in_array($arr, $ret['select_tables'])) { $ret['select_tables'][] = $arr; @@ -453,7 +447,7 @@ class Query /** * Gets a list of all tables used in this statement. * - * @param Statement $statement Statement to be scanned. + * @param Statement $statement statement to be scanned * * @return array */ @@ -495,33 +489,33 @@ class Query $ret[] = Expression::build($expr); } } + return $ret; } /** * Gets a specific clause. * - * @param Statement $statement The parsed query that has to be modified. - * @param TokensList $list The list of tokens. - * @param string $clause The clause to be returned. + * @param Statement $statement the parsed query that has to be modified + * @param TokensList $list the list of tokens + * @param string $clause the clause to be returned * @param int|string $type The type of the search. * If int, - * -1 for everything that was before - * 0 only for the clause - * 1 for everything after + * -1 for everything that was before + * 0 only for the clause + * 1 for everything after * If string, the name of the first clause that * should not be included. - * @param bool $skipFirst Whether to skip the first keyword in clause. + * @param bool $skipFirst whether to skip the first keyword in clause * * @return string */ public static function getClause($statement, $list, $clause, $type = 0, $skipFirst = true) { - /** * The index of the current clause. * - * @var int $currIdx + * @var int */ $currIdx = 0; @@ -529,42 +523,42 @@ class Query * The count of brackets. * We keep track of them so we won't insert the clause in a subquery. * - * @var int $brackets + * @var int */ $brackets = 0; /** * The string to be returned. * - * @var string $ret + * @var string */ $ret = ''; /** * The clauses of this type of statement and their index. * - * @var array $clauses + * @var array */ $clauses = array_flip(array_keys($statement->getClauses())); /** * Lexer used for lexing the clause. * - * @var Lexer $lexer + * @var Lexer */ $lexer = new Lexer($clause); /** * The type of this clause. * - * @var string $clauseType + * @var string */ $clauseType = $lexer->list->getNextOfType(Token::TYPE_KEYWORD)->value; /** * The index of this clause. * - * @var int $clauseIdx + * @var int */ $clauseIdx = $clauses[$clauseType]; @@ -637,14 +631,14 @@ class Query * * It is a very basic version of a query builder. * - * @param Statement $statement The parsed query that has to be modified. - * @param TokensList $list The list of tokens. + * @param Statement $statement the parsed query that has to be modified + * @param TokensList $list the list of tokens * @param string $old The type of the clause that should be * replaced. This can be an entire clause. * @param string $new The new clause. If this parameter is omitted * it is considered to be equal with `$old`. - * @param bool $onlyType Whether only the type of the clause should - * be replaced or the entire clause. + * @param bool $onlyType whether only the type of the clause should + * be replaced or the entire clause * * @return string */ @@ -670,8 +664,8 @@ class Query * Builds a query by rebuilding the statement from the tokens list supplied * and replaces multiple clauses. * - * @param Statement $statement The parsed query that has to be modified. - * @param TokensList $list The list of tokens. + * @param Statement $statement the parsed query that has to be modified + * @param TokensList $list the list of tokens * @param array $ops Clauses to be replaced. Contains multiple * arrays having two values: array($old, $new). * Clauses must be sorted. @@ -690,7 +684,7 @@ class Query /** * Value to be returned. * - * @var string $ret + * @var string */ $ret = ''; @@ -726,12 +720,12 @@ class Query /** * Gets the first full statement in the query. * - * @param string $query The query to be analyzed. - * @param string $delimiter The delimiter to be used. + * @param string $query the query to be analyzed + * @param string $delimiter the delimiter to be used * - * @return array Array containing the first full query, the - * remaining part of the query and the last - * delimiter. + * @return array array containing the first full query, the + * remaining part of the query and the last + * delimiter */ public static function getFirstStatement($query, $delimiter = null) { @@ -741,14 +735,14 @@ class Query /** * Whether a full statement was found. * - * @var bool $fullStatement + * @var bool */ $fullStatement = false; /** * The first full statement. * - * @var string $statement + * @var string */ $statement = ''; @@ -787,9 +781,9 @@ class Query /** * Gets a starting offset of a specific clause. * - * @param Statement $statement The parsed query that has to be modified. - * @param TokensList $list The list of tokens. - * @param string $clause The clause to be returned. + * @param Statement $statement the parsed query that has to be modified + * @param TokensList $list the list of tokens + * @param string $clause the clause to be returned * * @return int */ @@ -799,14 +793,14 @@ class Query * The count of brackets. * We keep track of them so we won't insert the clause in a subquery. * - * @var int $brackets + * @var int */ $brackets = 0; /** * The clauses of this type of statement and their index. * - * @var array $clauses + * @var array */ $clauses = array_flip(array_keys($statement->getClauses())); diff --git a/src/Utils/Routine.php b/src/Utils/Routine.php index d5a7891..5b0d5b9 100644 --- a/src/Utils/Routine.php +++ b/src/Utils/Routine.php @@ -2,10 +2,8 @@ /** * Routine utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Lexer; @@ -18,17 +16,15 @@ use SqlParser\Statements\CreateStatement; * Routine utilities. * * @category Routines - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Routine { - /** * Parses a parameter of a routine. * - * @param string $param Parameter's definition. + * @param string $param parameter's definition * * @return array */ @@ -53,14 +49,14 @@ class Routine '', $type->name, implode(',', $type->parameters), - implode(' ', $options) + implode(' ', $options), ); } /** * Parses a parameter of a routine. * - * @param string $param Parameter's definition. + * @param string $param parameter's definition * * @return array */ @@ -87,14 +83,14 @@ class Routine $param->name, $param->type->name, implode(',', $param->type->parameters), - implode(' ', $options) + implode(' ', $options), ); } /** * Gets the parameters of a routine from the parse tree. * - * @param CreateStatement $statement The statement to be processed. + * @param CreateStatement $statement the statement to be processed * * @return array */ diff --git a/src/Utils/Table.php b/src/Utils/Table.php index a31ddf2..58d4366 100644 --- a/src/Utils/Table.php +++ b/src/Utils/Table.php @@ -2,10 +2,8 @@ /** * Table utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Statements\CreateStatement; @@ -14,17 +12,15 @@ use SqlParser\Statements\CreateStatement; * Table utilities. * * @category Statement - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Table { - /** * Gets the foreign keys of the table. * - * @param CreateStatement $statement The statement to be processed. + * @param CreateStatement $statement the statement to be processed * * @return array */ @@ -73,7 +69,6 @@ class Table } $ret[] = $tmp; - } return $ret; @@ -82,7 +77,7 @@ class Table /** * Gets fields of the table. * - * @param CreateStatement $statement The statement to be processed. + * @param CreateStatement $statement the statement to be processed * * @return array */ @@ -133,7 +128,6 @@ class Table $ret[$field->name]['expr'] = $option; } } - } return $ret; diff --git a/src/Utils/Tokens.php b/src/Utils/Tokens.php index acec369..4637ed1 100644 --- a/src/Utils/Tokens.php +++ b/src/Utils/Tokens.php @@ -2,10 +2,8 @@ /** * Token utilities. - * - * @package SqlParser - * @subpackage Utils */ + namespace SqlParser\Utils; use SqlParser\Lexer; @@ -16,18 +14,16 @@ use SqlParser\TokensList; * Token utilities. * * @category Token - * @package SqlParser - * @subpackage Utils + * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class Tokens { - /** * Checks if a pattern is a match for the specified token. * - * @param Token $token The token to be matched. - * @param array $pattern The pattern to be matches. + * @param Token $token the token to be matched + * @param array $pattern the pattern to be matches * * @return bool */ @@ -72,7 +68,6 @@ class Tokens public static function replaceTokens($list, array $find, array $replace) { - /** * Whether the first parameter is a list. * @@ -151,7 +146,6 @@ class Tokens ++$k; } - // Checking if the sequence was found. if ($k === $findCount) { // Inserting new tokens. diff --git a/src/common.php b/src/common.php index 3575c24..cb63695 100644 --- a/src/common.php +++ b/src/common.php @@ -2,15 +2,12 @@ /** * Defines common elements used by the library. - * - * @package SqlParser */ - if (!function_exists('__')) { /** * Translates the given string. * - * @param string $str String to be translated. + * @param string $str string to be translated * * @return string */ |