From 2945ea841d660337a6bc6099d570d497d406a6fb Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 16 Jul 2014 18:38:07 +0200 Subject: [PATCH] [#3412] Add List DSLContext.fetchValues(...) similar to the existing fetchValue(...) methods --- jOOQ/src/main/java/org/jooq/DSLContext.java | 154 +++++++++++++++++- .../java/org/jooq/impl/DefaultDSLContext.java | 40 +++++ 2 files changed, 193 insertions(+), 1 deletion(-) diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 16890f6b9a..e9b9792b30 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -857,11 +857,97 @@ public interface DSLContext { * @return The results from the executed query. This may be * null if the database returned no records * @throws DataAccessException if something went wrong executing the query - * @throws InvalidResultException if the query returned more than one record or a record with more than one value. + * @throws InvalidResultException if the query returned more than one record + * or a record with more than one value. */ @Support Object fetchValue(String sql, QueryPart... parts) throws DataAccessException, InvalidResultException; + /** + * Execute a new query holding plain SQL. + *

+ * Example (Postgres): + *

+ *

+     * String sql = "FETCH ALL IN \"\"";
Example + * (SQLite): + *

+ *

+     * String sql = "pragma table_info('my_table')";
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @return The result values from the executed query. This is never + * null. + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(String sql) throws DataAccessException; + + /** + * Execute a new query holding plain SQL. + *

+ * There must be as many bind variables contained in the SQL, as passed in + * the bindings parameter + *

+ * Example (Postgres): + *

+ *

+     * String sql = "FETCH ALL IN \"\"";
Example + * (SQLite): + *

+ *

+     * String sql = "pragma table_info('my_table')";
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @param bindings The bindings + * @return The results from the executed query. This is never + * null. + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(String sql, Object... bindings) throws DataAccessException; + + /** + * Execute a new query holding plain SQL. + *

+ * Unlike {@link #fetchValue(String, Object...)}, the SQL passed to this + * method should not contain any bind variables. Instead, you can pass + * {@link QueryPart} objects to the method which will be rendered at indexed + * locations of your SQL string as such:

+     * // The following query
+     * fetchOne("select {0}, {1} from {2}", val(1), inline("test"), name("DUAL"));
+     *
+     * // Will execute this SQL on an Oracle database with RenderNameStyle.QUOTED:
+     * select ?, 'test' from "DUAL"
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link DSL#name(String...)} and similar methods + * + * @param sql The SQL clause, containing {numbered placeholders} where query + * parts can be injected + * @param parts The {@link QueryPart} objects that are rendered at the + * {numbered placeholder} locations + * @return The results from the executed query. This is never + * null. + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(String sql, QueryPart... parts) throws DataAccessException; + /** * Execute a query holding plain SQL. *

@@ -1277,6 +1363,62 @@ public interface DSLContext { @Support T fetchValue(ResultSet rs, Class type) throws DataAccessException, InvalidResultException; + /** + * Fetch a result from a JDBC {@link ResultSet} and return the only + * contained column's values. + * + * @param rs The JDBC ResultSet to fetch data from + * @return The resulting values + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(ResultSet rs) throws DataAccessException; + + /** + * Fetch a result from a JDBC {@link ResultSet} and return the only + * contained column's values. + *

+ * The additional field argument is used by jOOQ to coerce + * field names and data types to the desired output + * + * @param rs The JDBC ResultSet to fetch data from + * @param field The field to use in the desired output + * @return The resulting values + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(ResultSet rs, Field field) throws DataAccessException; + + /** + * Fetch a result from a JDBC {@link ResultSet} and return the only + * contained column's values. + *

+ * The additional type argument is used by jOOQ to coerce data + * types to the desired output + * + * @param rs The JDBC ResultSet to fetch data from + * @param type The data type to use in the desired output + * @return The resulting values + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(ResultSet rs, DataType type) throws DataAccessException; + + /** + * Fetch a result from a JDBC {@link ResultSet} and return the only + * contained column's values. + *

+ * The additional type argument is used by jOOQ to coerce data + * types to the desired output + * + * @param rs The JDBC ResultSet to fetch data from + * @param type The data types to use in the desired output + * @return The resulting values + * @throws DataAccessException if something went wrong executing the query + */ + @Support + List fetchValues(ResultSet rs, Class type) throws DataAccessException; + /** * Wrap a JDBC {@link ResultSet} into a jOOQ {@link Cursor}. *

@@ -5555,6 +5697,16 @@ public interface DSLContext { */ > T fetchValue(ResultQuery query) throws DataAccessException, InvalidResultException; + /** + * Execute a {@link ResultQuery} in the context of this + * DSLContext and return all values for the only column. + * + * @param query The query to execute + * @return The values. + * @throws DataAccessException if something went wrong executing the query + */ + > List fetchValues(ResultQuery query) throws DataAccessException; + /** * Execute a {@link Select} query in the context of this DSLContext and return * a COUNT(*) value. diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index c3a70c14ed..e1f151ab04 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -545,6 +545,21 @@ public class DefaultDSLContext implements DSLContext, Serializable { return fetchValue((ResultQuery) resultQuery(sql, parts)); } + @Override + public List fetchValues(String sql) { + return fetchValues((ResultQuery) resultQuery(sql)); + } + + @Override + public List fetchValues(String sql, Object... bindings) { + return fetchValues((ResultQuery) resultQuery(sql, bindings)); + } + + @Override + public List fetchValues(String sql, QueryPart... parts) { + return fetchValues((ResultQuery) resultQuery(sql, parts)); + } + @Override public int execute(String sql) { return query(sql).execute(); @@ -649,6 +664,26 @@ public class DefaultDSLContext implements DSLContext, Serializable { return (T) value1((Record1) fetchOne(rs, type)); } + @Override + public List fetchValues(ResultSet rs) { + return fetch(rs).getValues(0); + } + + @Override + public List fetchValues(ResultSet rs, Field field) { + return fetch(rs).getValues(field); + } + + @Override + public List fetchValues(ResultSet rs, DataType type) { + return fetch(rs).getValues(0, type.getType()); + } + + @Override + public List fetchValues(ResultSet rs, Class type) { + return fetch(rs).getValues(0, type); + } + @Override public Cursor fetchLazy(ResultSet rs) { try { @@ -2112,6 +2147,11 @@ public class DefaultDSLContext implements DSLContext, Serializable { } } + @Override + public > List fetchValues(ResultQuery query) { + return (List) fetch(query).getValues(0); + } + private final > T value1(R record) { if (record == null) return null;