first commit

This commit is contained in:
dev@siliconpin.com
2025-08-07 11:53:41 +05:30
commit a3067c5ad4
4795 changed files with 782758 additions and 0 deletions

View File

@@ -0,0 +1,410 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Context;
use function array_merge;
use function strlen;
use function substr;
use function trim;
/**
* Buffer query utilities.
*
* Implements a specialized lexer used to extract statements from large inputs
* that are being buffered. After each statement has been extracted, a lexer or
* a parser may be used.
*/
class BufferedQuery
{
// Constants that describe the current status of the parser.
// A string is being parsed.
public const STATUS_STRING = 16; // 0001 0000
public const STATUS_STRING_SINGLE_QUOTES = 17; // 0001 0001
public const STATUS_STRING_DOUBLE_QUOTES = 18; // 0001 0010
public const STATUS_STRING_BACKTICK = 20; // 0001 0100
// A comment is being parsed.
public const STATUS_COMMENT = 32; // 0010 0000
public const STATUS_COMMENT_BASH = 33; // 0010 0001
public const STATUS_COMMENT_C = 34; // 0010 0010
public const STATUS_COMMENT_SQL = 36; // 0010 0100
/**
* The query that is being processed.
*
* This field can be modified just by appending to it!
*
* @var string
*/
public $query = '';
/**
* The options of this parser.
*
* @var array<string, bool|string>
* @psalm-var array{delimiter?: non-empty-string, parse_delimiter?: bool, add_delimiter?: bool}
*/
public $options = [];
/**
* The last delimiter used.
*
* @var string
*/
public $delimiter;
/**
* The length of the delimiter.
*
* @var int
*/
public $delimiterLen;
/**
* The current status of the parser.
*
* @var int|null
*/
public $status;
/**
* The last incomplete query that was extracted.
*
* @var string
*/
public $current = '';
/**
* @param string $query the query to be parsed
* @param array<string, bool|string> $options the options of this parser
* @psalm-param array{delimiter?: non-empty-string, parse_delimiter?: bool, add_delimiter?: bool} $options
*/
public function __construct($query = '', array $options = [])
{
// Merges specified options with defaults.
$this->options = array_merge(
[
// The starting delimiter.
'delimiter' => ';',
// Whether `DELIMITER` statements should be parsed.
'parse_delimiter' => false,
// Whether a delimiter should be added at the end of the statement.
'add_delimiter' => false,
],
$options
);
$this->query = $query;
$this->setDelimiter($this->options['delimiter']);
}
/**
* Sets the delimiter.
*
* Used to update the length of it too.
*
* @param string $delimiter
*
* @return void
*/
public function setDelimiter($delimiter)
{
$this->delimiter = $delimiter;
$this->delimiterLen = strlen($delimiter);
}
/**
* Extracts a statement from the buffer.
*
* @param bool $end whether the end of the buffer was reached
*
* @return string|false
*/
public function extract($end = false)
{
/**
* The last parsed position.
*
* This is statically defined because it is not used outside anywhere
* outside this method and there is probably a (minor) performance
* improvement to it.
*
* @var int
*/
static $i = 0;
if (empty($this->query)) {
return false;
}
/**
* The length of the buffer.
*
* @var int
*/
$len = strlen($this->query);
/**
* The last index of the string that is going to be parsed.
*
* There must be a few characters left in the buffer so the parser can
* avoid confusing some symbols that may have multiple meanings.
*
* For example, if the buffer ends in `-` that may be an operator or the
* beginning of a comment.
*
* Another example if the buffer ends in `DELIMITE`. The parser is going
* to require a few more characters because that may be a part of the
* `DELIMITER` keyword or just a column named `DELIMITE`.
*
* Those extra characters are required only if there is more data
* expected (the end of the buffer was not reached).
*/
$loopLen = $end ? $len : $len - 16;
for (; $i < $loopLen; ++$i) {
/*
* Handling backslash.
*
* Even if the next character is a special character that should be
* treated differently, because of the preceding backslash, it will
* be ignored.
*/
if ((($this->status & self::STATUS_COMMENT) === 0) && ($this->query[$i] === '\\')) {
$this->current .= $this->query[$i] . ($i + 1 < $len ? $this->query[++$i] : '');
continue;
}
/*
* Handling special parses statuses.
*/
if ($this->status === self::STATUS_STRING_SINGLE_QUOTES) {
// Single-quoted strings like 'foo'.
if ($this->query[$i] === '\'') {
$this->status = 0;
}
$this->current .= $this->query[$i];
continue;
} elseif ($this->status === self::STATUS_STRING_DOUBLE_QUOTES) {
// Double-quoted strings like "bar".
if ($this->query[$i] === '"') {
$this->status = 0;
}
$this->current .= $this->query[$i];
continue;
} elseif ($this->status === self::STATUS_STRING_BACKTICK) {
if ($this->query[$i] === '`') {
$this->status = 0;
}
$this->current .= $this->query[$i];
continue;
} elseif (($this->status === self::STATUS_COMMENT_BASH) || ($this->status === self::STATUS_COMMENT_SQL)) {
// Bash-like (#) or SQL-like (-- ) comments end in new line.
if ($this->query[$i] === "\n") {
$this->status = 0;
}
$this->current .= $this->query[$i];
continue;
} elseif ($this->status === self::STATUS_COMMENT_C) {
// C-like comments end in */.
if (($this->query[$i - 1] === '*') && ($this->query[$i] === '/')) {
$this->status = 0;
}
$this->current .= $this->query[$i];
continue;
}
/*
* Checking if a string started.
*/
if ($this->query[$i] === '\'') {
$this->status = self::STATUS_STRING_SINGLE_QUOTES;
$this->current .= $this->query[$i];
continue;
}
if ($this->query[$i] === '"') {
$this->status = self::STATUS_STRING_DOUBLE_QUOTES;
$this->current .= $this->query[$i];
continue;
}
if ($this->query[$i] === '`') {
$this->status = self::STATUS_STRING_BACKTICK;
$this->current .= $this->query[$i];
continue;
}
/*
* Checking if a comment started.
*/
if ($this->query[$i] === '#') {
$this->status = self::STATUS_COMMENT_BASH;
$this->current .= $this->query[$i];
continue;
}
if ($i + 2 < $len) {
if (
($this->query[$i] === '-')
&& ($this->query[$i + 1] === '-')
&& Context::isWhitespace($this->query[$i + 2])
) {
$this->status = self::STATUS_COMMENT_SQL;
$this->current .= $this->query[$i];
continue;
}
if (($this->query[$i] === '/') && ($this->query[$i + 1] === '*') && ($this->query[$i + 2] !== '!')) {
$this->status = self::STATUS_COMMENT_C;
$this->current .= $this->query[$i];
continue;
}
}
/*
* Handling `DELIMITER` statement.
*
* The code below basically checks for
* `strtoupper(substr($this->query, $i, 9)) === 'DELIMITER'`
*
* This optimization makes the code about 3 times faster.
*
* `DELIMITER` is not being considered a keyword. The only context
* it has a special meaning is when it is the beginning of a
* statement. This is the reason for the last condition.
*/
if (
($i + 9 < $len)
&& (($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'))
&& (($this->query[$i + 4] === 'M') || ($this->query[$i + 4] === 'm'))
&& (($this->query[$i + 5] === 'I') || ($this->query[$i + 5] === 'i'))
&& (($this->query[$i + 6] === 'T') || ($this->query[$i + 6] === 't'))
&& (($this->query[$i + 7] === 'E') || ($this->query[$i + 7] === 'e'))
&& (($this->query[$i + 8] === 'R') || ($this->query[$i + 8] === 'r'))
&& Context::isWhitespace($this->query[$i + 9])
) {
// Saving the current index to be able to revert any parsing
// done in this block.
$iBak = $i;
$i += 9; // Skipping `DELIMITER`.
// Skipping whitespaces.
while (($i < $len) && Context::isWhitespace($this->query[$i])) {
++$i;
}
// Parsing the delimiter.
$delimiter = '';
while (($i < $len) && (! Context::isWhitespace($this->query[$i]))) {
$delimiter .= $this->query[$i++];
}
// Checking if the delimiter definition ended.
if (
($delimiter !== '')
&& (($i < $len) && Context::isWhitespace($this->query[$i])
|| (($i === $len) && $end))
) {
// Saving the delimiter.
$this->setDelimiter($delimiter);
// Whether this statement should be returned or not.
$ret = '';
if (! empty($this->options['parse_delimiter'])) {
// Appending the `DELIMITER` statement that was just
// found to the current statement.
$ret = trim(
$this->current . ' ' . substr($this->query, $iBak, $i - $iBak)
);
}
// Removing the statement that was just extracted from the
// query.
$this->query = substr($this->query, $i);
$i = 0;
// Resetting the current statement.
$this->current = '';
return $ret;
}
// Incomplete statement. Reverting
$i = $iBak;
return false;
}
/*
* Checking if the current statement finished.
*
* The first letter of the delimiter is being checked as an
* optimization. This code is almost as fast as the one above.
*
* There is no point in checking if two strings match if not even
* the first letter matches.
*/
if (
($this->query[$i] === $this->delimiter[0])
&& (($this->delimiterLen === 1)
|| (substr($this->query, $i, $this->delimiterLen) === $this->delimiter))
) {
// Saving the statement that just ended.
$ret = $this->current;
// If needed, adds a delimiter at the end of the statement.
if (! empty($this->options['add_delimiter'])) {
$ret .= $this->delimiter;
}
// Removing the statement that was just extracted from the
// query.
$this->query = substr($this->query, $i + $this->delimiterLen);
$i = 0;
// Resetting the current statement.
$this->current = '';
// Returning the statement.
return trim($ret);
}
/*
* Appending current character to current statement.
*/
$this->current .= $this->query[$i];
}
if ($end && ($i === $len)) {
// If the end of the buffer was reached, the buffer is emptied and
// the current statement that was extracted is returned.
$ret = $this->current;
// Emptying the buffer.
$this->query = '';
$i = 0;
// Resetting the current statement.
$this->current = '';
// Returning the statement.
return trim($ret);
}
return '';
}
}

View File

@@ -0,0 +1,316 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use function count;
use function getopt;
use function implode;
use function in_array;
use function rtrim;
use function stream_get_contents;
use function stream_select;
use function var_export;
use const STDIN;
/**
* CLI interface.
*/
class CLI
{
/**
* @param string[]|false[] $params
* @param string[] $longopts
*
* @return void
*/
public function mergeLongOpts(&$params, &$longopts)
{
foreach ($longopts as $value) {
$value = rtrim($value, ':');
if (! isset($params[$value])) {
continue;
}
$params[$value[0]] = $params[$value];
}
}
/**
* @return void
*/
public function usageHighlight()
{
echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
echo " cat file.sql | highlight-query\n";
}
/**
* @param string $opt
* @param string[] $long
*
* @return string[]|false[]|false
*/
public function getopt($opt, $long)
{
return getopt($opt, $long);
}
/**
* @return mixed|false
*/
public function parseHighlight()
{
$longopts = [
'help',
'query:',
'format:',
'ansi',
];
$params = $this->getopt('hq:f:a', $longopts);
if ($params === false) {
return false;
}
$this->mergeLongOpts($params, $longopts);
if (! isset($params['f'])) {
$params['f'] = 'cli';
}
if (! in_array($params['f'], ['html', 'cli', 'text'])) {
echo "ERROR: Invalid value for format!\n";
return false;
}
return $params;
}
/**
* @return int
*/
public function runHighlight()
{
$params = $this->parseHighlight();
if ($params === false) {
return 1;
}
if (isset($params['h'])) {
$this->usageHighlight();
return 0;
}
if (! isset($params['q'])) {
$stdIn = $this->readStdin();
if ($stdIn) {
$params['q'] = $stdIn;
}
}
if (isset($params['a'])) {
Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
}
if (isset($params['q'])) {
echo Formatter::format(
$params['q'],
['type' => $params['f']]
);
echo "\n";
return 0;
}
echo "ERROR: Missing parameters!\n";
$this->usageHighlight();
return 1;
}
/**
* @return void
*/
public function usageLint()
{
echo "Usage: lint-query --query SQL [--ansi]\n";
echo " cat file.sql | lint-query\n";
}
/**
* @return mixed
*/
public function parseLint()
{
$longopts = [
'help',
'query:',
'context:',
'ansi',
];
$params = $this->getopt('hq:c:a', $longopts);
$this->mergeLongOpts($params, $longopts);
return $params;
}
/**
* @return int
*/
public function runLint()
{
$params = $this->parseLint();
if ($params === false) {
return 1;
}
if (isset($params['h'])) {
$this->usageLint();
return 0;
}
if (isset($params['c'])) {
Context::load($params['c']);
}
if (! isset($params['q'])) {
$stdIn = $this->readStdin();
if ($stdIn) {
$params['q'] = $stdIn;
}
}
if (isset($params['a'])) {
Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
}
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
$parser = new Parser($lexer->list);
$errors = Error::get([$lexer, $parser]);
if (count($errors) === 0) {
return 0;
}
$output = Error::format($errors);
echo implode("\n", $output);
echo "\n";
return 10;
}
echo "ERROR: Missing parameters!\n";
$this->usageLint();
return 1;
}
/**
* @return void
*/
public function usageTokenize()
{
echo "Usage: tokenize-query --query SQL [--ansi]\n";
echo " cat file.sql | tokenize-query\n";
}
/**
* @return mixed
*/
public function parseTokenize()
{
$longopts = [
'help',
'query:',
'ansi',
];
$params = $this->getopt('hq:a', $longopts);
$this->mergeLongOpts($params, $longopts);
return $params;
}
/**
* @return int
*/
public function runTokenize()
{
$params = $this->parseTokenize();
if ($params === false) {
return 1;
}
if (isset($params['h'])) {
$this->usageTokenize();
return 0;
}
if (! isset($params['q'])) {
$stdIn = $this->readStdin();
if ($stdIn) {
$params['q'] = $stdIn;
}
}
if (isset($params['a'])) {
Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
}
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
foreach ($lexer->list->tokens as $idx => $token) {
echo '[TOKEN ', $idx, "]\n";
echo 'Type = ', $token->type, "\n";
echo 'Flags = ', $token->flags, "\n";
echo 'Value = ';
var_export($token->value);
echo "\n";
echo 'Token = ';
var_export($token->token);
echo "\n";
echo "\n";
}
return 0;
}
echo "ERROR: Missing parameters!\n";
$this->usageTokenize();
return 1;
}
/**
* @return string|false
*/
public function readStdin()
{
$read = [STDIN];
$write = [];
$except = [];
// Assume there's nothing to be read from STDIN.
$stdin = null;
// Try to read from STDIN. Wait 0.2 second before timing out.
$result = stream_select($read, $write, $except, 0, 2000);
if ($result > 0) {
$stdin = stream_get_contents(STDIN);
}
return $stdin;
}
}

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Exceptions\LexerException;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use function htmlspecialchars;
use function sprintf;
/**
* Error related utilities.
*/
class Error
{
/**
* Gets the errors of a lexer and a parser.
*
* @param array<int|string, Lexer|Parser> $objs objects from where the errors will be extracted
*
* @return array<int, array<int, int|string|null>> 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. `[$msg, $code, $str, $pos]`)
*/
public static function get($objs)
{
$ret = [];
foreach ($objs as $obj) {
if ($obj instanceof Lexer) {
/** @var LexerException $err */
foreach ($obj->errors as $err) {
$ret[] = [
$err->getMessage(),
$err->getCode(),
$err->ch,
$err->pos,
];
}
} elseif ($obj instanceof Parser) {
/** @var ParserException $err */
foreach ($obj->errors as $err) {
$ret[] = [
$err->getMessage(),
$err->getCode(),
$err->token->token,
$err->token->position,
];
}
}
}
return $ret;
}
/**
* Formats the specified errors.
*
* @param array<int, array<int, int|string|null>> $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 string[]
*/
public static function format(
$errors,
$format = '#%1$d: %2$s (near "%4$s" at position %5$d)'
) {
$ret = [];
$i = 0;
foreach ($errors as $key => $err) {
$ret[$key] = sprintf(
$format,
++$i,
$err[0],
$err[1],
htmlspecialchars((string) $err[2]),
$err[3]
);
}
return $ret;
}
}

