From 959e5253a95507e9bf1bfc57443c6549e9d05e3e Mon Sep 17 00:00:00 2001 From: Yako Date: Fri, 5 Sep 2014 15:05:57 +0800 Subject: [PATCH] improve sqlwall --- .../ast/statement/MySqlCreateTableStatement.java | 10 ++++++++++ .../mysql/parser/MySqlCreateTableParser.java | 5 ++++- .../dialect/mysql/visitor/MySqlOutputVisitor.java | 4 ++++ .../druid/bvt/filter/wall/MySqlWallTest_hint.java | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java index 7279975dfa..9bbc8af9c9 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java @@ -44,6 +44,8 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement private List hints = new ArrayList(); + private List optionHints = new ArrayList(); + private SQLExprTableSource like; public MySqlCreateTableStatement(){ @@ -156,4 +158,12 @@ public void accept0(MySqlASTVisitor visitor) { } } + + public List getOptionHints() { + return optionHints; + } + + public void setOptionHints(List optionHints) { + this.optionHints = optionHints; + } } 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 0680ae6d1a..43f293e5c1 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 @@ -559,7 +559,10 @@ public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) { SQLSelect query = new MySqlSelectParser(this.exprParser).select(); stmt.setQuery(query); } - + + while (lexer.token() == (Token.HINT)) { + this.exprParser.parseHints(stmt.getOptionHints()); + } 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 0fc377d48e..4897da7bfb 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 @@ -497,6 +497,10 @@ public boolean visit(MySqlCreateTableStatement x) { decrementIndent(); } + for (SQLCommentHint hint : x.getOptionHints()) { + print(' '); + hint.accept(this); + } return false; } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_hint.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_hint.java index 96e067daf8..48eb654b1a 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_hint.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest_hint.java @@ -95,4 +95,19 @@ public void test_true_8() throws Exception { Assert.assertTrue(WallUtils.isValidateMySql(sql)); // } + public void test_true_9() throws Exception { + WallConfig config = new WallConfig(); + config.setHintAllow(true); + config.setMultiStatementAllow(true); + String sql = "CREATE TABLE `session` ("// + + " `sess_id` varchar(128) NOT NULL,"// + + " `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',"// + + " `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',"// + + " `ip` varchar(40) NOT NULL,"// + + " `vars` mediumtext NOT NULL,"// + + " PRIMARY KEY(`sess_id`),"// + + " INDEX `changed_index` (`changed`)"// + + ") /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */ "; + Assert.assertTrue(WallUtils.isValidateMySql(sql, config)); // + } }