[#1716] Make all logic from ResultQuery.fetchXXX() available in

Result.XXX() - Added fetchMap() methods
This commit is contained in:
Lukas Eder 2012-08-17 11:21:50 +02:00
parent 818edad796
commit 1bf31a484d
7 changed files with 126 additions and 38 deletions

View File

@ -44,6 +44,7 @@ import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
@ -1122,6 +1123,13 @@ public interface Record extends FieldProvider, Store<Object> {
*/
Object[] intoArray();
/**
* Return this record as a name/value map.
*
* @return This record as a map
*/
Map<String, Object> intoMap();
/**
* Map resulting records onto a custom type.
* <p>

View File

@ -44,7 +44,9 @@ import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import org.jooq.exception.InvalidResultException;
import org.jooq.exception.MappingException;
import org.jooq.tools.Convert;
@ -1787,6 +1789,48 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
Document intoXML();
/**
* Return the generated result as a list of name/value maps.
*
* @return The result.
* @see Record#intoMap()
*/
List<Map<String, Object>> intoMaps();
/**
* Return a {@link Map} with one of the result's columns as key and the
* corresponding records as value.
* <p>
* An {@link InvalidResultException} is thrown, if the key turns out to be
* non-unique in the result set.
*
* @param <K> The key's generic field type
* @param key The key field. Client code must assure that this field is
* unique in the result set.
* @return A Map containing the results
* @throws InvalidResultException if the key field returned two or more
* equal values from the result set.
*/
<K> Map<K, R> intoMap(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>
* An {@link InvalidResultException} is thrown, if the key turns out to be
* non-unique 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. Client code must assure that this field is
* unique in the result set.
* @param value The value field
* @return A Map containing the results
* @throws InvalidResultException if the key field returned two or more
* equal values from the result set.
*/
<K, V> Map<K, V> intoMap(Field<K> key, Field<V> value);
/**
* Convert this result into an array of arrays
* <p>
@ -1908,7 +1952,8 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
<T> Result<R> sortAsc(Field<T> field, java.util.Comparator<? super T> comparator);
/**
* Reverse-sort this result by one of its contained fields using a comparator.
* Reverse-sort this result by one of its contained fields using a
* comparator.
* <p>
* <code>null</code> sorting must be handled by the supplied
* <code>comparator</code>.

View File

@ -439,6 +439,8 @@ public interface ResultQuery<R extends Record> extends Query {
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the key field returned two or more
* equal values from the result set.
* @see Result#intoMaps()
* @see Record#intoMap()
*/
List<Map<String, Object>> fetchMaps() throws DataAccessException;
@ -450,6 +452,8 @@ public interface ResultQuery<R extends Record> extends Query {
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
* @see Result#intoMaps()
* @see Record#intoMap()
*/
Map<String, Object> fetchOneMap() throws DataAccessException;
@ -471,6 +475,7 @@ public interface ResultQuery<R extends Record> extends Query {
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the key field returned two or more
* equal values from the result set.
* @see Result#intoMap(Field, Field)
*/
<K> Map<K, R> fetchMap(Field<K> key) throws DataAccessException;

View File

@ -56,7 +56,9 @@ import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jooq.ArrayRecord;
import org.jooq.Attachable;
@ -66,6 +68,7 @@ import org.jooq.FieldProvider;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.exception.InvalidResultException;
import org.jooq.exception.MappingException;
import org.jooq.tools.Convert;
import org.jooq.tools.reflect.Reflect;
@ -590,6 +593,24 @@ abstract class AbstractRecord extends AbstractStore<Object> implements Record {
return into(Object[].class);
}
@Override
public final Map<String, Object> intoMap() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
List<Field<?>> f = getFields();
int size = f.size();
for (int i = 0; i < size; i++) {
Field<?> field = f.get(i);
if (map.put(field.getName(), getValue(i)) != null) {
throw new InvalidResultException("Field " + field.getName() + " is not unique in Record : " + this);
}
}
return map;
}
@Override
public final <T> T into(Class<? extends T> type) {
try {

View File

@ -50,7 +50,6 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
@ -410,54 +409,22 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
@Override
public final <K> Map<K, R> fetchMap(Field<K> key) {
Map<K, R> map = new LinkedHashMap<K, R>();
for (R record : fetch()) {
if (map.put(record.getValue(key), record) != null) {
throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
}
}
return map;
return fetch().intoMap(key);
}
@Override
public final <K, V> Map<K, V> fetchMap(Field<K> key, Field<V> value) {
Map<K, V> map = new LinkedHashMap<K, V>();
for (Map.Entry<K, R> entry : fetchMap(key).entrySet()) {
map.put(entry.getKey(), entry.getValue().getValue(value));
}
return map;
return fetch().intoMap(key, value);
}
@Override
public final List<Map<String, Object>> fetchMaps() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (R record : fetch()) {
list.add(convertToMap(record));
}
return list;
return fetch().intoMaps();
}
@Override
public final Map<String, Object> fetchOneMap() {
return convertToMap(fetchOne());
}
private final Map<String, Object> convertToMap(R record) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (Field<?> field : record.getFields()) {
if (map.put(field.getName(), record.getValue(field)) != null) {
throw new InvalidResultException("Field " + field.getName() + " is not unique in Record for " + this);
}
}
return map;
return fetchOne().intoMap();
}
@Override

View File

@ -77,6 +77,7 @@ import org.jooq.RecordHandler;
import org.jooq.Result;
import org.jooq.Store;
import org.jooq.Table;
import org.jooq.exception.InvalidResultException;
import org.jooq.exception.MappingException;
import org.jooq.tools.Convert;
import org.jooq.tools.StringUtils;
@ -1275,6 +1276,44 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
new String[] { "&quot;", "&apos;", "&lt;", "&gt;", "&amp;"});
}
@Override
public final List<Map<String, Object>> intoMaps() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (R record : this) {
list.add(record.intoMap());
}
return list;
}
@Override
public final <K> Map<K, R> intoMap(Field<K> key) {
Map<K, R> map = new LinkedHashMap<K, R>();
for (R record : this) {
if (map.put(record.getValue(key), record) != null) {
throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
}
}
return map;
}
@Override
public final <K, V> Map<K, V> intoMap(Field<K> key, Field<V> value) {
Map<K, V> map = new LinkedHashMap<K, V>();
for (R record : this) {
if (map.put(record.getValue(key), record.getValue(value)) != null) {
throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
}
}
return map;
}
@Override
public final Object[][] intoArray() throws MappingException {
int size = size();

View File

@ -56,7 +56,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.persistence.Column;
@ -82,6 +84,7 @@ import org.jooq.UDT;
import org.jooq.UDTRecord;
import org.jooq.conf.Settings;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.InvalidResultException;
import org.jooq.tools.Convert;
import org.jooq.tools.LoggerListener;
import org.jooq.tools.StopWatchListener;