View File

@@ -0,0 +1,786 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Components\JoinKeyword;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function array_merge;
use function array_pop;
use function end;
use function htmlspecialchars;
use function in_array;
use function mb_strlen;
use function str_contains;
use function str_repeat;
use function str_replace;
use function strtoupper;
use const ENT_NOQUOTES;
use const PHP_SAPI;
/**
* Utilities that are used for formatting queries.
*/
class Formatter
{
/**
* The formatting options.
*
* @var array<string, bool|string|array<int, array<string, int|string>>>
*/
public $options;
/**
* Clauses that are usually short.
*
* These clauses share the line with the next clause.
*
* E.g. if INSERT was not here, the formatter would produce:
*
* INSERT
* INTO foo
* VALUES(0, 0, 0),(1, 1, 1);
*
* Instead of:
*
* INSERT INTO foo
* VALUES(0, 0, 0),(1, 1, 1)
*
* @var array<string, bool>
*/
public static $SHORT_CLAUSES = [
'CREATE' => true,
'INSERT' => true,
];
/**
* Clauses that must be inlined.
*
* These clauses usually are short and it's nicer to have them inline.
*
* @var array<string, bool>
*/
public static $INLINE_CLAUSES = [
'CREATE' => true,
'INTO' => true,
'LIMIT' => true,
'PARTITION BY' => true,
'PARTITION' => true,
'PROCEDURE' => true,
'SUBPARTITION BY' => true,
'VALUES' => true,
];
/**
* @param array<string, bool|string|array<int, array<string, int|string>>> $options the formatting options
*/
public function __construct(array $options = [])
{
$this->options = $this->getMergedOptions($options);
}
/**
* The specified formatting options are merged with the default values.
*
* @param array<string, bool|string|array<int, array<string, int|string>>> $options
*
* @return array<string, bool|string|array<int, array<string, int|string>>>
*/
protected function getMergedOptions(array $options)
{
$options = array_merge(
$this->getDefaultOptions(),
$options
);
if (isset($options['formats'])) {
$options['formats'] = self::mergeFormats($this->getDefaultFormats(), $options['formats']);
} else {
$options['formats'] = $this->getDefaultFormats();
}
if ($options['line_ending'] === null) {
$options['line_ending'] = $options['type'] === 'html' ? '<br/>' : "\n";
}
if ($options['indentation'] === null) {
$options['indentation'] = $options['type'] === 'html' ? '&nbsp;&nbsp;&nbsp;&nbsp;' : ' ';
}
// `parts_newline` requires `clause_newline`
$options['parts_newline'] &= $options['clause_newline'];
return $options;
}
/**
* The default formatting options.
*
* @return array<string, bool|string|null>
* @psalm-return array{
* type: ('cli'|'text'),
* line_ending: null,
* indentation: null,
* remove_comments: false,
* clause_newline: true,
* parts_newline: true,
* indent_parts: true
* }
*/
protected function getDefaultOptions()
{
return [
/*
* The format of the result.
*
* @var string The type ('text', 'cli' or 'html')
*/
'type' => PHP_SAPI === '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,
/*
* The string used for indentation.
*
* @var string
*/
'indentation' => null,
/*
* 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.
*
* @var bool
*/
'parts_newline' => true,
/*
* Whether each part of each clause should be indented.
*
* @var bool
*/
'indent_parts' => true,
];
}
/**
* The styles used for HTML formatting.
* [$type, $flags, $span, $callback].
*
* @return array<int, array<string, int|string>>
* @psalm-return list<array{type: int, flags: int, html: string, cli: string, function: string}>
*/
protected function getDefaultFormats()
{
return [
[
'type' => Token::TYPE_KEYWORD,
'flags' => Token::FLAG_KEYWORD_RESERVED,
'html' => 'class="sql-reserved"',
'cli' => "\x1b[35m",
'function' => 'strtoupper',
],
[
'type' => Token::TYPE_KEYWORD,
'flags' => 0,
'html' => 'class="sql-keyword"',
'cli' => "\x1b[95m",
'function' => 'strtoupper',
],
[
'type' => Token::TYPE_COMMENT,
'flags' => 0,
'html' => 'class="sql-comment"',
'cli' => "\x1b[37m",
'function' => '',
],
[
'type' => Token::TYPE_BOOL,
'flags' => 0,
'html' => 'class="sql-atom"',
'cli' => "\x1b[36m",
'function' => 'strtoupper',
],
[
'type' => Token::TYPE_NUMBER,
'flags' => 0,
'html' => 'class="sql-number"',
'cli' => "\x1b[92m",
'function' => 'strtolower',
],
[
'type' => Token::TYPE_STRING,
'flags' => 0,
'html' => 'class="sql-string"',
'cli' => "\x1b[91m",
'function' => '',
],
[
'type' => Token::TYPE_SYMBOL,
'flags' => Token::FLAG_SYMBOL_PARAMETER,
'html' => 'class="sql-parameter"',
'cli' => "\x1b[31m",
'function' => '',
],
[
'type' => Token::TYPE_SYMBOL,
'flags' => 0,
'html' => 'class="sql-variable"',
'cli' => "\x1b[36m",
'function' => '',
],
];
}
/**
* @param array<int, array<string, int|string>> $formats
* @param array<int, array<string, int|string>> $newFormats
*
* @return array<int, array<string, int|string>>
*/
private static function mergeFormats(array $formats, array $newFormats): array
{
$added = [];
$integers = [
'flags',
'type',
];
$strings = [
'html',
'cli',
'function',
];
/* 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])) {
continue;
}
$newFormats[$j][$name] = 0;
}
foreach ($strings as $name) {
if (isset($new[$name])) {
continue;
}
$newFormats[$j][$name] = '';
}
}
/* Process changes to existing formats */
foreach ($formats as $i => $original) {
foreach ($newFormats as $j => $new) {
if ($new['type'] !== $original['type'] || $original['flags'] !== $new['flags']) {
continue;
}
$formats[$i] = $new;
$added[] = $j;
}
}
/* Add not already handled formats */
foreach ($newFormats as $j => $new) {
if (in_array($j, $added)) {
continue;
}
$formats[] = $new;
}
return $formats;
}
/**
* Formats the given list of tokens.
*
* @param TokensList $list the list of tokens
*
* @return string
*/
public function formatList($list)
{
/**
* The query to be returned.
*
* @var string
*/
$ret = '';
/**
* The indentation level.
*
* @var int
*/
$indent = 0;
/**
* Whether the line ended.
*
* @var bool
*/
$lineEnded = false;
/**
* Whether current group is short (no linebreaks).
*
* @var bool
*/
$shortGroup = false;
/**
* The name of the last clause.
*
* @var string
*/
$lastClause = '';
/**
* A stack that keeps track of the indentation level every time a new
* block is found.
*/
$blocksIndentation = [];
/**
* A stack that keeps track of the line endings every time a new block
* is found.
*/
$blocksLineEndings = [];
/**
* Whether clause's options were formatted.
*
* @var bool
*/
$formattedOptions = false;
/**
* Previously parsed token.
*
* @var Token|null
*/
$prev = null;
// In order to be able to format the queries correctly, the next token
// must be taken into consideration. The loop below uses two pointers,
// `$prev` and `$curr` which store two consecutive tokens.
// Actually, at every iteration the previous token is being used.
for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$curr = $list->tokens[$list->idx];
if ($list->idx + 1 < $list->count) {
$next = $list->tokens[$list->idx + 1];
} else {
$next = null;
}
if ($curr->type === Token::TYPE_WHITESPACE) {
// Keep linebreaks before and after comments
if (
str_contains($curr->token, "\n") && (
($prev !== null && $prev->type === Token::TYPE_COMMENT) ||
($next !== null && $next->type === Token::TYPE_COMMENT)
)
) {
$lineEnded = true;
}
// Whitespaces are skipped because the formatter adds its own.
continue;
}
if ($curr->type === Token::TYPE_COMMENT && $this->options['remove_comments']) {
// Skip Comments if option `remove_comments` is enabled
continue;
}
// Checking if pointers were initialized.
if ($prev !== null) {
// Checking if a new clause started.
if (static::isClause($prev) !== false) {
$lastClause = $prev->value;
$formattedOptions = false;
}
// The options of a clause should stay on the same line and everything that follows.
if (
$this->options['parts_newline']
&& ! $formattedOptions
&& empty(self::$INLINE_CLAUSES[$lastClause])
&& (
$curr->type !== Token::TYPE_KEYWORD
|| (
$curr->type === Token::TYPE_KEYWORD
&& $curr->flags & Token::FLAG_KEYWORD_FUNCTION
)
)
) {
$formattedOptions = true;
$lineEnded = true;
++$indent;
}
// Checking if this clause ended.
$isClause = static::isClause($curr);
if ($isClause) {
if (
($isClause === 2 || $this->options['clause_newline'])
&& empty(self::$SHORT_CLAUSES[$lastClause])
) {
$lineEnded = true;
if ($this->options['parts_newline'] && $indent > 0) {
--$indent;
}
}
}
// Inline JOINs
if (
($prev->type === Token::TYPE_KEYWORD && isset(JoinKeyword::$JOINS[$prev->value]))
|| (in_array($curr->value, ['ON', 'USING'], true)
&& isset(JoinKeyword::$JOINS[$list->tokens[$list->idx - 2]->value]))
|| isset($list->tokens[$list->idx - 4], JoinKeyword::$JOINS[$list->tokens[$list->idx - 4]->value])
|| isset($list->tokens[$list->idx - 6], JoinKeyword::$JOINS[$list->tokens[$list->idx - 6]->value])
) {
$lineEnded = false;
}
// Indenting BEGIN ... END blocks.
if ($prev->type === Token::TYPE_KEYWORD && $prev->keyword === 'BEGIN') {
$lineEnded = true;
$blocksIndentation[] = $indent;
++$indent;
} elseif ($curr->type === Token::TYPE_KEYWORD && $curr->keyword === 'END') {
$lineEnded = true;
$indent = array_pop($blocksIndentation);
}
// Formatting fragments delimited by comma.
if ($prev->type === Token::TYPE_OPERATOR && $prev->value === ',') {
// Fragments delimited by a comma are broken into multiple
// pieces only if the clause is not inlined or this fragment
// is between brackets that are on new line.
if (
end($blocksLineEndings) === true
|| (
empty(self::$INLINE_CLAUSES[$lastClause])
&& ! $shortGroup
&& $this->options['parts_newline']
)
) {
$lineEnded = true;
}
}
// Handling brackets.
// Brackets are indented only if the length of the fragment between
// them is longer than 30 characters.
if ($prev->type === Token::TYPE_OPERATOR && $prev->value === '(') {
$blocksIndentation[] = $indent;
$shortGroup = true;
if (static::getGroupLength($list) > 30) {
++$indent;
$lineEnded = true;
$shortGroup = false;
}
$blocksLineEndings[] = $lineEnded;
} elseif ($curr->type === Token::TYPE_OPERATOR && $curr->value === ')') {
$indent = array_pop($blocksIndentation);
$lineEnded |= array_pop($blocksLineEndings);
$shortGroup = false;
}
// Adding the token.
$ret .= $this->toString($prev);
// Finishing the line.
if ($lineEnded) {
$ret .= $this->options['line_ending']
. str_repeat($this->options['indentation'], (int) $indent);
$lineEnded = false;
} else {
// If the line ended there is no point in adding whitespaces.
// Also, some tokens do not have spaces before or after them.
if (
// A space after delimiters that are longer than 2 characters.
$prev->keyword === 'DELIMITER'
|| ! (
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
// No space after . (
|| ($curr->type === Token::TYPE_OPERATOR
&& ($curr->value === '.' || $curr->value === ','
|| $curr->value === '(' || $curr->value === ')'))
// No space before . , ( )
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen((string) $curr->value, 'UTF-8') < 2
)
) {
$ret .= ' ';
}
}
}
// Iteration finished, consider current token as previous.
$prev = $curr;
}
if ($this->options['type'] === 'cli') {
return $ret . "\x1b[0m";
}
return $ret;
}
public function escapeConsole(string $string): string
{
return str_replace(
[
"\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
);
}
/**
* Tries to print the query and returns the result.
*
* @param Token $token the token to be printed
*
* @return string
*/
public function toString($token)
{
$text = $token->token;
static $prev;
foreach ($this->options['formats'] as $format) {
if ($token->type !== $format['type'] || ! (($token->flags & $format['flags']) === $format['flags'])) {
continue;
}
// Running transformation function.
if (! empty($format['function'])) {
$func = $format['function'];
$text = $func($text);
}
// Formatting HTML.
if ($this->options['type'] === 'html') {
return '<span ' . $format['html'] . '>' . htmlspecialchars($text, ENT_NOQUOTES) . '</span>';
}
if ($this->options['type'] === 'cli') {
if ($prev !== $format['cli']) {
$prev = $format['cli'];
return $format['cli'] . $this->escapeConsole($text);
}
return $this->escapeConsole($text);
}
break;
}
if ($this->options['type'] === 'cli') {
if ($prev !== "\x1b[39m") {
$prev = "\x1b[39m";
return "\x1b[39m" . $this->escapeConsole($text);
}
return $this->escapeConsole($text);
}
if ($this->options['type'] === 'html') {
return htmlspecialchars($text, ENT_NOQUOTES);
}
return $text;
}
/**
* Formats a query.
*
* @param string $query The query to be formatted
* @param array<string, bool|string|array<int, array<string, int|string>>> $options the formatting options
*
* @return string the formatted string
*/
public static function format($query, array $options = [])
{
$lexer = new Lexer($query);
$formatter = new self($options);
return $formatter->formatList($lexer->list);
}
/**
* Computes the length of a group.
*
* A group is delimited by a pair of brackets.
*
* @param TokensList $list the list of tokens
*
* @return int
*/
public static function getGroupLength($list)
{
/**
* The number of opening brackets found.
* This counter starts at one because by the time this function called,
* the list already advanced one position and the opening bracket was
* already parsed.
*
* @var int
*/
$count = 1;
/**
* The length of this group.
*
* @var int
*/
$length = 0;
for ($idx = $list->idx; $idx < $list->count; ++$idx) {
// Counting the brackets.
if ($list->tokens[$idx]->type === Token::TYPE_OPERATOR) {
if ($list->tokens[$idx]->value === '(') {
++$count;
} elseif ($list->tokens[$idx]->value === ')') {
--$count;
if ($count === 0) {
break;
}
}
}
// Keeping track of this group's length.
$length += mb_strlen((string) $list->tokens[$idx]->value, 'UTF-8');
}
return $length;
}
/**
* Checks if a token is a statement or a clause inside a statement.
*
* @param Token $token the token to be checked
*
* @return int|bool
*/
public static function isClause($token)
{
if (
($token->type === Token::TYPE_KEYWORD && isset(Parser::$STATEMENT_PARSERS[$token->keyword]))
|| ($token->type === Token::TYPE_NONE && strtoupper($token->token) === 'DELIMITER')
) {
return 2;
}
if ($token->type === Token::TYPE_KEYWORD && isset(Parser::$KEYWORD_PARSERS[$token->keyword])) {
return 1;
}
return false;
}
}

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
/**
* Miscellaneous utilities.
*/
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
*
* @return array<string, array<string, array<string, array<string, array<string, string>|string|null>>|null>>
*/
public static function getAliases($statement, $database)
{
if (! ($statement instanceof SelectStatement) || empty($statement->expr) || empty($statement->from)) {
return [];
}
$retval = [];
$tables = [];
/**
* Expressions that may contain aliases.
* These are extracted from `FROM` and `JOIN` keywords.
*
* @var Expression[]
*/
$expressions = $statement->from;
// Adding expressions from JOIN.
if (! empty($statement->join)) {
foreach ($statement->join as $join) {
$expressions[] = $join->expr;
}
}
foreach ($expressions as $expr) {
if (! isset($expr->table) || ($expr->table === '')) {
continue;
}
$thisDb = isset($expr->database) && ($expr->database !== '') ?
$expr->database : $database;
if (! isset($retval[$thisDb])) {
$retval[$thisDb] = [
'alias' => null,
'tables' => [],
];
}
if (! isset($retval[$thisDb]['tables'][$expr->table])) {
$retval[$thisDb]['tables'][$expr->table] = [
'alias' => isset($expr->alias) && ($expr->alias !== '') ?
$expr->alias : null,
'columns' => [],
];
}
if (! isset($tables[$thisDb])) {
$tables[$thisDb] = [];
}
$tables[$thisDb][$expr->alias] = $expr->table;
}
foreach ($statement->expr as $expr) {
if (! isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '')) {
continue;
}
$thisDb = isset($expr->database) && ($expr->database !== '') ?
$expr->database : $database;
if (isset($expr->table) && ($expr->table !== '')) {
$thisTable = $tables[$thisDb][$expr->table] ?? $expr->table;
$retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias;
} else {
foreach ($retval[$thisDb]['tables'] as &$table) {
$table['columns'][$expr->column] = $expr->alias;
}
}
}
return $retval;
}
}

