update parser_sql

This commit is contained in:
shreesh arora
2021-05-03 01:06:30 +05:30
parent b7ef2391cc
commit d91def3d84
172 changed files with 8743 additions and 2571 deletions

View File

@@ -4,54 +4,80 @@
*
* This file implements the processor for the create definition within the TABLE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once dirname(__FILE__) . '/AbstractProcessor.php';
require_once dirname(__FILE__) . '/ColumnDefinitionProcessor.php';
require_once dirname(__FILE__) . '/IndexColumnListProcessor.php';
require_once dirname(__FILE__) . '/ReferenceDefinitionProcessor.php';
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the create definition of the TABLE statements.
*
* @author arothe
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateDefinitionProcessor extends AbstractProcessor {
protected function processExpressionList($parsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($parsed);
}
protected function processIndexColumnList($parsed) {
$processor = new IndexColumnListProcessor($this->options);
return $processor->process($parsed);
}
protected function processColumnDefinition($parsed) {
$processor = new ColumnDefinitionProcessor($this->options);
return $processor->process($parsed);
}
protected function processReferenceDefinition($parsed) {
$processor = new ReferenceDefinitionProcessor($this->options);
return $processor->process($parsed);
}
protected function correctExpressionType(&$expr) {
$type = ExpressionType::EXPRESSION;
if (!isset($expr[0]) || !isset($expr[0]['expr_type'])) {
return $type;
}
# replace the constraint type with a more descriptive one
// replace the constraint type with a more descriptive one
switch ($expr[0]['expr_type']) {
case ExpressionType::CONSTRAINT:
@@ -74,9 +100,9 @@ class CreateDefinitionProcessor extends AbstractProcessor {
public function process($tokens) {
$base_expr = "";
$prevCategory = "";
$currCategory = "";
$base_expr = '';
$prevCategory = '';
$currCategory = '';
$expr = array();
$result = array();
$skip = 0;
@@ -91,7 +117,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue;
}
if ($trim === "") {
if ($trim === '') {
continue;
}
@@ -110,36 +136,36 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'FOREIGN':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT') {
$expr[] = array('expr_type' => ExpressionType::FOREIGN_KEY, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'PRIMARY':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
# next one is KEY
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT') {
// next one is KEY
$expr[] = array('expr_type' => ExpressionType::PRIMARY_KEY, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'UNIQUE':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
# next one is KEY
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT' || $prevCategory === 'INDEX_COL_LIST') {
// next one is KEY
$expr[] = array('expr_type' => ExpressionType::UNIQUE_IDX, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'KEY':
# the next one is an index name
// the next one is an index name
if ($currCategory === 'PRIMARY' || $currCategory === 'FOREIGN' || $currCategory === 'UNIQUE') {
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
@@ -173,7 +199,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'WITH':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_PARSER,
@@ -186,7 +212,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
case 'KEY_BLOCK_SIZE':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_SIZE,
@@ -199,7 +225,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
case 'USING':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST' || $currCategory === 'PRIMARY') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('base_expr' => substr($base_expr, 0, -strlen($token)), 'trim' => $trim,
@@ -208,18 +234,18 @@ class CreateDefinitionProcessor extends AbstractProcessor {
$currCategory = 'INDEX_TYPE';
continue 2;
}
# else ?
// else ?
break;
case 'REFERENCES':
if ($currCategory === 'INDEX_COL_LIST' && $prevCategory === 'FOREIGN') {
$processor = new ReferenceDefinitionProcessor();
$refs = $processor->process(array_slice($tokens, $k - 1, null, true));
$refs = $this->processReferenceDefinition(array_slice($tokens, $k - 1, null, true));
$skip = $refs['till'] - $k;
unset($refs['till']);
$expr[] = $refs;
$currCategory = $upper;
}
// else ?
break;
case 'BTREE':
@@ -231,16 +257,16 @@ class CreateDefinitionProcessor extends AbstractProcessor {
'sub_tree' => $last['sub_tree']);
$base_expr = $last['base_expr'] . $base_expr;
# FIXME: it could be wrong for index_type within index_option
// FIXME: it could be wrong for index_type within index_option
$currCategory = $last['category'];
continue 2;
}
#else
// else ?
break;
case '=':
if ($currCategory === 'INDEX_SIZE') {
# the optional character between KEY_BLOCK_SIZE and the numeric constant
// the optional character between KEY_BLOCK_SIZE and the numeric constant
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = $last;
@@ -255,16 +281,16 @@ class CreateDefinitionProcessor extends AbstractProcessor {
$expr[] = $last;
continue 2;
}
# else?
// else ?
break;
case ',':
# this starts the next definition
// this starts the next definition
$type = $this->correctExpressionType($expr);
$result['create-def'][] = array('expr_type' => $type,
'base_expr' => trim(substr($base_expr, 0, -strlen($token))),
'sub_tree' => $expr);
$base_expr = "";
$base_expr = '';
$expr = array();
break;
@@ -272,36 +298,34 @@ class CreateDefinitionProcessor extends AbstractProcessor {
switch ($currCategory) {
case 'LIKE':
# this is the tablename after LIKE
// this is the tablename after LIKE
$expr[] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
break;
case 'PRIMARY':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
# the column list
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
// the column list
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# else?
// else?
break;
case 'FOREIGN':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# index name
// index name
$expr[] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
continue 3;
@@ -309,20 +333,19 @@ class CreateDefinitionProcessor extends AbstractProcessor {
case 'UNIQUE':
case 'INDEX':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# index name
// index name
$expr[] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
continue 3;
case 'CONSTRAINT':
# constraint name
// constraint name
$last = array_pop($expr);
$last['base_expr'] = $base_expr;
$last['sub_tree'] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
@@ -330,7 +353,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 3;
case 'INDEX_PARSER':
# index parser name
// index parser name
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_PARSER, 'base_expr' => $base_expr,
@@ -340,7 +363,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 3;
case 'INDEX_SIZE':
# index key block size numeric constant
// index key block size numeric constant
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_SIZE, 'base_expr' => $base_expr,
@@ -351,28 +374,26 @@ class CreateDefinitionProcessor extends AbstractProcessor {
case 'CHECK':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new ExpressionListProcessor();
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$parsed = $processor->process($unparsed);
$parsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$parsed = $this->processExpressionList($parsed);
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
}
# else?
// else?
break;
case '':
# if the currCategory is empty, we have an unknown token,
# which is a column reference
// if the currCategory is empty, we have an unknown token,
// which is a column reference
$expr[] = array('expr_type' => ExpressionType::COLREF, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
$currCategory = 'COLUMN_NAME';
continue 3;
case 'COLUMN_NAME':
# the column-definition
# it stops on a comma or on a parenthesis
$processor = new ColumnDefinitionProcessor();
$parsed = $processor->process(array_slice($tokens, $k, null, true), $expr);
// the column-definition
// it stops on a comma or on a parenthesis
$parsed = $this->processColumnDefinition(array_slice($tokens, $k, null, true));
$skip = $parsed['till'] - $k;
unset($parsed['till']);
$expr[] = $parsed;
@@ -380,7 +401,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
default:
# ?
// ?
break;
}
break;
@@ -394,4 +415,4 @@ class CreateDefinitionProcessor extends AbstractProcessor {
return $result;
}
}
?>
?>