diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableAddPartition.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableAddPartition.java index 9e1ca8fe8b..5f4b517cbd 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableAddPartition.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableAddPartition.java @@ -18,17 +18,28 @@ package com.alibaba.druid.sql.ast.statement; import java.util.ArrayList; import java.util.List; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLObject; import com.alibaba.druid.sql.ast.SQLObjectImpl; import com.alibaba.druid.sql.visitor.SQLASTVisitor; public class SQLAlterTableAddPartition extends SQLObjectImpl implements SQLAlterTableItem { - private boolean ifNotExists = false; + private boolean ifNotExists = false; - private final List partition = new ArrayList(4); + private final List partitions = new ArrayList(4); - public List getPartition() { - return partition; + private SQLExpr partitionCount; + + public List getPartitions() { + return partitions; + } + + public void addPartition(SQLObject partition) { + if (partition != null) { + partition.setParent(this); + } + this.partitions.add(partition); } public boolean isIfNotExists() { @@ -39,10 +50,21 @@ public class SQLAlterTableAddPartition extends SQLObjectImpl implements SQLAlter this.ifNotExists = ifNotExists; } + public SQLExpr getPartitionCount() { + return partitionCount; + } + + public void setPartitionCount(SQLExpr partitionCount) { + if (partitionCount != null) { + partitionCount.setParent(this); + } + this.partitionCount = partitionCount; + } + @Override protected void accept0(SQLASTVisitor visitor) { if (visitor.visit(this)) { - acceptChild(visitor, partition); + acceptChild(visitor, partitions); } visitor.endVisit(this); } diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableCoalescePartition.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableCoalescePartition.java new file mode 100644 index 0000000000..18c70402b4 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableCoalescePartition.java @@ -0,0 +1,44 @@ +/* + * Copyright 1999-2101 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.ast.statement; + +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLObjectImpl; +import com.alibaba.druid.sql.visitor.SQLASTVisitor; + +public class SQLAlterTableCoalescePartition extends SQLObjectImpl implements SQLAlterTableItem { + + private SQLExpr count; + + public SQLExpr getCount() { + return count; + } + + public void setCount(SQLExpr count) { + if (count != null) { + count.setParent(this); + } + this.count = count; + } + + @Override + protected void accept0(SQLASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, count); + } + visitor.endVisit(this); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableDropPartition.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableDropPartition.java index 38c37af226..019c9d8bb2 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableDropPartition.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableDropPartition.java @@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.statement; import java.util.ArrayList; import java.util.List; +import com.alibaba.druid.sql.ast.SQLObject; import com.alibaba.druid.sql.ast.SQLObjectImpl; import com.alibaba.druid.sql.visitor.SQLASTVisitor; @@ -27,10 +28,17 @@ public class SQLAlterTableDropPartition extends SQLObjectImpl implements SQLAlte private boolean purge; - private final List partition = new ArrayList(4); + private final List partitions = new ArrayList(4); - public List getPartition() { - return partition; + public List getPartitions() { + return partitions; + } + + public void addPartition(SQLObject partition) { + if (partition != null) { + partition.setParent(this); + } + this.partitions.add(partition); } public boolean isIfNotExists() { @@ -52,7 +60,7 @@ public class SQLAlterTableDropPartition extends SQLObjectImpl implements SQLAlte @Override protected void accept0(SQLASTVisitor visitor) { if (visitor.visit(this)) { - acceptChild(visitor, partition); + acceptChild(visitor, partitions); } visitor.endVisit(this); } diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableReOrganizePartition.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableReOrganizePartition.java new file mode 100644 index 0000000000..c3efc84418 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLAlterTableReOrganizePartition.java @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2101 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.ast.statement; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.sql.ast.SQLName; +import com.alibaba.druid.sql.ast.SQLObject; +import com.alibaba.druid.sql.ast.SQLObjectImpl; +import com.alibaba.druid.sql.visitor.SQLASTVisitor; + +public class SQLAlterTableReOrganizePartition extends SQLObjectImpl implements SQLAlterTableItem { + + private final List names = new ArrayList(); + + private final List partitions = new ArrayList(4); + + public List getPartitions() { + return partitions; + } + + public void addPartition(SQLObject partition) { + if (partition != null) { + partition.setParent(this); + } + this.partitions.add(partition); + } + + public List getNames() { + return names; + } + + @Override + protected void accept0(SQLASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, partitions); + } + visitor.endVisit(this); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLExprTableSource.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLExprTableSource.java index 99e5e9526b..afec7cb414 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLExprTableSource.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLExprTableSource.java @@ -15,13 +15,19 @@ */ package com.alibaba.druid.sql.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.visitor.SQLASTVisitor; public class SQLExprTableSource extends SQLTableSourceImpl { protected SQLExpr expr; + private List partitions; + public SQLExprTableSource(){ } @@ -29,7 +35,7 @@ public class SQLExprTableSource extends SQLTableSourceImpl { public SQLExprTableSource(SQLExpr expr){ this(expr, null); } - + public SQLExprTableSource(SQLExpr expr, String alias){ this.setExpr(expr); this.setAlias(alias); @@ -46,6 +52,32 @@ public class SQLExprTableSource extends SQLTableSourceImpl { this.expr = expr; } + public List getPartitions() { + if (this.partitions == null) { + this.partitions = new ArrayList(2); + } + + return partitions; + } + + public int getPartitionSize() { + if (this.partitions == null) { + return 0; + } + return this.partitions.size(); + } + + public void addPartition(SQLName partition) { + if (partition != null) { + partition.setParent(this); + } + + if (this.partitions == null) { + this.partitions = new ArrayList(2); + } + this.partitions.add(partition); + } + @Override protected void accept0(SQLASTVisitor visitor) { if (visitor.visit(this)) { diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlCreateTableParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlCreateTableParser.java index 19a181c353..ba6619466e 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlCreateTableParser.java @@ -24,11 +24,11 @@ import com.alibaba.druid.sql.ast.SQLPartitionBy; import com.alibaba.druid.sql.ast.SQLPartitionByHash; import com.alibaba.druid.sql.ast.SQLPartitionByList; import com.alibaba.druid.sql.ast.SQLPartitionByRange; -import com.alibaba.druid.sql.ast.SQLPartitionValue; import com.alibaba.druid.sql.ast.SQLSubPartitionBy; import com.alibaba.druid.sql.ast.SQLSubPartitionByHash; import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; +import com.alibaba.druid.sql.ast.expr.SQLNumberExpr; import com.alibaba.druid.sql.ast.statement.SQLAssignItem; import com.alibaba.druid.sql.ast.statement.SQLCheck; import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; @@ -406,7 +406,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { continue; } - if (identifierEquals("PARTITION")) { + if (lexer.token() == Token.PARTITION) { lexer.nextToken(); accept(Token.BY); @@ -427,13 +427,15 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { } accept(Token.LPAREN); - for (;;) { - clause.addColumn(this.exprParser.name()); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; + if (lexer.token() != Token.RPAREN) { + for (;;) { + clause.addColumn(this.exprParser.name()); + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } + break; } - break; } accept(Token.RPAREN); @@ -497,79 +499,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { if (lexer.token() == Token.LPAREN) { lexer.nextToken(); for (;;) { - acceptIdentifier("PARTITION"); - - SQLPartition partitionDef = new SQLPartition(); - - partitionDef.setName(this.exprParser.name()); - - - SQLPartitionValue values = parsePartitionValues(); - if (values != null) { - partitionDef.setValues(values); - } - - for (;;) { - boolean storage = false; - if (identifierEquals("DATA")) { - lexer.nextToken(); - acceptIdentifier("DIRECTORY"); - if (lexer.token() == Token.EQ) { - 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"); - if (lexer.token() == Token.EQ) { - 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; - } - } + SQLPartition partitionDef = this.getExprParser().parsePartition(); partitionClause.addPartition(partitionDef); @@ -610,9 +540,11 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { return stmt; } + + protected SQLPartitionByRange partitionByRange() { acceptIdentifier("RANGE"); - + SQLPartitionByRange clause = new SQLPartitionByRange(); if (lexer.token() == Token.LPAREN) { @@ -639,12 +571,11 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { if (identifierEquals("PARTITIONS")) { lexer.nextToken(); - SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue()); - accept(Token.LITERAL_INT); + SQLIntegerExpr countExpr = this.exprParser.integerExpr(); clause.setPartitionsCount(countExpr); } - if (identifierEquals("PARTITION")) { + if (lexer.token() == Token.PARTITION) { lexer.nextToken(); if (identifierEquals("NUM")) { @@ -757,6 +688,14 @@ public class MySqlCreateTableParser extends SQLCreateTableParser { subPartitionByClause.getOptions().add(option); } + + if (identifierEquals("SUBPARTITIONS")) { + lexer.nextToken(); + Number intValue = lexer.integerValue(); + SQLNumberExpr numExpr = new SQLNumberExpr(intValue); + subPartitionByClause.setSubPartitionsCount(numExpr); + lexer.nextToken(); + } if (subPartitionByClause != null) { subPartitionByClause.setLinear(linear); diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlExprParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlExprParser.java index 0821ac80a1..ed0851f80c 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlExprParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlExprParser.java @@ -20,6 +20,9 @@ import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.SQLName; import com.alibaba.druid.sql.ast.SQLOrderBy; import com.alibaba.druid.sql.ast.SQLOrderingSpecification; +import com.alibaba.druid.sql.ast.SQLPartition; +import com.alibaba.druid.sql.ast.SQLPartitionValue; +import com.alibaba.druid.sql.ast.SQLSubPartition; import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr; import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; @@ -553,7 +556,7 @@ public class MySqlExprParser extends SQLExprParser { lexer.nextToken(); } - if (identifierEquals("PARTITION")) { + if (lexer.token() == Token.PARTITION) { throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal()); } @@ -894,4 +897,102 @@ public class MySqlExprParser extends SQLExprParser { return item; } + + public SQLPartition parsePartition() { + accept(Token.PARTITION); + + SQLPartition partitionDef = new SQLPartition(); + + partitionDef.setName(this.name()); + + SQLPartitionValue values = this.parsePartitionValues(); + if (values != null) { + partitionDef.setValues(values); + } + + for (;;) { + boolean storage = false; + if (identifierEquals("DATA")) { + lexer.nextToken(); + acceptIdentifier("DIRECTORY"); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + partitionDef.setDataDirectory(this.expr()); + } else if (lexer.token() == Token.TABLESPACE) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + SQLName tableSpace = this.name(); + partitionDef.setTableSpace(tableSpace); + } else if (lexer.token() == Token.INDEX) { + lexer.nextToken(); + acceptIdentifier("DIRECTORY"); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + partitionDef.setIndexDirectory(this.expr()); + } else if (identifierEquals("MAX_ROWS")) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + SQLExpr maxRows = this.primary(); + partitionDef.setMaxRows(maxRows); + } else if (identifierEquals("MIN_ROWS")) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + SQLExpr minRows = this.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.name(); + partitionDef.setEngine(engine); + } else if (lexer.token() == Token.COMMENT) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + SQLExpr comment = this.primary(); + partitionDef.setComment(comment); + } else { + break; + } + } + + if (lexer.token() == Token.LPAREN) { + lexer.nextToken(); + + for (;;) { + acceptIdentifier("SUBPARTITION"); + + SQLName subPartitionName = this.name(); + SQLSubPartition subPartition = new SQLSubPartition(); + subPartition.setName(subPartitionName); + + partitionDef.addSubPartition(subPartition); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } + break; + } + + accept(Token.RPAREN); + } + return partitionDef; + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java index 88f6c15ab3..b9f266abb6 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java @@ -53,6 +53,9 @@ public class MySqlLexer extends Lexer { map.put("ROW", Token.ROW); map.put("BEGIN", Token.BEGIN); map.put("END", Token.END); + + // for oceanbase & mysql 5.7 + map.put("PARTITION", Token.PARTITION); DEFAULT_MYSQL_KEYWORDS = new Keywords(map); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlSelectParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlSelectParser.java index 10e0f42526..51e40dfb84 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlSelectParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlSelectParser.java @@ -342,6 +342,13 @@ public class MySqlSelectParser extends SQLSelectParser { parseIndexHint(hint); tableSource.getHints().add(hint); } + + if (lexer.token() == Token.PARTITION) { + lexer.nextToken(); + accept(Token.LPAREN); + this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource); + accept(Token.RPAREN); + } return super.parseTableSourceRest(tableSource); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java index cacb9f852c..587e6b9137 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java @@ -26,6 +26,7 @@ import com.alibaba.druid.sql.ast.SQLName; import com.alibaba.druid.sql.ast.SQLOrderBy; import com.alibaba.druid.sql.ast.SQLParameter; import com.alibaba.druid.sql.ast.SQLParameter.ParameterType; +import com.alibaba.druid.sql.ast.SQLPartition; import com.alibaba.druid.sql.ast.SQLStatement; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator; @@ -39,6 +40,8 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterDatabaseStatement; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPartition; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableCoalescePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableConvertCharSet; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; @@ -47,9 +50,11 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableReOrganizePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement; import com.alibaba.druid.sql.ast.statement.SQLBlockStatement; import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; @@ -262,7 +267,7 @@ public class MySqlStatementParser extends SQLStatementParser { if (lexer.token() == Token.COMMENT) { lexer.nextToken(); } - + if (lexer.token() == Token.HINT) { this.getExprParser().parseHints(deleteStatement.getHints()); } @@ -382,13 +387,13 @@ public class MySqlStatementParser extends SQLStatementParser { } return parseCreateTrigger(); } - //parse create procedure - if (lexer.token() == Token.PROCEDURE || identifierEquals("DEFINER")) { - if (replace) { - lexer.reset(markBp, markChar, Token.CREATE); - } - return parseCreateProcedure(); - } + // parse create procedure + if (lexer.token() == Token.PROCEDURE || identifierEquals("DEFINER")) { + if (replace) { + lexer.reset(markBp, markChar, Token.CREATE); + } + return parseCreateProcedure(); + } throw new ParserException("TODO " + lexer.token()); } @@ -2286,12 +2291,11 @@ public class MySqlStatementParser extends SQLStatementParser { if (lexer.token() == Token.TABLE) { return parseAlterTable(ignore); } - if (lexer.token() == Token.DATABASE) { return parseAlterDatabase(); } - + throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); } @@ -2349,12 +2353,29 @@ public class MySqlStatementParser extends SQLStatementParser { SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk); - item.setParent(stmt); - stmt.addItem(item); } else { throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); } + } else if (lexer.token() == Token.PARTITION) { + lexer.nextToken(); + + SQLAlterTableAddPartition item = new SQLAlterTableAddPartition(); + + if (identifierEquals("PARTITIONS")) { + lexer.nextToken(); + item.setPartitionCount(this.exprParser.integerExpr()); + } + + if (lexer.token() == Token.LPAREN) { + lexer.nextToken(); + SQLPartition partition = this.getExprParser().parsePartition(); + accept(Token.RPAREN); + item.addPartition(partition); + } + + + stmt.addItem(item); } else if (identifierEquals(FULLTEXT)) { throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); } else if (identifierEquals(SPATIAL)) { @@ -2370,11 +2391,11 @@ public class MySqlStatementParser extends SQLStatementParser { MySqlAlterTableAlterColumn alterColumn = new MySqlAlterTableAlterColumn(); alterColumn.setColumn(this.exprParser.name()); - + if (lexer.token() == Token.SET) { lexer.nextToken(); accept(Token.DEFAULT); - + alterColumn.setDefaultExpr(this.exprParser.expr()); } else { accept(Token.DROP); @@ -2466,17 +2487,17 @@ public class MySqlStatementParser extends SQLStatementParser { accept(Token.TO); acceptIdentifier("CHARACTER"); accept(Token.SET); - + SQLAlterTableConvertCharSet item = new SQLAlterTableConvertCharSet(); SQLExpr charset = this.exprParser.primary(); item.setCharset(charset); - + if (identifierEquals("COLLATE")) { lexer.nextToken(); SQLExpr collate = this.exprParser.primary(); item.setCollate(collate); } - + stmt.addItem(item); } else if (lexer.token() == Token.DEFAULT) { throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); @@ -2495,9 +2516,37 @@ public class MySqlStatementParser extends SQLStatementParser { } else if (identifierEquals("TRUNCATE")) { throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); } else if (identifierEquals("COALESCE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); + lexer.nextToken(); + accept(Token.PARTITION); + + SQLAlterTableCoalescePartition item = new SQLAlterTableCoalescePartition(); + SQLIntegerExpr countExpr = this.exprParser.integerExpr(); + item.setCount(countExpr); + stmt.addItem(item); } else if (identifierEquals("REORGANIZE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); + lexer.nextToken(); + accept(Token.PARTITION); + + SQLAlterTableReOrganizePartition item = new SQLAlterTableReOrganizePartition(); + + this.exprParser.names(item.getNames(), item); + + accept(Token.INTO); + accept(Token.LPAREN); + for (;;) { + SQLPartition partition = this.getExprParser().parsePartition(); + + item.addPartition(partition); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } else { + break; + } + } + accept(Token.RPAREN); + stmt.addItem(item); } else if (identifierEquals("EXCHANGE")) { throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); } else if (identifierEquals("ANALYZE")) { @@ -2584,10 +2633,10 @@ public class MySqlStatementParser extends SQLStatementParser { lexer.nextToken(); parenFlag = true; } - + SQLAlterTableAddColumn item = new SQLAlterTableAddColumn(); for (;;) { - + SQLColumnDefinition columnDef = this.exprParser.parseColumn(); item.addColumn(columnDef); if (identifierEquals("AFTER")) { @@ -2601,17 +2650,17 @@ public class MySqlStatementParser extends SQLStatementParser { item.setFirst(true); } } - + if (parenFlag && lexer.token() == Token.COMMA) { lexer.nextToken(); continue; } - + break; } - + stmt.addItem(item); - + if (parenFlag) { accept(Token.RPAREN); } @@ -2674,6 +2723,9 @@ public class MySqlStatementParser extends SQLStatementParser { } stmt.addItem(item); + } else if (lexer.token() == Token.PARTITION) { + SQLAlterTableDropPartition dropPartition = parseAlterTableDropPartition(false); + stmt.addItem(dropPartition); } else if (lexer.token() == Token.IDENTIFIER) { SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem(); this.exprParser.names(item.getColumns()); @@ -2717,14 +2769,14 @@ public class MySqlStatementParser extends SQLStatementParser { accept(Token.DATABASE); SQLCreateDatabaseStatement stmt = new SQLCreateDatabaseStatement(JdbcConstants.MYSQL); - + if (lexer.token() == Token.IF) { lexer.nextToken(); accept(Token.NOT); accept(Token.EXISTS); stmt.setIfNotExists(true); } - + stmt.setName(this.exprParser.name()); if (lexer.token() == Token.DEFAULT) { @@ -2734,7 +2786,7 @@ public class MySqlStatementParser extends SQLStatementParser { if (lexer.token() == Token.HINT) { stmt.setHints(this.exprParser.parseHints()); } - + if (identifierEquals("CHARACTER")) { lexer.nextToken(); accept(Token.SET); @@ -2742,7 +2794,7 @@ public class MySqlStatementParser extends SQLStatementParser { accept(Token.IDENTIFIER); stmt.setCharacterSet(charset); } - + if (identifierEquals("COLLATE")) { lexer.nextToken(); String collate = lexer.stringVal(); @@ -2767,15 +2819,15 @@ public class MySqlStatementParser extends SQLStatementParser { lexer.nextToken(); } } - + public SQLStatement parseAlterDatabase() { accept(Token.DATABASE); - + SQLAlterDatabaseStatement stmt = new SQLAlterDatabaseStatement(); - + SQLName name = this.exprParser.name(); stmt.setName(name); - + if (identifierEquals("UPGRADE")) { lexer.nextToken(); acceptIdentifier("DATA"); @@ -2783,7 +2835,7 @@ public class MySqlStatementParser extends SQLStatementParser { acceptIdentifier("NAME"); stmt.setUpgradeDataDirectoryName(true); } - + return stmt; } @@ -2816,23 +2868,23 @@ public class MySqlStatementParser extends SQLStatementParser { MySqlHintStatement stmt = new MySqlHintStatement(); stmt.setHints(this.exprParser.parseHints()); - return stmt; - } + return stmt; + } - /** - * parse create procedure statement - */ - public SQLCreateProcedureStatement parseCreateProcedure() { - /** - * CREATE OR REPALCE PROCEDURE SP_NAME(parameter_list) BEGIN block_statement END - */ - SQLCreateProcedureStatement stmt = new SQLCreateProcedureStatement(); + /** + * parse create procedure statement + */ + public SQLCreateProcedureStatement parseCreateProcedure() { + /** + * CREATE OR REPALCE PROCEDURE SP_NAME(parameter_list) BEGIN block_statement END + */ + SQLCreateProcedureStatement stmt = new SQLCreateProcedureStatement(); - if(identifierEquals("DEFINER")) { - lexer.nextToken(); - accept(Token.EQ); - SQLName definer = this.exprParser.name(); - stmt.setDefiner(definer); + if (identifierEquals("DEFINER")) { + lexer.nextToken(); + accept(Token.EQ); + SQLName definer = this.exprParser.name(); + stmt.setDefiner(definer); } else { accept(Token.CREATE); if (lexer.token() == Token.OR) { @@ -2842,502 +2894,482 @@ public class MySqlStatementParser extends SQLStatementParser { } } - accept(Token.PROCEDURE); + accept(Token.PROCEDURE); - stmt.setName(this.exprParser.name()); + stmt.setName(this.exprParser.name()); - if (lexer.token() == Token.LPAREN) {// match "(" - lexer.nextToken(); - parserParameters(stmt.getParameters()); - accept(Token.RPAREN);// match ")" - } - SQLBlockStatement block = this.parseBlock(); + if (lexer.token() == Token.LPAREN) {// match "(" + lexer.nextToken(); + parserParameters(stmt.getParameters()); + accept(Token.RPAREN);// match ")" + } + SQLBlockStatement block = this.parseBlock(); - stmt.setBlock(block); + stmt.setBlock(block); - return stmt; - } + return stmt; + } - /** - * parse create procedure parameters - * @param parameters - */ - private void parserParameters(List parameters) { - if (lexer.token() == Token.RPAREN) { - return; - } - - for (;;) { - SQLParameter parameter = new SQLParameter(); + /** + * parse create procedure parameters + * + * @param parameters + */ + private void parserParameters(List parameters) { + if (lexer.token() == Token.RPAREN) { + return; + } - if (lexer.token() == Token.CURSOR) { - lexer.nextToken(); + for (;;) { + SQLParameter parameter = new SQLParameter(); - parameter.setName(this.exprParser.name()); + if (lexer.token() == Token.CURSOR) { + lexer.nextToken(); - accept(Token.IS); - SQLSelect select = this.createSQLSelectParser().select(); + parameter.setName(this.exprParser.name()); - SQLDataTypeImpl dataType = new SQLDataTypeImpl(); - dataType.setName("CURSOR"); - parameter.setDataType(dataType); + accept(Token.IS); + SQLSelect select = this.createSQLSelectParser().select(); - parameter.setDefaultValue(new SQLQueryExpr(select)); - - } else if (lexer.token() == Token.IN || lexer.token() == Token.OUT || lexer.token() == Token.INOUT) { - - if(lexer.token()==Token.IN) - { - parameter.setParamType(ParameterType.IN); - } - else if(lexer.token()==Token.OUT) - { - parameter.setParamType(ParameterType.OUT); - } - else if(lexer.token()==Token.INOUT) - { - parameter.setParamType(ParameterType.INOUT); - } - lexer.nextToken(); + SQLDataTypeImpl dataType = new SQLDataTypeImpl(); + dataType.setName("CURSOR"); + parameter.setDataType(dataType); - parameter.setName(this.exprParser.name()); + parameter.setDefaultValue(new SQLQueryExpr(select)); - parameter.setDataType(this.exprParser.parseDataType()); - } else { - parameter.setParamType(ParameterType.DEFAULT);//default parameter type is in - parameter.setName(this.exprParser.name()); - parameter.setDataType(this.exprParser.parseDataType()); + } else if (lexer.token() == Token.IN || lexer.token() == Token.OUT || lexer.token() == Token.INOUT) { - if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - parameter.setDefaultValue(this.exprParser.expr()); - } - } + if (lexer.token() == Token.IN) { + parameter.setParamType(ParameterType.IN); + } else if (lexer.token() == Token.OUT) { + parameter.setParamType(ParameterType.OUT); + } else if (lexer.token() == Token.INOUT) { + parameter.setParamType(ParameterType.INOUT); + } + lexer.nextToken(); - parameters.add(parameter); - if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) { - lexer.nextToken(); - } + parameter.setName(this.exprParser.name()); - if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) { - continue; - } + parameter.setDataType(this.exprParser.parseDataType()); + } else { + parameter.setParamType(ParameterType.DEFAULT);// default parameter type is in + parameter.setName(this.exprParser.name()); + parameter.setDataType(this.exprParser.parseDataType()); - break; - } - } + if (lexer.token() == Token.COLONEQ) { + lexer.nextToken(); + parameter.setDefaultValue(this.exprParser.expr()); + } + } - /** - * parse procedure statement block - * @param statementList - */ - private void parseProcedureStatementList(List statementList) - { - parseProcedureStatementList(statementList,-1); - } - - /** - * parse procedure statement block - */ - private void parseProcedureStatementList(List statementList, int max) { + parameters.add(parameter); + if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) { + lexer.nextToken(); + } - for (;;) { - if (max != -1) { - if (statementList.size() >= max) { - return; - } - } + if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) { + continue; + } - if (lexer.token() == Token.EOF) { - return; - } - if (lexer.token() == Token.END) { - return; - } - if (lexer.token() == Token.ELSE) { - return; - } - if (lexer.token() == (Token.SEMI)) { - lexer.nextToken(); - continue; - } - if (lexer.token() == Token.WHEN) { - return; - } - if (lexer.token() == Token.UNTIL) { - return; - } - // select into - if (lexer.token() == (Token.SELECT)) { - statementList.add(this.parseSelectInto()); - continue; - } - - // update - if (lexer.token() == (Token.UPDATE)) { - statementList.add(parseUpdateStatement()); - continue; - } - - // create - if (lexer.token() == (Token.CREATE)) { - statementList.add(parseCreate()); - continue; - } - - // insert - if (lexer.token() == Token.INSERT) { - statementList.add(parseInsert()); - continue; - } - - // delete - if (lexer.token() == (Token.DELETE)) { - statementList.add(parseDeleteStatement()); - continue; - } - - // call - if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) { - statementList.add(this.parseCall()); - continue; - } + break; + } + } - // begin - if (lexer.token() == Token.BEGIN) { - statementList.add(this.parseBlock()); - continue; - } + /** + * parse procedure statement block + * + * @param statementList + */ + private void parseProcedureStatementList(List statementList) { + parseProcedureStatementList(statementList, -1); + } - if (lexer.token() == Token.VARIANT) { - SQLExpr variant = this.exprParser.primary(); - if (variant instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant; - if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) { - SQLSetStatement stmt = new SQLSetStatement( - binaryOpExpr.getLeft(), - binaryOpExpr.getRight(), getDbType()); - statementList.add(stmt); - continue; - } - } - accept(Token.COLONEQ); - SQLExpr value = this.exprParser.expr(); + /** + * parse procedure statement block + */ + private void parseProcedureStatementList(List statementList, int max) { - SQLSetStatement stmt = new SQLSetStatement(variant, value, - getDbType()); - statementList.add(stmt); - continue; - } - - // select - if (lexer.token() == Token.LPAREN) { - char ch = lexer.current(); - int bp = lexer.bp(); - lexer.nextToken(); + for (;;) { + if (max != -1) { + if (statementList.size() >= max) { + return; + } + } - if (lexer.token() == Token.SELECT) { - lexer.reset(bp, ch, Token.LPAREN); - statementList.add(this.parseSelect()); - continue; - } else { - throw new ParserException("TODO : " + lexer.token() + " " - + lexer.stringVal()); - } - } - // assign statement - if (lexer.token() == Token.SET) { - statementList.add(this.parseAssign()); - continue; - } - - // while statement - if (lexer.token() == Token.WHILE) { - statementList.add(this.parseWhile()); - continue; - } - - // loop statement - if (lexer.token() == Token.LOOP) { - statementList.add(this.parseLoop()); - continue; - } - - // if statement - if (lexer.token() == Token.IF) { - statementList.add(this.parseIf()); - continue; - } - - // case statement - if (lexer.token() == Token.CASE) { - statementList.add(this.parseCase()); - continue; - } + if (lexer.token() == Token.EOF) { + return; + } + if (lexer.token() == Token.END) { + return; + } + if (lexer.token() == Token.ELSE) { + return; + } + if (lexer.token() == (Token.SEMI)) { + lexer.nextToken(); + continue; + } + if (lexer.token() == Token.WHEN) { + return; + } + if (lexer.token() == Token.UNTIL) { + return; + } + // select into + if (lexer.token() == (Token.SELECT)) { + statementList.add(this.parseSelectInto()); + continue; + } - // declare statement - if (lexer.token() == Token.DECLARE) { - char markChar = lexer.current(); - int markBp = lexer.bp(); - lexer.nextToken(); - lexer.nextToken(); - if(lexer.token()==Token.CURSOR)// cursor declare statement - { - lexer.reset(markBp, markChar, Token.DECLARE); - statementList.add(this.parseCursorDeclare()); - } - else - { - lexer.reset(markBp, markChar, Token.DECLARE); - statementList.add(this.parseDeclare()); - } - continue; - } - - // leave statement - if (lexer.token() == Token.LEAVE) { - statementList.add(this.parseLeave()); - continue; - } - - // iterate statement - if (lexer.token() == Token.ITERATE) { - statementList.add(this.parseIterate()); - continue; - } - - // repeat statement - if (lexer.token() == Token.REPEAT) { - statementList.add(this.parseRepeat()); - continue; - } - - // open cursor - if (lexer.token() == Token.OPEN) { - statementList.add(this.parseOpen()); - continue; - } - - // close cursor - if (lexer.token() == Token.CLOSE) { - statementList.add(this.parseClose()); - continue; - } - - // fetch cursor into - if (lexer.token() == Token.FETCH) { - statementList.add(this.parseFetch()); - continue; - } - - if(lexer.token() == Token.IDENTIFIER) - { - String label=lexer.stringVal(); - char ch = lexer.current(); - int bp = lexer.bp(); - lexer.nextToken(); - if(lexer.token()==Token.VARIANT&&lexer.stringVal().equals(":")) - { - lexer.nextToken(); - if(lexer.token()==Token.LOOP) - { - //parse loop statement - statementList.add(this.parseLoop(label)); - } - else if(lexer.token()==Token.WHILE) - { - //parse while statement with label - statementList.add(this.parseWhile(label)); - } - else if(lexer.token()==Token.BEGIN) - { - //parse begin-end statement with label - statementList.add(this.parseBlock(label)); - } - else if(lexer.token()==Token.REPEAT) - { - //parse repeat statement with label - statementList.add(this.parseRepeat(label)); - } - continue; - } - else - { - lexer.reset(bp, ch, Token.IDENTIFIER); - } - - } - throw new ParserException("TODO : " + lexer.token() + " " - + lexer.stringVal()); - } + // update + if (lexer.token() == (Token.UPDATE)) { + statementList.add(parseUpdateStatement()); + continue; + } - } + // create + if (lexer.token() == (Token.CREATE)) { + statementList.add(parseCreate()); + continue; + } - /** - * parse if statement - * @return MySqlIfStatement - */ - public SQLIfStatement parseIf() { - accept(Token.IF); + // insert + if (lexer.token() == Token.INSERT) { + statementList.add(parseInsert()); + continue; + } - SQLIfStatement stmt = new SQLIfStatement(); + // delete + if (lexer.token() == (Token.DELETE)) { + statementList.add(parseDeleteStatement()); + continue; + } - stmt.setCondition(this.exprParser.expr()); + // call + if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) { + statementList.add(this.parseCall()); + continue; + } - accept(Token.THEN); + // begin + if (lexer.token() == Token.BEGIN) { + statementList.add(this.parseBlock()); + continue; + } - this.parseProcedureStatementList(stmt.getStatements()); + if (lexer.token() == Token.VARIANT) { + SQLExpr variant = this.exprParser.primary(); + if (variant instanceof SQLBinaryOpExpr) { + SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant; + if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) { + SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), + getDbType()); + statementList.add(stmt); + continue; + } + } + accept(Token.COLONEQ); + SQLExpr value = this.exprParser.expr(); - while (lexer.token() == Token.ELSE) { - lexer.nextToken(); + SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType()); + statementList.add(stmt); + continue; + } - if (lexer.token() == Token.IF) { - lexer.nextToken(); + // select + if (lexer.token() == Token.LPAREN) { + char ch = lexer.current(); + int bp = lexer.bp(); + lexer.nextToken(); - SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf(); + if (lexer.token() == Token.SELECT) { + lexer.reset(bp, ch, Token.LPAREN); + statementList.add(this.parseSelect()); + continue; + } else { + throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal()); + } + } + // assign statement + if (lexer.token() == Token.SET) { + statementList.add(this.parseAssign()); + continue; + } - elseIf.setCondition(this.exprParser.expr()); + // while statement + if (lexer.token() == Token.WHILE) { + statementList.add(this.parseWhile()); + continue; + } - accept(Token.THEN); - this.parseProcedureStatementList(elseIf.getStatements()); + // loop statement + if (lexer.token() == Token.LOOP) { + statementList.add(this.parseLoop()); + continue; + } - stmt.getElseIfList().add(elseIf); - } else { - SQLIfStatement.Else elseItem = new SQLIfStatement.Else(); - this.parseProcedureStatementList(elseItem.getStatements()); - stmt.setElseItem(elseItem); - break; - } - } + // if statement + if (lexer.token() == Token.IF) { + statementList.add(this.parseIf()); + continue; + } - accept(Token.END); - accept(Token.IF); - accept(Token.SEMI); + // case statement + if (lexer.token() == Token.CASE) { + statementList.add(this.parseCase()); + continue; + } - return stmt; - } + // declare statement + if (lexer.token() == Token.DECLARE) { + char markChar = lexer.current(); + int markBp = lexer.bp(); + lexer.nextToken(); + lexer.nextToken(); + if (lexer.token() == Token.CURSOR)// cursor declare statement + { + lexer.reset(markBp, markChar, Token.DECLARE); + statementList.add(this.parseCursorDeclare()); + } else { + lexer.reset(markBp, markChar, Token.DECLARE); + statementList.add(this.parseDeclare()); + } + continue; + } - /** - * parse while statement - * @return MySqlWhileStatement - */ - public MySqlWhileStatement parseWhile() { - accept(Token.WHILE); - MySqlWhileStatement stmt = new MySqlWhileStatement(); + // leave statement + if (lexer.token() == Token.LEAVE) { + statementList.add(this.parseLeave()); + continue; + } - stmt.setCondition(this.exprParser.expr()); + // iterate statement + if (lexer.token() == Token.ITERATE) { + statementList.add(this.parseIterate()); + continue; + } - accept(Token.DO); + // repeat statement + if (lexer.token() == Token.REPEAT) { + statementList.add(this.parseRepeat()); + continue; + } - this.parseProcedureStatementList(stmt.getStatements()); + // open cursor + if (lexer.token() == Token.OPEN) { + statementList.add(this.parseOpen()); + continue; + } - accept(Token.END); + // close cursor + if (lexer.token() == Token.CLOSE) { + statementList.add(this.parseClose()); + continue; + } - accept(Token.WHILE); - - accept(Token.SEMI); + // fetch cursor into + if (lexer.token() == Token.FETCH) { + statementList.add(this.parseFetch()); + continue; + } - return stmt; + if (lexer.token() == Token.IDENTIFIER) { + String label = lexer.stringVal(); + char ch = lexer.current(); + int bp = lexer.bp(); + lexer.nextToken(); + if (lexer.token() == Token.VARIANT && lexer.stringVal().equals(":")) { + lexer.nextToken(); + if (lexer.token() == Token.LOOP) { + // parse loop statement + statementList.add(this.parseLoop(label)); + } else if (lexer.token() == Token.WHILE) { + // parse while statement with label + statementList.add(this.parseWhile(label)); + } else if (lexer.token() == Token.BEGIN) { + // parse begin-end statement with label + statementList.add(this.parseBlock(label)); + } else if (lexer.token() == Token.REPEAT) { + // parse repeat statement with label + statementList.add(this.parseRepeat(label)); + } + continue; + } else { + lexer.reset(bp, ch, Token.IDENTIFIER); + } - } - - /** - * parse while statement with label - * @return MySqlWhileStatement - */ - public MySqlWhileStatement parseWhile(String label) { - accept(Token.WHILE); - - MySqlWhileStatement stmt = new MySqlWhileStatement(); + } + throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal()); + } - stmt.setLabelName(label); - - stmt.setCondition(this.exprParser.expr()); + } - accept(Token.DO); + /** + * parse if statement + * + * @return MySqlIfStatement + */ + public SQLIfStatement parseIf() { + accept(Token.IF); - this.parseProcedureStatementList(stmt.getStatements()); + SQLIfStatement stmt = new SQLIfStatement(); - accept(Token.END); + stmt.setCondition(this.exprParser.expr()); - accept(Token.WHILE); - - acceptIdentifier(label); - - accept(Token.SEMI); + accept(Token.THEN); - return stmt; + this.parseProcedureStatementList(stmt.getStatements()); - } - - /** - * parse case statement - * @return MySqlCaseStatement - */ - public MySqlCaseStatement parseCase() - { - MySqlCaseStatement stmt=new MySqlCaseStatement(); - accept(Token.CASE); - - if(lexer.token()==Token.WHEN)// grammar 1 - { - while (lexer.token() == Token.WHEN) { - - MySqlWhenStatement when=new MySqlWhenStatement(); - //when expr - when.setCondition(exprParser.expr()); - - accept(Token.THEN); - - //when block - parseProcedureStatementList(when.getStatements()); - - stmt.addWhenStatement(when); - } - if(lexer.token()==Token.ELSE) - { - //parse else block - SQLIfStatement.Else elseStmt=new SQLIfStatement.Else(); - parseProcedureStatementList(elseStmt.getStatements()); - stmt.setElseItem(elseStmt); - } - } - else// grammar 2 - { - //case expr - stmt.setCondition(exprParser.expr()); - - while (lexer.token() == Token.WHEN) { - accept(Token.WHEN); - MySqlWhenStatement when=new MySqlWhenStatement(); - //when expr - when.setCondition(exprParser.expr()); - - accept(Token.THEN); - - //when block - parseProcedureStatementList(when.getStatements()); - - stmt.addWhenStatement(when); - } - if(lexer.token()==Token.ELSE) - { - accept(Token.ELSE); - //else block - SQLIfStatement.Else elseStmt=new SQLIfStatement.Else(); - parseProcedureStatementList(elseStmt.getStatements()); - stmt.setElseItem(elseStmt); - } - } - accept(Token.END); - accept(Token.CASE); - accept(Token.SEMI); - return stmt; - - } - /** - * parse declare statement - */ + while (lexer.token() == Token.ELSE) { + lexer.nextToken(); + + if (lexer.token() == Token.IF) { + lexer.nextToken(); + + SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf(); + + elseIf.setCondition(this.exprParser.expr()); + + accept(Token.THEN); + this.parseProcedureStatementList(elseIf.getStatements()); + + stmt.getElseIfList().add(elseIf); + } else { + SQLIfStatement.Else elseItem = new SQLIfStatement.Else(); + this.parseProcedureStatementList(elseItem.getStatements()); + stmt.setElseItem(elseItem); + break; + } + } + + accept(Token.END); + accept(Token.IF); + accept(Token.SEMI); + + return stmt; + } + + /** + * parse while statement + * + * @return MySqlWhileStatement + */ + public MySqlWhileStatement parseWhile() { + accept(Token.WHILE); + MySqlWhileStatement stmt = new MySqlWhileStatement(); + + stmt.setCondition(this.exprParser.expr()); + + accept(Token.DO); + + this.parseProcedureStatementList(stmt.getStatements()); + + accept(Token.END); + + accept(Token.WHILE); + + accept(Token.SEMI); + + return stmt; + + } + + /** + * parse while statement with label + * + * @return MySqlWhileStatement + */ + public MySqlWhileStatement parseWhile(String label) { + accept(Token.WHILE); + + MySqlWhileStatement stmt = new MySqlWhileStatement(); + + stmt.setLabelName(label); + + stmt.setCondition(this.exprParser.expr()); + + accept(Token.DO); + + this.parseProcedureStatementList(stmt.getStatements()); + + accept(Token.END); + + accept(Token.WHILE); + + acceptIdentifier(label); + + accept(Token.SEMI); + + return stmt; + + } + + /** + * parse case statement + * + * @return MySqlCaseStatement + */ + public MySqlCaseStatement parseCase() { + MySqlCaseStatement stmt = new MySqlCaseStatement(); + accept(Token.CASE); + + if (lexer.token() == Token.WHEN)// grammar 1 + { + while (lexer.token() == Token.WHEN) { + + MySqlWhenStatement when = new MySqlWhenStatement(); + // when expr + when.setCondition(exprParser.expr()); + + accept(Token.THEN); + + // when block + parseProcedureStatementList(when.getStatements()); + + stmt.addWhenStatement(when); + } + if (lexer.token() == Token.ELSE) { + // parse else block + SQLIfStatement.Else elseStmt = new SQLIfStatement.Else(); + parseProcedureStatementList(elseStmt.getStatements()); + stmt.setElseItem(elseStmt); + } + } else// grammar 2 + { + // case expr + stmt.setCondition(exprParser.expr()); + + while (lexer.token() == Token.WHEN) { + accept(Token.WHEN); + MySqlWhenStatement when = new MySqlWhenStatement(); + // when expr + when.setCondition(exprParser.expr()); + + accept(Token.THEN); + + // when block + parseProcedureStatementList(when.getStatements()); + + stmt.addWhenStatement(when); + } + if (lexer.token() == Token.ELSE) { + accept(Token.ELSE); + // else block + SQLIfStatement.Else elseStmt = new SQLIfStatement.Else(); + parseProcedureStatementList(elseStmt.getStatements()); + stmt.setElseItem(elseStmt); + } + } + accept(Token.END); + accept(Token.CASE); + accept(Token.SEMI); + return stmt; + + } + + /** + * parse declare statement + */ public MySqlDeclareStatement parseDeclare() { MySqlDeclareStatement stmt = new MySqlDeclareStatement(); accept(Token.DECLARE); @@ -3345,7 +3377,7 @@ public class MySqlStatementParser extends SQLStatementParser { for (;;) { SQLDeclareItem item = new SQLDeclareItem(); item.setName(exprParser.name()); - + stmt.addVar(item); if (lexer.token() == Token.COMMA) { accept(Token.COMMA); @@ -3353,13 +3385,13 @@ public class MySqlStatementParser extends SQLStatementParser { } else if (lexer.token() != Token.EOF) { // var type item.setDataType(exprParser.parseDataType()); - + if (lexer.token() == Token.DEFAULT) { lexer.nextToken(); SQLExpr defaultValue = this.exprParser.primary(); item.setValue(defaultValue); } - + break; } else { throw new ParserException("TODO"); @@ -3367,44 +3399,41 @@ public class MySqlStatementParser extends SQLStatementParser { } return stmt; } - - /** - * parse assign statement - */ - public SQLSetStatement parseAssign() - { - accept(Token.SET); - SQLSetStatement stmt = new SQLSetStatement(getDbType()); - parseAssignItems(stmt.getItems(), stmt); - return stmt; - } - - /** - * parse select into - */ - public MySqlSelectIntoStatement parseSelectInto() - { - MySqlSelectIntoParser parse=new MySqlSelectIntoParser(this.exprParser); - return parse.parseSelectInto(); - } - - /** - * parse loop statement - */ - public SQLLoopStatement parseLoop() - { - SQLLoopStatement loopStmt=new SQLLoopStatement(); - accept(Token.LOOP); - parseProcedureStatementList(loopStmt.getStatements()); - accept(Token.END); - accept(Token.LOOP); - accept(Token.SEMI); - return loopStmt; - } - - /** - * parse loop statement with label - */ + + /** + * parse assign statement + */ + public SQLSetStatement parseAssign() { + accept(Token.SET); + SQLSetStatement stmt = new SQLSetStatement(getDbType()); + parseAssignItems(stmt.getItems(), stmt); + return stmt; + } + + /** + * parse select into + */ + public MySqlSelectIntoStatement parseSelectInto() { + MySqlSelectIntoParser parse = new MySqlSelectIntoParser(this.exprParser); + return parse.parseSelectInto(); + } + + /** + * parse loop statement + */ + public SQLLoopStatement parseLoop() { + SQLLoopStatement loopStmt = new SQLLoopStatement(); + accept(Token.LOOP); + parseProcedureStatementList(loopStmt.getStatements()); + accept(Token.END); + accept(Token.LOOP); + accept(Token.SEMI); + return loopStmt; + } + + /** + * parse loop statement with label + */ public SQLLoopStatement parseLoop(String label) { SQLLoopStatement loopStmt = new SQLLoopStatement(); loopStmt.setLabelName(label); @@ -3416,10 +3445,10 @@ public class MySqlStatementParser extends SQLStatementParser { accept(Token.SEMI); return loopStmt; } - - /** - * parse loop statement with label - */ + + /** + * parse loop statement with label + */ public SQLBlockStatement parseBlock(String label) { SQLBlockStatement block = new SQLBlockStatement(); block.setLabelName(label); @@ -3429,88 +3458,86 @@ public class MySqlStatementParser extends SQLStatementParser { acceptIdentifier(label); return block; } - - /** - * parse leave statement - */ - public MySqlLeaveStatement parseLeave() - { - accept(Token.LEAVE); - MySqlLeaveStatement leaveStmt=new MySqlLeaveStatement(); - leaveStmt.setLabelName(exprParser.name().getSimpleName()); - accept(Token.SEMI); - return leaveStmt; - } - - /** - * parse iterate statement - */ - public MySqlIterateStatement parseIterate() - { - accept(Token.ITERATE); - MySqlIterateStatement iterateStmt=new MySqlIterateStatement(); - iterateStmt.setLabelName(exprParser.name().getSimpleName()); - accept(Token.SEMI); - return iterateStmt; - } - - /** - * parse repeat statement - * @return - */ - public MySqlRepeatStatement parseRepeat() - { - MySqlRepeatStatement repeatStmt=new MySqlRepeatStatement(); - accept(Token.REPEAT); - parseProcedureStatementList(repeatStmt.getStatements()); - accept(Token.UNTIL); - repeatStmt.setCondition(exprParser.expr()); - accept(Token.END); - accept(Token.REPEAT); - accept(Token.SEMI); - return repeatStmt; - } - - /** - * parse repeat statement with label - * @param label - * @return - */ - public MySqlRepeatStatement parseRepeat(String label) - { - MySqlRepeatStatement repeatStmt=new MySqlRepeatStatement(); - repeatStmt.setLabelName(label); - accept(Token.REPEAT); - parseProcedureStatementList(repeatStmt.getStatements()); - accept(Token.UNTIL); - repeatStmt.setCondition(exprParser.expr()); - accept(Token.END); - accept(Token.REPEAT); - acceptIdentifier(label); - accept(Token.SEMI); - return repeatStmt; - } - - /** - * parse cursor declare statement - * @return - */ - public MySqlCursorDeclareStatement parseCursorDeclare() - { - MySqlCursorDeclareStatement stmt=new MySqlCursorDeclareStatement(); - accept(Token.DECLARE); - - stmt.setCursorName(exprParser.name().getSimpleName()); - - accept(Token.CURSOR); - - accept(Token.FOR); - - stmt.setSelect(parseSelect()); - - accept(Token.SEMI); - - return stmt; - } - + + /** + * parse leave statement + */ + public MySqlLeaveStatement parseLeave() { + accept(Token.LEAVE); + MySqlLeaveStatement leaveStmt = new MySqlLeaveStatement(); + leaveStmt.setLabelName(exprParser.name().getSimpleName()); + accept(Token.SEMI); + return leaveStmt; + } + + /** + * parse iterate statement + */ + public MySqlIterateStatement parseIterate() { + accept(Token.ITERATE); + MySqlIterateStatement iterateStmt = new MySqlIterateStatement(); + iterateStmt.setLabelName(exprParser.name().getSimpleName()); + accept(Token.SEMI); + return iterateStmt; + } + + /** + * parse repeat statement + * + * @return + */ + public MySqlRepeatStatement parseRepeat() { + MySqlRepeatStatement repeatStmt = new MySqlRepeatStatement(); + accept(Token.REPEAT); + parseProcedureStatementList(repeatStmt.getStatements()); + accept(Token.UNTIL); + repeatStmt.setCondition(exprParser.expr()); + accept(Token.END); + accept(Token.REPEAT); + accept(Token.SEMI); + return repeatStmt; + } + + /** + * parse repeat statement with label + * + * @param label + * @return + */ + public MySqlRepeatStatement parseRepeat(String label) { + MySqlRepeatStatement repeatStmt = new MySqlRepeatStatement(); + repeatStmt.setLabelName(label); + accept(Token.REPEAT); + parseProcedureStatementList(repeatStmt.getStatements()); + accept(Token.UNTIL); + repeatStmt.setCondition(exprParser.expr()); + accept(Token.END); + accept(Token.REPEAT); + acceptIdentifier(label); + accept(Token.SEMI); + return repeatStmt; + } + + /** + * parse cursor declare statement + * + * @return + */ + public MySqlCursorDeclareStatement parseCursorDeclare() { + MySqlCursorDeclareStatement stmt = new MySqlCursorDeclareStatement(); + accept(Token.DECLARE); + + stmt.setCursorName(exprParser.name().getSimpleName()); + + accept(Token.CURSOR); + + accept(Token.FOR); + + stmt.setSelect(parseSelect()); + + accept(Token.SEMI); + + return stmt; + } + } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index 261d4ac469..c39adb221d 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -2575,6 +2575,12 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV print(' '); x.getHints().get(i).accept(this); } + + if (x.getPartitionSize() > 0) { + print0(ucase ? " PARTITION (" : " partition ("); + printlnAndAccept(x.getPartitions(), ", "); + print(')'); + } return false; } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/oracle/ast/stmt/OracleMultiInsertStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/oracle/ast/stmt/OracleMultiInsertStatement.java index d1d9b1d439..d917287f64 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/oracle/ast/stmt/OracleMultiInsertStatement.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/oracle/ast/stmt/OracleMultiInsertStatement.java @@ -20,7 +20,6 @@ import java.util.List; import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.SQLHint; -import com.alibaba.druid.sql.ast.SQLPartition; import com.alibaba.druid.sql.ast.statement.SQLInsertInto; import com.alibaba.druid.sql.ast.statement.SQLSelect; import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject; diff --git a/src/main/java/com/alibaba/druid/sql/dialect/oracle/parser/OracleCreateTableParser.java b/src/main/java/com/alibaba/druid/sql/dialect/oracle/parser/OracleCreateTableParser.java index 0f71fd71bc..51da6f64f5 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/oracle/parser/OracleCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/oracle/parser/OracleCreateTableParser.java @@ -263,39 +263,9 @@ public class OracleCreateTableParser extends SQLCreateTableParser { accept(Token.LPAREN); for (;;) { - acceptIdentifier("PARTITION"); - SQLPartition range = new SQLPartition(); - range.setName(this.exprParser.name()); + SQLPartition partition = parsePartition(); - SQLPartitionValue values = parsePartitionValues(); - if (values != null) { - range.setValues(values); - } - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - for (;;) { - SQLSubPartition subPartition = parseSubPartition(); - - range.addSubPartition(subPartition); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - accept(Token.RPAREN); - } else if (identifierEquals("SUBPARTITIONS")) { - lexer.nextToken(); - SQLExpr subPartitionsCount = this.exprParser.primary(); - range.setSubPartitionsCount(subPartitionsCount); - } - - clause.addPartition(range); + clause.addPartition(partition); if (lexer.token() == Token.COMMA) { lexer.nextToken(); @@ -308,6 +278,41 @@ public class OracleCreateTableParser extends SQLCreateTableParser { accept(Token.RPAREN); } + protected SQLPartition parsePartition() { + acceptIdentifier("PARTITION"); + SQLPartition partition = new SQLPartition(); + partition.setName(this.exprParser.name()); + + SQLPartitionValue values = this.exprParser.parsePartitionValues(); + if (values != null) { + partition.setValues(values); + } + + if (lexer.token() == Token.LPAREN) { + lexer.nextToken(); + + for (;;) { + SQLSubPartition subPartition = parseSubPartition(); + + partition.addSubPartition(subPartition); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } + + break; + } + + accept(Token.RPAREN); + } else if (identifierEquals("SUBPARTITIONS")) { + lexer.nextToken(); + SQLExpr subPartitionsCount = this.exprParser.primary(); + partition.setSubPartitionsCount(subPartitionsCount); + } + return partition; + } + protected SQLSubPartition parseSubPartition() { acceptIdentifier("SUBPARTITION"); @@ -315,7 +320,7 @@ public class OracleCreateTableParser extends SQLCreateTableParser { SQLName name = this.exprParser.name(); subPartition.setName(name); - SQLPartitionValue values = parsePartitionValues(); + SQLPartitionValue values = this.exprParser.parsePartitionValues(); if (values != null) { subPartition.setValues(values); } @@ -327,8 +332,7 @@ public class OracleCreateTableParser extends SQLCreateTableParser { if (identifierEquals("PARTITIONS")) { lexer.nextToken(); - SQLIntegerExpr countExpr = new SQLIntegerExpr(lexer.integerValue()); - accept(Token.LITERAL_INT); + SQLIntegerExpr countExpr = this.exprParser.integerExpr(); clause.setPartitionsCount(countExpr); } diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java b/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java index 46a579feb6..0cfc491c7b 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java @@ -16,8 +16,6 @@ package com.alibaba.druid.sql.parser; import com.alibaba.druid.sql.ast.SQLName; -import com.alibaba.druid.sql.ast.SQLPartitionValue; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; import com.alibaba.druid.sql.ast.statement.SQLConstraint; import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; @@ -129,44 +127,4 @@ public class SQLCreateTableParser extends SQLDDLParser { return new SQLCreateTableStatement(getDbType()); } - protected SQLPartitionValue parsePartitionValues() { - if (lexer.token() != Token.VALUES) { - return null; - } - lexer.nextToken(); - - SQLPartitionValue values = null; - - if (lexer.token() == Token.IN) { - lexer.nextToken(); - values = new SQLPartitionValue(SQLPartitionValue.Operator.In); - - accept(Token.LPAREN); - this.exprParser.exprList(values.getItems(), values); - accept(Token.RPAREN); - } else if (identifierEquals("LESS")) { - lexer.nextToken(); - acceptIdentifier("THAN"); - - values = new SQLPartitionValue(SQLPartitionValue.Operator.LessThan); - - if (identifierEquals("MAXVALUE")) { - SQLIdentifierExpr maxValue = new SQLIdentifierExpr(lexer.stringVal()); - lexer.nextToken(); - maxValue.setParent(values); - values.addItem(maxValue); - } else { - accept(Token.LPAREN); - this.exprParser.exprList(values.getItems(), values); - accept(Token.RPAREN); - } - } else if (lexer.token() == Token.LPAREN) { - values = new SQLPartitionValue(SQLPartitionValue.Operator.List); - lexer.nextToken(); - this.exprParser.exprList(values.getItems(), values); - accept(Token.RPAREN); - } - - return values; - } } diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java b/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java index f2c43f4de3..f13df09738 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java @@ -29,6 +29,7 @@ import com.alibaba.druid.sql.ast.SQLObject; import com.alibaba.druid.sql.ast.SQLOrderBy; import com.alibaba.druid.sql.ast.SQLOrderingSpecification; import com.alibaba.druid.sql.ast.SQLOver; +import com.alibaba.druid.sql.ast.SQLPartitionValue; import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr; import com.alibaba.druid.sql.ast.expr.SQLAggregateOption; import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; @@ -185,6 +186,12 @@ public class SQLExprParser extends SQLParser { } return expr; } + + public SQLIntegerExpr integerExpr() { + SQLIntegerExpr intExpr = new SQLIntegerExpr(lexer.integerValue()); + accept(Token.LITERAL_INT); + return intExpr; + } public SQLExpr primary() { List beforeComments = null; @@ -1902,4 +1909,45 @@ public class SQLExprParser extends SQLParser { return expr; } + + public SQLPartitionValue parsePartitionValues() { + if (lexer.token() != Token.VALUES) { + return null; + } + lexer.nextToken(); + + SQLPartitionValue values = null; + + if (lexer.token() == Token.IN) { + lexer.nextToken(); + values = new SQLPartitionValue(SQLPartitionValue.Operator.In); + + accept(Token.LPAREN); + this.exprList(values.getItems(), values); + accept(Token.RPAREN); + } else if (identifierEquals("LESS")) { + lexer.nextToken(); + acceptIdentifier("THAN"); + + values = new SQLPartitionValue(SQLPartitionValue.Operator.LessThan); + + if (identifierEquals("MAXVALUE")) { + SQLIdentifierExpr maxValue = new SQLIdentifierExpr(lexer.stringVal()); + lexer.nextToken(); + maxValue.setParent(values); + values.addItem(maxValue); + } else { + accept(Token.LPAREN); + this.exprList(values.getItems(), values); + accept(Token.RPAREN); + } + } else if (lexer.token() == Token.LPAREN) { + values = new SQLPartitionValue(SQLPartitionValue.Operator.List); + lexer.nextToken(); + this.exprList(values.getItems(), values); + accept(Token.RPAREN); + } + + return values; + } } diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java b/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java index 02c4f7d96e..e63c9e4b07 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java @@ -102,9 +102,9 @@ public class SQLStatementParser extends SQLParser { protected SQLExprParser exprParser; - protected boolean parseCompleteValues = true; + protected boolean parseCompleteValues = true; - protected int parseValuesSize = 3; + protected int parseValuesSize = 3; public SQLStatementParser(String sql){ this(sql, null); @@ -165,7 +165,7 @@ public class SQLStatementParser extends SQLParser { int line0 = lexer.getLine(); lexer.nextToken(); int line1 = lexer.getLine(); - + if (lexer.isKeepComments() && statementList.size() > 0) { SQLStatement stmt = statementList.get(statementList.size() - 1); if (line1 - line0 <= 1) { @@ -223,13 +223,13 @@ public class SQLStatementParser extends SQLParser { if (lexer.isKeepComments() && lexer.hasComment()) { beforeComments = lexer.readAndResetComments(); } - + lexer.nextToken(); if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) { SQLDropTableStatement stmt = parseDropTable(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } @@ -241,7 +241,7 @@ public class SQLStatementParser extends SQLParser { if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.INDEX) { @@ -249,61 +249,61 @@ public class SQLStatementParser extends SQLParser { if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.VIEW) { SQLStatement stmt = parseDropView(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.TRIGGER) { SQLStatement stmt = parseDropTrigger(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.DATABASE) { SQLStatement stmt = parseDropDatabase(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.FUNCTION) { SQLStatement stmt = parseDropFunction(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.TABLESPACE) { SQLStatement stmt = parseDropTablespace(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else if (lexer.token() == Token.PROCEDURE) { SQLStatement stmt = parseDropProcedure(false); - + if (beforeComments != null) { stmt.addBeforeComment(beforeComments); } - + statementList.add(stmt); continue; } else { @@ -862,7 +862,7 @@ public class SQLStatementParser extends SQLParser { accept(Token.LPAREN); - parseAssignItems(addPartition.getPartition(), addPartition); + parseAssignItems(addPartition.getPartitions(), addPartition); accept(Token.RPAREN); @@ -928,7 +928,7 @@ public class SQLStatementParser extends SQLParser { stmt.addItem(parseAlterTableRename()); } else if (lexer.token() == Token.SET) { lexer.nextToken(); - + if (lexer.token() == Token.COMMENT) { lexer.nextToken(); SQLAlterTableSetComment setComment = new SQLAlterTableSetComment(); @@ -952,43 +952,43 @@ public class SQLStatementParser extends SQLParser { parseAssignItems(renamePartition.getPartition(), renamePartition); accept(Token.RPAREN); - + if (lexer.token() == Token.ENABLE) { lexer.nextToken(); - if(identifierEquals("LIFECYCLE")) { + if (identifierEquals("LIFECYCLE")) { lexer.nextToken(); } - + SQLAlterTableEnableLifecycle enableLifeCycle = new SQLAlterTableEnableLifecycle(); for (SQLAssignItem condition : renamePartition.getPartition()) { enableLifeCycle.getPartition().add(condition); condition.setParent(enableLifeCycle); } stmt.addItem(enableLifeCycle); - + continue; } - + if (lexer.token() == Token.DISABLE) { lexer.nextToken(); - if(identifierEquals("LIFECYCLE")) { + if (identifierEquals("LIFECYCLE")) { lexer.nextToken(); } - + SQLAlterTableDisableLifecycle disableLifeCycle = new SQLAlterTableDisableLifecycle(); for (SQLAssignItem condition : renamePartition.getPartition()) { disableLifeCycle.getPartition().add(condition); condition.setParent(disableLifeCycle); } stmt.addItem(disableLifeCycle); - + continue; } - + acceptIdentifier("RENAME"); accept(Token.TO); accept(Token.PARTITION); - + accept(Token.LPAREN); parseAssignItems(renamePartition.getTo(), renamePartition); @@ -996,18 +996,18 @@ public class SQLStatementParser extends SQLParser { accept(Token.RPAREN); stmt.addItem(renamePartition); - } else if(identifierEquals("TOUCH")) { + } else if (identifierEquals("TOUCH")) { lexer.nextToken(); SQLAlterTableTouch item = new SQLAlterTableTouch(); - + if (lexer.token() == Token.PARTITION) { lexer.nextToken(); - + accept(Token.LPAREN); parseAssignItems(item.getPartition(), item); accept(Token.RPAREN); } - + stmt.addItem(item); } else { break; @@ -1107,14 +1107,24 @@ public class SQLStatementParser extends SQLParser { stmt.addItem(item); } else if (lexer.token() == Token.PARTITION) { - lexer.nextToken(); - SQLAlterTableDropPartition dropPartition = new SQLAlterTableDropPartition(); + SQLAlterTableDropPartition dropPartition = parseAlterTableDropPartition(ifNotExists); - dropPartition.setIfNotExists(ifNotExists); + stmt.addItem(dropPartition); + } else { + throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); + } + } + protected SQLAlterTableDropPartition parseAlterTableDropPartition(boolean ifNotExists) { + lexer.nextToken(); + SQLAlterTableDropPartition dropPartition = new SQLAlterTableDropPartition(); + + dropPartition.setIfNotExists(ifNotExists); + + if (lexer.token() == Token.LPAREN) { accept(Token.LPAREN); - parseAssignItems(dropPartition.getPartition(), dropPartition); + parseAssignItems(dropPartition.getPartitions(), dropPartition); accept(Token.RPAREN); @@ -1122,11 +1132,13 @@ public class SQLStatementParser extends SQLParser { lexer.nextToken(); dropPartition.setPurge(true); } - - stmt.addItem(dropPartition); } else { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); + SQLName partition = this.exprParser.name(); + dropPartition.addPartition(partition); } + + + return dropPartition; } public SQLStatement parseRename() { @@ -1306,15 +1318,15 @@ public class SQLStatementParser extends SQLParser { protected SQLDropTableSpaceStatement parseDropTablespace(boolean acceptDrop) { SQLDropTableSpaceStatement stmt = new SQLDropTableSpaceStatement(getDbType()); - + if (lexer.isKeepComments() && lexer.hasComment()) { stmt.addBeforeComment(lexer.readAndResetComments()); } - + if (acceptDrop) { accept(Token.DROP); } - + accept(Token.TABLESPACE); if (lexer.token() == Token.IF) { @@ -1549,7 +1561,7 @@ public class SQLStatementParser extends SQLParser { return stmt; } - public void parseAssignItems(List items, SQLObject parent) { + public void parseAssignItems(List items, SQLObject parent) { for (;;) { SQLAssignItem item = exprParser.parseAssignItem(); item.setParent(parent); @@ -2009,22 +2021,23 @@ public class SQLStatementParser extends SQLParser { accept(Token.RPAREN); return item; } - + /** * parse cursor open statement + * * @return */ public SQLOpenStatement parseOpen() { - SQLOpenStatement stmt=new SQLOpenStatement(); + SQLOpenStatement stmt = new SQLOpenStatement(); accept(Token.OPEN); stmt.setCursorName(exprParser.name().getSimpleName()); accept(Token.SEMI); return stmt; } - + public SQLFetchStatement parseFetch() { accept(Token.FETCH); - + SQLFetchStatement stmt = new SQLFetchStatement(); stmt.setCursorName(this.exprParser.name()); accept(Token.INTO); @@ -2037,12 +2050,12 @@ public class SQLStatementParser extends SQLParser { break; } - + return stmt; } - + public SQLStatement parseClose() { - SQLCloseStatement stmt=new SQLCloseStatement(); + SQLCloseStatement stmt = new SQLCloseStatement(); accept(Token.CLOSE); stmt.setCursorName(exprParser.name().getSimpleName()); accept(Token.SEMI); diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index f993671243..fe83a0d8c4 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.SQLCommentHint; import com.alibaba.druid.sql.ast.SQLDataType; import com.alibaba.druid.sql.ast.SQLDeclareItem; 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.SQLOrderBy; import com.alibaba.druid.sql.ast.SQLOver; @@ -86,6 +87,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableCoalescePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableConvertCharSet; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; @@ -101,6 +103,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableLifecycle; import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableReOrganizePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRename; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenameColumn; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenamePartition; @@ -272,7 +275,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab public void print(String text) { print0(text); } - + protected void print0(String text) { try { this.appender.append(text); @@ -343,7 +346,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(text); println(); } - + protected void println0(String text) { print0(text); println(); @@ -392,29 +395,29 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab for (int i = groupList.size() - 1; i >= 0; --i) { SQLExpr item = groupList.get(i); - + if (relational) { if (isPrettyFormat() && item.hasBeforeComment()) { printlnComments(item.getBeforeCommentsDirect()); } } - + if (isPrettyFormat() && item.hasBeforeComment()) { printlnComments(item.getBeforeCommentsDirect()); } - + visitBinaryLeft(item, x.getOperator()); if (isPrettyFormat() && item.hasAfterComment()) { print(' '); printComment(item.getAfterCommentsDirect(), "\n"); } - + if (i != groupList.size() - 1 && isPrettyFormat() && item.getParent().hasAfterComment()) { print(' '); printComment(item.getParent().getAfterCommentsDirect(), "\n"); } - + if (relational) { println(); } else { @@ -437,7 +440,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab if (isPrettyFormat() && x.getRight().hasBeforeComment()) { printlnComments(x.getRight().getBeforeCommentsDirect()); } - + if (x.getRight() instanceof SQLBinaryOpExpr) { SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight(); boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd @@ -461,7 +464,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } else { x.getRight().accept(this); } - + if (x.getRight().hasAfterComment() && isPrettyFormat()) { print(' '); printlnComments(x.getRight().getAfterCommentsDirect()); @@ -587,7 +590,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } final List list = x.getTargetList(); - + boolean printLn = false; if (list.size() > 5) { printLn = true; @@ -598,7 +601,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } } } - + if (printLn) { incrementIndent(); println(); @@ -634,7 +637,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); return false; } - + protected void printFunctionName(String name) { print0(name); } @@ -764,7 +767,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab for (int i = 0; i < itemSize; ++i) { if (i != 0) { if (groupItemSingleLine) { - println(", "); + println(", "); } else { print(", "); } @@ -779,15 +782,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? "HAVING " : "having "); x.getHaving().accept(this); } - + if (x.isWithRollUp()) { print0(ucase ? " WITH ROLLUP" : " with rollup"); } - + if (x.isWithCube()) { print0(ucase ? " WITH CUBE" : " with cube"); } - + return false; } @@ -817,7 +820,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab if (isPrettyFormat() && x.hasBeforeComment()) { printComment(x.getBeforeCommentsDirect(), "\n"); } - + print0(ucase ? "SELECT " : "select "); if (SQLSetQuantifier.ALL == x.getDistionOption()) { @@ -876,7 +879,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab if (x.isSibings()) { print0(ucase ? "ORDER SIBLINGS BY " : "order siblings by "); } else { - print0(ucase ? "ORDER BY " : "order by "); + print0(ucase ? "ORDER BY " : "order by "); } printAndAccept(x.getItems(), ", "); @@ -901,12 +904,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab public boolean visit(SQLExprTableSource x) { x.getExpr().accept(this); - + if (x.getAlias() != null) { print(' '); print0(x.getAlias()); } - + if (isPrettyFormat() && x.hasAfterComment()) { print(' '); printComment(x.getAfterCommentsDirect(), "\n"); @@ -1016,7 +1019,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } protected void printCascade() { - print0(ucase ? " CASCADE": " cascade"); + print0(ucase ? " CASCADE" : " cascade"); } public boolean visit(SQLDropViewStatement x) { @@ -1084,10 +1087,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? " COMMENT " : " comment "); x.getComment().accept(this); } - + return false; } - + @Override public boolean visit(SQLColumnDefinition.Identity x) { print0(ucase ? "IDENTITY (" : "identity ("); @@ -1629,20 +1632,20 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab public boolean visit(SQLCreateDatabaseStatement x) { print0(ucase ? "CREATE DATABASE " : "create database "); if (x.isIfNotExists()) { - print0(ucase ? "IF NOT EXISTS " : "if not exists "); + print0(ucase ? "IF NOT EXISTS " : "if not exists "); } x.getName().accept(this); - + if (x.getCharacterSet() != null) { print0(ucase ? " CHARACTER SET " : " character set "); print0(x.getCharacterSet()); } - + if (x.getCollate() != null) { print0(ucase ? " COLLATE " : " collate "); print0(x.getCollate()); } - + return false; } @@ -1852,7 +1855,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? "DROP PRIMARY KEY" : "drop primary key"); return false; } - + @Override public boolean visit(SQLAlterTableDropKey x) { print0(ucase ? "DROP KEY " : "drop key "); @@ -1930,10 +1933,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(" ("); printAndAccept(x.getItems(), ", "); print(')'); - + // for mysql if (x.getUsing() != null) { - print0(ucase ? " USING " : " using ");; + print0(ucase ? " USING " : " using "); + ; print0(x.getUsing()); } @@ -2353,7 +2357,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getTo().accept(this); return false; } - + @Override public boolean visit(SQLShowTablesStatement x) { print0(ucase ? "SHOW TABLES" : "show tables"); @@ -2361,14 +2365,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? " FROM " : " from "); x.getDatabase().accept(this); } - + if (x.getLike() != null) { print0(ucase ? " LIKE " : " like "); x.getLike().accept(this); } return false; } - + protected void printComment(List comments, String seperator) { if (comments != null) { for (int i = 0; i < comments.size(); ++i) { @@ -2380,7 +2384,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } } } - + protected void printlnComments(List comments) { if (comments != null) { for (int i = 0; i < comments.size(); ++i) { @@ -2390,7 +2394,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } } } - + @Override public boolean visit(SQLAlterViewRenameStatement x) { print0(ucase ? "ALTER VIEW " : "alter view "); @@ -2399,35 +2403,62 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getTo().accept(this); return false; } - + @Override public boolean visit(SQLAlterTableAddPartition x) { print0(ucase ? "ADD " : "add "); if (x.isIfNotExists()) { print0(ucase ? "IF NOT EXISTS " : "if not exists "); } - print0(ucase ? "PARTITION (" : "partition ("); - printAndAccept(x.getPartition(), ", "); + + if (x.getPartitionCount() != null) { + print0(ucase ? "PARTITION PARTITIONS " : "partition partitions "); + x.getPartitionCount().accept(this); + } + + if (x.getPartitions().size() > 0) { + print0(ucase ? "PARTITION (" : "partition ("); + printAndAccept(x.getPartitions(), ", "); + print(')'); + } + + return false; + } + + @Override + public boolean visit(SQLAlterTableReOrganizePartition x) { + print0(ucase ? "REORGANIZE " : "reorganize "); + + printAndAccept(x.getNames(), ", "); + + print0(ucase ? " INTO (" : " into ("); + printAndAccept(x.getPartitions(), ", "); print(')'); return false; } - + @Override public boolean visit(SQLAlterTableDropPartition x) { print0(ucase ? "DROP " : "drop "); if (x.isIfNotExists()) { print0(ucase ? "IF NOT EXISTS " : "if not exists "); } - print0(ucase ? "PARTITION (" : "partition ("); - printAndAccept(x.getPartition(), ", "); - print(')'); - + print0(ucase ? "PARTITION " : "partition "); + + if (x.getPartitions().size() == 1 && x.getPartitions().get(0) instanceof SQLName) { + x.getPartitions().get(0).accept(this); + } else { + print('('); + printAndAccept(x.getPartitions(), ", "); + print(')'); + } + if (x.isPurge()) { print0(ucase ? " PURGE" : " purge"); } return false; } - + @Override public boolean visit(SQLAlterTableRenamePartition x) { print0(ucase ? "PARTITION (" : "partition ("); @@ -2437,21 +2468,21 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); return false; } - + @Override public boolean visit(SQLAlterTableSetComment x) { print0(ucase ? "SET COMMENT " : "set comment "); x.getComment().accept(this); return false; } - + @Override public boolean visit(SQLAlterTableSetLifecycle x) { print0(ucase ? "SET LIFECYCLE " : "set lifecycle "); x.getLifecycle().accept(this); return false; } - + @Override public boolean visit(SQLAlterTableEnableLifecycle x) { if (x.getPartition().size() != 0) { @@ -2459,11 +2490,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab printAndAccept(x.getPartition(), ", "); print0(") "); } - + print0(ucase ? "ENABLE LIFECYCLE" : "enable lifecycle"); return false; } - + @Override public boolean visit(SQLAlterTableDisableLifecycle x) { if (x.getPartition().size() != 0) { @@ -2471,11 +2502,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab printAndAccept(x.getPartition(), ", "); print0(") "); } - + print0(ucase ? "DISABLE LIFECYCLE" : "disable lifecycle"); return false; } - + @Override public boolean visit(SQLAlterTableTouch x) { print0(ucase ? "TOUCH" : "touch"); @@ -2495,7 +2526,6 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(']'); return false; } - @Override public boolean visit(SQLOpenStatement x) { @@ -2503,7 +2533,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(x.getCursorName()); return false; } - + @Override public boolean visit(SQLFetchStatement x) { print0(ucase ? "FETCH " : "fetch "); @@ -2512,14 +2542,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab printAndAccept(x.getInto(), ", "); return false; } - + @Override public boolean visit(SQLCloseStatement x) { print0(ucase ? "CLOSE " : "close "); print0(x.getCursorName()); return false; } - + @Override public boolean visit(SQLGroupingSetExpr x) { print0(ucase ? "GROUPING SETS" : "grouping sets"); @@ -2528,7 +2558,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); return false; } - + @Override public boolean visit(SQLIfStatement x) { print0(ucase ? "IF " : "if "); @@ -2544,7 +2574,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } } decrementIndent(); - + for (SQLIfStatement.ElseIf elseIf : x.getElseIfList()) { println(); elseIf.accept(this); @@ -2556,7 +2586,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } return false; } - + @Override public boolean visit(SQLIfStatement.Else x) { print0(ucase ? "ELSE" : "else"); @@ -2575,7 +2605,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab decrementIndent(); return false; } - + @Override public boolean visit(SQLIfStatement.ElseIf x) { print0(ucase ? "ELSE IF" : "else if"); @@ -2583,7 +2613,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? " THEN" : " then"); incrementIndent(); println(); - + for (int i = 0, size = x.getStatements().size(); i < size; ++i) { if (i != 0) { println(); @@ -2592,11 +2622,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab item.setParent(x); item.accept(this); } - + decrementIndent(); return false; } - + @Override public boolean visit(SQLLoopStatement x) { print0(ucase ? "LOOP" : "loop"); @@ -2617,7 +2647,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print0(ucase ? "END LOOP" : "end loop"); return false; } - + @Override public boolean visit(SQLParameter x) { if (x.getDataType().getName().equalsIgnoreCase("CURSOR")) { @@ -2652,12 +2682,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab return false; } - + @Override public boolean visit(SQLDeclareItem x) { x.getName().accept(this); - - if(x.getType() == SQLDeclareItem.Type.TABLE) { + + if (x.getType() == SQLDeclareItem.Type.TABLE) { print0(ucase ? " TABLE" : " table"); int size = x.getTableElementList().size(); @@ -2692,23 +2722,22 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getValue().accept(this); } } - + return false; } - + @Override public boolean visit(SQLPartitionValue x) { if (x.getOperator() == SQLPartitionValue.Operator.LessThan // - && (!JdbcConstants.ORACLE.equals(getDbType())) - && x.getItems().size() == 1 // - && x.getItems().get(0) instanceof SQLIdentifierExpr) { + && (!JdbcConstants.ORACLE.equals(getDbType())) && 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; } } - + if (x.getOperator() == SQLPartitionValue.Operator.LessThan) { print0(ucase ? "VALUES LESS THAN (" : "values less than ("); } else if (x.getOperator() == SQLPartitionValue.Operator.In) { @@ -2720,7 +2749,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); return false; } - + public String getDbType() { return dbType; } @@ -2728,11 +2757,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab public boolean isUppCase() { return ucase; } - + public void setUppCase(boolean val) { this.ucase = val; } - + @Override public boolean visit(SQLPartition x) { print0(ucase ? "PARTITION " : "partition "); @@ -2749,7 +2778,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getDataDirectory().accept(this); decrementIndent(); } - + if (x.getIndexDirectory() != null) { incrementIndent(); println(); @@ -2757,32 +2786,32 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab 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); } - + if (x.getSubPartitionsCount() != null) { incrementIndent(); println(); @@ -2790,7 +2819,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getSubPartitionsCount().accept(this); decrementIndent(); } - + if (x.getSubPartitions().size() > 0) { println(); print('('); @@ -2809,7 +2838,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab return false; } - + @Override public boolean visit(SQLPartitionByRange x) { print0(ucase ? "PARTITION BY RANGE" : "partition by range"); @@ -2833,7 +2862,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } printPartitionsCountAndSubPartitions(x); - + println(); print('('); incrementIndent(); @@ -2847,10 +2876,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab decrementIndent(); println(); print(')'); - + return false; } - + @Override public boolean visit(SQLPartitionByList x) { print0(ucase ? "PARTITION BY LIST " : "partition by list "); @@ -2885,7 +2914,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } return false; } - + @Override public boolean visit(SQLPartitionByHash x) { if (x.isLinear()) { @@ -2906,7 +2935,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab return false; } - + protected void printPartitionsCountAndSubPartitions(SQLPartitionBy x) { if (x.getPartitionsCount() != null) { @@ -2918,12 +2947,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab x.getPartitionsCount().accept(this); } - + if (x.getSubPartitionBy() != null) { println(); x.getSubPartitionBy().accept(this); } - + if (x.getStoreIn().size() > 0) { println(); print0(ucase ? "STORE IN (" : "store in ("); @@ -2931,7 +2960,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); } } - + @Override public boolean visit(SQLSubPartitionByHash x) { if (x.isLinear()) { @@ -2955,7 +2984,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab return false; } - + @Override public boolean visit(SQLSubPartitionByList x) { if (x.isLinear()) { @@ -2963,16 +2992,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab } else { print0(ucase ? "SUBPARTITION BY HASH " : "subpartition by hash "); } - + print('('); x.getColumn().accept(this); print(')'); - + if (x.getSubPartitionsCount() != null) { print0(ucase ? " SUBPARTITIONS " : " subpartitions "); x.getSubPartitionsCount().accept(this); } - + if (x.getSubPartitionTemplate().size() > 0) { incrementIndent(); println(); @@ -2985,42 +3014,49 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab print(')'); decrementIndent(); } - + return false; } - + @Override public boolean visit(SQLSubPartition x) { print0(ucase ? "SUBPARTITION " : "subpartition "); x.getName().accept(this); - + if (x.getValues() != null) { print(' '); x.getValues().accept(this); } - + return false; } - + @Override public boolean visit(SQLAlterDatabaseStatement x) { print0(ucase ? "ALTER DATABASE " : "alter database "); x.getName().accept(this); if (x.isUpgradeDataDirectoryName()) { - print0(ucase ? " UPGRADE DATA DIRECTORY NAME" : " upgrade data directory name"); + print0(ucase ? " UPGRADE DATA DIRECTORY NAME" : " upgrade data directory name"); } return false; } - + @Override public boolean visit(SQLAlterTableConvertCharSet x) { print0(ucase ? "CONVERT TO CHARACTER SET " : "convert to character set "); x.getCharset().accept(this); - + if (x.getCollate() != null) { print0(ucase ? "COLLATE " : "collate "); x.getCollate().accept(this); } return false; } + + @Override + public boolean visit(SQLAlterTableCoalescePartition x) { + print0(ucase ? "COALESCE PARTITION " : "coalesce partition "); + x.getCount().accept(this); + return false; + } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitor.java index 9dc0a22c78..5c49b7ac99 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitor.java @@ -70,6 +70,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableCoalescePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableConvertCharSet; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; @@ -84,6 +85,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableLifecycle; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableReOrganizePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRename; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenameColumn; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenamePartition; @@ -740,4 +742,12 @@ public interface SQLASTVisitor { boolean visit(SQLAlterTableConvertCharSet x); void endVisit(SQLAlterTableConvertCharSet x); + + boolean visit(SQLAlterTableReOrganizePartition x); + + void endVisit(SQLAlterTableReOrganizePartition x); + + boolean visit(SQLAlterTableCoalescePartition x); + + void endVisit(SQLAlterTableCoalescePartition x); } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitorAdapter.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitorAdapter.java index c459f424a9..4317a659d8 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitorAdapter.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTVisitorAdapter.java @@ -71,6 +71,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableCoalescePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableConvertCharSet; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys; @@ -85,6 +86,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableLifecycle; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableReOrganizePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRename; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenameColumn; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRenamePartition; @@ -1493,4 +1495,24 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor { } + @Override + public boolean visit(SQLAlterTableReOrganizePartition x) { + return true; + } + + @Override + public void endVisit(SQLAlterTableReOrganizePartition x) { + + } + + @Override + public boolean visit(SQLAlterTableCoalescePartition x) { + return true; + } + + @Override + public void endVisit(SQLAlterTableCoalescePartition x) { + + } + } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SchemaStatVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SchemaStatVisitor.java index 72f399ab71..489e72bddc 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SchemaStatVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SchemaStatVisitor.java @@ -51,12 +51,15 @@ import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; import com.alibaba.druid.sql.ast.statement.SQLAlterDatabaseStatement; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn; import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableCoalescePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableConvertCharSet; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint; import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; +import com.alibaba.druid.sql.ast.statement.SQLAlterTableReOrganizePartition; import com.alibaba.druid.sql.ast.statement.SQLAlterTableRename; import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement; import com.alibaba.druid.sql.ast.statement.SQLBlockStatement; @@ -1514,4 +1517,19 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter { public boolean visit(SQLAlterTableConvertCharSet x) { return false; } + + @Override + public boolean visit(SQLAlterTableDropPartition x) { + return false; + } + + @Override + public boolean visit(SQLAlterTableReOrganizePartition x) { + return false; + } + + @Override + public boolean visit(SQLAlterTableCoalescePartition x) { + return false; + } } diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest.java new file mode 100644 index 0000000000..e30cbb8c26 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableAddPartitionTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "ALTER TABLE members ADD PARTITION (PARTITION p3 VALUES LESS THAN (2000));"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE members" + + "\n\tADD PARTITION (PARTITION p3 VALUES LESS THAN (2000))", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest2.java new file mode 100644 index 0000000000..a279a29846 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableAddPartitionTest2.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableAddPartitionTest2 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "alter table clients add partition partitions 18;"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE clients" + + "\n\tADD PARTITION PARTITIONS 18", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableCoalescePartitionTest.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableCoalescePartitionTest.java new file mode 100644 index 0000000000..53d7eed68d --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableCoalescePartitionTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableCoalescePartitionTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "ALTER TABLE clients COALESCE PARTITION 6"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE clients" + + "\n\tCOALESCE PARTITION 6", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableDropPartitionTest.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableDropPartitionTest.java new file mode 100644 index 0000000000..3a6e66f233 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableDropPartitionTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableDropPartitionTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "ALTER TABLE tnrange DROP PARTITION p1;"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE tnrange" + + "\n\tDROP PARTITION p1", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest.java new file mode 100644 index 0000000000..e4ebb88260 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableReOrganizePartitionTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "ALTER TABLE members " + + "REORGANIZE PARTITION p0 INTO ( PARTITION s0 VALUES LESS THAN (1960), PARTITION s1 VALUES LESS THAN (1970) );"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE members" + + "\n\tREORGANIZE p0 INTO (PARTITION s0 VALUES LESS THAN (1960), PARTITION s1 VALUES LESS THAN (1970))", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest2.java new file mode 100644 index 0000000000..308815d09f --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseAlterTableReOrganizePartitionTest2.java @@ -0,0 +1,64 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseAlterTableReOrganizePartitionTest2 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "ALTER TABLE members " // + + "REORGANIZE PARTITION s0,s1,p1,p2,p3 INTO ( "// + + " PARTITION m0 VALUES LESS THAN (1980), " // + + " PARTITION m1 VALUES LESS THAN (2000)" // + + ");"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("ALTER TABLE members" + + "\n\tREORGANIZE s0, s1, p1, p2, p3 INTO (PARTITION m0 VALUES LESS THAN (1980), PARTITION m1 VALUES LESS THAN (2000))", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(0, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash.java new file mode 100644 index 0000000000..83f46dcef5 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash.java @@ -0,0 +1,63 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_partitionByHash extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE tnhash ( id INT, name VARCHAR(5) ) PARTITION BY HASH(id) PARTITIONS 2;"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE tnhash (" + + "\n\tid INT, " + + "\n\tname VARCHAR(5)" + + "\n)" + + "\nPARTITION BY HASH (id) PARTITIONS 2", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash2.java new file mode 100644 index 0000000000..31e289bff6 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByHash2.java @@ -0,0 +1,75 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_partitionByHash2 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE employees ( " // + + "id INT NOT NULL, fname VARCHAR(30), " // + + "lname VARCHAR(30), " // + + "hired DATE NOT NULL DEFAULT '1970-01-01', " // + + "separated DATE NOT NULL DEFAULT '9999-12-31', " // + + "job_code INT, " // + + "store_id INT ) " + + "PARTITION BY HASH(YEAR(hired)) PARTITIONS 4"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE employees (" + + "\n\tid INT NOT NULL, " + + "\n\tfname VARCHAR(30), " + + "\n\tlname VARCHAR(30), " + + "\n\thired DATE NOT NULL DEFAULT '1970-01-01', " + + "\n\tseparated DATE NOT NULL DEFAULT '9999-12-31', " + + "\n\tjob_code INT, " + + "\n\tstore_id INT" + + "\n)" + + "\nPARTITION BY HASH (YEAR(hired)) PARTITIONS 4", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(7, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByKey.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByKey.java new file mode 100644 index 0000000000..494778e74b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_partitionByKey.java @@ -0,0 +1,63 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_partitionByKey extends MysqlTest { + + public void test_0() throws Exception { + String sql = "create table k2(id int primary key, name varchar(20)) partition by key() partitions 2"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE k2 (" + + "\n\tid int PRIMARY KEY, " + + "\n\tname varchar(20)" + + "\n)" + + "\nPARTITION BY KEY () PARTITIONS 2", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition.java new file mode 100644 index 0000000000..0ba59ee2de --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition.java @@ -0,0 +1,83 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_rangePartition extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE employees (" // + + " id INT NOT NULL, fname VARCHAR(30)," // + + " lname VARCHAR(30), hired DATE NOT NULL DEFAULT '1970-01-01'," // + + " separated DATE NOT NULL DEFAULT '9999-12-31', job_code INT NOT NULL," + + " store_id INT NOT NULL" // + + " ) PARTITION BY RANGE (store_id) " // + + "( PARTITION p0 VALUES LESS THAN (6), " // + + "PARTITION p1 VALUES LESS THAN (11), " // + + "PARTITION p2 VALUES LESS THAN (16), " // + + "PARTITION p3 VALUES LESS THAN (21) )"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE employees (" + + "\n\tid INT NOT NULL, " + + "\n\tfname VARCHAR(30), " + + "\n\tlname VARCHAR(30), " + + "\n\thired DATE NOT NULL DEFAULT '1970-01-01', " + + "\n\tseparated DATE NOT NULL DEFAULT '9999-12-31', " + + "\n\tjob_code INT NOT NULL, " + + "\n\tstore_id INT NOT NULL" + + "\n)" + + "\nPARTITION BY RANGE (store_id)" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (6)," + + "\n\tPARTITION p1 VALUES LESS THAN (11)," + + "\n\tPARTITION p2 VALUES LESS THAN (16)," + + "\n\tPARTITION p3 VALUES LESS THAN (21)" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(7, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition2.java new file mode 100644 index 0000000000..fb769ddf00 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition2.java @@ -0,0 +1,86 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_rangePartition2 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE employees ( " // + + "id INT NOT NULL, " // + + "fname VARCHAR(30), " // + + "lname VARCHAR(30), " // + + "hired DATE NOT NULL DEFAULT '1970-01-01', " // + + "separated DATE NOT NULL DEFAULT '9999-12-31', " // + + "job_code INT NOT NULL,store_id INT NOT NULL " // + + ") PARTITION BY RANGE (store_id) " // + + "( PARTITION p0 VALUES LESS THAN (6), " // + + "PARTITION p1 VALUES LESS THAN (11), " // + + "PARTITION p2 VALUES LESS THAN (16), " // + + "PARTITION p3 VALUES LESS THAN MAXVALUE " // + + ")"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE employees (" + + "\n\tid INT NOT NULL, " + + "\n\tfname VARCHAR(30), " + + "\n\tlname VARCHAR(30), " + + "\n\thired DATE NOT NULL DEFAULT '1970-01-01', " + + "\n\tseparated DATE NOT NULL DEFAULT '9999-12-31', " + + "\n\tjob_code INT NOT NULL, " + + "\n\tstore_id INT NOT NULL" + + "\n)" + + "\nPARTITION BY RANGE (store_id)" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (6)," + + "\n\tPARTITION p1 VALUES LESS THAN (11)," + + "\n\tPARTITION p2 VALUES LESS THAN (16)," + + "\n\tPARTITION p3 VALUES LESS THAN MAXVALUE" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(7, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition3.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition3.java new file mode 100644 index 0000000000..22b582e7c0 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition3.java @@ -0,0 +1,84 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_rangePartition3 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE employees ( " // + + "id INT NOT NULL, " // + + "fname VARCHAR(30), " // + + "lname VARCHAR(30), " // + + "hired DATE NOT NULL DEFAULT '1970-01-01', " // + + "separated DATE NOT NULL DEFAULT '9999-12-31', " // + + "job_code INT NOT NULL, store_id INT NOT NULL " // + + ") PARTITION BY RANGE (job_code) " // + + "( PARTITION p0 VALUES LESS THAN (100), " // + + "PARTITION p1 VALUES LESS THAN (1000), " // + + "PARTITION p2 VALUES LESS THAN (10000) " + + ")"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE employees (" + + "\n\tid INT NOT NULL, " + + "\n\tfname VARCHAR(30), " + + "\n\tlname VARCHAR(30), " + + "\n\thired DATE NOT NULL DEFAULT '1970-01-01', " + + "\n\tseparated DATE NOT NULL DEFAULT '9999-12-31', " + + "\n\tjob_code INT NOT NULL, " + + "\n\tstore_id INT NOT NULL" + + "\n)" + + "\nPARTITION BY RANGE (job_code)" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (100)," + + "\n\tPARTITION p1 VALUES LESS THAN (1000)," + + "\n\tPARTITION p2 VALUES LESS THAN (10000)" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(7, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition4.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition4.java new file mode 100644 index 0000000000..d53ef271ae --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition4.java @@ -0,0 +1,85 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_rangePartition4 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE employees ( " // + + "id INT NOT NULL, " // + + "fname VARCHAR(30), " // + + "lname VARCHAR(30), " // + + "hired DATE NOT NULL DEFAULT '1970-01-01', " // + + "separated DATE NOT NULL DEFAULT '9999-12-31', " // + + "job_code INT, store_id INT " // + + ") PARTITION BY RANGE (YEAR(separated)) ( " // + + "PARTITION p0 VALUES LESS THAN (1991), " // + + "PARTITION p1 VALUES LESS THAN (1996), " + + "PARTITION p2 VALUES LESS THAN (2001), " + + "PARTITION p3 VALUES LESS THAN MAXVALUE )"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE employees (" + + "\n\tid INT NOT NULL, " + + "\n\tfname VARCHAR(30), " + + "\n\tlname VARCHAR(30), " + + "\n\thired DATE NOT NULL DEFAULT '1970-01-01', " + + "\n\tseparated DATE NOT NULL DEFAULT '9999-12-31', " + + "\n\tjob_code INT, " + + "\n\tstore_id INT" + + "\n)" + + "\nPARTITION BY RANGE (YEAR(separated))" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (1991)," + + "\n\tPARTITION p1 VALUES LESS THAN (1996)," + + "\n\tPARTITION p2 VALUES LESS THAN (2001)," + + "\n\tPARTITION p3 VALUES LESS THAN MAXVALUE" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(7, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition5.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition5.java new file mode 100644 index 0000000000..ddfb4eb0ae --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_rangePartition5.java @@ -0,0 +1,71 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_rangePartition5 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE tnrange (id INT, name VARCHAR(5) )" + + " PARTITION BY RANGE(id) ( " + + " PARTITION p1 VALUES LESS THAN (1), " + + " PARTITION p2 VALUES LESS THAN MAXVALUE" + + ");"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE tnrange (" + + "\n\tid INT, " + + "\n\tname VARCHAR(5)" + + "\n)" + + "\nPARTITION BY RANGE (id)" + + "\n(" + + "\n\tPARTITION p1 VALUES LESS THAN (1)," + + "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition.java new file mode 100644 index 0000000000..a7500c00da --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition.java @@ -0,0 +1,75 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_subPartition extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE ts (id INT, purchased DATE) " // + + "PARTITION BY RANGE(YEAR(purchased)) " // + + "SUBPARTITION BY HASH(TO_DAYS(purchased)) " // + + "SUBPARTITIONS 2 ( " // + + "PARTITION p0 VALUES LESS THAN (1990), " // + + "PARTITION p1 VALUES LESS THAN (2000), " // + + "PARTITION p2 VALUES LESS THAN MAXVALUE )"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE ts (" + + "\n\tid INT, " + + "\n\tpurchased DATE" + + "\n)" + + "\nPARTITION BY RANGE (YEAR(purchased))" + + "\nSUBPARTITION BY HASH (TO_DAYS(purchased)) SUBPARTITIONS 2" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (1990)," + + "\n\tPARTITION p1 VALUES LESS THAN (2000)," + + "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition2.java new file mode 100644 index 0000000000..cb06d343b7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition2.java @@ -0,0 +1,90 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_subPartition2 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE ts (id INT, purchased DATE) " // + + "PARTITION BY RANGE(YEAR(purchased)) " // + + "SUBPARTITION BY HASH(TO_DAYS(purchased)) " // + + "( " + + " PARTITION p0 VALUES LESS THAN (1990) " // + + " ( SUBPARTITION s0, SUBPARTITION s1 ), " // + + " PARTITION p1 VALUES LESS THAN (2000) " // + + " (SUBPARTITION s2, SUBPARTITION s3), " // + + " PARTITION p2 VALUES LESS THAN MAXVALUE ( SUBPARTITION s4, SUBPARTITION s5 ) " // + + ")"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE ts (" + + "\n\tid INT, " + + "\n\tpurchased DATE" + + "\n)" + + "\nPARTITION BY RANGE (YEAR(purchased))" + + "\nSUBPARTITION BY HASH (TO_DAYS(purchased))" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (1990)" + + "\n\t(" + + "\n\t\tSUBPARTITION s0," + + "\n\t\tSUBPARTITION s1" + + "\n\t)," + + "\n\tPARTITION p1 VALUES LESS THAN (2000)" + + "\n\t(" + + "\n\t\tSUBPARTITION s2," + + "\n\t\tSUBPARTITION s3" + + "\n\t)," + + "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + + "\n\t(" + + "\n\t\tSUBPARTITION s4," + + "\n\t\tSUBPARTITION s5" + + "\n\t)" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition3.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition3.java new file mode 100644 index 0000000000..c58ac79bd1 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition3.java @@ -0,0 +1,81 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_subPartition3 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE ts (id INT, purchased DATE) " // + + "PARTITION BY RANGE(YEAR(purchased)) " // + + "SUBPARTITION BY HASH(TO_DAYS(purchased)) " // + + "( PARTITION p0 VALUES LESS THAN (1990) " // + + "( SUBPARTITION s0, SUBPARTITION s1 ), PARTITION p1 VALUES LESS THAN (2000), PARTITION p2 VALUES LESS THAN MAXVALUE ( SUBPARTITION s2, SUBPARTITION s3 ) )"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE ts (" + + "\n\tid INT, " + + "\n\tpurchased DATE" + + "\n)" + + "\nPARTITION BY RANGE (YEAR(purchased))" + + "\nSUBPARTITION BY HASH (TO_DAYS(purchased))" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (1990)" + + "\n\t(" + + "\n\t\tSUBPARTITION s0," + + "\n\t\tSUBPARTITION s1" + + "\n\t)," + + "\n\tPARTITION p1 VALUES LESS THAN (2000)," + + "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + + "\n\t(" + + "\n\t\tSUBPARTITION s2," + + "\n\t\tSUBPARTITION s3" + + "\n\t)" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition4.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition4.java new file mode 100644 index 0000000000..2a61fb346e --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseCreateTableTest_subPartition4.java @@ -0,0 +1,86 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseCreateTableTest_subPartition4 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE TABLE ts (id INT, purchased DATE) " // + + "PARTITION BY RANGE(YEAR(purchased)) " // + + "SUBPARTITION BY HASH(TO_DAYS(purchased)) " // + + "( PARTITION p0 VALUES LESS THAN (1990) ( SUBPARTITION s0, SUBPARTITION s1 ), " // + + "PARTITION p1 VALUES LESS THAN (2000) ( SUBPARTITION s0, SUBPARTITION s1 ), " // + + "PARTITION p2 VALUES LESS THAN MAXVALUE ( SUBPARTITION s0, SUBPARTITION s1 ) )"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("CREATE TABLE ts (" + + "\n\tid INT, " + + "\n\tpurchased DATE" + + "\n)" + + "\nPARTITION BY RANGE (YEAR(purchased))" + + "\nSUBPARTITION BY HASH (TO_DAYS(purchased))" + + "\n(" + + "\n\tPARTITION p0 VALUES LESS THAN (1990)" + + "\n\t(" + + "\n\t\tSUBPARTITION s0," + + "\n\t\tSUBPARTITION s1" + + "\n\t)," + + "\n\tPARTITION p1 VALUES LESS THAN (2000)" + + "\n\t(" + + "\n\t\tSUBPARTITION s0," + + "\n\t\tSUBPARTITION s1" + + "\n\t)," + + "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + + "\n\t(" + + "\n\t\tSUBPARTITION s0," + + "\n\t\tSUBPARTITION s1" + + "\n\t)" + + "\n)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index.java similarity index 95% rename from src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index.java rename to src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index.java index b4fe470f35..054ff096fd 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.bvt.sql.mysql; +package com.alibaba.druid.bvt.sql.oceanbase; import java.util.List; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index2.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index2.java similarity index 95% rename from src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index2.java rename to src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index2.java index 2a9a57c522..0b1e20477d 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Index2.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Index2.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.bvt.sql.mysql; +package com.alibaba.druid.bvt.sql.oceanbase; import java.util.List; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Topk.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Topk.java similarity index 95% rename from src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Topk.java rename to src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Topk.java index dffbc43f5f..2ae84ac231 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_Topk.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_Topk.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.bvt.sql.mysql; +package com.alibaba.druid.bvt.sql.oceanbase; import java.util.List; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_multiget.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_multiget.java similarity index 95% rename from src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_multiget.java rename to src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_multiget.java index 47d9ae9f14..91ae9dec56 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_multiget.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_multiget.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.bvt.sql.mysql; +package com.alibaba.druid.bvt.sql.oceanbase; import java.util.List; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_parallel.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_parallel.java similarity index 95% rename from src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_parallel.java rename to src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_parallel.java index d325bcfdea..bcdc3e2882 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/OceanbaseHintTest_parallel.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseHintTest_parallel.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.bvt.sql.mysql; +package com.alibaba.druid.bvt.sql.oceanbase; import java.util.List; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseSelectPartitionTest.java b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseSelectPartitionTest.java new file mode 100644 index 0000000000..a256200aea --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/oceanbase/OceanbaseSelectPartitionTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2101 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.bvt.sql.oceanbase; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class OceanbaseSelectPartitionTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "SELECT * FROM tnhash partition(p0);"; // + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List stmtList = parser.parseStatementList(); + SQLStatement stmt = stmtList.get(0); + + String result = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("SELECT *" + + "\nFROM tnhash PARTITION (p0)", + result); + print(stmtList); + + Assert.assertEquals(1, stmtList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + System.out.println("orderBy : " + visitor.getOrderByColumns()); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(1, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getConditions().size()); + + // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store"))); + + } +}