[#1721] Add <K, V> Map<K, List<V>> ResultQuery.fetchGroups(Field<K>,

Field<V>) and Result.intoGroups(Field<K>, Field<V>)
This commit is contained in:
Lukas Eder 2012-08-17 12:04:38 +02:00
parent 8c07e10014
commit 511757685e
6 changed files with 95 additions and 5 deletions

View File

@ -213,6 +213,32 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(4, (int) entry22.getValue().get(1).getValue(TBook_ID()));
assertFalse(it.hasNext());
// Key -> Value Map
// ----------------
// Grouping by BOOK.ID
Map<Integer, List<String>> map3 = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchGroups(TBook_ID(), TBook_TITLE());
ArrayList<List<String>> map3Values = new ArrayList<List<String>>(map3.values());
assertEquals(4, map3.size());
assertEquals(BOOK_IDS, new ArrayList<Integer>(map3.keySet()));
for (int i = 0; i < 4; i++) {
assertEquals(1, map3Values.get(i).size());
assertEquals(BOOK_TITLES.get(i), map3Values.get(i).get(0));
}
// Grouping by BOOK.AUTHOR_ID
Map<Integer, List<String>> map4 = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchGroups(TBook_AUTHOR_ID(), TBook_TITLE());
ArrayList<List<String>> map4Values = new ArrayList<List<String>>(map4.values());
assertEquals(2, map4.size());
assertEquals(AUTHOR_IDS, new ArrayList<Integer>(map4.keySet()));
for (int i = 0; i < 2; i++) {
assertEquals(2, map4Values.get(i).size());
assertEquals(BOOK_TITLES.get(i * 2 + 0), map4Values.get(i).get(0));
assertEquals(BOOK_TITLES.get(i * 2 + 1), map4Values.get(i).get(1));
}
}
@Test

View File

@ -1819,7 +1819,8 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
* one of the result's columns as value
* <p>
* An {@link InvalidResultException} is thrown, if the key turns out to be
* non-unique in the result set.
* non-unique in the result set. Use {@link #intoGroups(Field, Field)}
* instead, if your keys are non-unique
*
* @param <K> The key's generic field type
* @param <V> The value's generic field type
@ -1845,6 +1846,21 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
<K> Map<K, Result<R>> intoGroups(Field<K> key);
/**
* Return a {@link Map} with one of the result's columns as key and another
* one of the result's columns as value
* <p>
* Unlike {@link #intoMap(Field, Field)}, this method allows for non-unique
* keys in the result set.
*
* @param <K> The key's generic field type
* @param <V> The value's generic field type
* @param key The key field.
* @param value The value field
* @return A Map containing the results
*/
<K, V> Map<K, List<V>> intoGroups(Field<K> key, Field<V> value);
/**
* Convert this result into an array of arrays
* <p>

View File

@ -485,7 +485,8 @@ public interface ResultQuery<R extends Record> extends Query {
* columns as key and another one of the result's columns as value
* <p>
* An exception is thrown, if the key turns out to be non-unique in the
* result set.
* result set. Use {@link #fetchGroups(Field, Field)} instead, if your keys
* are non-unique
*
* @param <K> The key's generic field type
* @param <V> The value's generic field type
@ -519,6 +520,23 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<K> Map<K, Result<R>> fetchGroups(Field<K> key) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with one of the result's
* columns as key and another one of the result's columns as value
* <p>
* Unlike {@link #fetchMap(Field, Field)}, this method allows for non-unique
* keys in the result set.
*
* @param <K> The key's generic field type
* @param <V> The value's generic field type
* @param key The key field.
* @param value The value field
* @return A Map containing the results
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoGroups(Field, Field)
*/
<K, V> Map<K, List<V>> fetchGroups(Field<K> key, Field<V> value) throws DataAccessException;
/**
* Execute the query and return the generated result as an Object matrix
* <p>

View File

@ -242,6 +242,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetchGroups(key);
}
@Override
public final <K, V> Map<K, List<V>> fetchGroups(Field<K> key, Field<V> value) {
return getDelegate().fetchGroups(key, value);
}
@Override
public final Object[][] fetchArrays() {
return getDelegate().fetchArrays();

View File

@ -432,6 +432,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoGroups(key);
}
@Override
public final <K, V> Map<K, List<V>> fetchGroups(Field<K> key, Field<V> value) {
return fetch().intoGroups(key, value);
}
@Override
public final Object[][] fetchArrays() {
return fetch().intoArray();

View File

@ -1319,12 +1319,12 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
Map<K, Result<R>> map = new LinkedHashMap<K, Result<R>>();
for (R record : this) {
K value = record.getValue(key);
Result<R> result = map.get(value);
K val = record.getValue(key);
Result<R> result = map.get(val);
if (result == null) {
result = new ResultImpl<R>(configuration, fields);
map.put(value, result);
map.put(val, result);
}
result.add(record);
@ -1333,6 +1333,26 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return map;
}
@Override
public final <K, V> Map<K, List<V>> intoGroups(Field<K> key, Field<V> value) {
Map<K, List<V>> map = new LinkedHashMap<K, List<V>>();
for (R record : this) {
K k = record.getValue(key);
V v = record.getValue(value);
List<V> result = map.get(k);
if (result == null) {
result = new ArrayList<V>();
map.put(k, result);
}
result.add(v);
}
return map;
}
@Override
public final Object[][] intoArray() throws MappingException {
int size = size();