View File

@@ -0,0 +1,943 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\Statements\AlterStatement;
use PhpMyAdmin\SqlParser\Statements\AnalyzeStatement;
use PhpMyAdmin\SqlParser\Statements\CallStatement;
use PhpMyAdmin\SqlParser\Statements\CheckStatement;
use PhpMyAdmin\SqlParser\Statements\ChecksumStatement;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\Statements\DeleteStatement;
use PhpMyAdmin\SqlParser\Statements\DropStatement;
use PhpMyAdmin\SqlParser\Statements\ExplainStatement;
use PhpMyAdmin\SqlParser\Statements\InsertStatement;
use PhpMyAdmin\SqlParser\Statements\LoadStatement;
use PhpMyAdmin\SqlParser\Statements\OptimizeStatement;
use PhpMyAdmin\SqlParser\Statements\RenameStatement;
use PhpMyAdmin\SqlParser\Statements\RepairStatement;
use PhpMyAdmin\SqlParser\Statements\ReplaceStatement;
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
use PhpMyAdmin\SqlParser\Statements\SetStatement;
use PhpMyAdmin\SqlParser\Statements\ShowStatement;
use PhpMyAdmin\SqlParser\Statements\TruncateStatement;
use PhpMyAdmin\SqlParser\Statements\UpdateStatement;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function array_flip;
use function array_keys;
use function count;
use function in_array;
use function is_string;
use function trim;
/**
* Statement utilities.
*
* @psalm-type QueryFlagsType = array{
* distinct?: bool,
* drop_database?: bool,
* group?: bool,
* having?: bool,
* is_affected?: bool,
* is_analyse?: bool,
* is_count?: bool,
* is_delete?: bool,
* is_explain?: bool,
* is_export?: bool,
* is_func?: bool,
* is_group?: bool,
* is_insert?: bool,
* is_maint?: bool,
* is_procedure?: bool,
* is_replace?: bool,
* is_select?: bool,
* is_show?: bool,
* is_subquery?: bool,
* join?: bool,
* limit?: bool,
* offset?: bool,
* order?: bool,
* querytype: ('ALTER'|'ANALYZE'|'CALL'|'CHECK'|'CHECKSUM'|'CREATE'|'DELETE'|'DROP'|'EXPLAIN'|'INSERT'|'LOAD'|'OPTIMIZE'|'REPAIR'|'REPLACE'|'SELECT'|'SET'|'SHOW'|'UPDATE'|false),
* reload?: bool,
* select_from?: bool,
* union?: bool
* }
*/
class Query
{
/**
* Functions that set the flag `is_func`.
*
* @var string[]
*/
public static $FUNCTIONS = [
'SUM',
'AVG',
'STD',
'STDDEV',
'MIN',
'MAX',
'BIT_OR',
'BIT_AND',
];
/**
* @var array<string, false>
* @psalm-var array{
* distinct: false,
* drop_database: false,
* group: false,
* having: false,
* is_affected: false,
* is_analyse: false,
* is_count: false,
* is_delete: false,
* is_explain: false,
* is_export: false,
* is_func: false,
* is_group: false,
* is_insert: false,
* is_maint: false,
* is_procedure: false,
* is_replace: false,
* is_select: false,
* is_show: false,
* is_subquery: false,
* join: false,
* limit: false,
* offset: false,
* order: false,
* querytype: false,
* reload: false,
* select_from: false,
* union: false
* }
*/
public static $ALLFLAGS = [
/*
* select ... DISTINCT ...
*/
'distinct' => false,
/*
* drop ... DATABASE ...
*/
'drop_database' => false,
/*
* ... GROUP BY ...
*/
'group' => false,
/*
* ... HAVING ...
*/
'having' => false,
/*
* INSERT ...
* or
* REPLACE ...
* or
* DELETE ...
*/
'is_affected' => false,
/*
* select ... PROCEDURE ANALYSE( ... ) ...
*/
'is_analyse' => false,
/*
* select COUNT( ... ) ...
*/
'is_count' => false,
/*
* DELETE ...
*/
'is_delete' => false, // @deprecated; use `querytype`
/*
* EXPLAIN ...
*/
'is_explain' => false, // @deprecated; use `querytype`
/*
* select ... INTO OUTFILE ...
*/
'is_export' => false,
/*
* select FUNC( ... ) ...
*/
'is_func' => false,
/*
* select ... GROUP BY ...
* or
* select ... HAVING ...
*/
'is_group' => false,
/*
* INSERT ...
* or
* REPLACE ...
* or
* LOAD DATA ...
*/
'is_insert' => false,
/*
* ANALYZE ...
* or
* CHECK ...
* or
* CHECKSUM ...
* or
* OPTIMIZE ...
* or
* REPAIR ...
*/
'is_maint' => false,
/*
* CALL ...
*/
'is_procedure' => false,
/*
* REPLACE ...
*/
'is_replace' => false, // @deprecated; use `querytype`
/*
* SELECT ...
*/
'is_select' => false, // @deprecated; use `querytype`
/*
* SHOW ...
*/
'is_show' => false, // @deprecated; use `querytype`
/*
* Contains a subquery.
*/
'is_subquery' => false,
/*
* ... JOIN ...
*/
'join' => false,
/*
* ... LIMIT ...
*/
'limit' => false,
/*
* TODO
*/
'offset' => false,
/*
* ... ORDER ...
*/
'order' => false,
/*
* The type of the query (which is usually the first keyword of
* the statement).
*/
'querytype' => false,
/*
* Whether a page reload is required.
*/
'reload' => false,
/*
* SELECT ... FROM ...
*/
'select_from' => false,
/*
* ... UNION ...
*/
'union' => false,
];
/**
* Gets an array with flags select statement has.
*
* @param SelectStatement $statement the statement to be processed
* @param array<string, bool|string> $flags flags set so far
* @psalm-param QueryFlagsType $flags
*
* @return array<string, bool|string>
* @psalm-return QueryFlagsType
*/
private static function getFlagsSelect($statement, $flags)
{
$flags['querytype'] = 'SELECT';
$flags['is_select'] = true;
if (! empty($statement->from)) {
$flags['select_from'] = true;
}
if ($statement->options->has('DISTINCT')) {
$flags['distinct'] = true;
}
if (! empty($statement->group) || ! empty($statement->having)) {
$flags['is_group'] = true;
}
if (! empty($statement->into) && ($statement->into->type === 'OUTFILE')) {
$flags['is_export'] = true;
}
$expressions = $statement->expr;
if (! empty($statement->join)) {
foreach ($statement->join as $join) {
$expressions[] = $join->expr;
}
}
foreach ($expressions as $expr) {
if (! empty($expr->function)) {
if ($expr->function === 'COUNT') {
$flags['is_count'] = true;
} elseif (in_array($expr->function, static::$FUNCTIONS)) {
$flags['is_func'] = true;
}
}
if (empty($expr->subquery)) {
continue;
}
$flags['is_subquery'] = true;
}
if (! empty($statement->procedure) && ($statement->procedure->name === 'ANALYSE')) {
$flags['is_analyse'] = true;
}
if (! empty($statement->group)) {
$flags['group'] = true;
}
if (! empty($statement->having)) {
$flags['having'] = true;
}
if (! empty($statement->union)) {
$flags['union'] = true;
}
if (! empty($statement->join)) {
$flags['join'] = true;
}
return $flags;
}
/**
* 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
*
* @return array<string, bool|string>
* @psalm-return QueryFlagsType
*/
public static function getFlags($statement, $all = false)
{
$flags = ['querytype' => false];
if ($all) {
$flags = self::$ALLFLAGS;
}
if ($statement instanceof AlterStatement) {
$flags['querytype'] = 'ALTER';
$flags['reload'] = true;
} elseif ($statement instanceof CreateStatement) {
$flags['querytype'] = 'CREATE';
$flags['reload'] = true;
} elseif ($statement instanceof AnalyzeStatement) {
$flags['querytype'] = 'ANALYZE';
$flags['is_maint'] = true;
} elseif ($statement instanceof CheckStatement) {
$flags['querytype'] = 'CHECK';
$flags['is_maint'] = true;
} elseif ($statement instanceof ChecksumStatement) {
$flags['querytype'] = 'CHECKSUM';
$flags['is_maint'] = true;
} elseif ($statement instanceof OptimizeStatement) {
$flags['querytype'] = 'OPTIMIZE';
$flags['is_maint'] = true;
} elseif ($statement instanceof RepairStatement) {
$flags['querytype'] = 'REPAIR';
$flags['is_maint'] = true;
} elseif ($statement instanceof CallStatement) {
$flags['querytype'] = 'CALL';
$flags['is_procedure'] = true;
} elseif ($statement instanceof DeleteStatement) {
$flags['querytype'] = 'DELETE';
$flags['is_delete'] = true;
$flags['is_affected'] = true;
} elseif ($statement instanceof DropStatement) {
$flags['querytype'] = 'DROP';
$flags['reload'] = true;
if ($statement->options->has('DATABASE') || $statement->options->has('SCHEMA')) {
$flags['drop_database'] = true;
}
} elseif ($statement instanceof ExplainStatement) {
$flags['querytype'] = 'EXPLAIN';
$flags['is_explain'] = true;
} elseif ($statement instanceof InsertStatement) {
$flags['querytype'] = 'INSERT';
$flags['is_affected'] = true;
$flags['is_insert'] = true;
} elseif ($statement instanceof LoadStatement) {
$flags['querytype'] = 'LOAD';
$flags['is_affected'] = true;
$flags['is_insert'] = true;
} elseif ($statement instanceof ReplaceStatement) {
$flags['querytype'] = 'REPLACE';
$flags['is_affected'] = true;
$flags['is_replace'] = true;
$flags['is_insert'] = true;
} elseif ($statement instanceof SelectStatement) {
$flags = self::getFlagsSelect($statement, $flags);
} elseif ($statement instanceof ShowStatement) {
$flags['querytype'] = 'SHOW';
$flags['is_show'] = true;
} elseif ($statement instanceof UpdateStatement) {
$flags['querytype'] = 'UPDATE';
$flags['is_affected'] = true;
} elseif ($statement instanceof SetStatement) {
$flags['querytype'] = 'SET';
}
if (
($statement instanceof SelectStatement)
|| ($statement instanceof UpdateStatement)
|| ($statement instanceof DeleteStatement)
) {
if (! empty($statement->limit)) {
$flags['limit'] = true;
}
if (! empty($statement->order)) {
$flags['order'] = true;
}
}
return $flags;
}
/**
* Parses a query and gets all information about it.
*
* @param string $query the query to be parsed
*
* @return array<string, bool|string> 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
* @psalm-return QueryFlagsType&array{
* select_expr?: (string|null)[],
* select_tables?: array{string, string|null}[],
* statement?: Statement|null, parser?: Parser
* }
*/
public static function getAll($query)
{
$parser = new Parser($query);
if (empty($parser->statements[0])) {
return static::getFlags(null, true);
}
$statement = $parser->statements[0];
$ret = static::getFlags($statement, true);
$ret['parser'] = $parser;
$ret['statement'] = $statement;
if ($statement instanceof SelectStatement) {
$ret['select_tables'] = [];
$ret['select_expr'] = [];
// Finding tables' aliases and their associated real names.
$tableAliases = [];
foreach ($statement->from as $expr) {
if (! isset($expr->table, $expr->alias) || ($expr->table === '') || ($expr->alias === '')) {
continue;
}
$tableAliases[$expr->alias] = [
$expr->table,
$expr->database ?? null,
];
}
// Trying to find selected tables only from the select expression.
// Sometimes, this is not possible because the tables aren't defined
// explicitly (e.g. SELECT * FROM film, SELECT film_id FROM film).
foreach ($statement->expr as $expr) {
if (isset($expr->table) && ($expr->table !== '')) {
if (isset($tableAliases[$expr->table])) {
$arr = $tableAliases[$expr->table];
} else {
$arr = [
$expr->table,
isset($expr->database) && ($expr->database !== '') ?
$expr->database : null,
];
}
if (! in_array($arr, $ret['select_tables'])) {
$ret['select_tables'][] = $arr;
}
} else {
$ret['select_expr'][] = $expr->expr;
}
}
// If no tables names were found in the SELECT clause or if there
// are expressions like * or COUNT(*), etc. tables names should be
// extracted from the FROM clause.
if (empty($ret['select_tables'])) {
foreach ($statement->from as $expr) {
if (! isset($expr->table) || ($expr->table === '')) {
continue;
}
$arr = [
$expr->table,
isset($expr->database) && ($expr->database !== '') ?
$expr->database : null,
];
if (in_array($arr, $ret['select_tables'])) {
continue;
}
$ret['select_tables'][] = $arr;
}
}
}
return $ret;
}
/**
* Gets a list of all tables used in this statement.
*
* @param Statement $statement statement to be scanned
*
* @return array<int, string>
*/
public static function getTables($statement)
{
$expressions = [];
if (($statement instanceof InsertStatement) || ($statement instanceof ReplaceStatement)) {
$expressions = [$statement->into->dest];
} elseif ($statement instanceof UpdateStatement) {
$expressions = $statement->tables;
} elseif (($statement instanceof SelectStatement) || ($statement instanceof DeleteStatement)) {
$expressions = $statement->from;
} elseif (($statement instanceof AlterStatement) || ($statement instanceof TruncateStatement)) {
$expressions = [$statement->table];
} elseif ($statement instanceof DropStatement) {
if (! $statement->options->has('TABLE')) {
// No tables are dropped.
return [];
}
$expressions = $statement->fields;
} elseif ($statement instanceof RenameStatement) {
foreach ($statement->renames as $rename) {
$expressions[] = $rename->old;
}
}
$ret = [];
foreach ($expressions as $expr) {
if (empty($expr->table)) {
continue;
}
$expr->expr = null; // Force rebuild.
$expr->alias = null; // Aliases are not required.
$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 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
* If string, the name of the first clause that
* should not be included.
* @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 = 0;
/**
* The count of brackets.
* We keep track of them so we won't insert the clause in a subquery.
*
* @var int
*/
$brackets = 0;
/**
* The string to be returned.
*
* @var string
*/
$ret = '';
/**
* The clauses of this type of statement and their index.
*/
$clauses = array_flip(array_keys($statement->getClauses()));
/**
* Lexer used for lexing the clause.
*/
$lexer = new Lexer($clause);
/**
* The type of this clause.
*
* @var string
*/
$clauseType = $lexer->list->getNextOfType(Token::TYPE_KEYWORD)->keyword;
/**
* The index of this clause.
*/
$clauseIdx = $clauses[$clauseType] ?? -1;
$firstClauseIdx = $clauseIdx;
$lastClauseIdx = $clauseIdx;
// Determining the behavior of this function.
if ($type === -1) {
$firstClauseIdx = -1; // Something small enough.
$lastClauseIdx = $clauseIdx - 1;
} elseif ($type === 1) {
$firstClauseIdx = $clauseIdx + 1;
$lastClauseIdx = 10000; // Something big enough.
} elseif (is_string($type) && isset($clauses[$type])) {
if ($clauses[$type] > $clauseIdx) {
$firstClauseIdx = $clauseIdx + 1;
$lastClauseIdx = $clauses[$type] - 1;
} else {
$firstClauseIdx = $clauses[$type] + 1;
$lastClauseIdx = $clauseIdx - 1;
}
}
// This option is unavailable for multiple clauses.
if ($type !== 0) {
$skipFirst = false;
}
for ($i = $statement->first; $i <= $statement->last; ++$i) {
$token = $list->tokens[$i];
if ($token->type === Token::TYPE_COMMENT) {
continue;
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
--$brackets;
}
}
if ($brackets === 0) {
// Checking if the section was changed.
if (
($token->type === Token::TYPE_KEYWORD)
&& isset($clauses[$token->keyword])
&& ($clauses[$token->keyword] >= $currIdx)
) {
$currIdx = $clauses[$token->keyword];
if ($skipFirst && ($currIdx === $clauseIdx)) {
// This token is skipped (not added to the old
// clause) because it will be replaced.
continue;
}
}
}
if (($firstClauseIdx > $currIdx) || ($currIdx > $lastClauseIdx)) {
continue;
}
$ret .= $token->token;
}
return trim($ret);
}
/**
* Builds a query by rebuilding the statement from the tokens list supplied
* and replaces a clause.
*
* 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 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
*
* @return string
*/
public static function replaceClause($statement, $list, $old, $new = null, $onlyType = false)
{
// TODO: Update the tokens list and the statement.
if ($new === null) {
$new = $old;
}
if ($onlyType) {
return static::getClause($statement, $list, $old, -1, false) . ' ' .
$new . ' ' . static::getClause($statement, $list, $old, 0) . ' ' .
static::getClause($statement, $list, $old, 1, false);
}
return static::getClause($statement, $list, $old, -1, false) . ' ' .
$new . ' ' . static::getClause($statement, $list, $old, 1, false);
}
/**
* 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 array<int, array<int, string>> $ops Clauses to be replaced. Contains multiple
* arrays having two values: [$old, $new].
* Clauses must be sorted.
*
* @return string
*/
public static function replaceClauses($statement, $list, array $ops)
{
$count = count($ops);
// Nothing to do.
if ($count === 0) {
return '';
}
/**
* Value to be returned.
*
* @var string
*/
$ret = '';
// If there is only one clause, `replaceClause()` should be used.
if ($count === 1) {
return static::replaceClause($statement, $list, $ops[0][0], $ops[0][1]);
}
// Adding everything before first replacement.
$ret .= static::getClause($statement, $list, $ops[0][0], -1) . ' ';
// Doing replacements.
foreach ($ops as $i => $clause) {
$ret .= $clause[1] . ' ';
// Adding everything between this and next replacement.
if ($i + 1 === $count) {
continue;
}
$ret .= static::getClause($statement, $list, $clause[0], $ops[$i + 1][0]) . ' ';
}
// Adding everything after the last replacement.
return $ret . static::getClause($statement, $list, $ops[$count - 1][0], 1);
}
/**
* Gets the first full statement in the query.
*
* @param string $query the query to be analyzed
* @param string $delimiter the delimiter to be used
*
* @return array<int, string|null> array containing the first full query,
* the remaining part of the query and the last delimiter
* @psalm-return array{string|null, string, string|null}
*/
public static function getFirstStatement($query, $delimiter = null)
{
$lexer = new Lexer($query, false, $delimiter);
$list = $lexer->list;
/**
* Whether a full statement was found.
*
* @var bool
*/
$fullStatement = false;
/**
* The first full statement.
*
* @var string
*/
$statement = '';
for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];
if ($token->type === Token::TYPE_COMMENT) {
continue;
}
$statement .= $token->token;
if (($token->type === Token::TYPE_DELIMITER) && ! empty($token->token)) {
$delimiter = $token->token;
$fullStatement = true;
break;
}
}
// No statement was found so we return the entire query as being the
// remaining part.
if (! $fullStatement) {
return [
null,
$query,
$delimiter,
];
}
// At least one query was found so we have to build the rest of the
// remaining query.
$query = '';
for (++$list->idx; $list->idx < $list->count; ++$list->idx) {
$query .= $list->tokens[$list->idx]->token;
}
return [
trim($statement),
$query,
$delimiter,
];
}
/**
* 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
*
* @return int
*/
public static function getClauseStartOffset($statement, $list, $clause)
{
/**
* The count of brackets.
* We keep track of them so we won't insert the clause in a subquery.
*
* @var int
*/
$brackets = 0;
/**
* The clauses of this type of statement and their index.
*/
$clauses = array_flip(array_keys($statement->getClauses()));
for ($i = $statement->first; $i <= $statement->last; ++$i) {
$token = $list->tokens[$i];
if ($token->type === Token::TYPE_COMMENT) {
continue;
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
--$brackets;
}
}
if ($brackets !== 0) {
continue;
}
if (
($token->type === Token::TYPE_KEYWORD)
&& isset($clauses[$token->keyword])
&& ($clause === $token->keyword)
) {
return $i;
}
if ($token->keyword === 'UNION') {
return -1;
}
}
return -1;
}
}

