diff --git a/src/main/java/com/alibaba/druid/sql/dialect/hive/parser/HiveCreateTableParser.java b/src/main/java/com/alibaba/druid/sql/dialect/hive/parser/HiveCreateTableParser.java index b6dff806dd..873da9b785 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/hive/parser/HiveCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/hive/parser/HiveCreateTableParser.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2017 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.hive.parser; import com.alibaba.druid.sql.ast.SQLName; diff --git a/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java index 1006879c2a..306bc46382 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java @@ -1,7 +1,20 @@ +/* + * Copyright 1999-2017 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.hive.stmt; -import com.alibaba.druid.sql.ast.SQLName; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; import com.alibaba.druid.util.JdbcConstants; 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 ba962c5af2..5fa4c1cf3e 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLCreateTableParser.java @@ -19,6 +19,7 @@ import java.util.List; import com.alibaba.druid.sql.ast.SQLName; import com.alibaba.druid.sql.ast.statement.*; +import com.alibaba.druid.util.FnvHash; import com.alibaba.druid.util.JdbcConstants; public class SQLCreateTableParser extends SQLDDLParser { @@ -77,6 +78,14 @@ public class SQLCreateTableParser extends SQLDDLParser { accept(Token.TABLE); + if (lexer.token() == Token.IF) { + lexer.nextToken(); + accept(Token.NOT); + accept(Token.EXISTS); + + createTable.setIfNotExiists(true); + } + createTable.setName(this.exprParser.name()); if (lexer.token == Token.LPAREN) { 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 5cfeecdbb2..a6ba88ebbb 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java @@ -1309,6 +1309,7 @@ public class SQLExprParser extends SQLParser { case ORDER: case DO: case JOIN: + case TYPE: identName = lexer.stringVal(); lexer.nextToken(); break; 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 7a624efcbe..19b85f5fd0 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -2526,6 +2526,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet } print0(ucase ? "TABLE " : "table "); + if (x.isIfNotExiists()) { + print0(ucase ? "IF NOT EXISTS " : "if not exists "); + } + printTableSourceExpr(x.getName()); printTableElements(x.getTableElementList()); diff --git a/src/test/java/com/alibaba/druid/bvt/sql/h2/H2_CreateTableTest_0.java b/src/test/java/com/alibaba/druid/bvt/sql/h2/H2_CreateTableTest_0.java index 6627e5f26b..88cecaee3a 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/h2/H2_CreateTableTest_0.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/h2/H2_CreateTableTest_0.java @@ -45,7 +45,7 @@ public class H2_CreateTableTest_0 extends OracleTest { { String text = SQLUtils.toSQLString(stmt, JdbcConstants.HIVE); - assertEquals("CREATE TABLE sc001.test STORE AS parquet\n" + + assertEquals("CREATE TABLE IF NOT EXISTS sc001.test STORE AS parquet\n" + "AS\n" + "SELECT *\n" + "FROM sc002.test;", text); diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/createTable/MySqlCreateTableTest88.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/createTable/MySqlCreateTableTest88.java new file mode 100644 index 0000000000..a6a74a7a4f --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/createTable/MySqlCreateTableTest88.java @@ -0,0 +1,69 @@ +/* + * Copyright 1999-2017 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.createTable; + +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.sql.parser.SQLParserFeature; + +public class MySqlCreateTableTest88 extends MysqlTest { + + public void test_one() throws Exception { + String sql = "CREATE TABLE `t_share_like_info` (\n" + + " `id` bigint(20) NOT NULL AUTO_INCREMENT,\n" + + " `share_id` bigint(20) NOT NULL,\n" + + " `user_name` bigint(20) NOT NULL,\n" + + " PRIMARY KEY (`id`)\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + + MySqlStatementParser parser = new MySqlStatementParser(sql, SQLParserFeature.KeepComments); + SQLStatement stmt = parser.parseCreateTable(); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + visitor.containsTable("t_share_like_info"); +// System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); +// System.out.println("coditions : " + visitor.getConditions()); +// System.out.println("orderBy : " + visitor.getOrderByColumns()); + + System.out.println(stmt); + + { + String output = SQLUtils.toMySqlString(stmt); + assertEquals("CREATE TABLE `t_share_like_info` (\n" + + "\t`id` bigint(20) NOT NULL AUTO_INCREMENT,\n" + + "\t`share_id` bigint(20) NOT NULL,\n" + + "\t`user_name` bigint(20) NOT NULL,\n" + + "\tPRIMARY KEY (`id`)\n" + + ") ENGINE = InnoDB CHARSET = utf8", output); + } + + { + String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION); + assertEquals("create table `t_share_like_info` (\n" + + "\t`id` bigint(20) not null auto_increment,\n" + + "\t`share_id` bigint(20) not null,\n" + + "\t`user_name` bigint(20) not null,\n" + + "\tprimary key (`id`)\n" + + ") engine = InnoDB charset = utf8", output); + } + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_8.java b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_8.java new file mode 100644 index 0000000000..ca3f745302 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_8.java @@ -0,0 +1,67 @@ +/* + * Copyright 1999-2017 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.postgresql; + +import com.alibaba.druid.sql.PGTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; +import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor; +import com.alibaba.druid.stat.TableStat; + +import java.util.List; + +public class PGCreateTableTest_8 extends PGTest { + + public void test_0() throws Exception { + String sql = "create table if not exists test_site_data_select_111 AS select * from postman_trace_info_one where lng>0 and lat>0 and site_id='17814' ;"; + + PGSQLStatementParser parser = new PGSQLStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement stmt = statementList.get(0); + + assertEquals("CREATE TABLE IF NOT EXISTS test_site_data_select_111\n" + + "AS\n" + + "SELECT *\n" + + "FROM postman_trace_info_one\n" + + "WHERE lng > 0\n" + + "\tAND lat > 0\n" + + "\tAND site_id = '17814';", SQLUtils.toPGString(stmt)); + + assertEquals("create table if not exists test_site_data_select_111\n" + + "as\n" + + "select *\n" + + "from postman_trace_info_one\n" + + "where lng > 0\n" + + "\tand lat > 0\n" + + "\tand site_id = '17814';", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION)); + + assertEquals(1, statementList.size()); + + PGSchemaStatVisitor visitor = new PGSchemaStatVisitor(); + stmt.accept(visitor); + +// System.out.println("Tables : " + visitor.getTables()); +// System.out.println("fields : " + visitor.getColumns()); + + assertTrue(visitor.getTables().containsKey(new TableStat.Name("test_site_data_select_111"))); + + assertTrue(visitor.getTables().get(new TableStat.Name("test_site_data_select_111")).getCreateCount() == 1); + + assertEquals(4, visitor.getColumns().size() ); + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_9.java b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_9.java new file mode 100644 index 0000000000..0810bb25a7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGCreateTableTest_9.java @@ -0,0 +1,61 @@ +/* + * Copyright 1999-2017 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.postgresql; + +import com.alibaba.druid.sql.PGTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; +import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor; +import com.alibaba.druid.stat.TableStat; + +import java.util.List; + +public class PGCreateTableTest_9 extends PGTest { + + public void test_0() throws Exception { + String sql = "create table test(id bigint, TYPE varchar(20))"; + + PGSQLStatementParser parser = new PGSQLStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement stmt = statementList.get(0); + + assertEquals("CREATE TABLE test (\n" + + "\tid bigint,\n" + + "\tTYPE varchar(20)\n" + + ")", SQLUtils.toPGString(stmt)); + + assertEquals("create table test (\n" + + "\tid bigint,\n" + + "\tTYPE varchar(20)\n" + + ")", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION)); + + assertEquals(1, statementList.size()); + + PGSchemaStatVisitor visitor = new PGSchemaStatVisitor(); + stmt.accept(visitor); + +// System.out.println("Tables : " + visitor.getTables()); +// System.out.println("fields : " + visitor.getColumns()); + + assertTrue(visitor.getTables().containsKey(new TableStat.Name("test"))); + + assertTrue(visitor.getTables().get(new TableStat.Name("test")).getCreateCount() == 1); + + assertEquals(2, visitor.getColumns().size() ); + } + +}