[#2010] Implemented INSERT, UPDATE, STORE lifecycle events

This commit is contained in:
Lukas Eder 2013-08-05 15:53:37 +02:00
parent 6f4c232de9
commit 1595f09690
3 changed files with 78 additions and 10 deletions

View File

@ -112,6 +112,26 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
public void testRecordListenerStore() throws Exception {
jOOQAbstractTest.reset = false;
SimpleRecordListener listener1 = new SimpleRecordListener();
B book1 = newBook(5);
book1.attach(create(listener1).configuration());
assertEquals(1, book1.store());
assertEquals(asList("storeStart", "storeEnd"), listener1.events);
SimpleRecordListener listener2 = new SimpleRecordListener();
B book2 = newBook(6);
book2.attach(create(listener2).configuration());
assertEquals(1, book2.insert());
assertEquals(asList("insertStart", "insertEnd"), listener2.events);
listener2.events.clear();
book2.setValue(TBook_TITLE(), "1234");
assertEquals(1, book2.update());
assertEquals(asList("updateStart", "updateEnd"), listener2.events);
}
@Test
public void testRecordListenerRefresh() throws Exception {
B book =
create()
.selectFrom(TBook())

View File

@ -1508,6 +1508,11 @@ public abstract class jOOQAbstractTest<
new RecordListenerTests(this).testRecordListenerLoad();
}
@Test
public void testRecordListenerRefresh() throws Exception {
new RecordListenerTests(this).testRecordListenerRefresh();
}
@Test
public void testRecordListenerStore() throws Exception {
new RecordListenerTests(this).testRecordListenerStore();

View File

@ -37,7 +37,10 @@ package org.jooq.impl;
import static java.lang.Boolean.TRUE;
import static org.jooq.impl.RecordDelegate.delegate;
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.INSERT;
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.REFRESH;
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.STORE;
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.UPDATE;
import java.math.BigInteger;
import java.sql.Timestamp;
@ -111,6 +114,56 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
@Override
public final int store() {
final int[] result = new int[1];
delegate(configuration(), (Record) this, STORE)
.operate(new RecordOperation<Record, RuntimeException>() {
@Override
public Record operate(Record record) throws RuntimeException {
result[0] = store0();
return record;
}
});
return result[0];
}
@Override
public final int insert() {
final int[] result = new int[1];
delegate(configuration(), (Record) this, INSERT)
.operate(new RecordOperation<Record, RuntimeException>() {
@Override
public Record operate(Record record) throws RuntimeException {
result[0] = storeInsert();
return record;
}
});
return result[0];
}
@Override
public final int update() {
final int[] result = new int[1];
delegate(configuration(), (Record) this, UPDATE)
.operate(new RecordOperation<Record, RuntimeException>() {
@Override
public Record operate(Record record) throws RuntimeException {
result[0] = storeUpdate(getPrimaryKey().getFieldsArray());
return record;
}
});
return result[0];
}
private final int store0() {
TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray();
boolean executeUpdate = false;
@ -140,16 +193,6 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
return result;
}
@Override
public final int insert() {
return storeInsert();
}
@Override
public final int update() {
return storeUpdate(getPrimaryKey().getFieldsArray());
}
private final int storeInsert() {
DSLContext create = create();
InsertQuery<R> insert = create.insertQuery(getTable());