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

使用Java将SQL文件与现有数据库表进行比较,java,jdbc,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 var

我正在编写一些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 );
// 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? <-

    }

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

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 );
// 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? <-

    }

}
//步骤1,确认表存在
//数据库和表测试
DatabaseMetaData dbm=connection.getMetaData();
//检查“this.dbtable”是否存在。
//图珀案涉及甲骨文
ResultSet tables=dbm.getTables(null,null,this.dbtable.toUpperCase(),null);
if(tables.next()){
//表存在
info(“表:{}存在!”,this.dbtable);
//步骤2,获取每个字段并对文件进行测试
ResultSet columns=dbm.getColumns(null,null,this.dbtable,null);
while(columns.next()){
String name=columns.getString(4);//获取列名

->现在怎么办?如果SQL文件语法与您的示例相差不大,您可以编写一个简单的解析器来读取文件并生成java对象:表加上字段/类型和索引列表

  • “tablename”总是在“create table”之后
  • 字段名和类型总是在后面
  • 之后的索引
或者有可用的解析器: jsqlparser

本网站上的其他问题也涉及一些相同的问题

使用来自的JSqlParser 0.8.8

下面是一个解析示例,用于获取列名、表名和类型。因此,您可以从SQL中获取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

现在,您可以使用此对象对数据库进行验证。

这正是我所需要的!谢谢!在jsqlparser 0.8.9快照中,我包含了简单的脚本解析。因此分割部分将消失。