From 67830467bf3d94b39ce525eb76c8e2a1344e59a7 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 6 Aug 2014 14:15:05 +0200 Subject: [PATCH] [#3501] Fetch Set instead of List --- .../jooq/test/all/testcases/FetchTests.java | 8 ++ .../java/org/jooq/test/jOOQAbstractTest.java | 5 + jOOQ/src/main/java/org/jooq/Result.java | 108 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/ResultQuery.java | 91 +++++++++++++++ .../org/jooq/impl/AbstractResultQuery.java | 46 ++++++++ .../main/java/org/jooq/impl/ResultImpl.java | 47 ++++++++ .../main/java/org/jooq/impl/SelectImpl.java | 46 ++++++++ .../java/org/jooq/impl/SelectQueryImpl.java | 2 +- 8 files changed, 352 insertions(+), 1 deletion(-) diff --git a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FetchTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FetchTests.java index 495859b0bc..6d6ec292cd 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FetchTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FetchTests.java @@ -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(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()); diff --git a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java index 585232257b..c1a185897a 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java @@ -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(); diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index 98a247373a..0abc2f65ea 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -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 extends List, Attachable { U[] intoArray(Field field, Converter 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 Object[], depending on whether jOOQ has any + * knowledge about fieldIndex'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 + */ + Set intoSet(int fieldIndex, Class 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 + */ + Set intoSet(int fieldIndex, Converter 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 Object[], depending on whether jOOQ has any + * knowledge about fieldName'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 + */ + Set intoSet(String fieldName, Class 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 + */ + Set intoSet(String fieldName, Converter 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()} + */ + Set intoSet(Field 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 + */ + Set intoSet(Field field, Class 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 + */ + Set intoSet(Field field, Converter 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. diff --git a/jOOQ/src/main/java/org/jooq/ResultQuery.java b/jOOQ/src/main/java/org/jooq/ResultQuery.java index fc287de207..d924ab9381 100644 --- a/jOOQ/src/main/java/org/jooq/ResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/ResultQuery.java @@ -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 extends Query { */ U[] fetchArray(Field field, Converter 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) + */ + Set fetchSet(int fieldIndex, Class 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) + */ + Set fetchSet(int fieldIndex, Converter 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) + */ + Set fetchSet(String fieldName, Class 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) + */ + Set fetchSet(String fieldName, Converter 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) + */ + Set fetchSet(Field 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) + */ + Set fetchSet(Field field, Class 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) + */ + Set fetchSet(Field field, Converter converter) throws DataAccessException; + /** * Map resulting records onto a custom type. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index 1c971a17a7..6131ab5b1b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -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 extends AbstractQuery imple return fetch().intoArray(field, converter); } + @Override + public final Set fetchSet(int fieldIndex) { + return fetch().intoSet(fieldIndex); + } + + @Override + public final Set fetchSet(int fieldIndex, Class type) { + return fetch().intoSet(fieldIndex, type); + } + + @Override + public final Set fetchSet(int fieldIndex, Converter converter) { + return fetch().intoSet(fieldIndex, converter); + } + + @Override + public final Set fetchSet(String fieldName) { + return fetch().intoSet(fieldName); + } + + @Override + public final Set fetchSet(String fieldName, Class type) { + return fetch().intoSet(fieldName, type); + } + + @Override + public final Set fetchSet(String fieldName, Converter converter) { + return fetch().intoSet(fieldName, converter); + } + + @Override + public final Set fetchSet(Field field) { + return fetch().intoSet(field); + } + + @Override + public final Set fetchSet(Field field, Class type) { + return fetch().intoSet(field, type); + } + + @Override + public final Set fetchSet(Field field, Converter converter) { + return fetch().intoSet(field, converter); + } + /** * Subclasses may override this method *

diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index 17a272521f..dd155c8385 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -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 implements Result, AttachableInternal { return Convert.convertArray(intoArray(field), converter); } + @Override + public final Set intoSet(int fieldIndex) { + return new LinkedHashSet(getValues(fieldIndex)); + } + + @Override + public final Set intoSet(int fieldIndex, Class type) { + return new LinkedHashSet(getValues(fieldIndex, type)); + } + + @Override + public final Set intoSet(int fieldIndex, Converter converter) { + return new LinkedHashSet(getValues(fieldIndex, converter)); + } + + @Override + public final Set intoSet(String fieldName) { + return new LinkedHashSet(getValues(fieldName)); + } + + @Override + public final Set intoSet(String fieldName, Class type) { + return new LinkedHashSet(getValues(fieldName, type)); + } + + @Override + public final Set intoSet(String fieldName, Converter converter) { + return new LinkedHashSet(getValues(fieldName, converter)); + } + + @Override + public final Set intoSet(Field field) { + return new LinkedHashSet(getValues(field)); + } + + @Override + public final Set intoSet(Field field, Class type) { + return new LinkedHashSet(getValues(field, type)); + } + + @Override + public final Set intoSet(Field field, Converter converter) { + return new LinkedHashSet(getValues(field, converter)); + } + @Override public final Result into(Field... f) { Result result = new ResultImpl(Utils.configuration(this), f); diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java index fa9e3a34db..5e78298054 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java @@ -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 fetchSet(int fieldIndex) { + return getDelegate().fetchSet(fieldIndex); + } + + @Override + public final Set fetchSet(int fieldIndex, Class type) { + return getDelegate().fetchSet(fieldIndex, type); + } + + @Override + public final Set fetchSet(int fieldIndex, Converter converter) { + return getDelegate().fetchSet(fieldIndex, converter); + } + + @Override + public final Set fetchSet(String fieldName) { + return getDelegate().fetchSet(fieldName); + } + + @Override + public final Set fetchSet(String fieldName, Class type) { + return getDelegate().fetchSet(fieldName, type); + } + + @Override + public final Set fetchSet(String fieldName, Converter converter) { + return getDelegate().fetchSet(fieldName, converter); + } + + @Override + public final Set fetchSet(Field field) { + return getDelegate().fetchSet(field); + } + + @Override + public final Set fetchSet(Field field, Class type) { + return getDelegate().fetchSet(field, type); + } + + @Override + public final Set fetchSet(Field field, Converter converter) { + return getDelegate().fetchSet(field, converter); + } + @Override public final List fetchInto(Class type) { return getDelegate().fetchInto(type); diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index ef22fbef7a..5cd9a1f34c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -733,7 +733,7 @@ class SelectQueryImpl extends AbstractSelect 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] */