mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor mysql ddl partition by clause
This commit is contained in:
+6
-16
@@ -22,9 +22,8 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
|
||||
private SQLExpr expr;
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
private boolean linear;
|
||||
// for aliyun ads
|
||||
private boolean key;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
@@ -36,14 +35,6 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
@@ -52,12 +43,11 @@ public class MySqlPartitionByHash extends MySqlPartitioningClause {
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
public boolean isKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
public void setKey(boolean key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-21
@@ -18,7 +18,6 @@ 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;
|
||||
|
||||
@@ -26,10 +25,6 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
private boolean linear;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -40,14 +35,6 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
@@ -56,12 +43,4 @@ public class MySqlPartitionByKey extends MySqlPartitioningClause {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-10
@@ -28,8 +28,6 @@ public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -52,14 +50,6 @@ public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
-2
@@ -28,8 +28,6 @@ public class MySqlPartitionByRange extends MySqlPartitioningClause {
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
+20
@@ -18,6 +18,7 @@ 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.SQLPartitioningClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
|
||||
|
||||
@@ -25,6 +26,10 @@ public abstract class MySqlPartitioningClause extends MySqlObjectImpl implements
|
||||
|
||||
private List<MySqlPartitioningDef> partitions = new ArrayList<MySqlPartitioningDef>();
|
||||
|
||||
protected SQLExpr partitionCount;
|
||||
|
||||
protected boolean linear;
|
||||
|
||||
public List<MySqlPartitioningDef> getPartitions() {
|
||||
return partitions;
|
||||
}
|
||||
@@ -33,4 +38,19 @@ public abstract class MySqlPartitioningClause extends MySqlObjectImpl implements
|
||||
this.partitions = partitions;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public boolean isLinear() {
|
||||
return linear;
|
||||
}
|
||||
|
||||
public void setLinear(boolean linear) {
|
||||
this.linear = linear;
|
||||
}
|
||||
}
|
||||
|
||||
+16
-17
@@ -422,10 +422,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
} else if (identifierEquals("HASH")) {
|
||||
lexer.nextToken();
|
||||
MySqlPartitionByHash clause = new MySqlPartitionByHash();
|
||||
@@ -433,16 +430,18 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
if (linera) {
|
||||
clause.setLinear(true);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
lexer.nextToken();
|
||||
clause.setKey(true);
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
clause.setExpr(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (identifierEquals("RANGE")) {
|
||||
lexer.nextToken();
|
||||
@@ -467,11 +466,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
//
|
||||
partitionClauseRest(clause);
|
||||
|
||||
} else if (identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
@@ -496,10 +491,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
partitionClauseRest(clause);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
@@ -593,6 +585,13 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected void partitionClauseRest(MySqlPartitioningClause clause) {
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean parseTableOptionCharsetOrCollate(MySqlCreateTableStatement stmt) {
|
||||
if (identifierEquals("CHARACTER")) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -1620,11 +1620,16 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByHash x) {
|
||||
if (x.isLinear()) {
|
||||
print0(ucase ? "PARTITION BY LINEAR HASH (" : "partition by linear hash (");
|
||||
print0(ucase ? "PARTITION BY LINEAR HASH " : "partition by linear hash ");
|
||||
} else {
|
||||
print0(ucase ? "PARTITION BY HASH (" : "partition by hash (");
|
||||
print0(ucase ? "PARTITION BY HASH " : "partition by hash ");
|
||||
}
|
||||
//
|
||||
|
||||
if (x.isKey()) {
|
||||
print0(ucase ? "KEY" : "key");
|
||||
}
|
||||
|
||||
print('(');
|
||||
x.getExpr().accept(this);
|
||||
print(')');
|
||||
|
||||
|
||||
@@ -32,8 +32,12 @@ public class ExportConditions extends UDF {
|
||||
public String evaluate(String sql) {
|
||||
return evaluate(sql, null);
|
||||
}
|
||||
|
||||
|
||||
public String evaluate(String sql, String dbType) {
|
||||
return evaluate(sql, dbType, null);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbType, Boolean compactValues) {
|
||||
try {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
@@ -57,7 +61,11 @@ public class ExportConditions extends UDF {
|
||||
if (values.size() == 0) {
|
||||
row.add(null);
|
||||
} else if (values.size() == 1) {
|
||||
row.add(values.get(0));
|
||||
if (compactValues != null && compactValues.booleanValue()) {
|
||||
row.add(values);
|
||||
} else {
|
||||
row.add(values.get(0));
|
||||
}
|
||||
} else {
|
||||
row.add(values);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,9 @@ public interface JdbcConstants {
|
||||
|
||||
public static final String KINGBASE = "kingbase";
|
||||
public static final String KINGBASE_DRIVER = "com.kingbase.Driver";
|
||||
|
||||
public static final String OCEANBASE = "oceanbase";
|
||||
public static final String OCEANBASE_DRIVER = "com.mysql.jdbc.Driver";
|
||||
|
||||
/**
|
||||
* 阿里云odps
|
||||
|
||||
Reference in New Issue
Block a user