mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support drds ddl, for issue #2433
This commit is contained in:
@@ -125,6 +125,7 @@ public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisit
|
||||
SQLPartitionBy partitionBy = x.getPartitioning();
|
||||
if (partitionBy != null) {
|
||||
println();
|
||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
|
||||
+38
-9
@@ -22,10 +22,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
|
||||
@@ -39,15 +36,14 @@ import com.alibaba.druid.util.JdbcConstants;
|
||||
public class MySqlCreateTableStatement extends SQLCreateTableStatement implements MySqlStatement {
|
||||
|
||||
private Map<String, SQLObject> tableOptions = new LinkedHashMap<String, SQLObject>();
|
||||
|
||||
private List<SQLCommentHint> hints = new ArrayList<SQLCommentHint>();
|
||||
|
||||
private List<SQLCommentHint> optionHints = new ArrayList<SQLCommentHint>();
|
||||
|
||||
|
||||
|
||||
private SQLName tableGroup;
|
||||
|
||||
protected SQLPartitionBy dbPartitionBy;
|
||||
protected SQLPartitionBy tablePartitionBy;
|
||||
protected SQLExpr tbpartitions;
|
||||
|
||||
public MySqlCreateTableStatement(){
|
||||
super (JdbcConstants.MYSQL);
|
||||
}
|
||||
@@ -392,4 +388,37 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
public SQLPartitionBy getDbPartitionBy() {
|
||||
return dbPartitionBy;
|
||||
}
|
||||
|
||||
public void setDbPartitionBy(SQLPartitionBy x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.dbPartitionBy = x;
|
||||
}
|
||||
|
||||
public SQLPartitionBy getTablePartitionBy() {
|
||||
return tablePartitionBy;
|
||||
}
|
||||
|
||||
public void setTablePartitionBy(SQLPartitionBy x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.tablePartitionBy = x;
|
||||
}
|
||||
|
||||
public SQLExpr getTbpartitions() {
|
||||
return tbpartitions;
|
||||
}
|
||||
|
||||
public void setTbpartitions(SQLExpr x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.tbpartitions = x;
|
||||
}
|
||||
}
|
||||
|
||||
+141
-114
@@ -529,124 +529,31 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.PARTITION) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
SQLPartitionBy partitionClause;
|
||||
|
||||
boolean linera = false;
|
||||
if (lexer.identifierEquals("LINEAR")) {
|
||||
lexer.nextToken();
|
||||
linera = true;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
MySqlPartitionByKey clause = new MySqlPartitionByKey();
|
||||
lexer.nextToken();
|
||||
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.ALGORITHM)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.EQ);
|
||||
clause.setAlgorithm(lexer.integerValue().shortValue());
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
if (lexer.token() != Token.RPAREN) {
|
||||
for (;;) {
|
||||
clause.addColumn(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
} else if (lexer.identifierEquals("HASH")) {
|
||||
lexer.nextToken();
|
||||
SQLPartitionByHash clause = new SQLPartitionByHash();
|
||||
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
lexer.nextToken();
|
||||
clause.setKey(true);
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(clause.getColumns(), clause);
|
||||
accept(Token.RPAREN);
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (lexer.identifierEquals("RANGE")) {
|
||||
SQLPartitionByRange clause = partitionByRange();
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (lexer.identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
SQLPartitionByList clause = new SQLPartitionByList();
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
clause.addColumn(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
clause.addColumn(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
} else {
|
||||
throw new ParserException("TODO. " + lexer.info());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
for (;;) {
|
||||
SQLPartition partitionDef = this.getExprParser().parsePartition();
|
||||
|
||||
partitionClause.addPartition(partitionDef);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
SQLPartitionBy partitionClause = parsePartitionBy();
|
||||
stmt.setPartitioning(partitionClause);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.DBPARTITION)) {
|
||||
SQLPartitionBy partitionClause = parsePartitionBy();
|
||||
stmt.setDbPartitionBy(partitionClause);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.TBPARTITION)) {
|
||||
SQLPartitionBy partitionClause = parsePartitionBy();
|
||||
stmt.setTablePartitionBy(partitionClause);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.TBPARTITIONS)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr tbpartitions = this.exprParser.expr();
|
||||
stmt.setTbpartitions(tbpartitions);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -669,7 +576,127 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
private SQLPartitionBy parsePartitionBy() {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
SQLPartitionBy partitionClause;
|
||||
|
||||
boolean linera = false;
|
||||
if (lexer.identifierEquals("LINEAR")) {
|
||||
lexer.nextToken();
|
||||
linera = true;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
MySqlPartitionByKey clause = new MySqlPartitionByKey();
|
||||
lexer.nextToken();
|
||||
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.ALGORITHM)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.EQ);
|
||||
clause.setAlgorithm(lexer.integerValue().shortValue());
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
if (lexer.token() != Token.RPAREN) {
|
||||
for (;;) {
|
||||
clause.addColumn(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
} else if (lexer.identifierEquals("HASH") || lexer.identifierEquals("UNI_HASH")) {
|
||||
SQLPartitionByHash clause = new SQLPartitionByHash();
|
||||
|
||||
if (lexer.identifierEquals("UNI_HASH")) {
|
||||
clause.setUnique(true);
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
lexer.nextToken();
|
||||
clause.setKey(true);
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(clause.getColumns(), clause);
|
||||
accept(Token.RPAREN);
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (lexer.identifierEquals("RANGE")) {
|
||||
SQLPartitionByRange clause = partitionByRange();
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (lexer.identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
SQLPartitionByList clause = new SQLPartitionByList();
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
clause.addColumn(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
clause.addColumn(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
partitionClauseRest(clause);
|
||||
} else {
|
||||
throw new ParserException("TODO. " + lexer.info());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
for (;;) {
|
||||
SQLPartition partitionDef = this.getExprParser().parsePartition();
|
||||
|
||||
partitionClause.addPartition(partitionDef);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
return partitionClause;
|
||||
}
|
||||
|
||||
|
||||
protected SQLPartitionByRange partitionByRange() {
|
||||
acceptIdentifier("RANGE");
|
||||
|
||||
@@ -489,9 +489,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
comment.accept(this);
|
||||
}
|
||||
|
||||
if (x.getPartitioning() != null) {
|
||||
SQLPartitionBy partitionBy = x.getPartitioning();
|
||||
if (partitionBy != null) {
|
||||
println();
|
||||
x.getPartitioning().accept(this);
|
||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
SQLPartitionBy dbPartitionBy = x.getDbPartitionBy();
|
||||
if (dbPartitionBy != null) {
|
||||
println();
|
||||
print0(ucase ? "DBPARTITION BY " : "dbpartition by ");
|
||||
dbPartitionBy.accept(this);
|
||||
}
|
||||
|
||||
SQLPartitionBy tbPartitionsBy = x.getTablePartitionBy();
|
||||
if (tbPartitionsBy != null) {
|
||||
println();
|
||||
print0(ucase ? "TBPARTITION BY " : "tbpartition by ");
|
||||
tbPartitionsBy.accept(this);
|
||||
}
|
||||
|
||||
if (x.getTbpartitions() != null) {
|
||||
println();
|
||||
print0(ucase ? "TBPARTITIONS " : "tbpartitions ");
|
||||
x.getTbpartitions().accept(this);
|
||||
}
|
||||
|
||||
if (x.getTableGroup() != null) {
|
||||
@@ -1462,9 +1484,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByKey x) {
|
||||
if (x.isLinear()) {
|
||||
print0(ucase ? "PARTITION BY LINEAR KEY (" : "partition by linear key (");
|
||||
print0(ucase ? "LINEAR KEY (" : "linear key (");
|
||||
} else {
|
||||
print0(ucase ? "PARTITION BY KEY (" : "partition by key (");
|
||||
print0(ucase ? "KEY (" : "key (");
|
||||
}
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(')');
|
||||
|
||||
@@ -1747,6 +1747,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
for (SQLPartitionBy globalPartition : globalPartitions) {
|
||||
println();
|
||||
print0(ucase ? "GLOBAL " : "global ");
|
||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
globalPartition.accept(this);
|
||||
}
|
||||
|
||||
@@ -2097,9 +2098,11 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
print0(ucase ? "MONITORING" : "monitoring");
|
||||
}
|
||||
|
||||
if (x.getPartitioning() != null) {
|
||||
SQLPartitionBy partitionBy = x.getPartitioning();
|
||||
if (partitionBy != null) {
|
||||
println();
|
||||
x.getPartitioning().accept(this);
|
||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
if (x.getCluster() != null) {
|
||||
|
||||
@@ -2245,9 +2245,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
print0(ucase ? "MONITORING" : "monitoring");
|
||||
}
|
||||
|
||||
if (x.getPartitioning() != null) {
|
||||
SQLPartitionBy partitionBy = x.getPartitioning();
|
||||
if (partitionBy != null) {
|
||||
println();
|
||||
x.getPartitioning().accept(this);
|
||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
if (x.getCluster() != null) {
|
||||
|
||||
Reference in New Issue
Block a user