improved postgresql sql parser. support if not exists.

This commit is contained in:
wenshao
2017-10-22 20:43:27 +08:00
parent 1dba4fae47
commit dd0f8fe89c
9 changed files with 242 additions and 3 deletions
@@ -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);
}
}
}