java sql postgresql jooq upsert

java - UPSERT en PostgreSQL usando jOOQ



(3)

Estoy tratando de realizar un UPSERT en PostgreSQL usando la biblioteca jOOQ.

Para hacer esto, actualmente estoy intentando implementar la siguiente declaración SQL en jOOQ: https://stackoverflow.com/a/6527838

Mi código se ve así hasta ahora:

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) ).execute(); long rowsAffectedTotal = 0; for (int rowsAffected : result) { rowsAffectedTotal += rowsAffected; } if (rowsAffectedTotal != 1) { throw new RuntimeException("Upsert must only affect 1 row. Affected: " + rowsAffectedTotal + ". Table: " + table + ". Condition: " + condition); } } catch (DataAccessException e) { if (e.getCause() instanceof BatchUpdateException) { BatchUpdateException cause = (BatchUpdateException)e.getCause(); logger.error("Batch update error in upsert.", cause.getNextException()); } throw e; } } }

Sin embargo, este código no se compila, ya que select () no admite un mapa de valores:

SelectConditionStep<Record> insertIntoSelect = jooqProvider.getDSLContext().select(recordValues).whereNotExists(notExistsSelect);

La pregunta

¿Cómo proporciono select () con un conjunto de valores predefinidos como este: SELECT 3, ''C'', ''Z'' ?

Actualización 1

Me las arreglé para hacer funcionar el código. Aquí está la clase completa:

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, List<FieldValue<Field<?>, ?>> recordValues) { /* * All of this is for trying to do an UPSERT on PostgreSQL. See: * https://stackoverflow.com/a/6527838 */ Map<Field<?>, Object> recordValuesMap = new HashMap<Field<?>, Object>(); for (FieldValue<Field<?>, ?> entry : recordValues) { recordValuesMap.put(entry.getFieldName(), entry.getFieldValue()); } List<Param<?>> params = new LinkedList<Param<?>>(); for (FieldValue<Field<?>, ?> entry : recordValues) { params.add(val(entry.getFieldValue())); } List<Field<?>> fields = new LinkedList<Field<?>>(); for (FieldValue<Field<?>, ?> entry : recordValues) { fields.add(entry.getFieldName()); } SelectConditionStep<Record1<Integer>> notExistsSelect = jooqProvider.getDSLContext().selectOne().from(table).where(condition); SelectConditionStep<Record> insertIntoSelect = jooqProvider.getDSLContext().select(params).whereNotExists(notExistsSelect); try { int[] result = jooqProvider.getDSLContext().batch( jooqProvider.getDSLContext().update(table).set(recordValuesMap).where(condition), jooqProvider.getDSLContext().insertInto(table, fields).select(insertIntoSelect) ).execute(); long rowsAffectedTotal = 0; for (int rowsAffected : result) { rowsAffectedTotal += rowsAffected; } if (rowsAffectedTotal != 1) { throw new RuntimeException("Upsert must only affect 1 row. Affected: " + rowsAffectedTotal + ". Table: " + table + ". Condition: " + condition); } } catch (DataAccessException e) { if (e.getCause() instanceof BatchUpdateException) { BatchUpdateException cause = (BatchUpdateException)e.getCause(); logger.error("Batch update error in upsert.", cause.getNextException()); } throw e; } } }

Sin embargo, no se siente muy limpio con el List<FieldValue<Field<?>, ?>> recordValues . ¿Alguna mejor idea sobre cómo hacer esto?


Aquí hay un método de utilidad upsert derivado de la solución de Lucas anterior para los objetos UpdatableRecord:

public static int upsert(final DSLContext dslContext, final UpdatableRecord record) { return dslContext.insertInto(record.getTable()) .set(record) .onDuplicateKeyUpdate() .set(record) .execute();

}


Parece una forma un tanto complicada de lograr el objetivo. ¿Por qué no usar una función almacenada simple? cómo crear una función upsert se describe en el manual de postgresql y luego solo llame desde su código java.


jOOQ 3.7+ admite la cláusula ON CONFLICT PostgreSQL 9.5:

La sintaxis completa específica del proveedor de PostgreSQL aún no es compatible, pero puede usar la sintaxis MySQL o H2, que se pueden emular utilizando el ON CONFLICT PostgreSQL:

MySQL INSERT .. ON DUPLICATE KEY UPDATE :

DSL.using(configuration) .insertInto(TABLE) .columns(ID, A, B) .values(1, "a", "b") .onDuplicateKeyUpdate() .set(A, "a") .set(B, "b") .execute();

H2 MERGE INTO ..

DSL.using(configuration) .mergeInto(TABLE, A, B, C) .values(1, "a", "b") .execute();