[#1810 Add <T, E> Map<T, E> ResultQuery.fetchMap(Field<T>, Class<E>) and

Result.intoMap(Field<T>, Class<E>)]
This commit is contained in:
Ivan Dugic 2012-09-18 22:28:03 +02:00
parent 6cd084e77f
commit 45e6356e4e
7 changed files with 103 additions and 4 deletions

View File

@ -1599,7 +1599,45 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
}
@Test
public void testFetchIntoGroups() throws Exception {
public void testFetchMapPOJO() throws Exception {
if (TBookPojo() == null) {
log.info("SKIPPING", "Generated POJO tests");
return;
}
// Group by BOOK.ID
Map<Integer, Object> map1 =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchMap(TBook_ID(), TBookPojo());
assertEquals(4, map1.size());
assertEquals(BOOK_IDS, new ArrayList<Integer>(map1.keySet()));
List<Entry<Integer, Object>> entries =
new ArrayList<Map.Entry<Integer,Object>>(map1.entrySet());
for (int i = 0; i < map1.size(); i++) {
Entry<Integer, Object> entry = entries.get(i);
assertEquals(BOOK_IDS.get(i), on(entry.getValue()).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(i), on(entry.getValue()).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(i), on(entry.getValue()).call("getTitle").get());
}
try {
// Group by BOOK.AUTHOR_ID
create().selectFrom(TBook()).orderBy(TBook_ID()).fetchMap(TBook_AUTHOR_ID(), TBookPojo());
fail("Fetching map with the non-unique key - InvalidResultException not thrown.");
}
catch (Throwable t) {
assertEquals(InvalidResultException.class, t.getClass());
}
}
@Test
public void testFetchGroupsPOJO() throws Exception {
if (TBookPojo() == null) {
log.info("SKIPPING", "Generated POJO tests");
return;
@ -1656,5 +1694,4 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertFalse(it.hasNext());
}
}

View File

@ -819,6 +819,11 @@ public abstract class jOOQAbstractTest<
new FetchTests(this).testFetchMap();
}
@Test
public void testFetchMapPOJO() throws Exception {
new FetchTests(this).testFetchMapPOJO();
}
@Test
public void testFetchGroups() throws Exception {
new FetchTests(this).testFetchGroups();
@ -830,8 +835,8 @@ public abstract class jOOQAbstractTest<
}
@Test
public void testFetchIntoGroups() throws Exception {
new FetchTests(this).testFetchIntoGroups();
public void testFetchGroupsPOJO() throws Exception {
new FetchTests(this).testFetchGroupsPOJO();
}
@Test

View File

@ -1852,6 +1852,22 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
Map<List<?>, R> intoMap(Field<?>[] keys);
/**
* Return a {@link Map} with results grouped by the given key and mapped
* into the given entity type.
* <p>
* An {@link InvalidResultException} is thrown, if the key is non-unique in
* the result set. Use {@link #intoGroups(Field, Class)} instead, if your
* key is non-unique.
*
* @param key The key. Client code must assure that key is unique in the
* result set.
* @return A Map containing the result.
* @throws InvalidResultException if the key is non-unique in the result
* set.
*/
<K, E> Map<K, E> intoMap(Field<K> key, Class<? extends E> type) throws MappingException;
/**
* Return a {@link Map} with one of the result's columns as key and a list
* of corresponding records as value.

View File

@ -519,6 +519,24 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Map<List<?>, R> fetchMap(Field<?>[] keys) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with results grouped by the
* given key and mapped into the given entity type.
* <p>
* An exception is thrown, if the key turn out to be non-unique in the
* result set. Use {@link #fetchGroups(Field, Class)} instead, if your key
* is non-unique.
*
* @param key The key. Client code must assure that key is unique in the
* result set.
* @return A Map containing the result.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the key is non-unique in the result
* set.
* @see Result#intoMap(Field, Class)
*/
<K, E> Map<K, E> fetchMap(Field<K> key, Class<? extends E> type) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with one of the result's
* columns as key and a list of corresponding records as value.

View File

@ -238,6 +238,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetchMap(keys);
}
@Override
public final <K, E> Map<K, E> fetchMap(Field<K> key, Class<? extends E> type) {
return getDelegate().fetchMap(key, type);
}
@Override
public final List<Map<String, Object>> fetchMaps() {
return getDelegate().fetchMaps();

View File

@ -421,6 +421,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoMap(keys);
}
@Override
public final <K, E> Map<K, E> fetchMap(Field<K> key, Class<? extends E> type) {
return fetch().intoMap(key, type);
}
@Override
public final List<Map<String, Object>> fetchMaps() {
return fetch().intoMaps();

View File

@ -1413,6 +1413,19 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return map;
}
@Override
public final <K, E> Map<K, E> intoMap(Field<K> key, Class<? extends E> type) {
Map<K, E> map = new LinkedHashMap<K, E>();
for (R record : this) {
if (map.put(record.getValue(key), record.into(type)) != null) {
throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
}
}
return map;
}
@Override
public final <K> Map<K, Result<R>> intoGroups(Field<K> key) {
Map<K, Result<R>> map = new LinkedHashMap<K, Result<R>>();