mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve mysql parser
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLExpr expr;
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, expr);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, partitionCount);
|
||||
acceptChild(visitor, getPartitions());
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public void setExpr(SQLExpr expr) {
|
||||
if (expr != null) {
|
||||
expr.setParent(this);
|
||||
}
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<SQLName> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -15,15 +15,36 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
|
||||
private Values values;
|
||||
|
||||
private SQLExpr dataDirectory;
|
||||
private SQLExpr indexDirectory;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, name);
|
||||
acceptChild(visitor, values);
|
||||
acceptChild(visitor, dataDirectory);
|
||||
acceptChild(visitor, indexDirectory);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getIndexDirectory() {
|
||||
return indexDirectory;
|
||||
}
|
||||
|
||||
public void setIndexDirectory(SQLExpr indexDirectory) {
|
||||
this.indexDirectory = indexDirectory;
|
||||
}
|
||||
|
||||
public SQLExpr getDataDirectory() {
|
||||
return dataDirectory;
|
||||
}
|
||||
|
||||
public void setDataDirectory(SQLExpr dataDirectory) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
public Values getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
+67
-12
@@ -27,6 +27,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
@@ -358,7 +359,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
if (identifierEquals("PARTITION")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
|
||||
MySqlPartitioningClause partitionClause;
|
||||
|
||||
boolean linera = false;
|
||||
@@ -385,7 +386,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
@@ -438,38 +439,92 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
//
|
||||
|
||||
} else if (identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
MySqlPartitionByList clause = new MySqlPartitionByList();
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
clause.setExpr(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
clause.getColumns().add(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
for (;;) {
|
||||
acceptIdentifier("PARTITION");
|
||||
|
||||
|
||||
MySqlPartitioningDef partitionDef = new MySqlPartitioningDef();
|
||||
|
||||
|
||||
partitionDef.setName(this.exprParser.name());
|
||||
|
||||
|
||||
if (lexer.token() == Token.VALUES) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.IN) {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
MySqlPartitioningDef.InValues values = new MySqlPartitioningDef.InValues();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems());
|
||||
accept(Token.RPAREN);
|
||||
partitionDef.setValues(values);
|
||||
} else {
|
||||
acceptIdentifier("LESS");
|
||||
acceptIdentifier("THAN");
|
||||
|
||||
|
||||
MySqlPartitioningDef.LessThanValues values = new MySqlPartitioningDef.LessThanValues();
|
||||
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems());
|
||||
accept(Token.RPAREN);
|
||||
partitionDef.setValues(values);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (;;) {
|
||||
if (identifierEquals("DATA")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setDataDirectory(this.exprParser.expr());
|
||||
} else if (lexer.token() == Token.INDEX) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setIndexDirectory(this.exprParser.expr());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
partitionClause.getPartitions().add(partitionDef);
|
||||
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
@@ -479,7 +534,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
|
||||
stmt.setPartitioning(partitionClause);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
|
||||
@@ -556,6 +557,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(MySqlPartitionByRange x);
|
||||
|
||||
boolean visit(MySqlPartitionByList x);
|
||||
|
||||
void endVisit(MySqlPartitionByList x);
|
||||
|
||||
boolean visit(MySqlPartitioningDef x);
|
||||
|
||||
void endVisit(MySqlPartitioningDef x);
|
||||
|
||||
@@ -62,6 +62,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
@@ -1259,4 +1260,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
public void endVisit(InValues x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -82,7 +82,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.LessThanValues;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
|
||||
@@ -94,7 +96,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinitio
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
|
||||
@@ -1514,7 +1515,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByRange x) {
|
||||
|
||||
@@ -1522,15 +1523,22 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByRange x) {
|
||||
print("PARTITION BY RANGE (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
print("PARTITION BY RANGE");
|
||||
if (x.getExpr() != null) {
|
||||
print(" (");
|
||||
x.getExpr().accept(this);
|
||||
print(")");
|
||||
} else {
|
||||
print(" COLUMNS (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
}
|
||||
|
||||
if (x.getPartitionCount() != null) {
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
|
||||
List<MySqlPartitioningDef> partitions = x.getPartitions();
|
||||
int partitionsSize = partitions.size();
|
||||
if (partitionsSize > 0) {
|
||||
@@ -1549,8 +1557,50 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//MySqlPartitionByRange
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
print("PARTITION BY LIST ");
|
||||
if (x.getExpr() != null) {
|
||||
print("(");
|
||||
x.getExpr().accept(this);
|
||||
print(") ");
|
||||
} else {
|
||||
print("COLUMNS (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(") ");
|
||||
}
|
||||
|
||||
if (x.getPartitionCount() != null) {
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
List<MySqlPartitioningDef> partitions = x.getPartitions();
|
||||
int partitionsSize = partitions.size();
|
||||
if (partitionsSize > 0) {
|
||||
print("(");
|
||||
incrementIndent();
|
||||
for (int i = 0; i < partitionsSize; ++i) {
|
||||
println();
|
||||
partitions.get(i).accept(this);
|
||||
if (i != partitionsSize - 1) {
|
||||
print(", ");
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
println();
|
||||
print(")");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByHash x) {
|
||||
@@ -1564,7 +1614,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
} else {
|
||||
print("PARTITION BY HASH (");
|
||||
}
|
||||
//
|
||||
//
|
||||
x.getExpr().accept(this);
|
||||
print(")");
|
||||
|
||||
@@ -1572,7 +1622,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2956,12 +3006,28 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(' ');
|
||||
x.getValues().accept(this);
|
||||
}
|
||||
|
||||
if (x.getDataDirectory() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("DATA DIRECTORY ");
|
||||
x.getDataDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
if (x.getIndexDirectory() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("INDEX DIRECTORY ");
|
||||
x.getIndexDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitioningDef x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -2974,7 +3040,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public void endVisit(LessThanValues x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -2987,6 +3053,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public void endVisit(InValues x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
} //
|
||||
|
||||
@@ -75,6 +75,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
@@ -1343,4 +1344,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user