mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved postgresql sql parser. support if not exists.
This commit is contained in:
@@ -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;
|
||||
|
||||
+15
-2
@@ -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;
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1309,6 +1309,7 @@ public class SQLExprParser extends SQLParser {
|
||||
case ORDER:
|
||||
case DO:
|
||||
case JOIN:
|
||||
case TYPE:
|
||||
identName = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
break;
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<SQLStatement> 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() );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<SQLStatement> 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() );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user