[#3501] Fetch Set<T> instead of List<T>

This commit is contained in:
Lukas Eder 2014-08-06 14:15:05 +02:00
parent 30a859b550
commit 67830467bf
8 changed files with 352 additions and 1 deletions

View File

@ -75,6 +75,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -396,6 +397,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertTrue(array3 instanceof Integer[]);
}
public void testFetchSet() throws Exception {
assertEquals(
new LinkedHashSet<>(AUTHOR_IDS),
create().select(TBook_AUTHOR_ID()).from(TBook()).fetchSet(TBook_AUTHOR_ID())
);
}
public void testFetch() throws Exception {
SelectQuery<?> q = create().selectQuery();
q.addFrom(TAuthor());

View File

@ -1261,6 +1261,11 @@ public abstract class jOOQAbstractTest<
new FetchTests(this).testFetchArray();
}
@Test
public void testFetchSet() throws Exception {
new FetchTests(this).testFetchSet();
}
@Test
public void testFetchGroupsPOJO() throws Exception {
new FetchTests(this).testFetchGroupsPOJO();

View File

@ -45,6 +45,7 @@ import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Generated;
@ -896,6 +897,113 @@ public interface Result<R extends Record> extends List<R>, Attachable {
<T, U> U[] intoArray(Field<T> field, Converter<? super T, U> converter) throws IllegalArgumentException,
DataTypeException;
/**
* Return all values for a field index from the result.
*
* @return The resulting values. This may be an array type more concrete
* than <code>Object[]</code>, depending on whether jOOQ has any
* knowledge about <code>fieldIndex</code>'s actual type.
* @see #getValues(int)
* @throws IllegalArgumentException If the argument fieldIndex is not
* contained in {@link #fieldsRow()}
*/
Set<?> intoSet(int fieldIndex) throws IllegalArgumentException;
/**
* Return all values for a field index from the result.
*
* @return The resulting values.
* @see #getValues(int, Class)
* @throws IllegalArgumentException If the argument fieldIndex is not
* contained in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<T> Set<T> intoSet(int fieldIndex, Class<? extends T> type) throws IllegalArgumentException, DataTypeException;
/**
* Return all values for a field index from the result.
*
* @return The resulting values.
* @see #getValues(int, Converter)
* @throws IllegalArgumentException If the argument fieldIndex is not
* contained in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<U> Set<U> intoSet(int fieldIndex, Converter<?, U> converter) throws IllegalArgumentException, DataTypeException;
/**
* Return all values for a field name from the result.
*
* @return The resulting values. This may be an array type more concrete
* than <code>Object[]</code>, depending on whether jOOQ has any
* knowledge about <code>fieldName</code>'s actual type.
* @see #getValues(String)
* @throws IllegalArgumentException If the argument fieldName is not
* contained in {@link #fieldsRow()}
*/
Set<?> intoSet(String fieldName) throws IllegalArgumentException;
/**
* Return all values for a field name from the result.
*
* @return The resulting values.
* @see #getValues(String, Class)
* @throws IllegalArgumentException If the argument fieldName is not
* contained in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<T> Set<T> intoSet(String fieldName, Class<? extends T> type) throws IllegalArgumentException, DataTypeException;
/**
* Return all values for a field name from the result.
*
* @return The resulting values.
* @see #getValues(String, Converter)
* @throws IllegalArgumentException If the argument fieldName is not
* contained in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<U> Set<U> intoSet(String fieldName, Converter<?, U> converter) throws IllegalArgumentException, DataTypeException;
/**
* Return all values for a field from the result.
*
* @return The resulting values.
* @see #getValues(Field)
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #fieldsRow()}
*/
<T> Set<T> intoSet(Field<T> field) throws IllegalArgumentException;
/**
* Return all values for a field from the result.
*
* @return The resulting values.
* @see #getValues(Field, Class)
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<T> Set<T> intoSet(Field<?> field, Class<? extends T> type) throws IllegalArgumentException, DataTypeException;
/**
* Return all values for a field from the result.
*
* @return The resulting values.
* @see #getValues(Field, Converter)
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #fieldsRow()}
* @throws DataTypeException wrapping any data type conversion exception
* that might have occurred
*/
<T, U> Set<U> intoSet(Field<T> field, Converter<? super T, U> converter) throws IllegalArgumentException,
DataTypeException;
/**
* Copy all records from this result into a new result with new records
* holding only a subset of the previous fields.

View File

@ -46,6 +46,7 @@ import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import org.jooq.conf.Settings;
@ -1141,6 +1142,96 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<T, U> U[] fetchArray(Field<T> field, Converter<? super T, U> converter) throws DataAccessException;
/**
* Execute the query and return all values for a field index from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(int)
*/
Set<?> fetchSet(int fieldIndex) throws DataAccessException;
/**
* Execute the query and return all values for a field index from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(int, Class)
*/
<T> Set<T> fetchSet(int fieldIndex, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return all values for a field index from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(int, Converter)
*/
<U> Set<U> fetchSet(int fieldIndex, Converter<?, U> converter) throws DataAccessException;
/**
* Execute the query and return all values for a field name from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(String)
*/
Set<?> fetchSet(String fieldName) throws DataAccessException;
/**
* Execute the query and return all values for a field name from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(String, Converter)
*/
<T> Set<T> fetchSet(String fieldName, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return all values for a field name from the
* generated result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(String, Class)
*/
<U> Set<U> fetchSet(String fieldName, Converter<?, U> converter) throws DataAccessException;
/**
* Execute the query and return all values for a field from the generated
* result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(Field)
*/
<T> Set<T> fetchSet(Field<T> field) throws DataAccessException;
/**
* Execute the query and return all values for a field from the generated
* result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(Field, Class)
*/
<T> Set<T> fetchSet(Field<?> field, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return all values for a field from the generated
* result.
*
* @return The resulting values.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoArray(Field, Converter)
*/
<T, U> Set<U> fetchSet(Field<T> field, Converter<? super T, U> converter) throws DataAccessException;
/**
* Map resulting records onto a custom type.
* <p>

View File

@ -57,6 +57,7 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@ -704,6 +705,51 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoArray(field, converter);
}
@Override
public final Set<?> fetchSet(int fieldIndex) {
return fetch().intoSet(fieldIndex);
}
@Override
public final <T> Set<T> fetchSet(int fieldIndex, Class<? extends T> type) {
return fetch().intoSet(fieldIndex, type);
}
@Override
public final <U> Set<U> fetchSet(int fieldIndex, Converter<?, U> converter) {
return fetch().intoSet(fieldIndex, converter);
}
@Override
public final Set<?> fetchSet(String fieldName) {
return fetch().intoSet(fieldName);
}
@Override
public final <T> Set<T> fetchSet(String fieldName, Class<? extends T> type) {
return fetch().intoSet(fieldName, type);
}
@Override
public final <U> Set<U> fetchSet(String fieldName, Converter<?, U> converter) {
return fetch().intoSet(fieldName, converter);
}
@Override
public final <T> Set<T> fetchSet(Field<T> field) {
return fetch().intoSet(field);
}
@Override
public final <T> Set<T> fetchSet(Field<?> field, Class<? extends T> type) {
return fetch().intoSet(field, type);
}
@Override
public final <T, U> Set<U> fetchSet(Field<T> field, Converter<? super T, U> converter) {
return fetch().intoSet(field, converter);
}
/**
* Subclasses may override this method
* <p>

View File

@ -57,9 +57,11 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -1077,6 +1079,51 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return Convert.convertArray(intoArray(field), converter);
}
@Override
public final Set<?> intoSet(int fieldIndex) {
return new LinkedHashSet<Object>(getValues(fieldIndex));
}
@Override
public final <T> Set<T> intoSet(int fieldIndex, Class<? extends T> type) {
return new LinkedHashSet<T>(getValues(fieldIndex, type));
}
@Override
public final <U> Set<U> intoSet(int fieldIndex, Converter<?, U> converter) {
return new LinkedHashSet<U>(getValues(fieldIndex, converter));
}
@Override
public final Set<?> intoSet(String fieldName) {
return new LinkedHashSet<Object>(getValues(fieldName));
}
@Override
public final <T> Set<T> intoSet(String fieldName, Class<? extends T> type) {
return new LinkedHashSet<T>(getValues(fieldName, type));
}
@Override
public final <U> Set<U> intoSet(String fieldName, Converter<?, U> converter) {
return new LinkedHashSet<U>(getValues(fieldName, converter));
}
@Override
public final <T> Set<T> intoSet(Field<T> field) {
return new LinkedHashSet<T>(getValues(field));
}
@Override
public final <T> Set<T> intoSet(Field<?> field, Class<? extends T> type) {
return new LinkedHashSet<T>(getValues(field, type));
}
@Override
public final <T, U> Set<U> intoSet(Field<T> field, Converter<? super T, U> converter) {
return new LinkedHashSet<U>(getValues(field, converter));
}
@Override
public final Result<Record> into(Field<?>... f) {
Result<Record> result = new ResultImpl<Record>(Utils.configuration(this), f);

View File

@ -50,6 +50,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import javax.annotation.Generated;
@ -2594,6 +2595,51 @@ class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
return getDelegate().fetchArray(field, converter);
}
@Override
public final Set<?> fetchSet(int fieldIndex) {
return getDelegate().fetchSet(fieldIndex);
}
@Override
public final <T> Set<T> fetchSet(int fieldIndex, Class<? extends T> type) {
return getDelegate().fetchSet(fieldIndex, type);
}
@Override
public final <U> Set<U> fetchSet(int fieldIndex, Converter<?, U> converter) {
return getDelegate().fetchSet(fieldIndex, converter);
}
@Override
public final Set<?> fetchSet(String fieldName) {
return getDelegate().fetchSet(fieldName);
}
@Override
public final <T> Set<T> fetchSet(String fieldName, Class<? extends T> type) {
return getDelegate().fetchSet(fieldName, type);
}
@Override
public final <U> Set<U> fetchSet(String fieldName, Converter<?, U> converter) {
return getDelegate().fetchSet(fieldName, converter);
}
@Override
public final <T> Set<T> fetchSet(Field<T> field) {
return getDelegate().fetchSet(field);
}
@Override
public final <T> Set<T> fetchSet(Field<?> field, Class<? extends T> type) {
return getDelegate().fetchSet(field, type);
}
@Override
public final <T, U> Set<U> fetchSet(Field<T> field, Converter<? super T, U> converter) {
return getDelegate().fetchSet(field, converter);
}
@Override
public final <T> List<T> fetchInto(Class<? extends T> type) {
return getDelegate().fetchInto(type);

View File

@ -733,7 +733,7 @@ class SelectQueryImpl<R extends Record> extends AbstractSelect<R> implements Sel
// The simplest way to see if no FROM clause needs to be rendered is to
// render it. But use a new RenderContext (without any VisitListeners)
// for that purpose!
boolean hasFrom = true
boolean hasFrom = false
/* [pro] xx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxx
xx [/pro] */