0

我正在使用Apache DdlUtils查询 PostgreSQL 数据库以获取表和列元数据(最终目标是自动生成 javax.persistence 注释的实体 bean)。但是在我看来,DdlUtils库没有提供一种方法来获取自动增量列中使用的序列名称。Column类提供了一个isAutoIncrement方法来查询自动增量状态,但我找不到获取与其关联的序列名称的方法。这是 PostgreSQL 中 DDL 的一部分,例如:

orders=# \dS customer
                         Table "public.customer"
    Column     |       Type        |                    Modifiers
---------------+-------------------+--------------------------------------------------
 id            | integer           | not null default nextval('cst_id_seq'::regclass)
 name          | character varying | not null
 (...)

我应该直接查询一些元数据/目录表来获取那部分信息吗?

4

1 回答 1

0

回答我自己的问题......是的,就像millimoose建议的那样,它不能用DdlUtils来完成,你必须查询information_schema.columns表:

public String getPostgreSQLSequenceForColumn(String database, String table, String column) {
    try {
        Statement stmt = connection.createStatement();
        String sql="select column_default from information_schema.columns where "+
                   "table_catalog='"+database+"' and "                           +
                   "table_schema='public' and "                                  +
                   "table_name='"+table+"' and "                                 +
                   "column_name='"+column+"'";
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        String sequenceName = rs.getString(1);
        panicIf(rs.next());
        return sequenceName;
    } catch (Exception e) {
        throw new ExceptionAdapter(e);
    }
}
于 2012-08-06T08:31:50.150 回答