[#1849] Add Record.original() to obtain the originally fetched values
from a Record
This commit is contained in:
parent
e976d79243
commit
9f2a848b07
84
jOOQ-test/src/org/jooq/test/_/testcases/RecordTests.java
Normal file
84
jOOQ-test/src/org/jooq/test/_/testcases/RecordTests.java
Normal file
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._.testcases;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.test.BaseTest;
|
||||
import org.jooq.test.jOOQAbstractTest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RecordTests<
|
||||
A extends UpdatableRecord<A>,
|
||||
AP,
|
||||
B extends UpdatableRecord<B>,
|
||||
S extends UpdatableRecord<S>,
|
||||
B2S extends UpdatableRecord<B2S>,
|
||||
BS extends UpdatableRecord<BS>,
|
||||
L extends TableRecord<L>,
|
||||
X extends TableRecord<X>,
|
||||
DATE extends UpdatableRecord<DATE>,
|
||||
BOOL extends UpdatableRecord<BOOL>,
|
||||
D extends UpdatableRecord<D>,
|
||||
T extends UpdatableRecord<T>,
|
||||
U extends TableRecord<U>,
|
||||
I extends TableRecord<I>,
|
||||
IPK extends UpdatableRecord<IPK>,
|
||||
T658 extends TableRecord<T658>,
|
||||
T725 extends UpdatableRecord<T725>,
|
||||
T639 extends UpdatableRecord<T639>,
|
||||
T785 extends TableRecord<T785>>
|
||||
extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725, T639, T785> {
|
||||
|
||||
public RecordTests(jOOQAbstractTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725, T639, T785> delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecordOriginals() throws Exception {
|
||||
B book = create().selectFrom(TBook()).where(TBook_ID().eq(1)).fetchOne();
|
||||
assertEquals(book, book.original());
|
||||
assertEquals(book.getValue(TBook_ID()), book.original().getValue(TBook_ID()));
|
||||
assertEquals(book.getValue(TBook_TITLE()), book.original().getValue(TBook_TITLE()));
|
||||
|
||||
book.setValue(TBook_TITLE(), "abc");
|
||||
assertEquals("abc", book.getValue(TBook_TITLE()));
|
||||
assertEquals(BOOK_TITLES.get(0), book.original().getValue(TBook_TITLE()));
|
||||
}
|
||||
}
|
||||
@ -107,6 +107,7 @@ import org.jooq.test._.testcases.LoaderTests;
|
||||
import org.jooq.test._.testcases.OrderByTests;
|
||||
import org.jooq.test._.testcases.PlainSQLTests;
|
||||
import org.jooq.test._.testcases.PredicateTests;
|
||||
import org.jooq.test._.testcases.RecordTests;
|
||||
import org.jooq.test._.testcases.RenderAndBindTests;
|
||||
import org.jooq.test._.testcases.ResultTests;
|
||||
import org.jooq.test._.testcases.RoutineAndUDTTests;
|
||||
@ -1156,6 +1157,11 @@ public abstract class jOOQAbstractTest<
|
||||
new FetchTests(this).testFetchLater();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecordOriginals() throws Exception {
|
||||
new RecordTests(this).testRecordOriginals();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentExecution() throws Exception {
|
||||
new ThreadSafetyTests(this).testConcurrentExecution();
|
||||
|
||||
@ -1108,6 +1108,16 @@ public interface Record extends FieldProvider, Store<Object> {
|
||||
*/
|
||||
<T, U> void setValue(Field<T> field, U value, Converter<T, ? super U> converter);
|
||||
|
||||
/**
|
||||
* Get this record containing the original values 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 a new record containing those original values.
|
||||
*/
|
||||
Record original();
|
||||
|
||||
/**
|
||||
* Convert this record into an array.
|
||||
* <p>
|
||||
|
||||
@ -256,4 +256,10 @@ public interface TableRecord<R extends TableRecord<R>> extends Record {
|
||||
*/
|
||||
@Deprecated
|
||||
void refreshUsing(TableField<R, ?>... keys) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
R original();
|
||||
}
|
||||
|
||||
@ -594,6 +594,21 @@ abstract class AbstractRecord extends AbstractStore<Object> implements Record {
|
||||
return result == null ? defaultValue : result;
|
||||
}
|
||||
|
||||
/*
|
||||
* This method is overridden covariantly by TableRecordImpl
|
||||
*/
|
||||
@Override
|
||||
public Record original() {
|
||||
AbstractRecord result = Util.newRecord(getClass(), getFieldProvider(), getConfiguration());
|
||||
Value<?>[] v = getValues();
|
||||
|
||||
for (int i = 0; i < v.length; i++) {
|
||||
result.setValue(i, new Value<Object>(v[i].getOriginal()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object[] intoArray() {
|
||||
return into(Object[].class);
|
||||
|
||||
@ -97,6 +97,12 @@ public class TableRecordImpl<R extends TableRecord<R>> extends AbstractRecord im
|
||||
return (Table<R>) getFieldProvider();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final R original() {
|
||||
return (R) super.original();
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may override this method to provide an identity
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user