mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve mysql parser
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlPartitionByList extends MySqlPartitioningClause {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLExpr expr;
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>();
|
||||
|
||||
private SQLExpr partitionCount;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, expr);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, partitionCount);
|
||||
acceptChild(visitor, getPartitions());
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public void setExpr(SQLExpr expr) {
|
||||
if (expr != null) {
|
||||
expr.setParent(this);
|
||||
}
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLExpr getPartitionCount() {
|
||||
return partitionCount;
|
||||
}
|
||||
|
||||
public void setPartitionCount(SQLExpr partitionCount) {
|
||||
this.partitionCount = partitionCount;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<SQLName> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -15,15 +15,36 @@ public class MySqlPartitioningDef extends MySqlObjectImpl {
|
||||
|
||||
private Values values;
|
||||
|
||||
private SQLExpr dataDirectory;
|
||||
private SQLExpr indexDirectory;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, name);
|
||||
acceptChild(visitor, values);
|
||||
acceptChild(visitor, dataDirectory);
|
||||
acceptChild(visitor, indexDirectory);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getIndexDirectory() {
|
||||
return indexDirectory;
|
||||
}
|
||||
|
||||
public void setIndexDirectory(SQLExpr indexDirectory) {
|
||||
this.indexDirectory = indexDirectory;
|
||||
}
|
||||
|
||||
public SQLExpr getDataDirectory() {
|
||||
return dataDirectory;
|
||||
}
|
||||
|
||||
public void setDataDirectory(SQLExpr dataDirectory) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
public Values getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
+67
-12
@@ -27,6 +27,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
@@ -358,7 +359,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
if (identifierEquals("PARTITION")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
|
||||
MySqlPartitioningClause partitionClause;
|
||||
|
||||
boolean linera = false;
|
||||
@@ -385,7 +386,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
@@ -438,38 +439,92 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
//
|
||||
|
||||
} else if (identifierEquals("LIST")) {
|
||||
lexer.nextToken();
|
||||
MySqlPartitionByList clause = new MySqlPartitionByList();
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
clause.setExpr(this.exprParser.expr());
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
clause.getColumns().add(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
partitionClause = clause;
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
}
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
for (;;) {
|
||||
acceptIdentifier("PARTITION");
|
||||
|
||||
|
||||
MySqlPartitioningDef partitionDef = new MySqlPartitioningDef();
|
||||
|
||||
|
||||
partitionDef.setName(this.exprParser.name());
|
||||
|
||||
|
||||
if (lexer.token() == Token.VALUES) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.IN) {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
MySqlPartitioningDef.InValues values = new MySqlPartitioningDef.InValues();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems());
|
||||
accept(Token.RPAREN);
|
||||
partitionDef.setValues(values);
|
||||
} else {
|
||||
acceptIdentifier("LESS");
|
||||
acceptIdentifier("THAN");
|
||||
|
||||
|
||||
MySqlPartitioningDef.LessThanValues values = new MySqlPartitioningDef.LessThanValues();
|
||||
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.exprList(values.getItems());
|
||||
accept(Token.RPAREN);
|
||||
partitionDef.setValues(values);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (;;) {
|
||||
if (identifierEquals("DATA")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setDataDirectory(this.exprParser.expr());
|
||||
} else if (lexer.token() == Token.INDEX) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("DIRECTORY");
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
partitionDef.setIndexDirectory(this.exprParser.expr());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
partitionClause.getPartitions().add(partitionDef);
|
||||
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
@@ -479,7 +534,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
|
||||
stmt.setPartitioning(partitionClause);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
|
||||
@@ -556,6 +557,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(MySqlPartitionByRange x);
|
||||
|
||||
boolean visit(MySqlPartitionByList x);
|
||||
|
||||
void endVisit(MySqlPartitionByList x);
|
||||
|
||||
boolean visit(MySqlPartitioningDef x);
|
||||
|
||||
void endVisit(MySqlPartitioningDef x);
|
||||
|
||||
@@ -62,6 +62,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
@@ -1259,4 +1260,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
public void endVisit(InValues x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -82,7 +82,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.LessThanValues;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
|
||||
@@ -94,7 +96,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinitio
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
|
||||
@@ -1514,7 +1515,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByRange x) {
|
||||
|
||||
@@ -1522,15 +1523,22 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByRange x) {
|
||||
print("PARTITION BY RANGE (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
print("PARTITION BY RANGE");
|
||||
if (x.getExpr() != null) {
|
||||
print(" (");
|
||||
x.getExpr().accept(this);
|
||||
print(")");
|
||||
} else {
|
||||
print(" COLUMNS (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
}
|
||||
|
||||
if (x.getPartitionCount() != null) {
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
|
||||
List<MySqlPartitioningDef> partitions = x.getPartitions();
|
||||
int partitionsSize = partitions.size();
|
||||
if (partitionsSize > 0) {
|
||||
@@ -1549,8 +1557,50 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//MySqlPartitionByRange
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
print("PARTITION BY LIST ");
|
||||
if (x.getExpr() != null) {
|
||||
print("(");
|
||||
x.getExpr().accept(this);
|
||||
print(") ");
|
||||
} else {
|
||||
print("COLUMNS (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(") ");
|
||||
}
|
||||
|
||||
if (x.getPartitionCount() != null) {
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
List<MySqlPartitioningDef> partitions = x.getPartitions();
|
||||
int partitionsSize = partitions.size();
|
||||
if (partitionsSize > 0) {
|
||||
print("(");
|
||||
incrementIndent();
|
||||
for (int i = 0; i < partitionsSize; ++i) {
|
||||
println();
|
||||
partitions.get(i).accept(this);
|
||||
if (i != partitionsSize - 1) {
|
||||
print(", ");
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
println();
|
||||
print(")");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByHash x) {
|
||||
@@ -1564,7 +1614,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
} else {
|
||||
print("PARTITION BY HASH (");
|
||||
}
|
||||
//
|
||||
//
|
||||
x.getExpr().accept(this);
|
||||
print(")");
|
||||
|
||||
@@ -1572,7 +1622,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(" PARTITIONS ");
|
||||
x.getPartitionCount().accept(this);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2956,12 +3006,28 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(' ');
|
||||
x.getValues().accept(this);
|
||||
}
|
||||
|
||||
if (x.getDataDirectory() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("DATA DIRECTORY ");
|
||||
x.getDataDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
if (x.getIndexDirectory() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("INDEX DIRECTORY ");
|
||||
x.getIndexDirectory().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitioningDef x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -2974,7 +3040,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public void endVisit(LessThanValues x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -2987,6 +3053,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public void endVisit(InValues x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
} //
|
||||
|
||||
@@ -75,6 +75,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
|
||||
@@ -1343,4 +1344,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlPartitionByList x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlPartitionByList x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class MySqlCreateTableTest42 extends MysqlTest {
|
||||
Assert.assertEquals("CREATE TABLE rc (" + //
|
||||
"\n\ta INT NOT NULL, " + //
|
||||
"\n\tb INT NOT NULL" + //
|
||||
"\n) PARTITION BY RANGE (a, b)(" + //
|
||||
"\n) PARTITION BY RANGE COLUMNS (a, b)(" + //
|
||||
"\n\tPARTITION p0 VALUES LESS THAN (10, 5), " + //
|
||||
"\n\tPARTITION p1 VALUES LESS THAN (20, 10), " + //
|
||||
"\n\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15), " + //
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
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;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class MySqlCreateTableTest43 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE client_firms (" + //
|
||||
" id INT," + //
|
||||
" name VARCHAR(35)" + //
|
||||
")" + //
|
||||
"PARTITION BY LIST (id) (" + //
|
||||
" PARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21)," + //
|
||||
" PARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22)," + //
|
||||
" PARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23)," + //
|
||||
" PARTITION r3 VALUES IN (4, 8, 12, 16, 20, 24)" + //
|
||||
");"; //
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.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("client_firms")));
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE client_firms (" + //
|
||||
"\n\tid INT, " + //
|
||||
"\n\tname VARCHAR(35)" + //
|
||||
"\n) PARTITION BY LIST (id) (" + //
|
||||
"\n\tPARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21), " + //
|
||||
"\n\tPARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22), " + //
|
||||
"\n\tPARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23), " + //
|
||||
"\n\tPARTITION r3 VALUES IN (4, 8, 12, 16, 20, 24)" + //
|
||||
"\n)", output);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
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;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class MySqlCreateTableTest44 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE lc (" + //
|
||||
" a INT NULL, " + //
|
||||
" b INT NULL" + //
|
||||
")" + //
|
||||
"PARTITION BY LIST COLUMNS(a,b) (" + //
|
||||
" PARTITION p0 VALUES IN( (0,0), (NULL,NULL) )," + //
|
||||
" PARTITION p1 VALUES IN( (0,1), (0,2), (0,3), (1,1), (1,2) )," + //
|
||||
" PARTITION p2 VALUES IN( (1,0), (2,0), (2,1), (3,0), (3,1) )," + //
|
||||
" PARTITION p3 VALUES IN( (1,3), (2,2), (2,3), (3,2), (3,3) )" + //
|
||||
");"; //
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.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("lc")));
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE lc (" + //
|
||||
"\n\ta INT NULL, " + //
|
||||
"\n\tb INT NULL" + //
|
||||
"\n) PARTITION BY LIST COLUMNS (a, b) (" + //
|
||||
"\n\tPARTITION p0 VALUES IN ((0, 0), (NULL, NULL)), " + //
|
||||
"\n\tPARTITION p1 VALUES IN ((0, 1), (0, 2), (0, 3), (1, 1), (1, 2)), " + //
|
||||
"\n\tPARTITION p2 VALUES IN ((1, 0), (2, 0), (2, 1), (3, 0), (3, 1)), " + //
|
||||
"\n\tPARTITION p3 VALUES IN ((1, 3), (2, 2), (2, 3), (3, 2), (3, 3))" + //
|
||||
"\n)", output);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
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;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class MySqlCreateTableTest45 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE th (id INT, name VARCHAR(30), adate DATE)" + //
|
||||
"PARTITION BY LIST(YEAR(adate))" + //
|
||||
"(" + //
|
||||
" PARTITION p1999 VALUES IN (1995, 1999, 2003)" + //
|
||||
" DATA DIRECTORY = '/var/appdata/95/data'" + //
|
||||
" INDEX DIRECTORY = '/var/appdata/95/idx'," + //
|
||||
" PARTITION p2000 VALUES IN (1996, 2000, 2004)" + //
|
||||
" DATA DIRECTORY = '/var/appdata/96/data'" + //
|
||||
" INDEX DIRECTORY = '/var/appdata/96/idx'," + //
|
||||
" PARTITION p2001 VALUES IN (1997, 2001, 2005)" + //
|
||||
" DATA DIRECTORY = '/var/appdata/97/data'" + //
|
||||
" INDEX DIRECTORY = '/var/appdata/97/idx'," + //
|
||||
" PARTITION p2002 VALUES IN (1998, 2002, 2006)" + //
|
||||
" DATA DIRECTORY = '/var/appdata/98/data'" + //
|
||||
" INDEX DIRECTORY = '/var/appdata/98/idx'" + //
|
||||
");"; //
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.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(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("th")));
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE th (" + //
|
||||
"\n\tid INT, " + //
|
||||
"\n\tname VARCHAR(30), " + //
|
||||
"\n\tadate DATE" + //
|
||||
"\n) PARTITION BY LIST (YEAR(adate)) (" + //
|
||||
"\n\tPARTITION p1999 VALUES IN (1995, 1999, 2003)" + //
|
||||
"\n\t\tDATA DIRECTORY '/var/appdata/95/data'" + //
|
||||
"\n\t\tINDEX DIRECTORY '/var/appdata/95/idx', " + //
|
||||
"\n\tPARTITION p2000 VALUES IN (1996, 2000, 2004)" + //
|
||||
"\n\t\tDATA DIRECTORY '/var/appdata/96/data'" + //
|
||||
"\n\t\tINDEX DIRECTORY '/var/appdata/96/idx', " + //
|
||||
"\n\tPARTITION p2001 VALUES IN (1997, 2001, 2005)" + //
|
||||
"\n\t\tDATA DIRECTORY '/var/appdata/97/data'" + //
|
||||
"\n\t\tINDEX DIRECTORY '/var/appdata/97/idx', " + //
|
||||
"\n\tPARTITION p2002 VALUES IN (1998, 2002, 2006)" + //
|
||||
"\n\t\tDATA DIRECTORY '/var/appdata/98/data'" + //
|
||||
"\n\t\tINDEX DIRECTORY '/var/appdata/98/idx'" + //
|
||||
"\n)", output);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user