我使用Apache DdlUtils查询表和列元数据的PostgreSQL数据库(最终目标是自动生成javax.Persistence注释的实体bean)。但是,在我看来,DdlUtils库并没有提供获取自动增量列中使用的序列名称的方法。列类提供了一个查询自动增量状态的方法,但我找不到获得与其关联的序列名称的方法。这是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
(...)我是否应该直接查询一些元数据/目录表,而不是获取这些信息呢?
发布于 2012-08-06 08:31:50
回答我自己的问题。是的,就像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);
}
}https://stackoverflow.com/questions/11812945
复制相似问题