Merge pull request #1814 from idugic/master

#1709 change-set
This commit is contained in:
Lukas Eder 2012-09-11 00:20:01 -07:00
commit 817f0e1e2b
6 changed files with 149 additions and 2 deletions

View File

@ -143,6 +143,19 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(BOOK_IDS, new ArrayList<Integer>(map2.keySet()));
assertEquals(BOOK_TITLES, new ArrayList<String>(map2.values()));
// Key list -> Record
// ------------------
Map<List<?>, B> map3 = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchMap(TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE());
assertEquals(4, map3.keySet().size());
for (List<?> keyList : map3.keySet()) {
B record = map3.get(keyList);
assertEquals(keyList.get(0), record.getValue(TBook_ID()));
assertEquals(keyList.get(1), record.getValue(TBook_LANGUAGE_ID()));
assertEquals(keyList.get(2), record.getValue(TBook_TITLE()));
}
// List of Map
// -----------
Result<B> books = create().selectFrom(TBook()).orderBy(TBook_ID()).fetch();
@ -159,10 +172,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
// Single Map
// ----------
B book = create().selectFrom(TBook()).where(TBook_ID().equal(1)).fetchOne();
Map<String, Object> map3 = create().selectFrom(TBook()).where(TBook_ID().equal(1)).fetchOneMap();
Map<String, Object> map4 = create().selectFrom(TBook()).where(TBook_ID().equal(1)).fetchOneMap();
for (Field<?> field : books.getFields()) {
assertEquals(book.getValue(field), map3.get(field.getName()));
assertEquals(book.getValue(field), map4.get(field.getName()));
}
// Maps with two times the same field
@ -237,6 +250,20 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(BOOK_TITLES.get(i * 2 + 0), map4Values.get(i).get(0));
assertEquals(BOOK_TITLES.get(i * 2 + 1), map4Values.get(i).get(1));
}
// Key list -> Record
// ----------------------
Map<List<?>, Result<B>> map5 = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchGroups(TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE());
assertEquals(4, map5.size());
for (List<?> keyList : map5.keySet()) {
Result<B> result = map5.get(keyList);
assertEquals(1, result.size());
assertEquals(keyList.get(0), result.get(0).getValue(TBook_ID()));
assertEquals(keyList.get(1), result.get(0).getValue(TBook_LANGUAGE_ID()));
assertEquals(keyList.get(2), result.get(0).getValue(TBook_TITLE()));
}
}
@Test

View File

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

View File

@ -501,6 +501,24 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<K, V> Map<K, V> fetchMap(Field<K> key, Field<V> value) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with keys list as a map key
* and the corresponding record as value.
* <p>
* An exception is thrown, if the key list turns out to be non-unique in the
* result set. Use {@link #fetchGroups(Field...)} instead, if your key list
* is non-unique.
*
* @param keys The key list. Client code must assure that this key list is
* unique in the result set.
* @return A Map containing the results.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the key list is non-unique in the
* result set.
* @see Result#intoMap(Field...)
*/
Map<List<?>, R> fetchMap(Field<?>... keys) 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.
@ -537,6 +555,20 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<K, V> Map<K, List<V>> fetchGroups(Field<K> key, Field<V> value) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with the result grouped by the
* given key list.
* <p>
* Unlike {@link #fetchMap(Field...)}, this method allows for non-unique key
* list in the result set.
*
* @param keys The key list used for result grouping.
* @return A Map containing grouped results
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoGroups(Field...)
*/
Map<List<?>, Result<R>> fetchGroups(Field<?>... keys) throws DataAccessException;
/**
* Return a {@link Map} with results grouped by the given key and mapped
* into the given entity type.

View File

@ -233,6 +233,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetchMap(key, value);
}
@Override
public final Map<List<?>, R> fetchMap(Field<?>... keys) {
return getDelegate().fetchMap(keys);
}
@Override
public final List<Map<String, Object>> fetchMaps() {
return getDelegate().fetchMaps();
@ -253,6 +258,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetchGroups(key, value);
}
@Override
public final Map<List<?>, Result<R>> fetchGroups(Field<?>... keys) {
return getDelegate().fetchGroups(keys);
}
@Override
public final Object[][] fetchArrays() {
return getDelegate().fetchArrays();

View File

@ -416,6 +416,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoMap(key, value);
}
@Override
public final Map<List<?>, R> fetchMap(Field<?>... keys) {
return fetch().intoMap(keys);
}
@Override
public final List<Map<String, Object>> fetchMaps() {
return fetch().intoMaps();
@ -436,6 +441,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoGroups(key, value);
}
@Override
public final Map<List<?>, Result<R>> fetchGroups(Field<?>... keys) {
return fetch().intoGroups(keys);
}
@Override
public final Object[][] fetchArrays() {
return fetch().intoArray();

View File

@ -1400,6 +1400,24 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return map;
}
@Override
public final Map<List<?>, R> intoMap(Field<?>... keys) {
Map<List<?>, R> map = new LinkedHashMap<List<?>, R>();
for (R record : this) {
List<Object> keyList = new ArrayList<Object>();
for (Field<?> key : keys) {
keyList.add(record.getValue(key));
}
if (map.put(keyList, record) != null) {
throw new InvalidResultException("Key list " + keys + " 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>>();
@ -1439,6 +1457,28 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return map;
}
@Override
public final Map<List<?>, Result<R>> intoGroups(Field<?>... keys) {
Map<List<?>, Result<R>> map = new LinkedHashMap<List<?>, Result<R>>();
for (R record : this) {
List<Object> keyList = new ArrayList<Object>();
for (Field<?> key : keys) {
keyList.add(record.getValue(key));
}
Result<R> result = map.get(keyList);
if (result == null) {
result = new ResultImpl<R>(getConfiguration(), this.fields);
map.put(keyList, result);
}
result.add(record);
}
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>>();