View File

@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Components\DataType;
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use function implode;
use function is_string;
/**
* Routine utilities.
*/
class Routine
{
/**
* Parses a parameter of a routine.
*
* @param string $param parameter's definition
*
* @return string[]
*/
public static function getReturnType($param)
{
$lexer = new Lexer($param);
// A dummy parser is used for error reporting.
$type = DataType::parse(new Parser(), $lexer->list);
if ($type === null) {
return [
'',
'',
'',
'',
'',
];
}
$options = [];
foreach ($type->options->options as $opt) {
$options[] = is_string($opt) ? $opt : $opt['value'];
}
return [
'',
'',
$type->name,
implode(',', $type->parameters),
implode(' ', $options),
];
}
/**
* Parses a parameter of a routine.
*
* @param string $param parameter's definition
*
* @return string[]
*/
public static function getParameter($param)
{
$lexer = new Lexer('(' . $param . ')');
// A dummy parser is used for error reporting.
$param = ParameterDefinition::parse(new Parser(), $lexer->list);
if (empty($param[0])) {
return [
'',
'',
'',
'',
'',
];
}
$param = $param[0];
$options = [];
foreach ($param->type->options->options as $opt) {
$options[] = is_string($opt) ? $opt : $opt['value'];
}
return [
empty($param->inOut) ? '' : $param->inOut,
$param->name,
$param->type->name,
implode(',', $param->type->parameters),
implode(' ', $options),
];
}
/**
* Gets the parameters of a routine from the parse tree.
*
* @param CreateStatement $statement the statement to be processed
*
* @return array<string, int|array<int, mixed[]|string|null>>
*/
public static function getParameters($statement)
{
$retval = [
'num' => 0,
'dir' => [],
'name' => [],
'type' => [],
'length' => [],
'length_arr' => [],
'opts' => [],
];
if (! empty($statement->parameters)) {
$idx = 0;
foreach ($statement->parameters as $param) {
$retval['dir'][$idx] = $param->inOut;
$retval['name'][$idx] = $param->name;
$retval['type'][$idx] = $param->type->name;
$retval['length'][$idx] = implode(',', $param->type->parameters);
$retval['length_arr'][$idx] = $param->type->parameters;
$retval['opts'][$idx] = [];
foreach ($param->type->options->options as $opt) {
$retval['opts'][$idx][] = is_string($opt) ?
$opt : $opt['value'];
}
$retval['opts'][$idx] = implode(' ', $retval['opts'][$idx]);
++$idx;
}
$retval['num'] = $idx;
}
return $retval;
}
}

