[#2010] Added support for DELETE event. Slightly changed STORE event
semantics
This commit is contained in:
parent
1595f09690
commit
5b4cf0924c
@ -116,7 +116,12 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
B book1 = newBook(5);
|
||||
book1.attach(create(listener1).configuration());
|
||||
assertEquals(1, book1.store());
|
||||
assertEquals(asList("storeStart", "storeEnd"), listener1.events);
|
||||
assertEquals(asList("storeStart", "insertStart", "insertEnd", "storeEnd"), listener1.events);
|
||||
|
||||
listener1.events.clear();
|
||||
book1.setValue(TBook_TITLE(), "1234");
|
||||
assertEquals(1, book1.store());
|
||||
assertEquals(asList("storeStart", "updateStart", "updateEnd", "storeEnd"), listener1.events);
|
||||
|
||||
SimpleRecordListener listener2 = new SimpleRecordListener();
|
||||
B book2 = newBook(6);
|
||||
@ -128,6 +133,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
book2.setValue(TBook_TITLE(), "1234");
|
||||
assertEquals(1, book2.update());
|
||||
assertEquals(asList("updateStart", "updateEnd"), listener2.events);
|
||||
|
||||
listener2.events.clear();
|
||||
assertEquals(1, book2.delete());
|
||||
assertEquals(asList("deleteStart", "deleteEnd"), listener2.events);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -63,6 +63,9 @@ public interface RecordListener extends EventListener {
|
||||
* prior to storing. Note that modifying the record's primary key value may
|
||||
* influence whether storing results in an <code>INSERT</code> or
|
||||
* <code>UPDATE</code> statement.
|
||||
* <p>
|
||||
* A store event will generate a nested {@link #insertStart(RecordContext)}
|
||||
* or {@link #updateStart(RecordContext)} event.
|
||||
*
|
||||
* @see UpdatableRecord#store()
|
||||
*/
|
||||
@ -75,6 +78,9 @@ public interface RecordListener extends EventListener {
|
||||
* after storing. Note that modifying the record's primary key value may
|
||||
* influence whether storing results in an <code>INSERT</code> or
|
||||
* <code>UPDATE</code> statement.
|
||||
* <p>
|
||||
* A store event will generate a nested {@link #insertEnd(RecordContext)}
|
||||
* or {@link #updateEnd(RecordContext)} event.
|
||||
*
|
||||
* @see UpdatableRecord#store()
|
||||
*/
|
||||
|
||||
@ -37,6 +37,7 @@ package org.jooq.impl;
|
||||
|
||||
import static java.lang.Boolean.TRUE;
|
||||
import static org.jooq.impl.RecordDelegate.delegate;
|
||||
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.DELETE;
|
||||
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.INSERT;
|
||||
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.REFRESH;
|
||||
import static org.jooq.impl.RecordDelegate.RecordLifecycleType.STORE;
|
||||
@ -131,36 +132,12 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
|
||||
@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];
|
||||
return storeInsert();
|
||||
}
|
||||
|
||||
@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];
|
||||
return storeUpdate(getPrimaryKey().getFieldsArray());
|
||||
}
|
||||
|
||||
private final int store0() {
|
||||
@ -194,6 +171,22 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
}
|
||||
|
||||
private final int storeInsert() {
|
||||
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] = storeInsert0();
|
||||
return record;
|
||||
}
|
||||
});
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
private final int storeInsert0() {
|
||||
DSLContext create = create();
|
||||
InsertQuery<R> insert = create.insertQuery(getTable());
|
||||
addChangedValues(insert);
|
||||
@ -236,7 +229,24 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
return result;
|
||||
}
|
||||
|
||||
private final int storeUpdate(TableField<R, ?>[] keys) {
|
||||
private final int storeUpdate(final TableField<R, ?>[] keys) {
|
||||
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] = storeUpdate0(keys);
|
||||
return record;
|
||||
}
|
||||
});
|
||||
|
||||
return result[0];
|
||||
|
||||
}
|
||||
|
||||
private final int storeUpdate0(TableField<R, ?>[] keys) {
|
||||
UpdateQuery<R> update = create().updateQuery(getTable());
|
||||
addChangedValues(update);
|
||||
Utils.addConditions(update, this, keys);
|
||||
@ -347,6 +357,22 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
|
||||
@Override
|
||||
public final int delete() {
|
||||
final int[] result = new int[1];
|
||||
|
||||
delegate(configuration(), (Record) this, DELETE)
|
||||
.operate(new RecordOperation<Record, RuntimeException>() {
|
||||
|
||||
@Override
|
||||
public Record operate(Record record) throws RuntimeException {
|
||||
result[0] = delete0();
|
||||
return record;
|
||||
}
|
||||
});
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
private final int delete0() {
|
||||
TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray();
|
||||
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user