[#1708] Add <T, E> Map<T, List<E>> ResultQuery.fetchGroupsInto(Field<T>,

Class<E>)
This commit is contained in:
Ivan Dugic 2012-09-07 21:54:01 +02:00
parent eeaa60bee8
commit d8aac367f5
7 changed files with 105 additions and 0 deletions

View File

@ -1500,4 +1500,44 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
}
catch (SQLException expected) {}
}
@Test
public void testFetchIntoGroups() throws Exception {
// Use generated POJOs as entities
Reflect book = on(TBook().getClass().getPackage().getName() + ".pojos." + TBook().getClass().getSimpleName());
// Group by BOOK.ID
Map<Integer, List<Object>> map = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchIntoGroups(TBook_ID(), (Class<?>) book.get());
assertEquals(4, map.size());
assertEquals(BOOK_IDS, new ArrayList<Integer>(map.keySet()));
for (Entry<Integer, List<Object>> entry : map.entrySet()) {
assertEquals(1, entry.getValue().size());
assertEquals(entry.getKey(), on(entry.getValue().get(0)).call("getId").get());
}
// Group by BOOK.AUTHOR_ID
map = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchIntoGroups(TBook_AUTHOR_ID(), (Class<?>) book.get());
assertEquals(2, map.size());
assertEquals(AUTHOR_IDS, new ArrayList<Integer>(map.keySet()));
Iterator<Entry<Integer, List<Object>>> it = map.entrySet().iterator();
Entry<Integer, List<Object>> entry21 = it.next();
assertEquals(2, entry21.getValue().size());
assertEquals(1, ((Integer) on(entry21.getValue().get(0)).call("getId").get()).intValue());
assertEquals(2, ((Integer) on(entry21.getValue().get(1)).call("getId").get()).intValue());
Entry<Integer, List<Object>> entry22 = it.next();
assertEquals(2, entry22.getValue().size());
assertEquals(3, ((Integer) on(entry22.getValue().get(0)).call("getId").get()).intValue());
assertEquals(4, ((Integer) on(entry22.getValue().get(1)).call("getId").get()).intValue());
assertFalse(it.hasNext());
}
}

View File

@ -792,6 +792,11 @@ public abstract class jOOQAbstractTest<
new FetchTests(this).testFetchArray();
}
@Test
public void testFetchIntoGroups() throws Exception {
new FetchTests(this).testFetchIntoGroups();
}
@Test
public void testDAOMethods() throws Exception {
new DaoTests(this).testDAOMethods();

View File

@ -1863,6 +1863,20 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
<K, V> Map<K, List<V>> intoGroups(Field<K> key, Field<V> value);
/**
* Return a {@link Map} with results grouped by the given key and mapped
* into the given entity type.
* <p>
*
* @param <K> The key's generic field type
* @param <E> The generic entity type.
* @param key The key field.
* @param type The entity type.
* @throws MappingException wrapping any reflection or data type conversion
* exception that might have occurred while mapping records
*/
<K, E> Map<K, List<E>> intoGroups(Field<K> key, Class<? extends E> type) throws MappingException;
/**
* Convert this result into an array of arrays
* <p>

View File

@ -730,6 +730,22 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<H extends RecordHandler<R>> H fetchInto(H handler) throws DataAccessException;
/**
* Return a {@link Map} with results grouped by the given key and mapped
* into the given entity type.
*
* @param <K> The key's generic field type
* @param <E> The generic entity type.
* @param key The key field.
* @param type The entity type.
* @throws DataAccessException if something went wrong executing the query
* @throws MappingException wrapping any reflection or data type conversion
* exception that might have occurred while mapping records
* @see Result#intoGroups(Field, Class)
*/
<K, E> Map<K, List<E>> fetchIntoGroups(Field<K> key, Class<? extends E> type) throws DataAccessException,
MappingException;
/**
* Fetch results asynchronously.
* <p>

View File

@ -323,6 +323,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetchInto(handler);
}
@Override
public final <K, E> Map<K, List<E>> fetchIntoGroups(Field<K> key, Class<? extends E> type) {
return getDelegate().fetchIntoGroups(key, type);
}
@Override
public final FutureResult<R> fetchLater() {
return getDelegate().fetchLater();

View File

@ -516,6 +516,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().into(handler);
}
@Override
public final <K, E> Map<K, List<E>> fetchIntoGroups(Field<K> key, Class<? extends E> type) {
return fetch().intoGroups(key, type);
}
@Override
public final FutureResult<R> fetchLater() {
ExecutorService executor = newSingleThreadExecutor();

View File

@ -57,6 +57,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
@ -1439,6 +1440,25 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return map;
}
@Override
public final <K, E> Map<K, List<E>> intoGroups(Field<K> key, Class<? extends E> type) {
Map<K, List<E>> map = new LinkedHashMap<K, List<E>>();
for (R record : this) {
K keyVal = record.getValue(key);
List<E> list = map.get(keyVal);
if (list == null) {
list = new LinkedList<E>();
map.put(keyVal, list);
}
list.add(record.into(type));
}
return map;
}
@Override
public final Object[][] intoArray() {
int size = size();