View File

@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use function is_array;
use function str_replace;
/**
* Table utilities.
*/
class Table
{
/**
* Gets the foreign keys of the table.
*
* @param CreateStatement $statement the statement to be processed
*
* @return array<int, array<string, mixed[]|string|null>>
*/
public static function getForeignKeys($statement)
{
if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
return [];
}
$ret = [];
foreach ($statement->fields as $field) {
if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) {
continue;
}
$columns = [];
foreach ($field->key->columns as $column) {
if (! isset($column['name'])) {
continue;
}
$columns[] = $column['name'];
}
$tmp = [
'constraint' => $field->name,
'index_list' => $columns,
];
if (! empty($field->references)) {
$tmp['ref_db_name'] = $field->references->table->database;
$tmp['ref_table_name'] = $field->references->table->table;
$tmp['ref_index_list'] = $field->references->columns;
$opt = $field->references->options->has('ON UPDATE');
if ($opt) {
$tmp['on_update'] = str_replace(' ', '_', $opt);
}
$opt = $field->references->options->has('ON DELETE');
if ($opt) {
$tmp['on_delete'] = str_replace(' ', '_', $opt);
}
}
$ret[] = $tmp;
}
return $ret;
}
/**
* Gets fields of the table.
*
* @param CreateStatement $statement the statement to be processed
*
* @return array<int|string, array<string, bool|string|mixed>>
*/
public static function getFields($statement)
{
if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
return [];
}
$ret = [];
foreach ($statement->fields as $field) {
// Skipping keys.
if (empty($field->type)) {
continue;
}
$ret[$field->name] = [
'type' => $field->type->name,
'timestamp_not_null' => false,
];
if (! $field->options) {
continue;
}
if ($field->type->name === 'TIMESTAMP') {
if ($field->options->has('NOT NULL')) {
$ret[$field->name]['timestamp_not_null'] = true;
}
}
$option = $field->options->has('DEFAULT');
if ($option) {
$ret[$field->name]['default_value'] = $option;
if ($option === 'CURRENT_TIMESTAMP') {
$ret[$field->name]['default_current_timestamp'] = true;
}
}
$option = $field->options->has('ON UPDATE');
if ($option === 'CURRENT_TIMESTAMP') {
$ret[$field->name]['on_update_current_timestamp'] = true;
}
$option = $field->options->has('AS');
if (! $option) {
continue;
}
$ret[$field->name]['generated'] = true;
$ret[$field->name]['expr'] = $option;
}
return $ret;
}
}

