diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index 581d81076d..98cc9897f0 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -673,25 +673,6 @@ public interface Result extends List, Attachable { */ Map intoMap(Field key, Field value) throws IllegalArgumentException, InvalidResultException; - /** - * 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 keys are non-unique - * in the result set. Use {@link #intoGroups(Field[])} instead, if your keys - * are non-unique. - * - * @param keys The keys. Client code must assure that keys are unique in the - * result set. If this is null or an empty array, - * the resulting map will contain at most one entry. - * @return A Map containing the results. - * @throws IllegalArgumentException If any of the argument fields is not - * contained in {@link #fieldsRow()} - * @throws InvalidResultException if the keys are non-unique in the result - * set. - */ - Map intoMap(Field[] keys) throws IllegalArgumentException, InvalidResultException; - /** * Return a {@link Map} with results grouped by the given key and mapped * into the given entity type. @@ -738,6 +719,25 @@ public interface Result extends List, Attachable { Map intoMap(Field key, RecordMapper mapper) throws IllegalArgumentException, InvalidResultException, MappingException; + /** + * 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 keys are non-unique + * in the result set. Use {@link #intoGroups(Field[])} instead, if your keys + * are non-unique. + * + * @param keys The keys. Client code must assure that keys are unique in the + * result set. If this is null or an empty array, + * the resulting map will contain at most one entry. + * @return A Map containing the results. + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws InvalidResultException if the keys are non-unique in the result + * set. + */ + Map intoMap(Field[] keys) throws IllegalArgumentException, InvalidResultException; + /** * Return a {@link Map} with results grouped by the given keys and mapped * into the given entity type. @@ -785,6 +785,70 @@ public interface Result extends List, Attachable { Map, E> intoMap(Field[] keys, RecordMapper mapper) throws IllegalArgumentException, InvalidResultException, MappingException; + /** + * Return a {@link Map} with the given key table as a map key and the + * corresponding record as value. + *

+ * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #intoGroups(Table)} instead, if your keys + * are non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @return A Map containing the results. + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws InvalidResultException if the keys are non-unique in the result + * set. + */ + Map intoMap(Table table) throws IllegalArgumentException, InvalidResultException; + + /** + * Return a {@link Map} with results grouped by the given key table and + * mapped into the given entity type. + *

+ * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #intoGroups(Table, Class)} instead, if your + * keys are non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @param type The entity type. + * @return A Map containing the results. + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws InvalidResultException if the keys are non-unique in the result + * set. + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + Map intoMap(Table table, Class type) throws IllegalArgumentException, + InvalidResultException, MappingException; + + /** + * Return a {@link Map} with results grouped by the given key table and + * mapped by the given mapper. + *

+ * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #intoGroups(Table, Class)} instead, if your + * keys are non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @param mapper The mapper callback. + * @return A Map containing the results. + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws InvalidResultException if the keys are non-unique in the result + * set. + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + Map intoMap(Table table, RecordMapper mapper) throws IllegalArgumentException, + InvalidResultException, MappingException; + /** * Return a {@link Map} with one of the result's columns as key and a list * of corresponding records as value. @@ -817,20 +881,6 @@ public interface Result extends List, Attachable { */ Map> intoGroups(Field key, Field value) throws IllegalArgumentException; - /** - * Return a {@link Map} with the result grouped by the given keys. - *

- * Unlike {@link #intoMap(Field[])}, this method allows for non-unique keys - * in the result set. - * - * @param keys The keys. If this is null or an empty array, the - * resulting map will contain at most one entry. - * @return A Map containing grouped results - * @throws IllegalArgumentException If any of the argument fields is not - * contained in {@link #fieldsRow()} - */ - Map> intoGroups(Field[] keys) throws IllegalArgumentException; - /** * Return a {@link Map} with results grouped by the given key and mapped * into the given entity type. @@ -865,6 +915,20 @@ public interface Result extends List, Attachable { Map> intoGroups(Field key, RecordMapper mapper) throws IllegalArgumentException, MappingException; + /** + * Return a {@link Map} with the result grouped by the given keys. + *

+ * Unlike {@link #intoMap(Field[])}, this method allows for non-unique keys + * in the result set. + * + * @param keys The keys. If this is null or an empty array, the + * resulting map will contain at most one entry. + * @return A Map containing grouped results + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + */ + Map> intoGroups(Field[] keys) throws IllegalArgumentException; + /** * Return a {@link Map} with results grouped by the given keys and mapped * into the given entity type. @@ -889,7 +953,7 @@ public interface Result extends List, Attachable { * Return a {@link Map} with results grouped by the given keys and mapped * into the given entity type. *

- * Unlike {@link #intoMap(Field[], Class)}, this method allows for + * Unlike {@link #intoMap(Field[], RecordMapper)}, this method allows for * non-unique keys in the result set. * * @param keys The keys. If this is null or an empty array, the @@ -905,6 +969,57 @@ public interface Result extends List, Attachable { Map> intoGroups(Field[] keys, RecordMapper mapper) throws IllegalArgumentException, MappingException; + /** + * Return a {@link Map} with the result grouped by the given key table. + *

+ * Unlike {@link #intoMap(Table)}, this method allows for non-unique keys in + * the result set. + * + * @param table The key table. May not be null. + * @return A Map containing grouped results + * @throws IllegalArgumentException If any of the argument fields is not + * contained in {@link #fieldsRow()} + */ + Map> intoGroups(Table table) throws IllegalArgumentException; + + /** + * Return a {@link Map} with results grouped by the given key table and + * mapped into the given entity type. + *

+ * Unlike {@link #intoMap(Table, Class)}, this method allows for non-unique + * keys in the result set. + * + * @param table The key table. May not be null. + * @param type The entity type. + * @return A Map containing grouped results + * @throws IllegalArgumentException If the any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + Map> intoGroups(Table table, Class type) + throws IllegalArgumentException, MappingException; + + /** + * Return a {@link Map} with results grouped by the given key table and + * mapped into the given entity type. + *

+ * Unlike {@link #intoMap(Table, RecordMapper)}, this method allows for + * non-unique keys in the result set. + * + * @param table The key table. May not be null. + * @param mapper The mapper callback. + * @return A Map containing grouped results + * @throws IllegalArgumentException If the any of the argument fields is not + * contained in {@link #fieldsRow()} + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + Map> intoGroups(Table table, RecordMapper mapper) + throws IllegalArgumentException, MappingException; + /** * Convert this result into an array of arrays. *

diff --git a/jOOQ/src/main/java/org/jooq/ResultQuery.java b/jOOQ/src/main/java/org/jooq/ResultQuery.java index c0a23319c7..4f4f071ac4 100644 --- a/jOOQ/src/main/java/org/jooq/ResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/ResultQuery.java @@ -825,15 +825,15 @@ public interface ResultQuery extends Query { * @see Result#intoMap(Field[], Class) * @see DefaultRecordMapper */ - Map, E> fetchMap(Field[] keys, Class type) throws MappingException; + Map, E> fetchMap(Field[] keys, Class type) throws DataAccessException, MappingException; /** * Execute the query and return a {@link Map} with results grouped by the * given keys and mapped by the given mapper. *

* An {@link InvalidResultException} is thrown, if the keys are non-unique - * in the result set. Use {@link #fetchGroups(Field[], Class)} instead, if - * your keys are non-unique. + * in the result set. Use {@link #fetchGroups(Field[], RecordMapper)} + * instead, if your keys are non-unique. * * @param keys The keys. Client code must assure that keys are unique in the * result set. If this is null or an empty array, @@ -848,7 +848,72 @@ public interface ResultQuery extends Query { * @see Result#intoMap(Field[], Class) * @see DefaultRecordMapper */ - Map, E> fetchMap(Field[] keys, RecordMapper mapper) throws MappingException; + Map, E> fetchMap(Field[] keys, RecordMapper mapper) throws DataAccessException, + MappingException; + + /** + * Execute the query and return a {@link Map} with table as a map key and the + * corresponding record as value. + *

+ * An exception is thrown, if the keys turn out to be non-unique in the + * result set. Use {@link #fetchGroups(Table)} instead, if your keys are + * non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @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(Table) + */ + Map fetchMap(Table table) throws DataAccessException; + + /** + * Execute the query and return a {@link Map} with results grouped by the + * given table and mapped into the given entity type. + *

+ * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #fetchGroups(Table, Class)} instead, if + * your keys are non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @param type The entity type. + * @return A Map containing the results. + * @throws DataAccessException if something went wrong executing the query + * @throws InvalidResultException if the keys are non-unique in the result + * set. + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see Result#intoMap(Table, Class) + * @see DefaultRecordMapper + */ + Map fetchMap(Table table, Class type) throws DataAccessException, + MappingException; + + /** + * Execute the query and return a {@link Map} with results grouped by the + * given table and mapped by the given mapper. + *

+ * An {@link InvalidResultException} is thrown, if the keys are non-unique + * in the result set. Use {@link #fetchGroups(Table, RecordMapper)} instead, + * if your keys are non-unique. + * + * @param table The key table. Client code must assure that keys are unique + * in the result set. May not be null. + * @param mapper The mapper callback. + * @return A Map containing the results. + * @throws DataAccessException if something went wrong executing the query + * @throws InvalidResultException if the keys are non-unique in the result + * set. + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see Result#intoMap(Table, Class) + * @see DefaultRecordMapper + */ + Map fetchMap(Table table, RecordMapper mapper) + throws DataAccessException, MappingException; /** * Execute the query and return a {@link Map} with results grouped by the @@ -931,7 +996,9 @@ public interface ResultQuery extends Query { * Unlike {@link #fetchMap(Field[])}, this method allows for non-unique keys * in the result set. * - * @param keys The keys used for result grouping. + * @param keys The keys used for result grouping. If this is + * null or an empty array, the resulting map will + * contain at most one entry. * @return A Map containing grouped results * @throws DataAccessException if something went wrong executing the query * @see Result#intoGroups(Field[]) @@ -961,7 +1028,7 @@ public interface ResultQuery extends Query { * Execute the query and return a {@link Map} with results grouped by the * given keys and mapped by the given mapper. *

- * Unlike {@link #fetchMap(Field[], Class)}, this method allows for + * Unlike {@link #fetchMap(Field[], RecordMapper)}, this method allows for * non-unique keys in the result set. * * @param keys The keys. If this is null or an empty array, the @@ -976,6 +1043,58 @@ public interface ResultQuery extends Query { */ Map> fetchGroups(Field[] keys, RecordMapper mapper) throws MappingException; + /** + * Execute the query and return a {@link Map} with the result grouped by the + * given table. + *

+ * Unlike {@link #fetchMap(Table)}, this method allows for non-unique keys + * in the result set. + * + * @param table The key table. May not be null. + * @return A Map containing grouped results + * @throws DataAccessException if something went wrong executing the query + * @see Result#intoGroups(Field[]) + */ + Map> fetchGroups(Table table) throws DataAccessException; + + /** + * Execute the query and return a {@link Map} with results grouped by the + * given table and mapped into the given entity type. + *

+ * Unlike {@link #fetchMap(Table, Class)}, this method allows for non-unique + * keys in the result set. + * + * @param table The key table. May not be null. + * @param type The entity type. + * @return A Map containing grouped results + * @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(Table, Class) + * @see DefaultRecordMapper + */ + Map> fetchGroups(Table table, Class type) + throws DataAccessException, MappingException; + + /** + * Execute the query and return a {@link Map} with results grouped by the + * given table and mapped by the given mapper. + *

+ * Unlike {@link #fetchMap(Table, RecordMapper)}, this method allows for + * non-unique keys in the result set. + * + * @param table The key table. May not be null. + * @param mapper The mapper callback. + * @return A Map containing grouped results + * @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(Table, Class) + * @see DefaultRecordMapper + */ + Map> fetchGroups(Table table, RecordMapper mapper) + throws DataAccessException, MappingException; + /** * Return a {@link Map} with results grouped by the given key and mapped * into the given entity type. diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index f1924caa32..c921bd55ea 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -610,6 +610,21 @@ abstract class AbstractResultQuery extends AbstractQuery imple return fetch().intoMap(keys, mapper); } + @Override + public final Map fetchMap(Table table) { + return fetch().intoMap(table); + } + + @Override + public final Map fetchMap(Table table, Class type) { + return fetch().intoMap(table, type); + } + + @Override + public final Map fetchMap(Table table, RecordMapper mapper) { + return fetch().intoMap(table, mapper); + } + @Override public final Map fetchMap(Field key, Class type) { return fetch().intoMap(key, type); @@ -650,6 +665,21 @@ abstract class AbstractResultQuery extends AbstractQuery imple return fetch().intoGroups(keys, mapper); } + @Override + public final Map> fetchGroups(Table table) { + return fetch().intoGroups(table); + } + + @Override + public final Map> fetchGroups(Table table, Class type) { + return fetch().intoGroups(table, type); + } + + @Override + public final Map> fetchGroups(Table table, RecordMapper mapper) { + return fetch().intoGroups(table, mapper); + } + @Override public final Map> fetchGroups(Field key, Class type) { return fetch().intoGroups(key, type); diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index cae35757c2..0864d0b7c8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -1041,6 +1041,41 @@ class ResultImpl implements Result, AttachableInternal { return map; } + @Override + public final Map intoMap(Table table) { + Map map = new LinkedHashMap(); + + for (R record : this) { + S key = record.into(table); + + if (map.put(key, record) != null) { + throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this); + } + } + + return map; + } + + @Override + public final Map intoMap(Table table, Class type) { + return intoMap(table, Utils.configuration(this).recordMapperProvider().provide(fields, type)); + } + + @Override + public final Map intoMap(Table table, RecordMapper mapper) { + Map map = new LinkedHashMap(); + + for (R record : this) { + S key = record.into(table); + + if (map.put(key, mapper.map(record)) != null) { + throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this); + } + } + + return map; + } + @Override public final Map intoMap(Field key, Class type) { return intoMap(key, Utils.configuration(this).recordMapperProvider().provide(fields, type)); @@ -1103,6 +1138,31 @@ class ResultImpl implements Result, AttachableInternal { return map; } + @Override + public final Map> intoGroups(Field key, Class type) { + return intoGroups(key, Utils.configuration(this).recordMapperProvider().provide(fields, type)); + } + + @Override + public final Map> intoGroups(Field key, RecordMapper mapper) { + int index = indexOrFail(fieldsRow(), key); + Map> map = new LinkedHashMap>(); + + for (R record : this) { + K keyVal = (K) record.getValue(index); + + List list = map.get(keyVal); + if (list == null) { + list = new ArrayList(); + map.put(keyVal, list); + } + + list.add(mapper.map(record)); + } + + return map; + } + @Override public final Map> intoGroups(Field[] keys) { if (keys == null) { @@ -1129,31 +1189,6 @@ class ResultImpl implements Result, AttachableInternal { return map; } - @Override - public final Map> intoGroups(Field key, Class type) { - return intoGroups(key, Utils.configuration(this).recordMapperProvider().provide(fields, type)); - } - - @Override - public final Map> intoGroups(Field key, RecordMapper mapper) { - int index = indexOrFail(fieldsRow(), key); - Map> map = new LinkedHashMap>(); - - for (R record : this) { - K keyVal = (K) record.getValue(index); - - List list = map.get(keyVal); - if (list == null) { - list = new ArrayList(); - map.put(keyVal, list); - } - - list.add(mapper.map(record)); - } - - return map; - } - @Override public final Map> intoGroups(Field[] keys, Class type) { return intoGroups(keys, Utils.configuration(this).recordMapperProvider().provide(fields, type)); @@ -1185,6 +1220,49 @@ class ResultImpl implements Result, AttachableInternal { return map; } + @Override + public final Map> intoGroups(Table table) { + Map> map = new LinkedHashMap>(); + + for (R record : this) { + S key = record.into(table); + + Result result = map.get(key); + if (result == null) { + result = new ResultImpl(configuration(), this.fields); + map.put(key, result); + } + + result.add(record); + } + + return map; + } + + @Override + public final Map> intoGroups(Table table, Class type) { + return intoGroups(table, Utils.configuration(this).recordMapperProvider().provide(fields, type)); + } + + @Override + public final Map> intoGroups(Table table, RecordMapper mapper) { + Map> map = new LinkedHashMap>(); + + for (R record : this) { + S key = record.into(table); + + List list = map.get(key); + if (list == null) { + list = new ArrayList(); + map.put(key, list); + } + + list.add(mapper.map(record)); + } + + return map; + } + @Override public final Object[][] intoArray() { int size = size(); diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java index 628aa48858..04fa33c476 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java @@ -2540,6 +2540,21 @@ class SelectImpl Map fetchMap(Table table) { + return getDelegate().fetchMap(table); + } + + @Override + public final Map fetchMap(Table table, Class type) { + return getDelegate().fetchMap(table, type); + } + + @Override + public final Map fetchMap(Table table, RecordMapper mapper) { + return getDelegate().fetchMap(table, mapper); + } + @Override public final Map fetchMap(Field key, Class type) { return getDelegate().fetchMap(key, type); @@ -2580,6 +2595,21 @@ class SelectImpl Map> fetchGroups(Table table) { + return getDelegate().fetchGroups(table); + } + + @Override + public final Map> fetchGroups(Table table, Class type) { + return getDelegate().fetchGroups(table, type); + } + + @Override + public final Map> fetchGroups(Table table, RecordMapper mapper) { + return getDelegate().fetchGroups(table, mapper); + } + @Override public final Map> fetchGroups(Field key, Class type) { return getDelegate().fetchGroups(key, type);