[#1990] Add <T> T Record.original(Field<T>), original(int),

original(String) to get a field's original value
This commit is contained in:
Lukas Eder 2012-12-02 12:28:01 +01:00
parent c6e3b61aca
commit 8f2633f5d9
3 changed files with 79 additions and 5 deletions

View File

@ -41,6 +41,7 @@ import static junit.framework.Assert.assertTrue;
import java.sql.Date;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Record3;
@ -85,12 +86,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
assertEquals(book, orig);
assertEquals(book.getValue(TBook_ID()), orig.getValue(TBook_ID()));
assertEquals(book.getValue(TBook_TITLE()), orig.getValue(TBook_TITLE()));
testOriginalMethods(book, orig);
book.setValue(TBook_TITLE(), "abc");
assertFalse(book.equals(orig));
assertFalse(book.equals(book.original()));
assertEquals("abc", book.getValue(TBook_TITLE()));
assertEquals(BOOK_TITLES.get(0), orig.getValue(TBook_TITLE()));
testOriginalMethods(book, orig);
book = orig;
orig = orig.original();
@ -99,6 +102,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
assertFalse(book.equals(book.original()));
assertEquals("abc", book.getValue(TBook_TITLE()));
assertEquals(BOOK_TITLES.get(0), orig.getValue(TBook_TITLE()));
testOriginalMethods(book, orig);
}
private void testOriginalMethods(B changed, B original) {
for (Field<?> field : changed.getFields()) {
assertEquals(changed.original(field), original.getValue(field));
}
}
@Test

View File

@ -365,9 +365,46 @@ public interface Record extends FieldProvider, Attachable {
* Record values can be freely modified after having fetched a record from
* the database. Every record also references the originally fetched values.
* This method returns a new record containing those original values.
*
* @see #original(Field)
* @see #original(int)
* @see #original(String)
*/
Record original();
/**
* Get an original value from this record as fetched from the database.
* <p>
* Record values can be freely modified after having fetched a record from
* the database. Every record also references the originally fetched values.
* This method returns such an original value for a field.
*
* @see #original()
*/
<T> T original(Field<T> field);
/**
* Get an original value from this record as fetched from the database.
* <p>
* Record values can be freely modified after having fetched a record from
* the database. Every record also references the originally fetched values.
* This method returns such an original value for a field.
*
* @see #original()
*/
Object original(int fieldIndex);
/**
* Get an original value from this record as fetched from the database.
* <p>
* Record values can be freely modified after having fetched a record from
* the database. Every record also references the originally fetched values.
* This method returns such an original value for a field.
*
* @see #original()
*/
Object original(String fieldName);
/**
* Check if this record has been changed from its original as fetched from
* the database.
@ -376,24 +413,36 @@ public interface Record extends FieldProvider, Attachable {
* <code>record.equals(record.original())</code> is true.
*
* @see #original()
* @see #changed(Field)
* @see #changed(int)
* @see #changed(String)
*/
boolean changed();
/**
* Check if a field's value has been changed from its original as fetched
* from the database.
*
* @see #changed()
* @see #original(Field)
*/
boolean changed(Field<?> field);
/**
* Check if a field's value has been changed from its original as fetched
* from the database.
*
* @see #changed()
* @see #original(int)
*/
boolean changed(int fieldIndex);
/**
* Check if a field's value has been changed from its original as fetched
* from the database.
*
* @see #changed()
* @see #original(String)
*/
boolean changed(String fieldName);
@ -606,11 +655,10 @@ public interface Record extends FieldProvider, Attachable {
/**
* Load data into this record from a source. The mapping algorithm is this:
* <h3>If <code>source</code> is a {@link Map}</h3>
* Loading of data is delegated to {@link #fromMap(Map)}
* <h3>If any JPA {@link Column} annotations are found on the {@link Class}
* of the provided <code>source</code>, only those are used. Matching
* candidates are:</h3>
* <h3>If <code>source</code> is a {@link Map}</h3> Loading of data is
* delegated to {@link #fromMap(Map)} <h3>If any JPA {@link Column}
* annotations are found on the {@link Class} of the provided
* <code>source</code>, only those are used. Matching candidates are:</h3>
* <ul>
* <li>Public no-argument instance methods annotated with
* <code>Column</code></li>

View File

@ -358,6 +358,22 @@ abstract class AbstractRecord extends AbstractStore implements Record {
return result;
}
@SuppressWarnings("unchecked")
@Override
public final <T> T original(Field<T> field) {
return (T) original(getIndex(field));
}
@Override
public final Object original(int fieldIndex) {
return getValues()[fieldIndex].getOriginal();
}
@Override
public final Object original(String fieldName) {
return original(getIndex(fieldName));
}
@Override
public final boolean changed() {
for (Value<?> value : getValues()) {