View File

@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\UtfString;
use function count;
use function strcasecmp;
/**
* Token utilities.
*/
class Tokens
{
/**
* Checks if a pattern is a match for the specified token.
*
* @param Token $token the token to be matched
* @param array<string, int|string|null> $pattern the pattern to be matches
*
* @return bool
*/
public static function match(Token $token, array $pattern)
{
// Token.
if (isset($pattern['token']) && ($pattern['token'] !== $token->token)) {
return false;
}
// Value.
if (isset($pattern['value']) && ($pattern['value'] !== $token->value)) {
return false;
}
if (isset($pattern['value_str']) && strcasecmp($pattern['value_str'], (string) $token->value)) {
return false;
}
// Type.
if (isset($pattern['type']) && ($pattern['type'] !== $token->type)) {
return false;
}
// Flags.
return ! isset($pattern['flags'])
|| (! (($pattern['flags'] & $token->flags) === 0));
}
/**
* @param TokensList|string|UtfString $list
* @param Token[] $find
* @param Token[] $replace
*
* @return TokensList
*/
public static function replaceTokens($list, array $find, array $replace)
{
/**
* Whether the first parameter is a list.
*/
$isList = $list instanceof TokensList;
// Parsing the tokens.
if (! $isList) {
$list = Lexer::getTokens($list);
}
/**
* The list to be returned.
*
* @var Token[]
*/
$newList = [];
/**
* The length of the find pattern is calculated only once.
*
* @var int
*/
$findCount = count($find);
/**
* The starting index of the pattern.
*
* @var int
*/
$i = 0;
while ($i < $list->count) {
// A sequence may not start with a comment.
if ($list->tokens[$i]->type === Token::TYPE_COMMENT) {
$newList[] = $list->tokens[$i];
++$i;
continue;
}
/**
* The index used to parse `$list->tokens`.
*
* This index might be running faster than `$k` because some tokens
* are skipped.
*/
$j = $i;
/**
* The index used to parse `$find`.
*
* This index might be running slower than `$j` because some tokens
* are skipped.
*
* @var int
*/
$k = 0;
// Checking if the next tokens match the pattern described.
while (($j < $list->count) && ($k < $findCount)) {
// Comments are being skipped.
if ($list->tokens[$j]->type === Token::TYPE_COMMENT) {
++$j;
}
if (! static::match($list->tokens[$j], $find[$k])) {
// This token does not match the pattern.
break;
}
// Going to next token and segment of find pattern.
++$j;
++$k;
}
// Checking if the sequence was found.
if ($k === $findCount) {
// Inserting new tokens.
foreach ($replace as $token) {
$newList[] = $token;
}
// Skipping next `$findCount` tokens.
$i = $j;
} else {
// Adding the same token.
$newList[] = $list->tokens[$i];
++$i;
}
}
return $isList ? new TokensList($newList) : TokensList::build($newList);
}
}