From cd05526a97fc4ae610c6e091c51165251424522c Mon Sep 17 00:00:00 2001 From: Ivan Dugic Date: Tue, 11 Sep 2012 21:49:28 +0200 Subject: [PATCH] [#1709] Add Map, Result> ResultQuery.fetchGroups(Field...) Update --- .../org/jooq/test/_/testcases/FetchTests.java | 43 +++++++++++++++---- jOOQ/src/main/java/org/jooq/Result.java | 30 ++++++------- jOOQ/src/main/java/org/jooq/ResultQuery.java | 30 ++++++------- .../jooq/impl/AbstractDelegatingSelect.java | 4 +- .../org/jooq/impl/AbstractResultQuery.java | 4 +- .../main/java/org/jooq/impl/ResultImpl.java | 12 +++++- 6 files changed, 78 insertions(+), 45 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/FetchTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/FetchTests.java index 1130a11d82..b149312ead 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/FetchTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/FetchTests.java @@ -143,10 +143,10 @@ extends BaseTest(map2.keySet())); assertEquals(BOOK_TITLES, new ArrayList(map2.values())); - // Key list -> Record - // ------------------ + // Keys -> Record + // -------------- Map, B> map3 = create().selectFrom(TBook()).orderBy(TBook_ID()) - .fetchMap(TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE()); + .fetchMap(new Field[] { TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE() }); assertEquals(4, map3.keySet().size()); for (List keyList : map3.keySet()) { @@ -251,14 +251,39 @@ extends BaseTest Record - // ---------------------- + // Keys -> Record + // -------------- + // Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID Map, Result> map5 = create().selectFrom(TBook()).orderBy(TBook_ID()) - .fetchGroups(TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE()); - assertEquals(4, map5.size()); + .fetchGroups(new Field[] { TBook_AUTHOR_ID(), TBook_LANGUAGE_ID() }); - for (List keyList : map5.keySet()) { - Result result = map5.get(keyList); + Iterator, Result>> iterator = map5.entrySet().iterator(); + Entry, Result> entry1_en = iterator.next(); + assertEquals(2, entry1_en.getValue().size()); + assertEquals(entry1_en.getKey().get(0), entry1_en.getValue().get(0).getValue(TBook_AUTHOR_ID())); + assertEquals(entry1_en.getKey().get(0), entry1_en.getValue().get(1).getValue(TBook_AUTHOR_ID())); + assertEquals(entry1_en.getKey().get(1), entry1_en.getValue().get(0).getValue(TBook_LANGUAGE_ID())); + assertEquals(entry1_en.getKey().get(1), entry1_en.getValue().get(1).getValue(TBook_LANGUAGE_ID())); + + Entry, Result> entry2_pt = iterator.next(); + assertEquals(1, entry2_pt.getValue().size()); + assertEquals(entry2_pt.getKey().get(0), entry2_pt.getValue().get(0).getValue(TBook_AUTHOR_ID())); + assertEquals(entry2_pt.getKey().get(1), entry2_pt.getValue().get(0).getValue(TBook_LANGUAGE_ID())); + + Entry, Result> entry2_de = iterator.next(); + assertEquals(1, entry2_de.getValue().size()); + assertEquals(entry2_de.getKey().get(0), entry2_de.getValue().get(0).getValue(TBook_AUTHOR_ID())); + assertEquals(entry2_de.getKey().get(1), entry2_de.getValue().get(0).getValue(TBook_LANGUAGE_ID())); + + assertFalse(iterator.hasNext()); + + // Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID, BOOK.TITLE + Map, Result> map6 = create().selectFrom(TBook()).orderBy(TBook_ID()) + .fetchGroups(new Field[] { TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE() }); + assertEquals(4, map6.size()); + + for (List keyList : map6.keySet()) { + Result result = map6.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())); diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index db12fc2c82..ea8e4c2fa8 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -1836,20 +1836,20 @@ public interface Result extends FieldProvider, List, Attach Map intoMap(Field key, Field value); /** - * Return a {@link Map} with keys list as a map key and the corresponding - * record as value. + * Return a {@link Map} with the given keys as a map key and the + * corresponding record as value. *

- * 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. + * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #intoGroups(Field[])} instead, if your keys + * are non-unique. * - * @param keys The key list. Client code must assure that this key list is - * unique in the result set. + * @param keys The keys. Client code must assure that keys are unique in the + * result set. * @return A Map containing the results. - * @throws InvalidResultException if the key list is non-unique in the - * result set. + * @throws InvalidResultException if the keys are non-unique in the result + * set. */ - Map, R> intoMap(Field... keys); + Map, R> intoMap(Field[] keys); /** * Return a {@link Map} with one of the result's columns as key and a list @@ -1881,15 +1881,15 @@ public interface Result extends FieldProvider, List, Attach /** * Execute the query and return a {@link Map} with the result grouped by the - * given key list. + * given keys. *

- * Unlike {@link #intoMap(Field...)}, this method allows for non-unique key - * list in the result set. + * Unlike {@link #intoMap(Field[])}, this method allows for non-unique keys + * in the result set. * - * @param keys The key list. + * @param keys The keys. * @return A Map containing grouped results */ - Map, Result> intoGroups(Field... keys); + Map, Result> intoGroups(Field[] keys); /** * Return a {@link Map} with results grouped by the given key and mapped diff --git a/jOOQ/src/main/java/org/jooq/ResultQuery.java b/jOOQ/src/main/java/org/jooq/ResultQuery.java index 60bde2f664..6f9ebcb7bb 100644 --- a/jOOQ/src/main/java/org/jooq/ResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/ResultQuery.java @@ -502,22 +502,22 @@ public interface ResultQuery extends Query { Map fetchMap(Field key, Field value) throws DataAccessException; /** - * Execute the query and return a {@link Map} with keys list as a map key - * and the corresponding record as value. + * Execute the query and return a {@link Map} with keys as a map key and the + * corresponding record as value. *

- * 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. + * An exception is thrown, if the keys turn out to be non-unique in the + * result set. Use {@link #fetchGroups(Field[])} instead, if your keys are + * non-unique. * - * @param keys The key list. Client code must assure that this key list is - * unique in the result set. + * @param keys The keys. Client code must assure that keys are 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...) + * @see Result#intoMap(Field[]) */ - Map, R> fetchMap(Field... keys) throws DataAccessException; + Map, R> fetchMap(Field[] keys) throws DataAccessException; /** * Execute the query and return a {@link Map} with one of the result's @@ -557,17 +557,17 @@ public interface ResultQuery extends Query { /** * Execute the query and return a {@link Map} with the result grouped by the - * given key list. + * given keys. *

- * Unlike {@link #fetchMap(Field...)}, this method allows for non-unique key - * list in the result set. + * Unlike {@link #fetchMap(Field[])}, this method allows for non-unique keys + * in the result set. * - * @param keys The key list used for result grouping. + * @param keys The keys used for result grouping. * @return A Map containing grouped results * @throws DataAccessException if something went wrong executing the query - * @see Result#intoGroups(Field...) + * @see Result#intoGroups(Field[]) */ - Map, Result> fetchGroups(Field... keys) throws DataAccessException; + Map, Result> fetchGroups(Field[] keys) throws DataAccessException; /** * Return a {@link Map} with results grouped by the given key and mapped diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java index f53afa443d..1875f1d907 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java @@ -234,7 +234,7 @@ abstract class AbstractDelegatingSelect } @Override - public final Map, R> fetchMap(Field... keys) { + public final Map, R> fetchMap(Field[] keys) { return getDelegate().fetchMap(keys); } @@ -259,7 +259,7 @@ abstract class AbstractDelegatingSelect } @Override - public final Map, Result> fetchGroups(Field... keys) { + public final Map, Result> fetchGroups(Field[] keys) { return getDelegate().fetchGroups(keys); } diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index 64525343c9..d698c241b3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -417,7 +417,7 @@ abstract class AbstractResultQuery extends AbstractQuery imple } @Override - public final Map, R> fetchMap(Field... keys) { + public final Map, R> fetchMap(Field[] keys) { return fetch().intoMap(keys); } @@ -442,7 +442,7 @@ abstract class AbstractResultQuery extends AbstractQuery imple } @Override - public final Map, Result> fetchGroups(Field... keys) { + public final Map, Result> fetchGroups(Field[] keys) { return fetch().intoGroups(keys); } diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index 14b99b5371..28e5ffb586 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -1401,7 +1401,11 @@ class ResultImpl implements Result, AttachableInternal { } @Override - public final Map, R> intoMap(Field... keys) { + public final Map, R> intoMap(Field[] keys) { + if (keys == null || keys.length == 0) { + throw new IllegalArgumentException("Keys must not be null or empty."); + } + Map, R> map = new LinkedHashMap, R>(); for (R record : this) { @@ -1458,7 +1462,11 @@ class ResultImpl implements Result, AttachableInternal { } @Override - public final Map, Result> intoGroups(Field... keys) { + public final Map, Result> intoGroups(Field[] keys) { + if (keys == null || keys.length == 0) { + throw new IllegalArgumentException("Keys must not be null or empty."); + } + Map, Result> map = new LinkedHashMap, Result>(); for (R record : this) {