mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser for mysql ddl subpartition
This commit is contained in:
+74
@@ -31,6 +31,11 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
|
||||
private SQLExpr dataDirectory;
|
||||
private SQLExpr indexDirectory;
|
||||
private SQLName tableSpace;
|
||||
private SQLExpr maxRows;
|
||||
private SQLExpr minRows;
|
||||
private SQLExpr engine;
|
||||
private SQLExpr comment;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
@@ -39,6 +44,11 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
acceptChild(visitor, values);
|
||||
acceptChild(visitor, dataDirectory);
|
||||
acceptChild(visitor, indexDirectory);
|
||||
acceptChild(visitor, tableSpace);
|
||||
acceptChild(visitor, maxRows);
|
||||
acceptChild(visitor, minRows);
|
||||
acceptChild(visitor, engine);
|
||||
acceptChild(visitor, comment);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
@@ -48,6 +58,9 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
}
|
||||
|
||||
public void setIndexDirectory(SQLExpr indexDirectory) {
|
||||
if (indexDirectory != null) {
|
||||
indexDirectory.setParent(this);
|
||||
}
|
||||
this.indexDirectory = indexDirectory;
|
||||
}
|
||||
|
||||
@@ -56,6 +69,9 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
}
|
||||
|
||||
public void setDataDirectory(SQLExpr dataDirectory) {
|
||||
if (dataDirectory != null) {
|
||||
dataDirectory.setParent(this);
|
||||
}
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
@@ -72,9 +88,67 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
}
|
||||
|
||||
public void setName(SQLName name) {
|
||||
if (name != null) {
|
||||
name.setParent(this);
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SQLName getTableSpace() {
|
||||
return tableSpace;
|
||||
}
|
||||
|
||||
public void setTableSpace(SQLName tableSpace) {
|
||||
if (tableSpace != null) {
|
||||
tableSpace.setParent(this);
|
||||
}
|
||||
this.tableSpace = tableSpace;
|
||||
}
|
||||
|
||||
public SQLExpr getMaxRows() {
|
||||
return maxRows;
|
||||
}
|
||||
|
||||
public void setMaxRows(SQLExpr maxRows) {
|
||||
if (maxRows != null) {
|
||||
maxRows.setParent(this);
|
||||
}
|
||||
this.maxRows = maxRows;
|
||||
}
|
||||
|
||||
public SQLExpr getMinRows() {
|
||||
return minRows;
|
||||
}
|
||||
|
||||
public void setMinRows(SQLExpr minRows) {
|
||||
if (minRows != null) {
|
||||
minRows.setParent(this);
|
||||
}
|
||||
this.minRows = minRows;
|
||||
}
|
||||
|
||||
public SQLExpr getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public void setEngine(SQLExpr engine) {
|
||||
if (engine != null) {
|
||||
engine.setParent(this);
|
||||
}
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public SQLExpr getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(SQLExpr comment) {
|
||||
if (comment != null) {
|
||||
comment.setParent(this);
|
||||
}
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public static abstract class Values extends MySqlObjectImpl {
|
||||
|
||||
private final List<SQLExpr> items = new ArrayList<SQLExpr>();
|
||||
|
||||
+58
-4
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
@@ -536,14 +537,22 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
MySqlPartitioningDef.LessThanValues values = new MySqlPartitioningDef.LessThanValues();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems(), values);
|
||||
accept(Token.RPAREN);
|
||||
if (identifierEquals("MAXVALUE")) {
|
||||
SQLIdentifierExpr maxValue = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
maxValue.setParent(values);
|
||||
values.getItems().add(maxValue);
|
||||
} else {
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems(), values);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
partitionDef.setValues(values);
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
boolean storage = false;
|
||||
if (identifierEquals("DATA")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
@@ -551,6 +560,13 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setDataDirectory(this.exprParser.expr());
|
||||
} else if (lexer.token() == Token.TABLESPACE) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
SQLName tableSpace = this.exprParser.name();
|
||||
partitionDef.setTableSpace(tableSpace);
|
||||
} else if (lexer.token() == Token.INDEX) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
@@ -558,6 +574,41 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setIndexDirectory(this.exprParser.expr());
|
||||
} else if (identifierEquals("MAX_ROWS")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
SQLExpr maxRows = this.exprParser.primary();
|
||||
partitionDef.setMaxRows(maxRows);
|
||||
} else if (identifierEquals("MIN_ROWS")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
SQLExpr minRows = this.exprParser.primary();
|
||||
partitionDef.setMaxRows(minRows);
|
||||
} else if (identifierEquals("ENGINE") || //
|
||||
(storage = (lexer.token() == Token.STORAGE || identifierEquals("STORAGE")))
|
||||
) {
|
||||
if (storage) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
acceptIdentifier("ENGINE");
|
||||
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
SQLName engine = this.exprParser.name();
|
||||
partitionDef.setEngine(engine);
|
||||
} else if (lexer.token() == Token.COMMENT) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
SQLExpr comment = this.exprParser.primary();
|
||||
partitionDef.setComment(comment);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -605,7 +656,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
protected void partitionClauseRest(MySqlPartitionByClause clause) {
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionsCount(this.exprParser.expr());
|
||||
|
||||
SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue());
|
||||
accept(Token.LITERAL_INT);
|
||||
clause.setPartitionsCount(countExpr);
|
||||
}
|
||||
|
||||
if (identifierEquals("PARTITION")) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
@@ -524,7 +525,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
if (x.getPartitioning() != null) {
|
||||
print(' ');
|
||||
println();
|
||||
x.getPartitioning().accept(this);
|
||||
}
|
||||
|
||||
@@ -1568,7 +1569,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
List<MySqlPartitioningDef> partitions = x.getPartitions();
|
||||
int partitionsSize = partitions.size();
|
||||
if (partitionsSize > 0) {
|
||||
print('(');
|
||||
print0(" (");
|
||||
incrementIndent();
|
||||
for (int i = 0; i < partitionsSize; ++i) {
|
||||
println();
|
||||
@@ -3001,6 +3002,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
x.getDataDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
if (x.getIndexDirectory() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
@@ -3008,6 +3010,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
x.getIndexDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
if (x.getTableSpace() != null) {
|
||||
print0(ucase ? " TABLESPACE " : " tablespace ");
|
||||
x.getTableSpace().accept(this);
|
||||
}
|
||||
|
||||
if (x.getEngine() != null) {
|
||||
print0(ucase ? " STORAGE ENGINE " : " storage engine ");
|
||||
x.getEngine().accept(this);
|
||||
}
|
||||
|
||||
if (x.getMaxRows() != null) {
|
||||
print0(ucase ? " MAX_ROWS " : " max_rows ");
|
||||
x.getMaxRows().accept(this);
|
||||
}
|
||||
|
||||
if (x.getMinRows() != null) {
|
||||
print0(ucase ? " MIN_ROWS " : " min_rows ");
|
||||
x.getMinRows().accept(this);
|
||||
}
|
||||
|
||||
if (x.getComment() != null) {
|
||||
print0(ucase ? " COMMENT " : " comment ");
|
||||
x.getComment().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -3019,6 +3046,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(LessThanValues x) {
|
||||
if (x.getItems().size() == 1 && x.getItems().get(0) instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr ident = (SQLIdentifierExpr) x.getItems().get(0);
|
||||
if ("MAXVALUE".equalsIgnoreCase(ident.getName())) {
|
||||
print0(ucase ? "VALUES LESS THAN MAXVALUE" : "values less than maxvalue");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
print0(ucase ? "VALUES LESS THAN (" : "values less than (");
|
||||
printAndAccept(x.getItems(), ", ");
|
||||
print(')');
|
||||
|
||||
Reference in New Issue
Block a user