使用Java将SQL文件与现有数据库表进行比较

时间:2014-02-13 20:49:10

标签: java jdbc

我正在编写一些Java(1.7)代码来针对给定的sql文件测试给定的数据库表。我想要的是将我的sql文件转换为java对象,然后测试db字段名称和字段类型与文件支持对象相同。

示例sql文件如下所示:

create table foo (
    id int not null auto_increment,
    term_id varchar(128) not null,
    term_name varchar(255) not null,
    parent_id varchar(128) not null,
    parent_name varchar(255),
    top_term_flag varchar(5),
    primary key (id)
);
create index foo_pn on foo ( parent_name );
create index foo_ttf on foo ( top_term_flag );

我的Java程序执行此检查的部分如下所示:

// Step 1, confirm the table exists

// Database and table tests
DatabaseMetaData dbm = connection.getMetaData();

// check if "this.dbtable" exists.
// The ToUpperCase covers Oracle
ResultSet tables = dbm.getTables(null, null, this.dbtable.toUpperCase(), null);
if (tables.next()) {
    // Table exists
    log.info("Table: {} exists!", this.dbtable);

    // Step 2, get each field and test against the file

    ResultSet columns = dbm.getColumns(null, null, this.dbtable, null);

    while ( columns.next()) {
        String name = columns.getString(4);  // this gets the column name

        -> Now what? <-

    }

}

我看过Spring JDBCTestUnit和Flyway,但它们似乎没有提供我需要的功能。

谢谢。

更新 我知道我也可以使用Hibernate生成代表我的sql文件的Java类,然后针对这些来测试数据库表。有没有人有关于如何完成这项工作的样本?

2 个答案:

答案 0 :(得分:0)

如果SQL文件语法与您的示例差异不大,您可以编写一个简单的解析器来读取文件并生成您的java对象:table plus fields of fields / types and index

  • “tablename”始终位于“create table”
  • 之后
  • 字段名称和类型总是在
  • 之后
  • 之后的索引

或者有可用的解析器: jsqlparser http://jsqlparser.sourceforge.net/

本网站上的其他问题涵盖了一些相同的理由 SQL parser library for Java

答案 1 :(得分:0)

使用https://github.com/JSQLParser/JSqlParser中的JSqlParser 0.8.8。

这是获取列名,表名,类型的解析示例。因此,您可以从sqls获得java对象的层次结构。

public class CheckSQLs {

  public static void main(String[] args) throws JSQLParserException {
    String sqls = "create table foo (\n"
            + "    id int not null auto_increment,\n"
            + "    term_id varchar(128) not null,\n"
            + "    term_name varchar(255) not null,\n"
            + "    parent_id varchar(128) not null,\n"
            + "    parent_name varchar(255),\n"
            + "    top_term_flag varchar(5),\n"
            + "    primary key (id)\n"
            + ");\n"
            + "create index foo_pn on foo( parent_name );\n"
            + "create index foo_ttf on foo ( top_term_flag );";

    for (String sql : sqls.split(";")) {
        Statement parse = CCJSqlParserUtil.parse(sql);
        System.out.println(parse);
        if (parse instanceof CreateTable) {
            CreateTable ct = (CreateTable)parse;
            System.out.println("table=" + ct.getTable().getFullyQualifiedName());
            for (ColumnDefinition colDef : ct.getColumnDefinitions()) {
                System.out.println("column=" + colDef.getColumnName() + " " + colDef.getColDataType() + " " + colDef.getColumnSpecStrings());
            }
        } 
    }
  }
}

这与输出:

一起运行
CREATE TABLE foo (id int not null auto_increment, term_id varchar (128) not null, term_name varchar (255) not null, parent_id varchar (128) not null, parent_name varchar (255), top_term_flag varchar (5), primary key (id))
table=foo
column=id int [not, null, auto_increment]
column=term_id varchar (128) [not, null]
column=term_name varchar (255) [not, null]
column=parent_id varchar (128) [not, null]
column=parent_name varchar (255) null
column=top_term_flag varchar (5) null

现在您可以使用此对象来验证您的数据库。