diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/RecordListenerTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/RecordListenerTests.java index 9298c16ecc..4cd8cae8e0 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/RecordListenerTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/RecordListenerTests.java @@ -108,6 +108,25 @@ extends BaseTest events = new ArrayList(); @@ -120,5 +139,55 @@ extends BaseTest) getClass(), fields.fields.fields, configuration()) - .initialise(new RecordInitialiser() { + .operate(new RecordOperation() { @Override - public AbstractRecord initialise(AbstractRecord record) throws RuntimeException { + public AbstractRecord operate(AbstractRecord record) throws RuntimeException { Value[] v = getValues(); for (int i = 0; i < v.length; i++) { @@ -498,10 +498,10 @@ abstract class AbstractRecord extends AbstractStore implements Record { @Override public final R into(final Table table) { return Utils.newRecord(table, configuration()) - .initialise(new RecordInitialiser() { + .operate(new RecordOperation() { @Override - public R initialise(R record) throws MappingException { + public R operate(R record) throws MappingException { try { for (Field targetField : table.fields()) { Field sourceField = field(targetField); diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractRoutine.java b/jOOQ/src/main/java/org/jooq/impl/AbstractRoutine.java index b0752fdd08..1a136a2d1d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractRoutine.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractRoutine.java @@ -514,7 +514,7 @@ public abstract class AbstractRoutine extends AbstractQueryPart implements Ro if (sqlType == Types.STRUCT) { UDTRecord record = Utils .newRecord((Class>) parameter.getType()) - .initialise(null); + .operate(null); statement.registerOutParameter(index, Types.STRUCT, record.getSQLTypeName()); } diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractStoreQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractStoreQuery.java index 731fc4a652..1d14d0d913 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractStoreQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractStoreQuery.java @@ -378,10 +378,10 @@ abstract class AbstractStoreQuery extends AbstractQuery implem for (final Number id : ids) { getReturnedRecords().add( Utils.newRecord(into, configuration) - .initialise(new RecordInitialiser() { + .operate(new RecordOperation() { @Override - public R initialise(R record) throws RuntimeException { + public R operate(R record) throws RuntimeException { ((AbstractRecord) record).setValue(field, new Value(id)); return record; } diff --git a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java index 20c57ffbde..9b7fff7dab 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java @@ -1403,7 +1403,7 @@ class CursorImpl implements Cursor { } record = Utils.newRecord((Class) type, fields, ctx.configuration()) - .initialise(initialiser); + .operate(initialiser); } } catch (SQLException e) { @@ -1427,10 +1427,10 @@ class CursorImpl implements Cursor { throw new UnsupportedOperationException(); } - private class CursorRecordInitialiser implements RecordInitialiser { + private class CursorRecordInitialiser implements RecordOperation { @Override - public AbstractRecord initialise(AbstractRecord record) throws SQLException { + public AbstractRecord operate(AbstractRecord record) throws SQLException { ctx.record(record); listener.recordStart(ctx); diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java index 2d09c1a970..27843f0e14 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java @@ -131,7 +131,7 @@ class DefaultBindContext extends AbstractBindContext { // [#1126] Oracle's UDTs need to be bound with their type name else if (UDTRecord.class.isAssignableFrom(type)) { String typeName = Utils.newRecord((Class>) type) - .initialise(null) + .operate(null) .getUDT() .getName(); stmt.setNull(nextIndex(), sqlType, typeName); diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 3de16c7009..8791de51fa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -1480,21 +1480,21 @@ public class DefaultDSLContext implements DSLContext, Serializable { @Override public > R newRecord(UDT type) { - return Utils.newRecord(type, configuration).initialise(null); + return Utils.newRecord(type, configuration).operate(null); } @Override public R newRecord(Table table) { - return Utils.newRecord(table, configuration).initialise(null); + return Utils.newRecord(table, configuration).operate(null); } @Override public R newRecord(Table table, final Object source) { return Utils.newRecord(table, configuration) - .initialise(new RecordInitialiser() { + .operate(new RecordOperation() { @Override - public R initialise(R record) { + public R operate(R record) { record.from(source); return record; } diff --git a/jOOQ/src/main/java/org/jooq/impl/RecordStub.java b/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java similarity index 51% rename from jOOQ/src/main/java/org/jooq/impl/RecordStub.java rename to jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java index dd8825f5c9..c2b8582b19 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RecordStub.java +++ b/jOOQ/src/main/java/org/jooq/impl/RecordDelegate.java @@ -36,8 +36,12 @@ package org.jooq.impl; import static org.jooq.ExecuteType.READ; +import static org.jooq.ExecuteType.WRITE; +import static org.jooq.impl.RecordDelegate.RecordLifecycleType.LOAD; +import static org.jooq.impl.RecordDelegate.RecordLifecycleType.REFRESH; import org.jooq.Configuration; +import org.jooq.ExecuteType; import org.jooq.Record; import org.jooq.RecordContext; import org.jooq.RecordListener; @@ -49,17 +53,31 @@ import org.jooq.RecordListenerProvider; * * @author Lukas Eder */ -class RecordStub { +class RecordDelegate { - private final Configuration configuration; - private final R record; + private final Configuration configuration; + private final R record; + private final RecordLifecycleType type; - RecordStub(Configuration configuration, R record) { - this.configuration = configuration; - this.record = record; + RecordDelegate(Configuration configuration, R record) { + this(configuration, record, LOAD); } - final R initialise(RecordInitialiser initialiser) throws E { + RecordDelegate(Configuration configuration, R record, RecordLifecycleType type) { + this.configuration = configuration; + this.record = record; + this.type = type; + } + + static final RecordDelegate delegate(Configuration configuration, R record) { + return new RecordDelegate(configuration, record); + } + + static final RecordDelegate delegate(Configuration configuration, R record, RecordLifecycleType type) { + return new RecordDelegate(configuration, record, type); + } + + final R operate(RecordOperation operation) throws E { RecordListenerProvider[] providers = null; RecordListener[] listeners = null; RecordContext ctx = null; @@ -69,7 +87,7 @@ class RecordStub { if (providers != null) { listeners = new RecordListener[providers.length]; - ctx = new DefaultRecordContext(configuration, READ, record); + ctx = new DefaultRecordContext(configuration, executeType(), record); for (int i = 0; i < providers.length; i++) { listeners[i] = providers[i].provide(); @@ -79,20 +97,51 @@ class RecordStub { if (listeners != null) { for (RecordListener listener : listeners) { - listener.loadStart(ctx); + switch (type) { + case LOAD: listener.loadStart(ctx); break; + case REFRESH: listener.refreshStart(ctx); break; + case STORE: listener.storeStart(ctx); break; + case INSERT: listener.insertStart(ctx); break; + case UPDATE: listener.updateStart(ctx); break; + case DELETE: listener.deleteStart(ctx); break; + default: + throw new IllegalStateException("Type not supported: " + type); + } } } - if (initialiser != null) { - initialiser.initialise(record); + if (operation != null) { + operation.operate(record); } if (listeners != null) { for (RecordListener listener : listeners) { - listener.loadEnd(ctx); + switch (type) { + case LOAD: listener.loadEnd(ctx); break; + case REFRESH: listener.refreshEnd(ctx); break; + case STORE: listener.storeEnd(ctx); break; + case INSERT: listener.insertEnd(ctx); break; + case UPDATE: listener.updateEnd(ctx); break; + case DELETE: listener.deleteEnd(ctx); break; + default: + throw new IllegalStateException("Type not supported: " + type); + } } } return record; } + + private final ExecuteType executeType() { + return type == LOAD || type == REFRESH ? READ : WRITE; + } + + enum RecordLifecycleType { + LOAD, + REFRESH, + STORE, + INSERT, + UPDATE, + DELETE + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/RecordInitialiser.java b/jOOQ/src/main/java/org/jooq/impl/RecordOperation.java similarity index 88% rename from jOOQ/src/main/java/org/jooq/impl/RecordInitialiser.java rename to jOOQ/src/main/java/org/jooq/impl/RecordOperation.java index 89e6150810..804836a1d1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RecordInitialiser.java +++ b/jOOQ/src/main/java/org/jooq/impl/RecordOperation.java @@ -39,15 +39,15 @@ import org.jooq.Record; import org.jooq.RecordListener; /** - * An stub for {@link Record} objects, abstracting {@link RecordListener} - * lifecycle handling. + * An operation callback for {@link Record} objects, abstracting + * {@link RecordListener} lifecycle handling. * * @author Lukas Eder */ -interface RecordInitialiser { +interface RecordOperation { /** * Callback method to initialise a record. */ - R initialise(R record) throws E; + R operate(R record) throws E; } diff --git a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java index d810ebe555..2fbcf2fd13 100644 --- a/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java @@ -36,6 +36,8 @@ package org.jooq.impl; import static java.lang.Boolean.TRUE; +import static org.jooq.impl.RecordDelegate.delegate; +import static org.jooq.impl.RecordDelegate.RecordLifecycleType.REFRESH; import java.math.BigInteger; import java.sql.Timestamp; @@ -340,15 +342,21 @@ public class UpdatableRecordImpl> extends TableReco } @Override - public final void refresh(Field... f) { - SelectQuery select = create().selectQuery(); + public final void refresh(final Field... f) { + SelectQuery select = create().selectQuery(); select.addSelect(f); select.addFrom(getTable()); Utils.addConditions(select, this, getPrimaryKey().getFieldsArray()); if (select.execute() == 1) { - AbstractRecord record = (AbstractRecord) select.getResult().get(0); - setValues(f, record); + delegate(configuration(), select.getResult().get(0), REFRESH) + .operate(new RecordOperation() { + @Override + public Record operate(Record record) throws RuntimeException { + setValues(f, (AbstractRecord) record); + return record; + } + }); } else { throw new InvalidResultException("Exactly one row expected for refresh. Record does not exist in database."); @@ -370,10 +378,10 @@ public class UpdatableRecordImpl> extends TableReco @Override public final R copy() { return Utils.newRecord(getTable(), configuration()) - .initialise(new RecordInitialiser() { - + .operate(new RecordOperation() { + @Override - public R initialise(R copy) throws RuntimeException { + public R operate(R copy) throws RuntimeException { // Copy all fields. This marks them all as isChanged, which is important List> key = getPrimaryKey().getFields(); for (Field field : fields.fields.fields) { diff --git a/jOOQ/src/main/java/org/jooq/impl/Utils.java b/jOOQ/src/main/java/org/jooq/impl/Utils.java index 95105fdb8d..6a6ed1d5fa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Utils.java +++ b/jOOQ/src/main/java/org/jooq/impl/Utils.java @@ -236,21 +236,21 @@ final class Utils { /** * Create a new record */ - static final RecordStub newRecord(Class type) { + static final RecordDelegate newRecord(Class type) { return newRecord(type, null); } /** * Create a new record */ - static final RecordStub newRecord(Class type, Field[] fields) { + static final RecordDelegate newRecord(Class type, Field[] fields) { return newRecord(type, fields, null); } /** * Create a new record */ - static final RecordStub newRecord(Table type) { + static final RecordDelegate newRecord(Table type) { return newRecord(type, null); } @@ -258,21 +258,21 @@ final class Utils { * Create a new record */ @SuppressWarnings("unchecked") - static final RecordStub newRecord(Table type, Configuration configuration) { - return (RecordStub) newRecord(type.getRecordType(), type.fields(), configuration); + static final RecordDelegate newRecord(Table type, Configuration configuration) { + return (RecordDelegate) newRecord(type.getRecordType(), type.fields(), configuration); } /** * Create a new UDT record */ - static final > RecordStub newRecord(UDT type) { + static final > RecordDelegate newRecord(UDT type) { return newRecord(type, null); } /** * Create a new UDT record */ - static final > RecordStub newRecord(UDT type, Configuration configuration) { + static final > RecordDelegate newRecord(UDT type, Configuration configuration) { return newRecord(type.getRecordType(), type.fields(), configuration); } @@ -280,7 +280,7 @@ final class Utils { * Create a new record */ @SuppressWarnings({ "unchecked", "rawtypes" }) - static final RecordStub newRecord(Class type, Field[] fields, Configuration configuration) { + static final RecordDelegate newRecord(Class type, Field[] fields, Configuration configuration) { try { R record; @@ -301,7 +301,7 @@ final class Utils { record.attach(configuration); } - return new RecordStub(configuration, record); + return new RecordDelegate(configuration, record); } catch (Exception e) { throw new IllegalStateException("Could not construct new record", e); @@ -2398,10 +2398,10 @@ final class Utils { } return Utils.newRecord((Class>) type) - .initialise(new RecordInitialiser, SQLException>() { + .operate(new RecordOperation, SQLException>() { @Override - public UDTRecord initialise(UDTRecord record) throws SQLException { + public UDTRecord operate(UDTRecord record) throws SQLException { List values = PostgresUtils.toPGObject(object.toString()); Row row = record.fieldsRow();