[#2021] Add UpdatableRecord.refresh(Field<?>...) to allow for refreshing
a subset of the Record's values
This commit is contained in:
parent
88bdc09a8c
commit
56d1dd1250
@ -394,11 +394,16 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
|
||||
|
||||
// Modify the record
|
||||
book1.setValue(TBook_TITLE(), "1999");
|
||||
book1.setValue(TBook_AUTHOR_ID(), 3);
|
||||
assertEquals("1999", book1.getValue(TBook_TITLE()));
|
||||
|
||||
// And refresh it again
|
||||
book1.refresh();
|
||||
book1.refresh(TBook_TITLE());
|
||||
assertEquals("1985", book1.getValue(TBook_TITLE()));
|
||||
assertEquals(3, (int) book1.getValue(TBook_AUTHOR_ID()));
|
||||
|
||||
book1.refresh();
|
||||
assertEquals(1, (int) book1.getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(0, book1.store());
|
||||
|
||||
// Refresh the other copy of the original record
|
||||
|
||||
@ -276,7 +276,7 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends Updatable
|
||||
* key or main unique key.
|
||||
* <p>
|
||||
* This is in fact the same as calling
|
||||
* <code>refresh(getTable().getMainKey().getFieldsArray())</code>
|
||||
* <code>refresh(getFields().toArray(new Field[0])</code>
|
||||
* <p>
|
||||
* The executed statement is <code><pre>
|
||||
* SELECT * FROM [table]
|
||||
@ -290,6 +290,22 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends Updatable
|
||||
*/
|
||||
void refresh() throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Refresh this record from the database, based on the value of the primary
|
||||
* key or main unique key.
|
||||
* <p>
|
||||
* The executed statement is <code><pre>
|
||||
* SELECT [fields] FROM [table]
|
||||
* WHERE [main key fields = main key values]</pre></code>
|
||||
*
|
||||
* @throws DataAccessException This exception is thrown if
|
||||
* <ul>
|
||||
* <li>something went wrong executing the query</li> <li>the
|
||||
* record does not exist anymore in the database</li>
|
||||
* </ul>
|
||||
*/
|
||||
void refresh(Field<?>... fields);
|
||||
|
||||
/**
|
||||
* Duplicate this record (in memory) and reset all fields from the primary
|
||||
* key or main unique key, such that a subsequent call to {@link #store()}
|
||||
|
||||
@ -51,6 +51,7 @@ import org.jooq.Identity;
|
||||
import org.jooq.InsertQuery;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.SelectQuery;
|
||||
import org.jooq.SimpleSelectQuery;
|
||||
import org.jooq.StoreQuery;
|
||||
import org.jooq.TableField;
|
||||
@ -317,13 +318,20 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
|
||||
@Override
|
||||
public final void refresh() {
|
||||
SimpleSelectQuery<R> select = create().selectQuery(getTable());
|
||||
refresh(getFields().toArray(new Field[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void refresh(Field<?>... fields) {
|
||||
SelectQuery select = create().selectQuery();
|
||||
select.addSelect(fields);
|
||||
select.addFrom(getTable());
|
||||
Utils.addConditions(select, this, getMainKey().getFieldsArray());
|
||||
|
||||
if (select.execute() == 1) {
|
||||
AbstractRecord record = (AbstractRecord) select.getResult().get(0);
|
||||
|
||||
for (Field<?> field : getFields()) {
|
||||
for (Field<?> field : fields) {
|
||||
setValue(field, record.getValue0(field));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user