jooq mysql select 通过 <R extends Record> 唯一键,不考虑字段名称
jooq mysql select by <R extends Record> unique keys, with no regard to field name
我正在编写一个方法,将记录插入或更新到 mysql table,更新仅在字段 "sample_time" 大于当前记录的这个值时发生在 table。下面是我现在想做的。
public <R extends Record> void upsertRecord(Table<R> table, R record) {
Connection conn = getConnection();
DSL.using(getConfiguration()).transaction(configuration -> {
try (DSLContext dslContext = DSL.using(configuration)) {
R old = // select with unique keys of table
// something like if (old.get("update_time) < record.get("update_time"))
dslContext.insertInto(table).set(record).onDuplicateKeyUpdate().set(record).execute();
conn.commit();
// }
} catch (SQLException e) {
// TODO
} finally {
closeConnection(conn);
}
});
}
但我不知道如何 select 通过记录的唯一键而不考虑确切的字段名称或字段编号。所以我的问题是:
- 我如何通过记录的唯一键实现“select,而不考虑确切的字段名称或字段编号”
- 否则我怎么能在 jooq
中使用有条件的重复更新条件
在此先感谢您的帮助!
您可以通过Table.getPrimaryKey()
获得对PrimaryKey
元信息的引用:
UniqueKey<R> key = table.getPrimaryKey();
if (key != null) {
List<TableField<R, ?>> fields = key.getFields();
// Now create your condition from these fields
}
我正在编写一个方法,将记录插入或更新到 mysql table,更新仅在字段 "sample_time" 大于当前记录的这个值时发生在 table。下面是我现在想做的。
public <R extends Record> void upsertRecord(Table<R> table, R record) {
Connection conn = getConnection();
DSL.using(getConfiguration()).transaction(configuration -> {
try (DSLContext dslContext = DSL.using(configuration)) {
R old = // select with unique keys of table
// something like if (old.get("update_time) < record.get("update_time"))
dslContext.insertInto(table).set(record).onDuplicateKeyUpdate().set(record).execute();
conn.commit();
// }
} catch (SQLException e) {
// TODO
} finally {
closeConnection(conn);
}
});
}
但我不知道如何 select 通过记录的唯一键而不考虑确切的字段名称或字段编号。所以我的问题是:
- 我如何通过记录的唯一键实现“select,而不考虑确切的字段名称或字段编号”
- 否则我怎么能在 jooq 中使用有条件的重复更新条件
在此先感谢您的帮助!
您可以通过Table.getPrimaryKey()
获得对PrimaryKey
元信息的引用:
UniqueKey<R> key = table.getPrimaryKey();
if (key != null) {
List<TableField<R, ?>> fields = key.getFields();
// Now create your condition from these fields
}