Skip to content

Commit

Permalink
[#3412] Add List DSLContext.fetchValues(...) similar to the existing …
Browse files Browse the repository at this point in the history
…fetchValue(...) methods
  • Loading branch information
lukaseder committed Jul 16, 2014
1 parent 6e963d2 commit 2945ea8
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 1 deletion.
154 changes: 153 additions & 1 deletion jOOQ/src/main/java/org/jooq/DSLContext.java
Expand Up @@ -857,11 +857,97 @@ public interface DSLContext {
* @return The results from the executed query. This may be
* <code>null</code> 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.
* <p>
* Example (Postgres):
* <p>
* <code><pre>
* String sql = "FETCH ALL IN \"<unnamed cursor 1>\"";</pre></code> Example
* (SQLite):
* <p>
* <code><pre>
* String sql = "pragma table_info('my_table')";</pre></code>
* <p>
* <b>NOTE</b>: 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
* <code>null</code>.
* @throws DataAccessException if something went wrong executing the query
*/
@Support
List<?> fetchValues(String sql) throws DataAccessException;

/**
* Execute a new query holding plain SQL.
* <p>
* There must be as many bind variables contained in the SQL, as passed in
* the bindings parameter
* <p>
* Example (Postgres):
* <p>
* <code><pre>
* String sql = "FETCH ALL IN \"<unnamed cursor 1>\"";</pre></code> Example
* (SQLite):
* <p>
* <code><pre>
* String sql = "pragma table_info('my_table')";</pre></code>
* <p>
* <b>NOTE</b>: 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
* <code>null</code>.
* @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.
* <p>
* 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: <code><pre>
* // 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"
* </pre></code>
* <p>
* <b>NOTE</b>: 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
* <code>null</code>.
* @throws DataAccessException if something went wrong executing the query
*/
@Support
List<?> fetchValues(String sql, QueryPart... parts) throws DataAccessException;

/**
* Execute a query holding plain SQL.
* <p>
Expand Down Expand Up @@ -1277,6 +1363,62 @@ public interface DSLContext {
@Support
<T> T fetchValue(ResultSet rs, Class<T> 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.
* <p>
* The additional <code>field</code> 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
<T> List<T> fetchValues(ResultSet rs, Field<T> field) throws DataAccessException;

/**
* Fetch a result from a JDBC {@link ResultSet} and return the only
* contained column's values.
* <p>
* The additional <code>type</code> 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
<T> List<T> fetchValues(ResultSet rs, DataType<T> type) throws DataAccessException;

/**
* Fetch a result from a JDBC {@link ResultSet} and return the only
* contained column's values.
* <p>
* The additional <code>type</code> 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
<T> List<T> fetchValues(ResultSet rs, Class<T> type) throws DataAccessException;

/**
* Wrap a JDBC {@link ResultSet} into a jOOQ {@link Cursor}.
* <p>
Expand Down Expand Up @@ -5555,6 +5697,16 @@ public interface DSLContext {
*/
<T, R extends Record1<T>> T fetchValue(ResultQuery<R> query) throws DataAccessException, InvalidResultException;

/**
* Execute a {@link ResultQuery} in the context of this
* <code>DSLContext</code> 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
*/
<T, R extends Record1<T>> List<T> fetchValues(ResultQuery<R> query) throws DataAccessException;

/**
* Execute a {@link Select} query in the context of this <code>DSLContext</code> and return
* a <code>COUNT(*)</code> value.
Expand Down
40 changes: 40 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
Expand Up @@ -545,6 +545,21 @@ public Object fetchValue(String sql, QueryPart... parts) {
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();
Expand Down Expand Up @@ -649,6 +664,26 @@ public <T> T fetchValue(ResultSet rs, Class<T> type) {
return (T) value1((Record1) fetchOne(rs, type));
}

@Override
public List<?> fetchValues(ResultSet rs) {
return fetch(rs).getValues(0);
}

@Override
public <T> List<T> fetchValues(ResultSet rs, Field<T> field) {
return fetch(rs).getValues(field);
}

@Override
public <T> List<T> fetchValues(ResultSet rs, DataType<T> type) {
return fetch(rs).getValues(0, type.getType());
}

@Override
public <T> List<T> fetchValues(ResultSet rs, Class<T> type) {
return fetch(rs).getValues(0, type);
}

@Override
public Cursor<Record> fetchLazy(ResultSet rs) {
try {
Expand Down Expand Up @@ -2112,6 +2147,11 @@ public <T, R extends Record1<T>> T fetchValue(ResultQuery<R> query) {
}
}

@Override
public <T, R extends Record1<T>> List<T> fetchValues(ResultQuery<R> query) {
return (List) fetch(query).getValues(0);
}

private final <T, R extends Record1<T>> T value1(R record) {
if (record == null)
return null;
Expand Down

0 comments on commit 2945ea8

Please sign in to comment.