* Update sqlparser to fit php8.

This commit is contained in:
songchenxuan
2024-03-19 14:52:48 +08:00
parent 39f5d2905b
commit ea516fd9b3
663 changed files with 140347 additions and 16174 deletions
@@ -1,13 +1,11 @@
<?php
/**
* `INSERT` statement.
*/
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Statements;
use PhpMyAdmin\SqlParser\Components\ArrayObj;
use PhpMyAdmin\SqlParser\Components\Array2d;
use PhpMyAdmin\SqlParser\Components\ArrayObj;
use PhpMyAdmin\SqlParser\Components\IntoKeyword;
use PhpMyAdmin\SqlParser\Components\OptionsArray;
use PhpMyAdmin\SqlParser\Components\SetOperation;
@@ -16,6 +14,10 @@ use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function count;
use function strlen;
use function trim;
/**
* `INSERT` statement.
*
@@ -48,29 +50,26 @@ use PhpMyAdmin\SqlParser\TokensList;
* [ ON DUPLICATE KEY UPDATE
* col_name=expr
* [, col_name=expr] ... ]
*
* @category Statements
*
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
class InsertStatement extends Statement
{
/**
* Options for `INSERT` statements.
*
* @var array
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $OPTIONS = array(
public static $OPTIONS = [
'LOW_PRIORITY' => 1,
'DELAYED' => 2,
'HIGH_PRIORITY' => 3,
'IGNORE' => 4
);
'IGNORE' => 4,
];
/**
* Tables used as target for this statement.
*
* @var IntoKeyword
* @var IntoKeyword|null
*/
public $into;
@@ -85,7 +84,7 @@ class InsertStatement extends Statement
* If SET clause is present
* holds the SetOperation.
*
* @var SetOperation[]
* @var SetOperation[]|null
*/
public $set;
@@ -93,15 +92,23 @@ class InsertStatement extends Statement
* If SELECT clause is present
* holds the SelectStatement.
*
* @var SelectStatement
* @var SelectStatement|null
*/
public $select;
/**
* If WITH CTE is present
* holds the WithStatement.
*
* @var WithStatement|null
*/
public $with;
/**
* If ON DUPLICATE KEY UPDATE clause is present
* holds the SetOperation.
*
* @var SetOperation[]
* @var SetOperation[]|null
*/
public $onDuplicateSet;
@@ -113,15 +120,15 @@ class InsertStatement extends Statement
$ret = 'INSERT ' . $this->options;
$ret = trim($ret) . ' INTO ' . $this->into;
if (! is_null($this->values) && count($this->values) > 0) {
if ($this->values !== null && count($this->values) > 0) {
$ret .= ' VALUES ' . Array2d::build($this->values);
} elseif (! is_null($this->set) && count($this->set) > 0) {
} elseif ($this->set !== null && count($this->set) > 0) {
$ret .= ' SET ' . SetOperation::build($this->set);
} elseif (! is_null($this->select) && strlen($this->select) > 0) {
} elseif ($this->select !== null && strlen((string) $this->select) > 0) {
$ret .= ' ' . $this->select->build();
}
if (! is_null($this->onDuplicateSet) && count($this->onDuplicateSet) > 0) {
if ($this->onDuplicateSet !== null && count($this->onDuplicateSet) > 0) {
$ret .= ' ON DUPLICATE KEY UPDATE ' . SetOperation::build($this->onDuplicateSet);
}
@@ -137,11 +144,7 @@ class InsertStatement extends Statement
++$list->idx; // Skipping `INSERT`.
// parse any options if provided
$this->options = OptionsArray::parse(
$parser,
$list,
static::$OPTIONS
);
$this->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
++$list->idx;
/**
@@ -168,8 +171,6 @@ class InsertStatement extends Statement
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*
* @var Token
*/
$token = $list->tokens[$list->idx];
@@ -184,9 +185,7 @@ class InsertStatement extends Statement
}
if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword !== 'INTO'
) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword !== 'INTO') {
$parser->error('Unexpected keyword.', $token);
break;
}
@@ -195,40 +194,35 @@ class InsertStatement extends Statement
$this->into = IntoKeyword::parse(
$parser,
$list,
array('fromInsert' => true)
['fromInsert' => true]
);
$state = 1;
} elseif ($state === 1) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'VALUE'
|| $token->keyword === 'VALUES'
) {
++$list->idx; // skip VALUES
$this->values = Array2d::parse($parser, $list);
} elseif ($token->keyword === 'SET') {
++$list->idx; // skip SET
$this->set = SetOperation::parse($parser, $list);
} elseif ($token->keyword === 'SELECT') {
$this->select = new SelectStatement($parser, $list);
} else {
$parser->error(
'Unexpected keyword.',
$token
);
break;
}
$state = 2;
$miniState = 1;
} else {
$parser->error(
'Unexpected token.',
$token
);
if ($token->type !== Token::TYPE_KEYWORD) {
$parser->error('Unexpected token.', $token);
break;
}
if ($token->keyword === 'VALUE' || $token->keyword === 'VALUES') {
++$list->idx; // skip VALUES
$this->values = Array2d::parse($parser, $list);
} elseif ($token->keyword === 'SET') {
++$list->idx; // skip SET
$this->set = SetOperation::parse($parser, $list);
} elseif ($token->keyword === 'SELECT') {
$this->select = new SelectStatement($parser, $list);
} elseif ($token->keyword === 'WITH') {
$this->with = new WithStatement($parser, $list);
} else {
$parser->error('Unexpected keyword.', $token);
break;
}
$state = 2;
$miniState = 1;
} elseif ($state === 2) {
$lastCount = $miniState;
@@ -243,10 +237,7 @@ class InsertStatement extends Statement
}
if ($lastCount === $miniState) {
$parser->error(
'Unexpected token.',
$token
);
$parser->error('Unexpected token.', $token);
break;
}