From 01760a20b5eb95e63ffed7835d16adfae521b06b Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 14 Sep 2014 21:48:18 +0800 Subject: [PATCH] improve mysql createTable parser --- .../mysql/parser/MySqlCreateTableParser.java | 24 +++++++++++++++++++ .../mysql/visitor/MySqlOutputVisitor.java | 5 ++++ .../mysql/visitor/MySqlSchemaStatVisitor.java | 13 ++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) 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 43f293e5c1..c0dcbce1e1 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 @@ -22,6 +22,7 @@ import com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint; import com.alibaba.druid.sql.ast.statement.SQLSelect; import com.alibaba.druid.sql.ast.statement.SQLTableConstraint; +import com.alibaba.druid.sql.ast.statement.SQLTableSource; import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey; import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey; import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique; @@ -338,6 +339,29 @@ public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) { stmt.getTableOptions().put("STATS_PERSISTENT", this.exprParser.expr()); continue; } + + if (identifierEquals("STATS_SAMPLE_PAGES")) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + + stmt.getTableOptions().put("STATS_SAMPLE_PAGES", this.exprParser.expr()); + continue; + } + + if (lexer.token() == Token.UNION) { + lexer.nextToken(); + if (lexer.token() == Token.EQ) { + lexer.nextToken(); + } + + accept(Token.LPAREN); + SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource(); + stmt.getTableOptions().put("UNION", tableSrc); + accept(Token.RPAREN); + continue; + } if (lexer.token() == Token.TABLESPACE) { lexer.nextToken(); 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 4897da7bfb..76a8228030 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 @@ -478,6 +478,11 @@ public boolean visit(MySqlCreateTableStatement x) { print(' '); option.getValue().accept(this); continue; + } else if ("UNION".equals(key)) { + print(" = ("); + option.getValue().accept(this); + print(')'); + continue; } print(" = "); diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java index 5febee2adb..3c0e104d5a 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java @@ -27,6 +27,7 @@ import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; +import com.alibaba.druid.sql.ast.statement.SQLTableSource; import com.alibaba.druid.sql.ast.statement.SQLUnionQuery; import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint; @@ -1156,7 +1157,15 @@ public void endVisit(MySqlAlterTableOption x) { @Override public boolean visit(MySqlCreateTableStatement x) { - return super.visit((SQLCreateTableStatement) x); + boolean val = super.visit((SQLCreateTableStatement) x); + + for (SQLObject option : x.getTableOptions().values()) { + if (option instanceof SQLTableSource) { + option.accept(this); + } + } + + return val; } @Override @@ -1341,7 +1350,7 @@ public boolean visit(MySqlHintStatement x) { @Override public void endVisit(MySqlHintStatement x) { - + } }