相关疑难解决方法(0)

使用jOOQ在PostgreSQL中进行UPSERT

我正在尝试使用jOOQ库在PostgreSQL中执行UPSERT.

为此,我目前正在尝试在jOOQ中实现以下SQL语句:https://stackoverflow.com/a/6527838

到目前为止我的代码看起来像这样:

public class UpsertExecutor {

    private static final Logger logger = LoggerFactory.getLogger(UpsertExecutor.class);

    private final JOOQContextProvider jooqProvider;

    @Inject
    public UpsertExecutor(JOOQContextProvider jooqProvider) {
        Preconditions.checkNotNull(jooqProvider);

        this.jooqProvider = jooqProvider;
    }

    @Transactional
    public <T extends Record> void executeUpsert(Table<T> table, Condition condition, Map<? extends Field<?>, ?> recordValues) {
        /*
         * All of this is for trying to do an UPSERT on PostgreSQL. See:
         * https://stackoverflow.com/a/6527838
         */

        SelectConditionStep<Record1<Integer>> notExistsSelect = jooqProvider.getDSLContext().selectOne().from(table).where(condition);
        SelectConditionStep<Record> insertIntoSelect = jooqProvider.getDSLContext().select(recordValues).whereNotExists(notExistsSelect);

        try {
            int[] result = jooqProvider.getDSLContext().batch(
                jooqProvider.getDSLContext().update(table).set(recordValues).where(condition),
                jooqProvider.getDSLContext().insertInto(table).select(insertIntoSelect) …
Run Code Online (Sandbox Code Playgroud)

java sql postgresql upsert jooq

36
推荐指数
3
解决办法
7002
查看次数

标签 统计

java ×1

jooq ×1

postgresql ×1

sql ×1

upsert ×1