update parser_sql
This commit is contained in:
@@ -30,10 +30,8 @@
|
||||
* DAMAGE.
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/AbstractProcessor.php';
|
||||
require_once dirname(__FILE__) . '/ReferenceDefinitionProcessor.php';
|
||||
require_once dirname(__FILE__) . '/ExpressionListProcessor.php';
|
||||
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
|
||||
namespace PHPSQLParser\processors;
|
||||
use PHPSQLParser\utils\ExpressionType;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -45,17 +43,20 @@ require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
|
||||
class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
|
||||
protected function processExpressionList($parsed) {
|
||||
$processor = new ExpressionListProcessor();
|
||||
return $processor->process($parsed);
|
||||
$processor = new ExpressionListProcessor($this->options);
|
||||
$expr = $this->removeParenthesisFromStart($parsed);
|
||||
$expr = $this->splitSQLIntoTokens($expr);
|
||||
$expr = $this->removeComma($expr);
|
||||
return $processor->process($expr);
|
||||
}
|
||||
|
||||
|
||||
protected function processReferenceDefinition($parsed) {
|
||||
$processor = new ReferenceDefinitionProcessor();
|
||||
$processor = new ReferenceDefinitionProcessor($this->options);
|
||||
return $processor->process($parsed);
|
||||
}
|
||||
|
||||
|
||||
protected function removeComma($tokens) {
|
||||
$res = array();
|
||||
$res = array();
|
||||
foreach ($tokens as $token) {
|
||||
if (trim($token) !== ',') {
|
||||
$res[] = $token;
|
||||
@@ -63,7 +64,7 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
protected function buildColDef($expr, $base_expr, $options, $refs, $key) {
|
||||
$expr = array('expr_type' => ExpressionType::COLUMN_TYPE, 'base_expr' => $base_expr, 'sub_tree' => $expr);
|
||||
|
||||
@@ -134,24 +135,32 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
continue 2;
|
||||
|
||||
case 'UNSIGNED':
|
||||
$last = array_pop($expr);
|
||||
$last['unsigned'] = true;
|
||||
$expr[] = $last;
|
||||
foreach (array_reverse(array_keys($expr)) as $i) {
|
||||
if (isset($expr[$i]['expr_type']) && (ExpressionType::DATA_TYPE === $expr[$i]['expr_type'])) {
|
||||
$expr[$i]['unsigned'] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
continue 2;
|
||||
|
||||
case 'ZEROFILL':
|
||||
$last = array_pop($expr);
|
||||
$last['zerofill'] = true;
|
||||
$expr[] = $last;
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
continue 2;
|
||||
|
||||
case 'BIT':
|
||||
case 'TINYBIT':
|
||||
case 'TINYINT':
|
||||
case 'SMALLINT':
|
||||
case 'MEDIUMINT':
|
||||
case 'INT':
|
||||
case 'INTEGER':
|
||||
case 'BIGINT':
|
||||
case 'BOOL':
|
||||
case 'BOOLEAN':
|
||||
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'unsigned' => false,
|
||||
'zerofill' => false, 'length' => false);
|
||||
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
|
||||
@@ -194,11 +203,16 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
$prevCategory = $upper;
|
||||
continue 2;
|
||||
|
||||
case 'YEAR':
|
||||
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'length' => false);
|
||||
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
|
||||
$prevCategory = $upper;
|
||||
continue 2;
|
||||
|
||||
case 'DATE':
|
||||
case 'TIME':
|
||||
case 'TIMESTAMP':
|
||||
case 'DATETIME':
|
||||
case 'YEAR':
|
||||
case 'TINYBLOB':
|
||||
case 'BLOB':
|
||||
case 'MEDIUMBLOB':
|
||||
@@ -217,7 +231,6 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
continue 2;
|
||||
|
||||
case 'ENUM':
|
||||
case 'SET':
|
||||
$currCategory = 'MULTIPLE_PARAM_PARENTHESIS';
|
||||
$prevCategory = 'TEXT';
|
||||
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim, 'sub_tree' => false);
|
||||
@@ -238,40 +251,24 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
continue 2;
|
||||
|
||||
case 'CHARACTER':
|
||||
if ($prevCategory === 'TEXT') {
|
||||
$parsed = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim);
|
||||
$expr[] = array('expr_type' => ExpressionType::CHARSET, 'base_expr' => substr($base_expr, 0, -1),
|
||||
'sub_tree' => $parsed);
|
||||
$base_expr = $token;
|
||||
$currCategory = 'CHARSET';
|
||||
continue 2;
|
||||
}
|
||||
// else ?
|
||||
break;
|
||||
$currCategory = 'CHARSET';
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
continue 2;
|
||||
|
||||
case 'SET':
|
||||
if ($currCategory === 'CHARSET') {
|
||||
// TODO: is it necessary to set special properties like the name or collation?
|
||||
$parsed = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
$last = array_pop($expr);
|
||||
$last['sub_tree'][] = $parsed;
|
||||
$expr[] = $last;
|
||||
continue 2;
|
||||
}
|
||||
// else ?
|
||||
break;
|
||||
if ($currCategory == 'CHARSET') {
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
} else {
|
||||
$currCategory = 'MULTIPLE_PARAM_PARENTHESIS';
|
||||
$prevCategory = 'TEXT';
|
||||
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim, 'sub_tree' => false);
|
||||
}
|
||||
continue 2;
|
||||
|
||||
case 'COLLATE':
|
||||
if ($prevCategory === 'TEXT') {
|
||||
$parsed = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
$expr[] = array('expr_type' => ExpressionType::COLLATE, 'base_expr' => substr($base_expr, 0, -1),
|
||||
'sub_tree' => $parsed);
|
||||
$base_expr = $token;
|
||||
$currCategory = 'COLLATION';
|
||||
continue 2;
|
||||
}
|
||||
// else ?
|
||||
break;
|
||||
$currCategory = $upper;
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
|
||||
continue 2;
|
||||
|
||||
case 'NOT':
|
||||
case 'NULL':
|
||||
@@ -361,32 +358,24 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
|
||||
case 'COLLATE':
|
||||
// this is the collation name
|
||||
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
|
||||
$last = array_pop($expr);
|
||||
$last['sub_tree'][] = $parsed;
|
||||
$t = $base_expr;
|
||||
$base_expr = $last['base_expr'] . $base_expr;
|
||||
$last['base_expr'] = $t;
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::COLLATE, 'base_expr' => $trim);
|
||||
$options['collate'] = $trim;
|
||||
$currCategory = $prevCategory;
|
||||
break;
|
||||
|
||||
case 'CHARSET':
|
||||
// this is the character set name
|
||||
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
|
||||
$last = array_pop($expr);
|
||||
$last['sub_tree'][] = $parsed;
|
||||
$t = $base_expr;
|
||||
$base_expr = $last['base_expr'] . $base_expr;
|
||||
$last['base_expr'] = $t;
|
||||
$options['sub_tree'][] = array('expr_type' => ExpressionType::CHARSET, 'base_expr' => $trim);
|
||||
$options['charset'] = $trim;
|
||||
$currCategory = $prevCategory;
|
||||
break;
|
||||
break;
|
||||
|
||||
case 'SINGLE_PARAM_PARENTHESIS':
|
||||
$parsed = $this->removeParenthesisFromStart($trim);
|
||||
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => trim($parsed));
|
||||
$last = array_pop($expr);
|
||||
$last['length'] = $parsed['base_expr'];
|
||||
|
||||
|
||||
$expr[] = $last;
|
||||
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
|
||||
'sub_tree' => array($parsed));
|
||||
@@ -395,17 +384,12 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
|
||||
case 'TWO_PARAM_PARENTHESIS':
|
||||
// maximum of two parameters
|
||||
$parsed = $this->removeParenthesisFromStart($trim);
|
||||
$parsed = $this->splitSQLIntoTokens($parsed);
|
||||
$parsed = $this->removeComma($parsed);
|
||||
$parsed = $this->processExpressionList($parsed);
|
||||
|
||||
// TODO: check that
|
||||
$parsed = $this->processExpressionList($trim);
|
||||
|
||||
$last = array_pop($expr);
|
||||
$last['length'] = $parsed[0]['base_expr'];
|
||||
$last['decimals'] = isset($parsed[1]) ? $parsed[1]['base_expr'] : false;
|
||||
|
||||
|
||||
$expr[] = $last;
|
||||
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
|
||||
'sub_tree' => $parsed);
|
||||
@@ -414,14 +398,17 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
|
||||
|
||||
case 'MULTIPLE_PARAM_PARENTHESIS':
|
||||
// some parameters
|
||||
$parsed = $this->removeParenthesisFromStart($trim);
|
||||
$parsed = $this->splitSQLIntoTokens($parsed);
|
||||
$parsed = $this->removeComma($parsed);
|
||||
$this->processExpressionList($parsed);
|
||||
$parsed = $this->processExpressionList($trim);
|
||||
|
||||
$last = array_pop($expr);
|
||||
$last['sub_tree'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
|
||||
'sub_tree' => $parsed);
|
||||
$subTree = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
|
||||
'sub_tree' => $parsed);
|
||||
|
||||
if ($this->options->getConsistentSubtrees()) {
|
||||
$subTree = array($subTree);
|
||||
}
|
||||
|
||||
$last['sub_tree'] = $subTree;
|
||||
$expr[] = $last;
|
||||
$currCategory = $prevCategory;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user