update parser_sql
This commit is contained in:
@@ -4,41 +4,49 @@
|
||||
*
|
||||
* This file implements the processor for 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__) . '/CreateDefinitionProcessor.php');
|
||||
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
|
||||
namespace PHPSQLParser\processors;
|
||||
use PHPSQLParser\utils\ExpressionType;
|
||||
|
||||
/**
|
||||
*
|
||||
* This class processes 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 TableProcessor extends AbstractProcessor {
|
||||
@@ -55,6 +63,16 @@ class TableProcessor extends AbstractProcessor {
|
||||
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
|
||||
}
|
||||
|
||||
protected function processPartitionOptions($tokens) {
|
||||
$processor = new PartitionOptionsProcessor($this->options);
|
||||
return $processor->process($tokens);
|
||||
}
|
||||
|
||||
protected function processCreateDefinition($tokens) {
|
||||
$processor = new CreateDefinitionProcessor($this->options);
|
||||
return $processor->process($tokens);
|
||||
}
|
||||
|
||||
protected function clear(&$expr, &$base_expr, &$category) {
|
||||
$expr = array();
|
||||
$base_expr = '';
|
||||
@@ -63,14 +81,14 @@ class TableProcessor extends AbstractProcessor {
|
||||
|
||||
public function process($tokens) {
|
||||
|
||||
$currCategory = "TABLE_NAME";
|
||||
$currCategory = 'TABLE_NAME';
|
||||
$result = array('base_expr' => false, 'name' => false, 'no_quotes' => false, 'create-def' => false,
|
||||
'options' => false, 'like' => false, 'select-option' => false);
|
||||
$expr = array();
|
||||
$base_expr = '';
|
||||
$skip = 0;
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
foreach ($tokens as $tokenKey => $token) {
|
||||
$trim = trim($token);
|
||||
$base_expr .= $token;
|
||||
|
||||
@@ -83,7 +101,7 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($trim === "") {
|
||||
if ($trim === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -91,12 +109,12 @@ class TableProcessor extends AbstractProcessor {
|
||||
switch ($upper) {
|
||||
|
||||
case ',':
|
||||
# it is possible to separate the table options with comma!
|
||||
// it is possible to separate the table options with comma!
|
||||
if ($prevCategory === 'CREATE_DEF') {
|
||||
$last = array_pop($result['options']);
|
||||
$last['delim'] = ',';
|
||||
$result['options'][] = $last;
|
||||
$base_expr = "";
|
||||
$base_expr = '';
|
||||
}
|
||||
continue 2;
|
||||
|
||||
@@ -109,7 +127,7 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
case 'LIKE':
|
||||
# like without parenthesis
|
||||
// like without parenthesis
|
||||
if ($prevCategory === 'TABLE_NAME') {
|
||||
$currCategory = $upper;
|
||||
continue 2;
|
||||
@@ -117,10 +135,10 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
case '=':
|
||||
# the optional operator
|
||||
// the optional operator
|
||||
if ($prevCategory === 'TABLE_OPTION') {
|
||||
$expr[] = $this->getOperatorType($trim);
|
||||
continue 2; # don't change the category
|
||||
continue 2; // don't change the category
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -130,15 +148,16 @@ class TableProcessor extends AbstractProcessor {
|
||||
$currCategory = 'TABLE_OPTION';
|
||||
}
|
||||
if ($prevCategory === 'TABLE_OPTION') {
|
||||
# add it to the previous DEFAULT
|
||||
// add it to the previous DEFAULT
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
continue 2;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SET':
|
||||
case 'CHARSET':
|
||||
if ($prevCategory === 'TABLE_OPTION') {
|
||||
# add it to a previous CHARACTER
|
||||
// add it to a previous CHARACTER
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
$currCategory = 'CHARSET';
|
||||
continue 2;
|
||||
@@ -146,8 +165,8 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
case 'COLLATE':
|
||||
if ($prevCategory === 'TABLE_OPTION') {
|
||||
# add it to the previous DEFAULT
|
||||
if ($prevCategory === 'TABLE_OPTION' || $prevCategory === 'CREATE_DEF') {
|
||||
// add it to the previous DEFAULT
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
$currCategory = 'COLLATE';
|
||||
continue 2;
|
||||
@@ -156,7 +175,7 @@ class TableProcessor extends AbstractProcessor {
|
||||
|
||||
case 'DIRECTORY':
|
||||
if ($currCategory === 'INDEX_DIRECTORY' || $currCategory === 'DATA_DIRECTORY') {
|
||||
# after INDEX or DATA
|
||||
// after INDEX or DATA
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
continue 2;
|
||||
}
|
||||
@@ -212,16 +231,15 @@ class TableProcessor extends AbstractProcessor {
|
||||
case 'LAST':
|
||||
case 'DEFAULT':
|
||||
if ($prevCategory === 'CREATE_DEF') {
|
||||
# DEFAULT before CHARACTER SET and COLLATE
|
||||
// DEFAULT before CHARACTER SET and COLLATE
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
$currCategory = 'TABLE_OPTION';
|
||||
}
|
||||
if ($prevCategory === 'TABLE_OPTION') {
|
||||
# all assignments with the keywords
|
||||
// all assignments with the keywords
|
||||
$expr[] = $this->getReservedType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::EXPRESSION,
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $currCategory);
|
||||
}
|
||||
break;
|
||||
@@ -244,46 +262,47 @@ class TableProcessor extends AbstractProcessor {
|
||||
continue 2;
|
||||
|
||||
case 'PARTITION':
|
||||
# TODO: parse partition options
|
||||
$skip = -1;
|
||||
if ($prevCategory === 'CREATE_DEF') {
|
||||
$part = $this->processPartitionOptions(array_slice($tokens, $tokenKey - 1, null, true));
|
||||
$skip = $part['last-parsed'] - $tokenKey;
|
||||
$result['partition-options'] = $part['partition-options'];
|
||||
continue 2;
|
||||
}
|
||||
// else
|
||||
break;
|
||||
|
||||
default:
|
||||
switch ($currCategory) {
|
||||
|
||||
case 'CHARSET':
|
||||
# the charset name
|
||||
// the charset name
|
||||
$expr[] = $this->getConstantType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::CHARSET,
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $currCategory);
|
||||
break;
|
||||
|
||||
case 'COLLATE':
|
||||
# the collate name
|
||||
// the collate name
|
||||
$expr[] = $this->getConstantType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::COLLATE,
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $currCategory);
|
||||
break;
|
||||
|
||||
case 'DATA_DIRECTORY':
|
||||
# we have the directory name
|
||||
// we have the directory name
|
||||
$expr[] = $this->getConstantType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::DIRECTORY, 'kind' => 'DATA',
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $prevCategory);
|
||||
continue 3;
|
||||
|
||||
case 'INDEX_DIRECTORY':
|
||||
# we have the directory name
|
||||
// we have the directory name
|
||||
$expr[] = $this->getConstantType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::DIRECTORY, 'kind' => 'INDEX',
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $prevCategory);
|
||||
continue 3;
|
||||
|
||||
@@ -294,17 +313,16 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
case 'LIKE':
|
||||
$result['like'] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim, 'base_expr' => $trim,
|
||||
'no_quotes' => $this->revokeQuotation($trim));
|
||||
$result['like'] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim,
|
||||
'base_expr' => $trim, 'no_quotes' => $this->revokeQuotation($trim));
|
||||
$this->clear($expr, $base_expr, $currCategory);
|
||||
break;
|
||||
|
||||
case '':
|
||||
# after table name
|
||||
// after table name
|
||||
if ($prevCategory === 'TABLE_NAME' && $upper[0] === '(' && substr($upper, -1) === ')') {
|
||||
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
|
||||
$processor = new CreateDefinitionProcessor();
|
||||
$coldef = $processor->process($unparsed);
|
||||
$coldef = $this->processCreateDefinition($unparsed);
|
||||
$result['create-def'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION,
|
||||
'base_expr' => $base_expr, 'sub_tree' => $coldef['create-def']);
|
||||
$expr = array();
|
||||
@@ -314,10 +332,10 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
case 'UNION':
|
||||
# TODO: this token starts and ends with parenthesis
|
||||
# and contains a list of table names (comma-separated)
|
||||
# split the token and add the list as subtree
|
||||
# we must change the DefaultProcessor
|
||||
// TODO: this token starts and ends with parenthesis
|
||||
// and contains a list of table names (comma-separated)
|
||||
// split the token and add the list as subtree
|
||||
// we must change the DefaultProcessor
|
||||
|
||||
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
|
||||
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
|
||||
@@ -328,11 +346,10 @@ class TableProcessor extends AbstractProcessor {
|
||||
break;
|
||||
|
||||
default:
|
||||
# strings and numeric constants
|
||||
// strings and numeric constants
|
||||
$expr[] = $this->getConstantType($trim);
|
||||
$result['options'][] = array('expr_type' => ExpressionType::EXPRESSION,
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ',
|
||||
'sub_tree' => $expr);
|
||||
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
|
||||
$this->clear($expr, $base_expr, $currCategory);
|
||||
break;
|
||||
}
|
||||
@@ -340,7 +357,7 @@ class TableProcessor extends AbstractProcessor {
|
||||
}
|
||||
|
||||
$prevCategory = $currCategory;
|
||||
$currCategory = "";
|
||||
$currCategory = '';
|
||||
}
|
||||
|
||||
if ($result['like'] === false) {
|
||||
|
||||
Reference in New Issue
Block a user