Initial commit
This commit is contained in:
416
#pma/libraries/sql-parser/src/Utils/BufferedQuery.php
Normal file
416
#pma/libraries/sql-parser/src/Utils/BufferedQuery.php
Normal file
@ -0,0 +1,416 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Buffered query utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Context;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
|
||||
// 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
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public $options = array();
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* The last incomplete query that was extracted.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $current = '';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
'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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
$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).
|
||||
*
|
||||
* @var int $loopLen
|
||||
*/
|
||||
$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 & static::STATUS_COMMENT) == 0) && ($this->query[$i] === '\\')) {
|
||||
$this->current .= $this->query[$i] . $this->query[++$i];
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handling special parses statuses.
|
||||
*/
|
||||
if ($this->status === static::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 === static::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 === static::STATUS_STRING_BACKTICK) {
|
||||
if ($this->query[$i] === '`') {
|
||||
$this->status = 0;
|
||||
}
|
||||
$this->current .= $this->query[$i];
|
||||
continue;
|
||||
} elseif (($this->status === static::STATUS_COMMENT_BASH)
|
||||
|| ($this->status === static::STATUS_COMMENT_SQL)
|
||||
) {
|
||||
// Bash-like (#) or SQL-like (-- ) comments end in new line.
|
||||
if ($this->query[$i] === "\n") {
|
||||
$this->status = 0;
|
||||
}
|
||||
continue;
|
||||
} elseif ($this->status === static::STATUS_COMMENT_C) {
|
||||
// C-like comments end in */.
|
||||
if (($this->query[$i - 1] === '*') && ($this->query[$i] === '/')) {
|
||||
$this->status = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checking if a string started.
|
||||
*/
|
||||
if ($this->query[$i] === '\'') {
|
||||
$this->status = static::STATUS_STRING_SINGLE_QUOTES;
|
||||
$this->current .= $this->query[$i];
|
||||
continue;
|
||||
} elseif ($this->query[$i] === '"') {
|
||||
$this->status = static::STATUS_STRING_DOUBLE_QUOTES;
|
||||
$this->current .= $this->query[$i];
|
||||
continue;
|
||||
} elseif ($this->query[$i] === '`') {
|
||||
$this->status = static::STATUS_STRING_BACKTICK;
|
||||
$this->current .= $this->query[$i];
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checking if a comment started.
|
||||
*/
|
||||
if ($this->query[$i] === '#') {
|
||||
$this->status = static::STATUS_COMMENT_BASH;
|
||||
continue;
|
||||
} elseif (($i + 2 < $len)
|
||||
&& ($this->query[$i] === '-')
|
||||
&& ($this->query[$i + 1] === '-')
|
||||
&& (Context::isWhitespace($this->query[$i + 2]))
|
||||
) {
|
||||
$this->status = static::STATUS_COMMENT_SQL;
|
||||
continue;
|
||||
} elseif (($i + 2 < $len)
|
||||
&& ($this->query[$i] === '/')
|
||||
&& ($this->query[$i + 1] === '*')
|
||||
&& ($this->query[$i + 2] !== '!')
|
||||
) {
|
||||
$this->status = static::STATUS_COMMENT_C;
|
||||
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]))
|
||||
&& (trim($this->current) === '')
|
||||
) {
|
||||
// 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 '';
|
||||
}
|
||||
}
|
127
#pma/libraries/sql-parser/src/Utils/CLI.php
Normal file
127
#pma/libraries/sql-parser/src/Utils/CLI.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CLI interface
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Parser;
|
||||
use SqlParser\Lexer;
|
||||
|
||||
/**
|
||||
* CLI interface
|
||||
*
|
||||
* @category Exceptions
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
|
||||
*/
|
||||
class CLI
|
||||
{
|
||||
public function mergeLongOpts(&$params, &$longopts)
|
||||
{
|
||||
foreach ($longopts as $value) {
|
||||
$value = rtrim($value, ':');
|
||||
if (isset($params[$value])) {
|
||||
$params[$value[0]] = $params[$value];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function usageHighlight()
|
||||
{
|
||||
echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
|
||||
}
|
||||
|
||||
public function getopt($opt, $long)
|
||||
{
|
||||
return getopt($opt, $long);
|
||||
}
|
||||
|
||||
public function parseHighlight()
|
||||
{
|
||||
$longopts = array('help', 'query:', 'format:');
|
||||
$params = $this->getopt(
|
||||
'hq:f:', $longopts
|
||||
);
|
||||
if ($params === false) {
|
||||
return false;
|
||||
}
|
||||
$this->mergeLongOpts($params, $longopts);
|
||||
if (! isset($params['f'])) {
|
||||
$params['f'] = 'cli';
|
||||
}
|
||||
if (! in_array($params['f'], array('html', 'cli', 'text'))) {
|
||||
echo "ERROR: Invalid value for format!\n";
|
||||
return false;
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function runHighlight()
|
||||
{
|
||||
$params = $this->parseHighlight();
|
||||
if ($params === false) {
|
||||
return 1;
|
||||
}
|
||||
if (isset($params['h'])) {
|
||||
$this->usageHighlight();
|
||||
return 0;
|
||||
}
|
||||
if (isset($params['q'])) {
|
||||
echo Formatter::format(
|
||||
$params['q'], array('type' => $params['f'])
|
||||
);
|
||||
echo "\n";
|
||||
return 0;
|
||||
}
|
||||
echo "ERROR: Missing parameters!\n";
|
||||
$this->usageHighlight();
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function usageLint()
|
||||
{
|
||||
echo "Usage: lint-query --query SQL\n";
|
||||
}
|
||||
|
||||
public function parseLint()
|
||||
{
|
||||
$longopts = array('help', 'query:');
|
||||
$params = $this->getopt(
|
||||
'hq:', $longopts
|
||||
);
|
||||
$this->mergeLongOpts($params, $longopts);
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function runLint()
|
||||
{
|
||||
$params = $this->parseLint();
|
||||
if ($params === false) {
|
||||
return 1;
|
||||
}
|
||||
if (isset($params['h'])) {
|
||||
$this->usageLint();
|
||||
return 0;
|
||||
}
|
||||
if (isset($params['q'])) {
|
||||
$lexer = new Lexer($params['q'], false);
|
||||
$parser = new Parser($lexer->list);
|
||||
$errors = Error::get(array($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;
|
||||
}
|
||||
}
|
99
#pma/libraries/sql-parser/src/Utils/Error.php
Normal file
99
#pma/libraries/sql-parser/src/Utils/Error.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Error related utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Lexer;
|
||||
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.
|
||||
*
|
||||
* @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)`)
|
||||
*/
|
||||
public static function get($objs)
|
||||
{
|
||||
$ret = array();
|
||||
|
||||
foreach ($objs as $obj) {
|
||||
if ($obj instanceof Lexer) {
|
||||
foreach ($obj->errors as $err) {
|
||||
$ret[] = array(
|
||||
$err->getMessage(),
|
||||
$err->getCode(),
|
||||
$err->ch,
|
||||
$err->pos
|
||||
);
|
||||
}
|
||||
} elseif ($obj instanceof Parser) {
|
||||
foreach ($obj->errors as $err) {
|
||||
$ret[] = array(
|
||||
$err->getMessage(),
|
||||
$err->getCode(),
|
||||
$err->token->token,
|
||||
$err->token->position
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return array
|
||||
*/
|
||||
public static function format(
|
||||
$errors,
|
||||
$format = '#%1$d: %2$s (near "%4$s" at position %5$d)'
|
||||
) {
|
||||
$ret = array();
|
||||
|
||||
$i = 0;
|
||||
foreach ($errors as $key => $err) {
|
||||
$ret[$key] = sprintf(
|
||||
$format,
|
||||
++$i,
|
||||
$err[0],
|
||||
$err[1],
|
||||
htmlspecialchars($err[2]),
|
||||
$err[3]
|
||||
);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
573
#pma/libraries/sql-parser/src/Utils/Formatter.php
Normal file
573
#pma/libraries/sql-parser/src/Utils/Formatter.php
Normal file
@ -0,0 +1,573 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Utilities that are used for formatting queries.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Lexer;
|
||||
use SqlParser\Parser;
|
||||
use SqlParser\Token;
|
||||
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.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* Clauses that must be inlined.
|
||||
*
|
||||
* These clauses usually are short and it's nicer to have them inline.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $INLINE_CLAUSES = array(
|
||||
'CREATE' => true,
|
||||
'LIMIT' => true,
|
||||
'PARTITION BY' => true,
|
||||
'PARTITION' => true,
|
||||
'PROCEDURE' => true,
|
||||
'SUBPARTITION BY' => true,
|
||||
'VALUES' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $options The formatting options.
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
// The specified formatting options are merged with the default values.
|
||||
$this->options = array_merge(
|
||||
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,
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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.
|
||||
* array($type, $flags, $span, $callback)
|
||||
*
|
||||
* @var array[]
|
||||
*/
|
||||
'formats' => array(
|
||||
array(
|
||||
'type' => Token::TYPE_KEYWORD,
|
||||
'flags' => Token::FLAG_KEYWORD_RESERVED,
|
||||
'html' => 'class="sql-reserved"',
|
||||
'cli' => "\x1b[35m",
|
||||
'function' => 'strtoupper',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_KEYWORD,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-keyword"',
|
||||
'cli' => "\x1b[95m",
|
||||
'function' => 'strtoupper',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_COMMENT,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-comment"',
|
||||
'cli' => "\x1b[37m",
|
||||
'function' => '',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_BOOL,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-atom"',
|
||||
'cli' => "\x1b[36m",
|
||||
'function' => 'strtoupper',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_NUMBER,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-number"',
|
||||
'cli' => "\x1b[92m",
|
||||
'function' => 'strtolower',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_STRING,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-string"',
|
||||
'cli' => "\x1b[91m",
|
||||
'function' => '',
|
||||
),
|
||||
array(
|
||||
'type' => Token::TYPE_SYMBOL,
|
||||
'flags' => 0,
|
||||
'html' => 'class="sql-variable"',
|
||||
'cli' => "\x1b[36m",
|
||||
'function' => '',
|
||||
),
|
||||
)
|
||||
),
|
||||
$options
|
||||
);
|
||||
|
||||
if (is_null($this->options['line_ending'])) {
|
||||
$this->options['line_ending'] = $this->options['type'] == 'html' ? '<br/>' : "\n";
|
||||
}
|
||||
|
||||
// `parts_newline` requires `clause_newline`
|
||||
$this->options['parts_newline'] &= $this->options['clause_newline'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
$ret = '';
|
||||
|
||||
/**
|
||||
* The indentation level.
|
||||
*
|
||||
* @var int $indent
|
||||
*/
|
||||
$indent = 0;
|
||||
|
||||
/**
|
||||
* Whether the line ended.
|
||||
*
|
||||
* @var bool $lineEnded
|
||||
*/
|
||||
$lineEnded = false;
|
||||
|
||||
/**
|
||||
* Whether current group is short (no linebreaks)
|
||||
*
|
||||
* @var bool $shortGroup
|
||||
*/
|
||||
$shortGroup = false;
|
||||
|
||||
/**
|
||||
* The name of the last clause.
|
||||
*
|
||||
* @var string $lastClause
|
||||
*/
|
||||
$lastClause = '';
|
||||
|
||||
/**
|
||||
* A stack that keeps track of the indentation level every time a new
|
||||
* block is found.
|
||||
*
|
||||
* @var array $blocksIndentation
|
||||
*/
|
||||
$blocksIndentation = array();
|
||||
|
||||
/**
|
||||
* A stack that keeps track of the line endings every time a new block
|
||||
* is found.
|
||||
*
|
||||
* @var array $blocksLineEndings
|
||||
*/
|
||||
$blocksLineEndings = array();
|
||||
|
||||
/**
|
||||
* Whether clause's options were formatted.
|
||||
*
|
||||
* @var bool $formattedOptions
|
||||
*/
|
||||
$formattedOptions = false;
|
||||
|
||||
/**
|
||||
* Previously parsed token.
|
||||
*
|
||||
* @var Token $prev
|
||||
*/
|
||||
$prev = null;
|
||||
|
||||
/**
|
||||
* Comments are being formatted separately to maintain the whitespaces
|
||||
* before and after them.
|
||||
*
|
||||
* @var string $comment
|
||||
*/
|
||||
$comment = '';
|
||||
|
||||
// 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.
|
||||
*
|
||||
* @var Token $curr
|
||||
*/
|
||||
$curr = $list->tokens[$list->idx];
|
||||
|
||||
if ($curr->type === Token::TYPE_WHITESPACE) {
|
||||
// Whitespaces are skipped because the formatter adds its own.
|
||||
continue;
|
||||
} elseif ($curr->type === Token::TYPE_COMMENT) {
|
||||
// Whether the comments should be parsed.
|
||||
if (!empty($this->options['remove_comments'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($list->tokens[$list->idx - 1]->type === Token::TYPE_WHITESPACE) {
|
||||
// The whitespaces before and after are preserved for
|
||||
// formatting reasons.
|
||||
$comment .= $list->tokens[$list->idx - 1]->token;
|
||||
}
|
||||
$comment .= $this->toString($curr);
|
||||
if (($list->tokens[$list->idx + 1]->type === Token::TYPE_WHITESPACE)
|
||||
&& ($list->tokens[$list->idx + 2]->type !== Token::TYPE_COMMENT)
|
||||
) {
|
||||
// Adding the next whitespace only there is no comment that
|
||||
// follows it immediately which may cause adding a
|
||||
// whitespace twice.
|
||||
$comment .= $list->tokens[$list->idx + 1]->token;
|
||||
}
|
||||
|
||||
// Everything was handled here, no need to continue.
|
||||
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.
|
||||
if ($tmp = static::isClause($curr)) {
|
||||
if (($tmp == 2) || ($this->options['clause_newline'])) {
|
||||
$lineEnded = true;
|
||||
if ($this->options['parts_newline']) {
|
||||
--$indent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Indenting BEGIN ... END blocks.
|
||||
if (($prev->type === Token::TYPE_KEYWORD) && ($prev->value === 'BEGIN')) {
|
||||
$lineEnded = true;
|
||||
array_push($blocksIndentation, $indent);
|
||||
++$indent;
|
||||
} elseif (($curr->type === Token::TYPE_KEYWORD) && ($curr->value === '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 (((empty(self::$INLINE_CLAUSES[$lastClause]))
|
||||
&& ! $shortGroup
|
||||
&& ($this->options['parts_newline']))
|
||||
|| (end($blocksLineEndings) === true)
|
||||
) {
|
||||
$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 === '(')) {
|
||||
array_push($blocksIndentation, $indent);
|
||||
$shortGroup = true;
|
||||
if (static::getGroupLength($list) > 30) {
|
||||
++$indent;
|
||||
$lineEnded = true;
|
||||
$shortGroup = false;
|
||||
}
|
||||
array_push($blocksLineEndings, $lineEnded);
|
||||
} elseif (($curr->type === Token::TYPE_OPERATOR) && ($curr->value === ')')) {
|
||||
$indent = array_pop($blocksIndentation);
|
||||
$lineEnded |= array_pop($blocksLineEndings);
|
||||
$shortGroup = false;
|
||||
}
|
||||
|
||||
// Delimiter must be placed on the same line with the last
|
||||
// clause.
|
||||
if ($curr->type === Token::TYPE_DELIMITER) {
|
||||
$lineEnded = false;
|
||||
}
|
||||
|
||||
// Adding the token.
|
||||
$ret .= $this->toString($prev);
|
||||
|
||||
// Finishing the line.
|
||||
if ($lineEnded) {
|
||||
if ($indent < 0) {
|
||||
// TODO: Make sure this never occurs and delete it.
|
||||
$indent = 0;
|
||||
}
|
||||
|
||||
if ($curr->type !== Token::TYPE_COMMENT) {
|
||||
$ret .= $this->options['line_ending']
|
||||
. str_repeat($this->options['indentation'], $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 (!((($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($curr->value, 'UTF-8') < 2))
|
||||
// A space after delimiters that are longer than 2 characters.
|
||||
|| ($prev->value === 'DELIMITER')
|
||||
) {
|
||||
$ret .= ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($comment)) {
|
||||
$ret .= $comment;
|
||||
$comment = '';
|
||||
}
|
||||
|
||||
// Iteration finished, consider current token as previous.
|
||||
$prev = $curr;
|
||||
}
|
||||
|
||||
if ($this->options['type'] === 'cli') {
|
||||
return $ret . "\x1b[0m";
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function escapeConsole($string)
|
||||
{
|
||||
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"
|
||||
),
|
||||
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'
|
||||
),
|
||||
$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;
|
||||
|
||||
foreach ($this->options['formats'] as $format) {
|
||||
if (($token->type === $format['type'])
|
||||
&& (($token->flags & $format['flags']) === $format['flags'])
|
||||
) {
|
||||
// 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>';
|
||||
} elseif ($this->options['type'] === 'cli') {
|
||||
return $format['cli'] . $this->escapeConsole($text);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->options['type'] === 'cli') {
|
||||
return "\x1b[39m" . $this->escapeConsole($text);
|
||||
} elseif ($this->options['type'] === 'html') {
|
||||
return htmlspecialchars($text, ENT_NOQUOTES);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a query.
|
||||
*
|
||||
* @param string $query The query to be formatted
|
||||
* @param array $options The formatting options.
|
||||
*
|
||||
* @return string The formatted string.
|
||||
*/
|
||||
public static function format($query, array $options = array())
|
||||
{
|
||||
$lexer = new Lexer($query);
|
||||
$formatter = new Formatter($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
|
||||
*/
|
||||
$count = 1;
|
||||
|
||||
/**
|
||||
* The length of this group.
|
||||
*
|
||||
* @var int $length
|
||||
*/
|
||||
$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($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_NONE) && (strtoupper($token->token) === 'DELIMITER'))
|
||||
|| (($token->type === Token::TYPE_KEYWORD) && (isset(Parser::$STATEMENT_PARSERS[$token->value])))
|
||||
) {
|
||||
return 2;
|
||||
} elseif (($token->type === Token::TYPE_KEYWORD) && (isset(Parser::$KEYWORD_PARSERS[$token->value]))) {
|
||||
return 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
113
#pma/libraries/sql-parser/src/Utils/Misc.php
Normal file
113
#pma/libraries/sql-parser/src/Utils/Misc.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Miscellaneous utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Components\Expression;
|
||||
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.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAliases($statement, $database)
|
||||
{
|
||||
if (!($statement instanceof SelectStatement)
|
||||
|| (empty($statement->expr))
|
||||
|| (empty($statement->from))
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$retval = array();
|
||||
|
||||
$tables = array();
|
||||
|
||||
/**
|
||||
* Expressions that may contain aliases.
|
||||
* These are extracted from `FROM` and `JOIN` keywords.
|
||||
*
|
||||
* @var Expression[] $expressions
|
||||
*/
|
||||
$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] = array(
|
||||
'alias' => null,
|
||||
'tables' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
if (!isset($retval[$thisDb]['tables'][$expr->table])) {
|
||||
$retval[$thisDb]['tables'][$expr->table] = array(
|
||||
'alias' => ((isset($expr->alias)) && ($expr->alias !== '')) ?
|
||||
$expr->alias : null,
|
||||
'columns' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
if (!isset($tables[$thisDb])) {
|
||||
$tables[$thisDb] = array();
|
||||
}
|
||||
$tables[$thisDb][$expr->alias] = $expr->table;
|
||||
}
|
||||
|
||||
foreach ($statement->expr as $expr) {
|
||||
if ((!isset($expr->column)) || ($expr->column === '')
|
||||
|| (!isset($expr->alias)) || ($expr->alias === '')
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$thisDb = ((isset($expr->database)) && ($expr->database !== '')) ?
|
||||
$expr->database : $database;
|
||||
|
||||
if ((isset($expr->table)) && ($expr->table !== '')) {
|
||||
$thisTable = isset($tables[$thisDb][$expr->table]) ?
|
||||
$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;
|
||||
}
|
||||
}
|
840
#pma/libraries/sql-parser/src/Utils/Query.php
Normal file
840
#pma/libraries/sql-parser/src/Utils/Query.php
Normal file
@ -0,0 +1,840 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Statement utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Lexer;
|
||||
use SqlParser\Parser;
|
||||
use SqlParser\Statement;
|
||||
use SqlParser\Token;
|
||||
use SqlParser\TokensList;
|
||||
use SqlParser\Components\Expression;
|
||||
use SqlParser\Statements\AlterStatement;
|
||||
use SqlParser\Statements\AnalyzeStatement;
|
||||
use SqlParser\Statements\CallStatement;
|
||||
use SqlParser\Statements\CheckStatement;
|
||||
use SqlParser\Statements\ChecksumStatement;
|
||||
use SqlParser\Statements\CreateStatement;
|
||||
use SqlParser\Statements\DeleteStatement;
|
||||
use SqlParser\Statements\DropStatement;
|
||||
use SqlParser\Statements\ExplainStatement;
|
||||
use SqlParser\Statements\InsertStatement;
|
||||
use SqlParser\Statements\OptimizeStatement;
|
||||
use SqlParser\Statements\RenameStatement;
|
||||
use SqlParser\Statements\RepairStatement;
|
||||
use SqlParser\Statements\ReplaceStatement;
|
||||
use SqlParser\Statements\SelectStatement;
|
||||
use SqlParser\Statements\ShowStatement;
|
||||
use SqlParser\Statements\TruncateStatement;
|
||||
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'
|
||||
);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static function getFlags($statement, $all = false)
|
||||
{
|
||||
$flags = array();
|
||||
if ($all) {
|
||||
$flags = array(
|
||||
|
||||
/**
|
||||
* 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
|
||||
* TODO: 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
|
||||
);
|
||||
}
|
||||
|
||||
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 ReplaceStatement) {
|
||||
$flags['querytype'] = 'REPLACE';
|
||||
$flags['is_affected'] = true;
|
||||
$flags['is_replace'] = true;
|
||||
$flags['is_insert'] = true;
|
||||
} elseif ($statement instanceof SelectStatement) {
|
||||
$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)) {
|
||||
$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;
|
||||
}
|
||||
|
||||
} elseif ($statement instanceof ShowStatement) {
|
||||
$flags['querytype'] = 'SHOW';
|
||||
$flags['is_show'] = true;
|
||||
} elseif ($statement instanceof UpdateStatement) {
|
||||
$flags['querytype'] = 'UPDATE';
|
||||
$flags['is_affected'] = true;
|
||||
}
|
||||
|
||||
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 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
|
||||
*/
|
||||
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'] = array();
|
||||
$ret['select_expr'] = array();
|
||||
|
||||
// Finding tables' aliases and their associated real names.
|
||||
$tableAliases = array();
|
||||
foreach ($statement->from as $expr) {
|
||||
if ((isset($expr->table)) && ($expr->table !== '')
|
||||
&& (isset($expr->alias)) && ($expr->alias !== '')
|
||||
) {
|
||||
$tableAliases[$expr->alias] = array(
|
||||
$expr->table,
|
||||
isset($expr->database) ? $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 = array(
|
||||
$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 !== '')) {
|
||||
$arr = array(
|
||||
$expr->table,
|
||||
((isset($expr->database)) && ($expr->database !== '')) ?
|
||||
$expr->database : null
|
||||
);
|
||||
if (!in_array($arr, $ret['select_tables'])) {
|
||||
$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
|
||||
*/
|
||||
public static function getTables($statement)
|
||||
{
|
||||
$expressions = array();
|
||||
|
||||
if (($statement instanceof InsertStatement)
|
||||
|| ($statement instanceof ReplaceStatement)
|
||||
) {
|
||||
$expressions = array($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 = array($statement->table);
|
||||
} elseif ($statement instanceof DropStatement) {
|
||||
if (!$statement->options->has('TABLE')) {
|
||||
// No tables are dropped.
|
||||
return array();
|
||||
}
|
||||
$expressions = $statement->fields;
|
||||
} elseif ($statement instanceof RenameStatement) {
|
||||
foreach ($statement->renames as $rename) {
|
||||
$expressions[] = $rename->old;
|
||||
}
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
$currIdx = 0;
|
||||
|
||||
/**
|
||||
* The count of brackets.
|
||||
* We keep track of them so we won't insert the clause in a subquery.
|
||||
*
|
||||
* @var int $brackets
|
||||
*/
|
||||
$brackets = 0;
|
||||
|
||||
/**
|
||||
* The string to be returned.
|
||||
*
|
||||
* @var string $ret
|
||||
*/
|
||||
$ret = '';
|
||||
|
||||
/**
|
||||
* The clauses of this type of statement and their index.
|
||||
*
|
||||
* @var array $clauses
|
||||
*/
|
||||
$clauses = array_flip(array_keys($statement->getClauses()));
|
||||
|
||||
/**
|
||||
* Lexer used for lexing the clause.
|
||||
*
|
||||
* @var Lexer $lexer
|
||||
*/
|
||||
$lexer = new Lexer($clause);
|
||||
|
||||
/**
|
||||
* The type of this clause.
|
||||
*
|
||||
* @var string $clauseType
|
||||
*/
|
||||
$clauseType = $lexer->list->getNextOfType(Token::TYPE_KEYWORD)->value;
|
||||
|
||||
/**
|
||||
* The index of this clause.
|
||||
*
|
||||
* @var int $clauseIdx
|
||||
*/
|
||||
$clauseIdx = $clauses[$clauseType];
|
||||
|
||||
$firstClauseIdx = $clauseIdx;
|
||||
$lastClauseIdx = $clauseIdx + 1;
|
||||
|
||||
// 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)) {
|
||||
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->value]))
|
||||
&& ($clauses[$token->value] >= $currIdx)
|
||||
) {
|
||||
$currIdx = $clauses[$token->value];
|
||||
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)) {
|
||||
$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 $ops Clauses to be replaced. Contains multiple
|
||||
* arrays having two values: array($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
|
||||
*/
|
||||
$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.
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$ret .= $ops[$i][1] . ' ';
|
||||
|
||||
// Adding everything between this and next replacement.
|
||||
if ($i + 1 !== $count) {
|
||||
$ret .= static::getClause($statement, $list, $ops[$i][0], $ops[$i + 1][0]) . ' ';
|
||||
}
|
||||
}
|
||||
|
||||
// Adding everything after the last replacement.
|
||||
$ret .= static::getClause($statement, $list, $ops[$count - 1][0], 1);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Array containing the first full query, the
|
||||
* remaining part of the query and the last
|
||||
* delimiter.
|
||||
*/
|
||||
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
|
||||
*/
|
||||
$fullStatement = false;
|
||||
|
||||
/**
|
||||
* The first full statement.
|
||||
*
|
||||
* @var string $statement
|
||||
*/
|
||||
$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 array(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 array(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
|
||||
*/
|
||||
$brackets = 0;
|
||||
|
||||
/**
|
||||
* The clauses of this type of statement and their index.
|
||||
*
|
||||
* @var array $clauses
|
||||
*/
|
||||
$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) {
|
||||
if (($token->type === Token::TYPE_KEYWORD)
|
||||
&& (isset($clauses[$token->value]))
|
||||
&& ($clause === $token->value)
|
||||
) {
|
||||
return $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
135
#pma/libraries/sql-parser/src/Utils/Routine.php
Normal file
135
#pma/libraries/sql-parser/src/Utils/Routine.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Routine utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Lexer;
|
||||
use SqlParser\Parser;
|
||||
use SqlParser\Components\DataType;
|
||||
use SqlParser\Components\ParameterDefinition;
|
||||
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.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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 array('', '', '', '', '');
|
||||
}
|
||||
|
||||
$options = array();
|
||||
foreach ($type->options->options as $opt) {
|
||||
$options[] = is_string($opt) ? $opt : $opt['value'];
|
||||
}
|
||||
|
||||
return array(
|
||||
'',
|
||||
'',
|
||||
$type->name,
|
||||
implode(',', $type->parameters),
|
||||
implode(' ', $options)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a parameter of a routine.
|
||||
*
|
||||
* @param string $param Parameter's definition.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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 array('', '', '', '', '');
|
||||
}
|
||||
|
||||
$param = $param[0];
|
||||
|
||||
$options = array();
|
||||
foreach ($param->type->options->options as $opt) {
|
||||
$options[] = is_string($opt) ? $opt : $opt['value'];
|
||||
}
|
||||
|
||||
return array(
|
||||
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
|
||||
*/
|
||||
public static function getParameters($statement)
|
||||
{
|
||||
$retval = array(
|
||||
'num' => 0,
|
||||
'dir' => array(),
|
||||
'name' => array(),
|
||||
'type' => array(),
|
||||
'length' => array(),
|
||||
'length_arr' => array(),
|
||||
'opts' => array(),
|
||||
);
|
||||
|
||||
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] = array();
|
||||
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;
|
||||
}
|
||||
}
|
141
#pma/libraries/sql-parser/src/Utils/Table.php
Normal file
141
#pma/libraries/sql-parser/src/Utils/Table.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Table utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
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.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getForeignKeys($statement)
|
||||
{
|
||||
if ((empty($statement->fields))
|
||||
|| (!is_array($statement->fields))
|
||||
|| (!$statement->options->has('TABLE'))
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
|
||||
foreach ($statement->fields as $field) {
|
||||
if ((empty($field->key)) || ($field->key->type !== 'FOREIGN KEY')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columns = array();
|
||||
foreach ($field->key->columns as $column) {
|
||||
$columns[] = $column['name'];
|
||||
}
|
||||
|
||||
$tmp = array(
|
||||
'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;
|
||||
|
||||
if (($opt = $field->references->options->has('ON UPDATE'))) {
|
||||
$tmp['on_update'] = str_replace(' ', '_', $opt);
|
||||
}
|
||||
|
||||
if (($opt = $field->references->options->has('ON DELETE'))) {
|
||||
$tmp['on_delete'] = str_replace(' ', '_', $opt);
|
||||
}
|
||||
|
||||
// if (($opt = $field->references->options->has('MATCH'))) {
|
||||
// $tmp['match'] = str_replace(' ', '_', $opt);
|
||||
// }
|
||||
}
|
||||
|
||||
$ret[] = $tmp;
|
||||
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets fields of the table.
|
||||
*
|
||||
* @param CreateStatement $statement The statement to be processed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getFields($statement)
|
||||
{
|
||||
if ((empty($statement->fields))
|
||||
|| (!is_array($statement->fields))
|
||||
|| (!$statement->options->has('TABLE'))
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
|
||||
foreach ($statement->fields as $field) {
|
||||
// Skipping keys.
|
||||
if (empty($field->type)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ret[$field->name] = array(
|
||||
'type' => $field->type->name,
|
||||
'timestamp_not_null' => false,
|
||||
);
|
||||
|
||||
if ($field->options) {
|
||||
if ($field->type->name === 'TIMESTAMP') {
|
||||
if ($field->options->has('NOT NULL')) {
|
||||
$ret[$field->name]['timestamp_not_null'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (($option = $field->options->has('DEFAULT'))) {
|
||||
$ret[$field->name]['default_value'] = $option;
|
||||
if ($option === 'CURRENT_TIMESTAMP') {
|
||||
$ret[$field->name]['default_current_timestamp'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (($option = $field->options->has('ON UPDATE'))) {
|
||||
if ($option === 'CURRENT_TIMESTAMP') {
|
||||
$ret[$field->name]['on_update_current_timestamp'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (($option = $field->options->has('AS'))) {
|
||||
$ret[$field->name]['generated'] = true;
|
||||
$ret[$field->name]['expr'] = $option;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
174
#pma/libraries/sql-parser/src/Utils/Tokens.php
Normal file
174
#pma/libraries/sql-parser/src/Utils/Tokens.php
Normal file
@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Token utilities.
|
||||
*
|
||||
* @package SqlParser
|
||||
* @subpackage Utils
|
||||
*/
|
||||
namespace SqlParser\Utils;
|
||||
|
||||
use SqlParser\Lexer;
|
||||
use SqlParser\Token;
|
||||
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.
|
||||
*
|
||||
* @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'], $token->value))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Type.
|
||||
if ((isset($pattern['type']))
|
||||
&& ($pattern['type'] !== $token->type)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Flags.
|
||||
if ((isset($pattern['flags']))
|
||||
&& (($pattern['flags'] & $token->flags) === 0)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function replaceTokens($list, array $find, array $replace)
|
||||
{
|
||||
|
||||
/**
|
||||
* Whether the first parameter is a list.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
$isList = $list instanceof TokensList;
|
||||
|
||||
// Parsing the tokens.
|
||||
if (!$isList) {
|
||||
$list = Lexer::getTokens($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* The list to be returned.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
$newList = array();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
$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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user