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/all/testcases/InsertUpdateTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/InsertUpdateTests.java index b8d3a77e34..556a1fd206 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/InsertUpdateTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/InsertUpdateTests.java @@ -1001,7 +1001,8 @@ extends BaseTest 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/Coalesce.java b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java new file mode 100644 index 0000000000..13856be015 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.impl; + +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +class Coalesce extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = -4546488210418866103L; + private final Field[] fields; + + @SuppressWarnings("unchecked") + Coalesce(DataType dataType, Field[] fields) { + super("coalesce", dataType); + + this.fields = (Field[]) fields; + } + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + /* [pro] xx + xxxx xxxxxxxxx x + xxxxxxxx xxxxxxxx x xxxxxxxxxx + + xxx xxxx x x xx x x xxxxxxxxxxxxxx xxxx + xxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxx + + xxxxxxxxxxxxxxxxxxxx + xxxxxx + x + xx [/pro] */ + + default: { + ctx.visit(DSL.function("coalesce", getDataType(), fields)); + break; + } + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 223f033970..c652968e7f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -6384,7 +6384,7 @@ public class DSL { */ @Support public static Field coalesce(Field field, Field... fields) { - return function("coalesce", nullSafeDataType(field), nullSafe(combine(field, fields))); + return new Coalesce(nullSafeDataType(field), nullSafe(combine(field, fields))); } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/DateAdd.java b/jOOQ/src/main/java/org/jooq/impl/DateAdd.java index 9b79d65ea9..af0c7436ae 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DateAdd.java +++ b/jOOQ/src/main/java/org/jooq/impl/DateAdd.java @@ -196,6 +196,20 @@ class DateAdd extends AbstractFunction { xxxxxx xxxxxxxxxxxxxxxxxxxxx xxxx xxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx xxxxxxxxx xxxxxx x + xxxx xxxxxxxxx x + xxxxxx xxxxxxxxxx x + xxxx xxxxx xxxxxxx x xxxxxxx xxxxxx + xxxx xxxxxx xxxxxxx x xxxxxxxx xxxxxx + xxxx xxxx xxxxxxx x xxxxxx xxxxxx + xxxx xxxxx xxxxxxx x xxxxxxx xxxxxx + xxxx xxxxxxx xxxxxxx x xxxxxxxxx xxxxxx + xxxx xxxxxxx xxxxxxx x xxxxxxxxx xxxxxx + xxxxxxxx xxxxxxxxxxxxxxxxxxx + x + + xxxxxx xxxxxxxxxxxxxxxxxxx xxxxxxx xxxxx xxxxxxxxx xxxxxxxxxxxxxxxxxxx + x + xxxx xxxx x xxxxxx xxxxxxxxxx x xxxx xxxxx xxxxxxx x xxxxxxx xxxxxx diff --git a/jOOQ/src/main/java/org/jooq/impl/Expression.java b/jOOQ/src/main/java/org/jooq/impl/Expression.java index a8c190828c..4d99a20c7a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Expression.java +++ b/jOOQ/src/main/java/org/jooq/impl/Expression.java @@ -400,6 +400,32 @@ class Expression extends AbstractFunction { x x + xxxx xxxxxxxxx x + xx xxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxx x + xx xxxxxxxxx xx xxxx x + xxxxxx xxxxxxxxxxxxxxxxxx xxxxxx xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + x + xxxx x + xxxxxx xxxxxxxxxxxxxxxxxx xxxxxx xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + x + x + xxxx x + xx xxxxxxxx xxxxx xxxx xxxx xx xxx xx xx xxxx xxxxx + xxxxxxxxxxx xxxx x xxxxxxxxxxxxxxxxxx + + xx xxxxxxxxx xx xxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxx xxxxx xxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x xxxxxxxxx + xxxxxxxxxxxx + x + xxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxx xxxxx xxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x xxxxxxxxx + xxxxxxxxxxxx + x + x + x + xxxx xxxx x xx xxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxx x xx xxxxxxxxx xx xxxx x @@ -520,10 +546,10 @@ class Expression extends AbstractFunction { /* [pro] xx xxxx xxxxxxxxx x xx xxxxxxxxx xx xxxx x - xxxxxx xxxxxxxxxx x xxxxxxxxxx xxxxx xxxx xx xxxxxx xxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxx + xxxxxx xxxxxxxxxx x xxx xxxxxx xxxxxx xxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxx x xxxx x - xxxxxx xxxxxxxxxx x xxxxxxxxxx xxxxx xxxx xx xxxxxx xxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxx + xxxxxx xxxxxxxxxx x xxx xxxxxx xxxxxx xxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxx x x 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] */