Merge pull request #11371 from udan11/update

Update sql-parser.
This commit is contained in:
Marc Delisle 2015-08-09 05:50:28 -04:00
commit 2a14d62115
11 changed files with 79 additions and 57 deletions

View File

@ -4288,8 +4288,8 @@ class PMA_DisplayResults
if (!empty($statement->order)) {
foreach ($statement->order as $o) {
$sort_expression[] = $o->field->expr . ' ' . $o->type;
$sort_expression_nodirection[] = $o->field->expr;
$sort_expression[] = $o->expr->expr . ' ' . $o->type;
$sort_expression_nodirection[] = $o->expr->expr;
$sort_direction[] = $o->type;
}
} else {

View File

@ -1,7 +1,7 @@
<?php
/**
* Parses a reference to a field.
* Parses an alter operation.
*
* @package SqlParser
* @subpackage Components
@ -14,7 +14,7 @@ use SqlParser\Token;
use SqlParser\TokensList;
/**
* Parses a reference to a field.
* Parses an alter operation.
*
* @category Components
* @package SqlParser
@ -178,10 +178,8 @@ class AlterOperation extends Component
++$brackets;
} elseif ($token->value === ')') {
--$brackets;
} elseif ($token->value === ',') {
if ($brackets === 0) {
break;
}
} elseif (($token->value === ',') && ($brackets === 0)) {
break;
}
}
$ret->unknown[] = $token;

View File

@ -1,7 +1,7 @@
<?php
/**
* Parses the definition of a field.
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*
@ -17,7 +17,7 @@ use SqlParser\Token;
use SqlParser\TokensList;
/**
* Parses the definition of a field.
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*
@ -27,7 +27,7 @@ use SqlParser\TokensList;
* @author Dan Ungureanu <udan1107@gmail.com>
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License
*/
class FieldDefinition extends Component
class CreateDefinition extends Component
{
/**
@ -145,13 +145,13 @@ class FieldDefinition extends Component
* @param TokensList $list The list of tokens that are being parsed.
* @param array $options Parameters for parsing.
*
* @return FieldDefinition[]
* @return CreateDefinition[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = array())
{
$ret = array();
$expr = new FieldDefinition();
$expr = new CreateDefinition();
/**
* The state of the parser.
@ -234,7 +234,7 @@ class FieldDefinition extends Component
if ((!empty($expr->type)) || (!empty($expr->key))) {
$ret[] = $expr;
}
$expr = new FieldDefinition();
$expr = new CreateDefinition();
if ($token->value === ',') {
$state = 1;
} elseif ($token->value === ')') {
@ -269,7 +269,7 @@ class FieldDefinition extends Component
}
/**
* @param FieldDefinition|FieldDefinition[] $component The component to be built.
* @param CreateDefinition|CreateDefinition[] $component The component to be built.
*
* @return string
*/

View File

@ -1,7 +1,8 @@
<?php
/**
* Parses a reference to a field.
* Parses a reference to an expression (column, table or database name, function
* call, mathematical expression, etc.).
*
* @package SqlParser
* @subpackage Components
@ -15,7 +16,8 @@ use SqlParser\Token;
use SqlParser\TokensList;
/**
* Parses a reference to a field.
* Parses a reference to an expression (column, table or database name, function
* call, mathematical expression, etc.).
*
* @category Components
* @package SqlParser

View File

@ -1,7 +1,7 @@
<?php
/**
* Parses a a list of fields delimited by a single comma.
* Parses a a list of expression delimited by a comma.
*
* @package SqlParser
* @subpackage Components
@ -14,7 +14,7 @@ use SqlParser\Token;
use SqlParser\TokensList;
/**
* Parses a a list of fields delimited by a single comma.
* Parses a a list of expression delimited by a comma.
*
* @category Keywords
* @package SqlParser

View File

@ -44,7 +44,7 @@ class IntoKeyword extends Component
*
* @var array
*/
public $fields;
public $columns;
/**
* @param Parser $parser The parser that serves as context.
@ -114,7 +114,7 @@ class IntoKeyword extends Component
$state = 1;
} elseif ($state === 1) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
$ret->fields = ArrayObj::parse($parser, $list)->values;
$ret->columns = ArrayObj::parse($parser, $list)->values;
++$list->idx;
}
break;

View File

@ -26,11 +26,11 @@ class OrderKeyword extends Component
{
/**
* The field that is used for ordering.
* The expression that is used for ordering.
*
* @var Expression
*/
public $field;
public $expr;
/**
* The order type.
@ -42,12 +42,12 @@ class OrderKeyword extends Component
/**
* Constructor.
*
* @param Expression $field The field 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($field = null, $type = 'ASC')
public function __construct($expr = null, $type = 'ASC')
{
$this->field = $field;
$this->expr = $expr;
$this->type = $type;
}
@ -69,7 +69,7 @@ class OrderKeyword extends Component
*
* Below are the states of the parser.
*
* 0 ----------------------[ field ]----------------------> 1
* 0 --------------------[ expression ]-------------------> 1
*
* 1 ------------------------[ , ]------------------------> 0
* 1 -------------------[ ASC / DESC ]--------------------> 1
@ -96,13 +96,13 @@ class OrderKeyword extends Component
}
if ($state === 0) {
$expr->field = Expression::parse($parser, $list);
$expr->expr = Expression::parse($parser, $list);
$state = 1;
} elseif ($state === 1) {
if (($token->type === Token::TYPE_KEYWORD) && (($token->value === 'ASC') || ($token->value === 'DESC'))) {
$expr->type = $token->value;
} elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
if (!empty($expr->field)) {
if (!empty($expr->expr)) {
$ret[] = $expr;
}
$expr = new OrderKeyword();
@ -115,7 +115,7 @@ class OrderKeyword extends Component
}
// Last iteration was not processed.
if (!empty($expr->field)) {
if (!empty($expr->expr)) {
$ret[] = $expr;
}
@ -137,7 +137,7 @@ class OrderKeyword extends Component
}
return implode(", ", $ret);
} else {
return Expression::build($component->field) . ' ' . $component->type;
return Expression::build($component->expr) . ' ' . $component->type;
}
}
}

View File

@ -57,7 +57,7 @@ class SetOperation extends Component
*
* Below are the states of the parser.
*
* 0 -------------------[ field name ]--------------------> 1
* 0 -------------------[ column name ]-------------------> 1
*
* 1 ------------------------[ , ]------------------------> 0
* 1 ----------------------[ value ]----------------------> 1

View File

@ -515,15 +515,39 @@ namespace SqlParser {
$token .= $this->str[$this->last];
if (Context::isComment($token)) {
$flags = Token::FLAG_COMMENT_C;
if (($this->last + 1 < $this->len) && ($this->str[$this->last + 1] === '!')) {
// It is a MySQL-specific command.
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
// This comment already ended. It may be a part of a
// previous MySQL specific command.
if ($token === '*/') {
return new Token($token, Token::TYPE_COMMENT, $flags);
}
// Checking if this is a MySQL-specific command.
if (($this->last + 1 < $this->len) && ($this->str[$this->last + 1] === '!')) {
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
$token .= $this->str[++$this->last];
while ((++$this->last < $this->len)
&& ('0' <= $this->str[$this->last])
&& ($this->str[$this->last] <= '9')
) {
$token .= $this->str[$this->last];
}
--$this->last;
// We split this comment and parse only its beginning
// here.
return new Token($token, Token::TYPE_COMMENT, $flags);
}
// Parsing the comment.
while ((++$this->last < $this->len)
&& (($this->str[$this->last - 1] !== '*') || ($this->str[$this->last] !== '/'))
) {
$token .= $this->str[$this->last];
}
// Adding the ending.
if ($this->last < $this->len) {
$token .= $this->str[$this->last];
}

View File

@ -14,7 +14,7 @@ use SqlParser\Token;
use SqlParser\TokensList;
use SqlParser\Components\ArrayObj;
use SqlParser\Components\DataType;
use SqlParser\Components\FieldDefinition;
use SqlParser\Components\CreateDefinition;
use SqlParser\Components\Expression;
use SqlParser\Components\OptionsArray;
use SqlParser\Components\ParameterDefinition;
@ -164,12 +164,12 @@ class CreateStatement extends Statement
public $entityOptions;
/**
* If `CREATE TABLE`, a list of fields in the new table.
* If `CREATE TABLE`, a list of columns and keys.
* If `CREATE VIEW`, a list of columns.
*
* Used by `CREATE TABLE` and `CREATE VIEW`.
*
* @var FieldDefinition[]|ArrayObj
* @var CreateDefinition[]|ArrayObj
*/
public $fields;
@ -218,7 +218,7 @@ class CreateStatement extends Statement
$fields = '';
if (!empty($this->fields)) {
if (is_array($this->fields)) {
$fields = FieldDefinition::build($this->fields) . ' ';
$fields = CreateDefinition::build($this->fields) . ' ';
} elseif ($this->fields instanceof ArrayObj) {
$fields = ArrayObj::build($this->fields);
}
@ -304,10 +304,10 @@ class CreateStatement extends Statement
static::$DB_OPTIONS
);
} elseif ($this->options->has('TABLE')) {
$this->fields = FieldDefinition::parse($parser, $list);
$this->fields = CreateDefinition::parse($parser, $list);
if (empty($this->fields)) {
$parser->error(
__('At least one field definition was expected.'),
__('At least one column definition was expected.'),
$list->tokens[$list->idx]
);
}

View File

@ -449,40 +449,40 @@ class Query
*/
public static function getTables($statement)
{
$fields = array();
$expressions = array();
if (($statement instanceof InsertStatement)
|| ($statement instanceof ReplaceStatement)
) {
$fields = array($statement->into->dest);
$expressions = array($statement->into->dest);
} elseif ($statement instanceof UpdateStatement) {
$fields = $statement->tables;
$expressions = $statement->tables;
} elseif (($statement instanceof SelectStatement)
|| ($statement instanceof DeleteStatement)
) {
$fields = $statement->from;
$expressions = $statement->from;
} elseif (($statement instanceof AlterStatement)
|| ($statement instanceof TruncateStatement)
) {
$fields = array($statement->table);
$expressions = array($statement->table);
} elseif ($statement instanceof DropStatement) {
if (!$statement->options->has('TABLE')) {
// No tables are dropped.
return array();
}
$fields = $statement->fields;
$expressions = $statement->fields;
} elseif ($statement instanceof RenameStatement) {
foreach ($statement->renames as $rename) {
$fields[] = $rename->old;
$expressions[] = $rename->old;
}
}
$ret = array();
foreach ($fields as $field) {
if (!empty($field->table)) {
$field->expr = null; // Force rebuild.
$field->alias = null; // Aliases are not required.
$ret[] = Expression::build($field);
foreach ($expressions as $expr) {
if (!empty($expr->table)) {
$expr->expr = null; // Force rebuild.
$expr->alias = null; // Aliases are not required.
$ret[] = Expression::build($expr);
}
}
return $ret;
@ -743,9 +743,7 @@ class Query
for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];
if (($token->type === Token::TYPE_COMMENT)
&& (!($token->flags & Token::FLAG_COMMENT_MYSQL_CMD))
) {
if ($token->type === Token::TYPE_COMMENT) {
continue;
}