[#2265] Added Record.refresh(Field...) and more test cases

This commit is contained in:
Lukas Eder 2013-05-11 10:23:54 +02:00
parent 712a0f2f62
commit 2df2a478a6
5 changed files with 74 additions and 57 deletions

View File

@ -101,6 +101,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
private void testFailRefresh(Record record) {
try {
record.refresh();
fail();
}
catch (DataAccessException expected) {}
}
@Test
public void testKeepRSWithCloseAfterFetch() throws Exception {
Result<B> b1 = create().selectFrom(TBook()).fetch();
@ -143,19 +151,31 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
// Changing a TITLE has no effect
r.setValue(TBook_TITLE(), "XX");
r.setValue(TBook_AUTHOR_ID(), 15);
assertEquals("XX", r.getValue(TBook_TITLE()));
assertEquals(15, (int) r.getValue(TBook_AUTHOR_ID()));
assertTrue(r.changed());
assertFalse(r.original().equals(r));
assertEquals(BOOK_TITLES.get(0), getBook(1).getValue(TBook_TITLE()));
B dbBook = getBook(1);
assertEquals(BOOK_TITLES.get(0), dbBook.getValue(TBook_TITLE()));
assertEquals(BOOK_AUTHOR_IDS.get(0), dbBook.getValue(TBook_AUTHOR_ID()));
// Refresh the record
r.refresh(TBook_TITLE());
assertEquals(BOOK_TITLES.get(0), r.getValue(TBook_TITLE()));
assertEquals(15, (int) r.getValue(TBook_AUTHOR_ID()));
assertTrue(r.changed());
assertFalse(r.original().equals(r));
r.refresh();
assertEquals("1984", r.getValue(TBook_TITLE()));
assertEquals(BOOK_TITLES.get(0), r.getValue(TBook_TITLE()));
assertEquals(BOOK_AUTHOR_IDS.get(0), r.getValue(TBook_AUTHOR_ID()));
assertFalse(r.changed());
assertEquals(r.original(), r);
b2.close();
assertNull(b2.resultSet());
testFailRefresh(r);
Cursor<Record> c1 = create().select().from(TBook().getName()).keepResultSet(KEEP_AFTER_FETCH).fetchLazy();
assertFalse(c1.closesAfterFetch());
@ -298,7 +318,6 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
jOOQAbstractTest.reset = false;
}
/*
@ -308,12 +327,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
* [#2265] Pull up store(), delete(), refresh() from UpdatableRecord
* - store() will perform a scan and update if UPDATE_ON_STORE is set. Otherwise: no-op
* - delete() will remove the record
* - refresh() will pefrom a scan from the ResultSet
* - refresh() should not execute a new query if a ResultSet is available
*
* [#1846] Add ResultQuery.keepResultSet() with UPDATE_ON_CHANGE
* - Implement all data types from ResultSet.updateXXX() (e.g. updateInt(), etc)
* - Implement UPDATE_ON_STORE
* - refresh() should not execute a new query if a ResultSet is available
* - TYPE_SCROLL_SENSITIVE should be active for KEEP_AFTER_FETCH (for refresh())
*/
}

View File

@ -1077,21 +1077,40 @@ public interface Record extends Attachable, Comparable<Record> {
*/
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 [primary key fields = primary 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) throws DataAccessException;
/**
* Refresh parts of this record from the database.
* <p>
* A successful refresh results in the following:
* <ul>
* <li>{@link #valuesRow()} will have been restored to the respective values
* from the database</li>
* <li>{@link #original()} will match this record</li>
* <li>{@link #changed()} will be <code>false</code></li>
* </ul>
* <p>
* Refreshing can trigger any of the following actions:
* <ul>
* <li>Re-reading the underlying {@link #resultSet()}, if that
* <code>ResultSet</code> is available.</li>
* <li>Executing a new <code>SELECT</code> statement, if this is an
* {@link UpdatableRecord}.</li>
* <li>Failing, otherwise</li>
* </ul>
* <p>
* This is the same as calling <code>record.refresh(record.fields())</code>
*
* @throws DataAccessException This exception is thrown if
* <ul>
* <li>something went wrong executing the query</li> <li>the
* {@link #resultSet()} is not available, or is in
* {@link ResultSet#TYPE_FORWARD_ONLY} mode, such that
* refreshing is not possible.</li><li>the record does not exist
* anymore in the database</li>
* </ul>
* @see UpdatableRecord#refresh()
* @see ResultQuery#keepResultSet(KeepResultSetMode)
*/
void refresh(Field<?>... fields) throws DataAccessException;
/**
* Close the underlying JDBC {@link ResultSet}, if applicable.

View File

@ -301,39 +301,15 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
int delete() throws DataAccessException, DataChangedException;
/**
* Refresh this record from the database, based on the value of the primary
* key or main unique key.
* <p>
* This is in fact the same as calling
* <code>refresh(getFields().toArray(new Field[0]))</code>
* <p>
* The executed statement is <code><pre>
* SELECT * FROM [table]
* WHERE [primary key fields = primary 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>
* {@inheritDoc}
*/
@Override
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 [primary key fields = primary 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>
* {@inheritDoc}
*/
@Override
void refresh(Field<?>... fields) throws DataAccessException;
/**

View File

@ -694,17 +694,27 @@ abstract class AbstractRecord extends AbstractStore implements Record {
// -------------------------------------------------------------------------
@Override
public void refresh() {
public final void refresh() {
refresh(fields.fields.fields);
}
/**
* {@inheritDoc}
* <p>
* Subclasses may override this
*/
@Override
public void refresh(Field<?>... f) throws DataAccessException {
if (rs != null) {
try {
// [#2265] TODO: This code is prototypical. fetchLazy() is not
// the best way to fetch a record
rs.absolute(rsIndex - 1);
Record record = create().fetchLazy(rs).fetchOne();
AbstractRecord record = (AbstractRecord) create().fetchLazy(rs).fetchOne();
for (int i = 0; i < record.size(); i++) {
setValue(i, new Value<Object>(record.getValue(i)));
for (Field<?> field : f) {
setValue(field, record.getValue0(field));
}
}
catch (SQLException e) {

View File

@ -338,11 +338,6 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
}
}
@Override
public final void refresh() {
refresh(fields.fields.fields());
}
@Override
public final void refresh(Field<?>... f) {
SelectQuery<?> select = create().selectQuery();