* Support php 5.3 for sql parser lib.

This commit is contained in:
qixinzhi
2023-02-10 11:24:24 +08:00
parent 7c2e937eda
commit 7c122a0b29
648 changed files with 13815 additions and 279048 deletions
@@ -1,12 +1,11 @@
<?php
/**
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*/
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
@@ -15,16 +14,14 @@ use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function is_array;
use function trim;
/**
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*
* @final
* @category Components
*
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
class CreateDefinition extends Component
{
@@ -33,61 +30,61 @@ class CreateDefinition extends Component
*
* @var array
*/
public static $FIELD_OPTIONS = [
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' => [
'DEFAULT' => array(
2,
'expr',
['breakOnAlias' => true],
],
array('breakOnAlias' => true)
),
/* Following are not according to grammar, but MySQL happily accepts
* these at any location */
'CHARSET' => [
'CHARSET' => array(
2,
'var',
],
'COLLATE' => [
),
'COLLATE' => array(
3,
'var',
],
),
'AUTO_INCREMENT' => 3,
'PRIMARY' => 4,
'PRIMARY KEY' => 4,
'UNIQUE' => 4,
'UNIQUE KEY' => 4,
'COMMENT' => [
'COMMENT' => array(
5,
'var',
],
'COLUMN_FORMAT' => [
),
'COLUMN_FORMAT' => array(
6,
'var',
],
'ON UPDATE' => [
),
'ON UPDATE' => array(
7,
'expr',
],
),
// Generated columns options.
'GENERATED ALWAYS' => 8,
'AS' => [
'AS' => array(
9,
'expr',
['parenthesesDelimited' => true],
],
array('parenthesesDelimited' => true)
),
'VIRTUAL' => 10,
'PERSISTENT' => 11,
'STORED' => 11,
'CHECK' => [
'CHECK' => array(
12,
'expr',
['parenthesesDelimited' => true],
],
array('parenthesesDelimited' => true),
),
'INVISIBLE' => 13,
'ENFORCED' => 14,
'NOT' => 15,
@@ -100,12 +97,12 @@ class CreateDefinition extends Component
//
// 'UNIQUE' => 4,
// 'UNIQUE KEY' => 4,
// 'COMMENT' => [5, 'var'],
// 'COMMENT' => array(5, 'var'),
// 'NOT NULL' => 1,
// 'NULL' => 1,
// 'PRIMARY' => 4,
// 'PRIMARY KEY' => 4,
];
);
/**
* The name of the new column.
@@ -150,6 +147,8 @@ class CreateDefinition extends Component
public $options;
/**
* 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
@@ -181,11 +180,11 @@ class CreateDefinition extends Component
*
* @return CreateDefinition[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
public static function parse(Parser $parser, TokensList $list, array $options = array())
{
$ret = [];
$ret = array();
$expr = new static();
$expr = new self();
/**
* The state of the parser.
@@ -230,13 +229,16 @@ class CreateDefinition extends Component
}
if ($state === 0) {
if (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '(')) {
$parser->error('An opening bracket was expected.', $token);
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
$state = 1;
} else {
$parser->error(
'An opening bracket was expected.',
$token
);
break;
}
$state = 1;
} elseif ($state === 1) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'CONSTRAINT') {
$expr->isConstraint = true;
@@ -266,7 +268,10 @@ class CreateDefinition extends Component
$expr->name = $token->value;
$state = 2;
} else {
$parser->error('A symbol name was expected!', $token);
$parser->error(
'A symbol name was expected!',
$token
);
return $ret;
}
@@ -283,14 +288,12 @@ class CreateDefinition extends Component
} else {
--$list->idx;
}
$state = 5;
} elseif ($state === 5) {
if (! empty($expr->type) || ! empty($expr->key)) {
$ret[] = $expr;
}
$expr = new static();
$expr = new self();
if ($token->value === ',') {
$state = 1;
} elseif ($token->value === ')') {
@@ -298,7 +301,10 @@ class CreateDefinition extends Component
++$list->idx;
break;
} else {
$parser->error('A comma or a closing bracket was expected.', $token);
$parser->error(
'A comma or a closing bracket was expected.',
$token
);
$state = 0;
break;
}
@@ -311,7 +317,10 @@ class CreateDefinition extends Component
}
if (($state !== 0) && ($state !== 6)) {
$parser->error('A closing bracket was expected.', $list->tokens[$list->idx - 1]);
$parser->error(
'A closing bracket was expected.',
$list->tokens[$list->idx - 1]
);
}
--$list->idx;
@@ -325,7 +334,7 @@ class CreateDefinition extends Component
*
* @return string
*/
public static function build($component, array $options = [])
public static function build($component, array $options = array())
{
if (is_array($component)) {
return "(\n " . implode(",\n ", $component) . "\n)";
@@ -344,7 +353,7 @@ class CreateDefinition extends Component
if (! empty($component->type)) {
$tmp .= DataType::build(
$component->type,
['lowercase' => true]
array('lowercase' => true)
) . ' ';
}