[#2869] DefaultRecordMapper should attach resulting records according to Settings.attachRecords, if target type implements Attachable

This commit is contained in:
Lukas Eder 2013-12-06 11:29:23 +01:00
parent 5d93112b96
commit bcd073a402
4 changed files with 46 additions and 4 deletions

View File

@ -77,6 +77,7 @@ import java.util.Queue;
import junit.framework.Assert;
import org.jooq.AttachableInternal;
import org.jooq.Cursor;
import org.jooq.DSLContext;
import org.jooq.Field;
@ -95,6 +96,7 @@ import org.jooq.Select;
import org.jooq.SelectQuery;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.conf.Settings;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.InvalidResultException;
import org.jooq.exception.MappingException;
@ -1193,7 +1195,6 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
@Test
public void testFetchIntoTableRecords() throws Exception {
jOOQAbstractTest.reset = false;
// [#1819] Check if only applicable setters are used
// JOIN two tables into a generated UpdatableRecord
@ -1219,6 +1220,25 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
}
@Test
public void testFetchAttachables() throws Exception {
// [#2869] DefaultRecordMapper should recognise Attachable types and attach them
// according to the Settings.
B b1 = create().selectFrom(TBook()).where(TBook_ID().eq(1)).fetchOne();
B b2 = b1.into(TBook().getRecordType());
assertNotNull(((AttachableInternal) b1).configuration());
assertNotNull(((AttachableInternal) b2).configuration());
B b3 = create(new Settings().withAttachRecords(false))
.selectFrom(TBook()).where(TBook_ID().eq(1)).fetchOne();
B b4 = b3.into(TBook().getRecordType());
assertNull(((AttachableInternal) b3).configuration());
assertNull(((AttachableInternal) b4).configuration());
}
@Test
public void testFetchIntoTableRecordsWithUDTs() throws Exception {
if (cUAddressType() == null) {

View File

@ -1495,6 +1495,11 @@ public abstract class jOOQAbstractTest<
new FetchTests(this).testFetchIntoTableRecords();
}
@Test
public void testFetchAttachables() throws Exception {
new FetchTests(this).testFetchAttachables();
}
@Test
public void testFetchIntoTableRecordsWithUDTs() throws Exception {
new FetchTests(this).testFetchIntoTableRecordsWithUDTs();

View File

@ -62,6 +62,8 @@ import java.util.List;
import javax.persistence.Column;
import org.jooq.Attachable;
import org.jooq.AttachableInternal;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.Record;
@ -278,7 +280,7 @@ public class DefaultRecordMapper<R extends Record, E> implements RecordMapper<R,
}
try {
return delegate.map(record);
return attach(delegate.map(record), record);
}
// Pass MappingExceptions on to client code
@ -543,4 +545,19 @@ public class DefaultRecordMapper<R extends Record, E> implements RecordMapper<R,
}
}
}
private static <E> E attach(E attachable, Record record) {
// [#2869] Attach the mapped outcome if it is Attachable and if the context's
// Settings.attachRecords flag is set
if (attachable instanceof Attachable && record instanceof AttachableInternal) {
Attachable a = (Attachable) attachable;
AttachableInternal r = (AttachableInternal) record;
if (Utils.attachRecords(r.configuration())) {
a.attach(r.configuration());
}
}
return attachable;
}
}

View File

@ -405,7 +405,7 @@ final class Utils {
return configuration(configuration).settings();
}
private static final boolean attachRecords(Configuration configuration) {
static final boolean attachRecords(Configuration configuration) {
if (configuration != null) {
Settings settings = configuration.settings();
@ -1293,7 +1293,7 @@ final class Utils {
*/
static final Field<String> escapeForLike(Object value, Configuration configuration) {
if (value != null && value.getClass() == String.class) {
/* [pro] xx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxx x
xxxxxx xxxxxxx x xxxxx x xxxxx