From 2dce62d61d3e001d30457a12db7c8d0f043d019a Mon Sep 17 00:00:00 2001 From: lukaseder Date: Sat, 30 May 2015 17:41:29 +0200 Subject: [PATCH] [#4312] Add various fetchOptional() methods --- jOOQ/src/main/java/org/jooq/Cursor.java | 169 +++++--- jOOQ/src/main/java/org/jooq/DAO.java | 17 + jOOQ/src/main/java/org/jooq/DSLContext.java | 407 +++++++++++++++++- .../main/java/org/jooq/DeleteResultStep.java | 20 +- .../main/java/org/jooq/InsertResultStep.java | 20 +- jOOQ/src/main/java/org/jooq/ResultQuery.java | 244 +++++++++++ .../main/java/org/jooq/UpdateResultStep.java | 20 +- .../org/jooq/impl/AbstractResultQuery.java | 93 ++++ .../main/java/org/jooq/impl/CursorImpl.java | 24 ++ .../java/org/jooq/impl/DefaultDSLContext.java | 105 +++++ .../main/java/org/jooq/impl/DeleteImpl.java | 6 + .../main/java/org/jooq/impl/InsertImpl.java | 6 + .../main/java/org/jooq/impl/SelectImpl.java | 91 ++++ .../main/java/org/jooq/impl/UpdateImpl.java | 6 + 14 files changed, 1170 insertions(+), 58 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/Cursor.java b/jOOQ/src/main/java/org/jooq/Cursor.java index da73330a60..37822a722f 100644 --- a/jOOQ/src/main/java/org/jooq/Cursor.java +++ b/jOOQ/src/main/java/org/jooq/Cursor.java @@ -45,6 +45,7 @@ import java.sql.ResultSet; import java.sql.Statement; import java.util.Iterator; import java.util.List; +import java.util.Optional; import org.jooq.conf.Settings; import org.jooq.exception.DataAccessException; @@ -155,6 +156,65 @@ public interface Cursor extends Iterable { */ Result fetch(int number) throws DataAccessException; + /** + * Fetch results into a custom handler callback. + *

+ * The resulting records are attached to the original {@link Configuration} + * by default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @param handler The handler callback + * @return Convenience result, returning the parameter handler itself + * @throws DataAccessException if something went wrong executing the query + */ + > H fetchInto(H handler) throws DataAccessException; + + /** + * Fetch results into a custom mapper callback. + * + * @param mapper The mapper callback + * @return The custom mapped records + * @throws DataAccessException if something went wrong executing the query + */ + List fetch(RecordMapper mapper) throws DataAccessException; + + /** + * Map resulting records onto a custom type. + *

+ * This is the same as calling fetch().into(type). See + * {@link Record#into(Class)} for more details + * + * @param The generic entity type. + * @param type The entity type. + * @see Record#into(Class) + * @see Result#into(Class) + * @throws DataAccessException if something went wrong executing the query + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + List fetchInto(Class type) throws DataAccessException, MappingException; + + /** + * Map resulting records onto a custom record. + *

+ * This is the same as calling fetch().into(table). See + * {@link Record#into(Class)} for more details + *

+ * The result and its contained records are attached to the original + * {@link Configuration} by default. Use {@link Settings#isAttachRecords()} + * to override this behaviour. + * + * @param The generic table record type. + * @param table The table type. + * @see Record#into(Class) + * @see Result#into(Class) + * @throws DataAccessException if something went wrong executing the query + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + */ + Result fetchInto(Table table) throws DataAccessException, MappingException; + /** * Fetch the next record from the cursor. *

@@ -187,40 +247,6 @@ public interface Cursor extends Iterable { */ > H fetchOneInto(H handler) throws DataAccessException; - /** - * Fetch results into a custom handler callback. - *

- * The resulting records are attached to the original {@link Configuration} - * by default. Use {@link Settings#isAttachRecords()} to override this - * behaviour. - * - * @param handler The handler callback - * @return Convenience result, returning the parameter handler itself - * @throws DataAccessException if something went wrong executing the query - */ - > H fetchInto(H handler) throws DataAccessException; - - /** - * Fetch the next record into a custom mapper callback. - *

- * This will conveniently close the Cursor, after the last - * Record was fetched. - * - * @param mapper The mapper callback - * @return The custom mapped record - * @throws DataAccessException if something went wrong executing the query - */ - E fetchOne(RecordMapper mapper) throws DataAccessException; - - /** - * Fetch results into a custom mapper callback. - * - * @param mapper The mapper callback - * @return The custom mapped records - * @throws DataAccessException if something went wrong executing the query - */ - List fetch(RecordMapper mapper) throws DataAccessException; - /** * Map the next resulting record onto a custom type. *

@@ -239,21 +265,16 @@ public interface Cursor extends Iterable { E fetchOneInto(Class type) throws DataAccessException, MappingException; /** - * Map resulting records onto a custom type. + * Fetch the next record into a custom mapper callback. *

- * This is the same as calling fetch().into(type). See - * {@link Record#into(Class)} for more details + * This will conveniently close the Cursor, after the last + * Record was fetched. * - * @param The generic entity type. - * @param type The entity type. - * @see Record#into(Class) - * @see Result#into(Class) + * @param mapper The mapper callback + * @return The custom mapped record * @throws DataAccessException if something went wrong executing the query - * @throws MappingException wrapping any reflection or data type conversion - * exception that might have occurred while mapping records - * @see DefaultRecordMapper */ - List fetchInto(Class type) throws DataAccessException, MappingException; + E fetchOne(RecordMapper mapper) throws DataAccessException; /** * Map the next resulting record onto a custom record. @@ -275,15 +296,60 @@ public interface Cursor extends Iterable { */ Z fetchOneInto(Table table) throws DataAccessException, MappingException; + /* [java-8] */ /** - * Map resulting records onto a custom record. + * Fetch the next record from the cursor. *

- * This is the same as calling fetch().into(table). See + * This will conveniently close the Cursor, after the last + * Record was fetched. + *

+ * The resulting record is attached to the original {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @return The next record from the cursor + * @throws DataAccessException if something went wrong executing the query + */ + Optional fetchOptional() throws DataAccessException; + + /** + * Map the next resulting record onto a custom type. + *

+ * This is the same as calling fetchOne().into(type). See + * {@link Record#into(Class)} for more details + * + * @param The generic entity type. + * @param type The entity type. + * @see Record#into(Class) + * @see Result#into(Class) + * @throws DataAccessException if something went wrong executing the query + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @see DefaultRecordMapper + */ + Optional fetchOptionalInto(Class type) throws DataAccessException, MappingException; + + /** + * Fetch the next record into a custom mapper callback. + *

+ * This will conveniently close the Cursor, after the last + * Record was fetched. + * + * @param mapper The mapper callback + * @return The custom mapped record + * @throws DataAccessException if something went wrong executing the query + */ + Optional fetchOptional(RecordMapper mapper) throws DataAccessException; + + /** + * Map the next resulting record onto a custom record. + *

+ * This is the same as calling fetchOne().into(table). See * {@link Record#into(Class)} for more details *

- * The result and its contained records are attached to the original - * {@link Configuration} by default. Use {@link Settings#isAttachRecords()} - * to override this behaviour. + * The resulting record is attached to the original {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. * * @param The generic table record type. * @param table The table type. @@ -293,7 +359,8 @@ public interface Cursor extends Iterable { * @throws MappingException wrapping any reflection or data type conversion * exception that might have occurred while mapping records */ - Result fetchInto(Table table) throws DataAccessException, MappingException; + Optional fetchOptionalInto(Table table) throws DataAccessException, MappingException; + /* [/java-8] */ /** * Explicitly close the underlying {@link PreparedStatement} and diff --git a/jOOQ/src/main/java/org/jooq/DAO.java b/jOOQ/src/main/java/org/jooq/DAO.java index dc4fff160d..16903fec1f 100644 --- a/jOOQ/src/main/java/org/jooq/DAO.java +++ b/jOOQ/src/main/java/org/jooq/DAO.java @@ -42,6 +42,7 @@ package org.jooq; import java.util.Collection; import java.util.List; +import java.util.Optional; import org.jooq.conf.Settings; import org.jooq.exception.DataAccessException; @@ -259,6 +260,22 @@ public interface DAO, P, T> { */ P fetchOne(Field field, Z value) throws DataAccessException; + /* [java-8] */ + /** + * Find a unique record by a given field and a value. + * + * @param field The field to compare value against + * @param value The accepted value + * @return A record fulfilling field = value + * @throws DataAccessException This exception is thrown + *

    + *
  • if something went wrong executing the query
  • + *
  • if the query returned more than one value
  • + *
+ */ + Optional

fetchOptional(Field field, Z value) throws DataAccessException; + /* [/java-8] */ + /** * Get the underlying table */ diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 9b819a8fc8..8f0c0d3661 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -69,6 +69,7 @@ import java.sql.Statement; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Optional; import javax.annotation.Generated; @@ -799,6 +800,96 @@ public interface DSLContext extends Scope { @PlainSQL Record fetchOne(String sql, QueryPart... parts) throws DataAccessException, TooManyRowsException; + /* [java-8] */ + /** + * 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 results from the executed query + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + @PlainSQL + Optional fetchOptional(String sql) throws DataAccessException, TooManyRowsException; + + /** + * 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 + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + @PlainSQL + Optional fetchOptional(String sql, Object... bindings) throws DataAccessException, TooManyRowsException; + + /** + * Execute a new query holding plain SQL. + *

+ * Unlike {@link #fetchOne(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 + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + @PlainSQL + Optional fetchOptional(String sql, QueryPart... parts) throws DataAccessException, TooManyRowsException; + /* [/java-8] */ + /** * Execute a new query holding plain SQL. *

@@ -896,6 +987,102 @@ public interface DSLContext extends Scope { @PlainSQL Object fetchValue(String sql, QueryPart... parts) throws DataAccessException, TooManyRowsException, InvalidResultException; + /* [java-8] */ + /** + * 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 value from the executed query + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + @PlainSQL + Optional fetchOptionalValue(String sql) throws DataAccessException, TooManyRowsException, InvalidResultException; + + /** + * 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 + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + @PlainSQL + Optional fetchOptionalValue(String sql, Object... bindings) throws DataAccessException, TooManyRowsException, InvalidResultException; + + /** + * 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 + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + @PlainSQL + Optional fetchOptionalValue(String sql, QueryPart... parts) throws DataAccessException, TooManyRowsException, InvalidResultException; + /* [/java-8] */ + /** * Execute a new query holding plain SQL. *

@@ -1329,6 +1516,80 @@ public interface DSLContext extends Scope { @Support Record fetchOne(ResultSet rs, Class... types) throws DataAccessException, TooManyRowsException; + /* [java-8] */ + /** + * Fetch a record from a JDBC {@link ResultSet} and transform it to a jOOQ + * {@link Record}. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + * + * @param rs The JDBC ResultSet to fetch data from + * @return The resulting jOOQ record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(ResultSet rs) throws DataAccessException, TooManyRowsException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and transform it to a jOOQ + * {@link Record}. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * The additional fields 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 fields The fields to use in the desired output + * @return The resulting jOOQ record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(ResultSet rs, Field... fields) throws DataAccessException, TooManyRowsException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and transform it to a jOOQ + * {@link Record}. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * The additional types argument is used by jOOQ to coerce data + * types to the desired output + * + * @param rs The JDBC ResultSet to fetch data from + * @param types The data types to use in the desired output + * @return The resulting jOOQ record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(ResultSet rs, DataType... types) throws DataAccessException, TooManyRowsException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and transform it to a jOOQ + * {@link Record}. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * The additional types argument is used by jOOQ to coerce data + * types to the desired output + * + * @param rs The JDBC ResultSet to fetch data from + * @param types The data types to use in the desired output + * @return The resulting jOOQ record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(ResultSet rs, Class... types) throws DataAccessException, TooManyRowsException; + /* [/java-8] */ + /** * Fetch a record from a JDBC {@link ResultSet} and return the only * contained value. @@ -1409,6 +1670,88 @@ public interface DSLContext extends Scope { @Support T fetchValue(ResultSet rs, Class type) throws DataAccessException, TooManyRowsException, InvalidResultException; + /* [java-8] */ + /** + * Fetch a record from a JDBC {@link ResultSet} and return the only + * contained value. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + * + * @param rs The JDBC ResultSet to fetch data from + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + Optional fetchOptionalValue(ResultSet rs) throws DataAccessException, TooManyRowsException, InvalidResultException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and return the only + * contained value. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * 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 value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + Optional fetchOptionalValue(ResultSet rs, Field field) throws DataAccessException, TooManyRowsException, InvalidResultException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and return the only + * contained value. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * 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 value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + Optional fetchOptionalValue(ResultSet rs, DataType type) throws DataAccessException, TooManyRowsException, InvalidResultException; + + /** + * Fetch a record from a JDBC {@link ResultSet} and return the only + * contained value. + *

+ * This will internally fetch all records and throw an exception if there + * was more than one resulting record. + *

+ * 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 value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + @Support + Optional fetchOptionalValue(ResultSet rs, Class type) throws DataAccessException, TooManyRowsException, InvalidResultException; + /* [/java-8] */ + /** * Fetch a result from a JDBC {@link ResultSet} and return the only * contained column's values. @@ -6105,6 +6448,20 @@ public interface DSLContext extends Scope { */ R fetchOne(ResultQuery query) throws DataAccessException, TooManyRowsException; + /* [java-8] */ + /** + * Execute a {@link ResultQuery} in the context of this DSLContext and return + * a record. + * + * @param query The query to execute + * @return The record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @see ResultQuery#fetchOptional() + */ + Optional fetchOptional(ResultQuery query) throws DataAccessException, TooManyRowsException; + /* [/java-8] */ + /** * Execute a {@link ResultQuery} in the context of this * DSLContext and return a single value. @@ -6116,7 +6473,23 @@ public interface DSLContext extends Scope { * @throws InvalidResultException if the query returned a record with more * than one value */ - > T fetchValue(ResultQuery query) throws DataAccessException, TooManyRowsException, InvalidResultException; + > T fetchValue(ResultQuery query) + throws DataAccessException, TooManyRowsException, InvalidResultException; + + /* [java-8] */ + /** + * Execute a {@link ResultQuery} in the context of this + * DSLContext and return a single value. + * + * @param query The query to execute + * @return The value. + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @throws InvalidResultException if the query returned a record with more + * than one value + */ + > Optional fetchOptionalValue(ResultQuery query) throws DataAccessException, TooManyRowsException, InvalidResultException; + /* [/java-8] */ /** * Execute a {@link ResultQuery} in the context of this @@ -6296,6 +6669,38 @@ public interface DSLContext extends Scope { @Support R fetchOne(Table table, Condition condition) throws DataAccessException, TooManyRowsException; + /* [java-8] */ + /** + * Execute and return zero or one record for + *

SELECT * FROM [table]
. + *

+ * The resulting record is attached to this {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @return The record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(Table table) throws DataAccessException, TooManyRowsException; + + /** + * Execute and return zero or one record for + *

SELECT * FROM [table] WHERE [condition] 
. + *

+ * The resulting record is attached to this {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @return The record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + @Support + Optional fetchOptional(Table table, Condition condition) throws DataAccessException, TooManyRowsException; + /* [/java-8] */ + /** * Execute and return zero or one record for *

SELECT * FROM [table] LIMIT 1
. diff --git a/jOOQ/src/main/java/org/jooq/DeleteResultStep.java b/jOOQ/src/main/java/org/jooq/DeleteResultStep.java index bde833cdfd..8a367f1cc9 100644 --- a/jOOQ/src/main/java/org/jooq/DeleteResultStep.java +++ b/jOOQ/src/main/java/org/jooq/DeleteResultStep.java @@ -44,6 +44,8 @@ package org.jooq; import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.POSTGRES; +import java.util.Optional; + import org.jooq.exception.DataAccessException; /** @@ -71,7 +73,7 @@ public interface DeleteResultStep extends Delete { /** * The result holding returned values as specified by the - * {@link UpdateReturningStep} + * {@link UpdateReturningStep}. * * @return The returned values as specified by the * {@link UpdateReturningStep}. Note: @@ -89,7 +91,7 @@ public interface DeleteResultStep extends Delete { /** * The record holding returned values as specified by the - * {@link UpdateReturningStep} + * {@link UpdateReturningStep}. * * @return The returned value as specified by the * {@link UpdateReturningStep}. This may return null in @@ -100,4 +102,18 @@ public interface DeleteResultStep extends Delete { */ @Support({ FIREBIRD, POSTGRES }) R fetchOne() throws DataAccessException; + + /* [java-8] */ + /** + * The record holding returned values as specified by the + * {@link UpdateReturningStep}. + * + * @return The returned value as specified by the + * {@link UpdateReturningStep} + * @throws DataAccessException if something went wrong executing the query + * @see UpdateQuery#getReturnedRecord() + */ + @Support({ FIREBIRD, POSTGRES }) + Optional fetchOptional() throws DataAccessException; + /* [/java-8] */ } diff --git a/jOOQ/src/main/java/org/jooq/InsertResultStep.java b/jOOQ/src/main/java/org/jooq/InsertResultStep.java index b442084d73..0b19ea7f1d 100644 --- a/jOOQ/src/main/java/org/jooq/InsertResultStep.java +++ b/jOOQ/src/main/java/org/jooq/InsertResultStep.java @@ -40,6 +40,8 @@ */ package org.jooq; +import java.util.Optional; + import org.jooq.exception.DataAccessException; /** @@ -78,7 +80,7 @@ public interface InsertResultStep extends Insert { /** * The result holding returned values as specified by the - * {@link InsertReturningStep} + * {@link InsertReturningStep}. * * @return The returned values as specified by the * {@link InsertReturningStep}. Note: @@ -96,7 +98,7 @@ public interface InsertResultStep extends Insert { /** * The record holding returned values as specified by the - * {@link InsertReturningStep} + * {@link InsertReturningStep}. * * @return The returned value as specified by the * {@link InsertReturningStep}. This may return null in @@ -107,4 +109,18 @@ public interface InsertResultStep extends Insert { */ @Support R fetchOne() throws DataAccessException; + + /* [java-8] */ + /** + * The record holding returned values as specified by the + * {@link InsertReturningStep}. + * + * @return The returned value as specified by the + * {@link InsertReturningStep} + * @throws DataAccessException if something went wrong executing the query + * @see InsertQuery#getReturnedRecord() + */ + @Support + Optional fetchOptional() throws DataAccessException; + /* [/java-8] */ } diff --git a/jOOQ/src/main/java/org/jooq/ResultQuery.java b/jOOQ/src/main/java/org/jooq/ResultQuery.java index 3753920598..68a6ce2f8f 100644 --- a/jOOQ/src/main/java/org/jooq/ResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/ResultQuery.java @@ -48,6 +48,7 @@ import java.sql.Statement; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -625,6 +626,249 @@ public interface ResultQuery extends Query, Iterable { */ Z fetchOneInto(Table table) throws DataAccessException, TooManyRowsException; + /* [java-8] */ + /** + * Execute the query and return at most one resulting value for a + * field from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Field)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Field field) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Field, Class)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Field field, Class type) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Field, Converter)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Field field, Converter converter) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field index from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(int)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(int fieldIndex) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field index from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(int, Class)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(int fieldIndex, Class type) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field index from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(int, Converter)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(int fieldIndex, Converter converter) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(String)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(String fieldName) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(String, Class)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(String fieldName, Class type) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(String, Converter)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(String fieldName, Converter converter) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Name)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Name fieldName) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Name, Class)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Name fieldName, Class type) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value for a + * field name from the generated result. + *

+ * This is the same as calling {@link #fetchOptional()} and then + * {@link Record#getValue(Name, Converter)} + * + * @return The resulting value + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(Name fieldName, Converter converter) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting record. + *

+ * The resulting record is attached to the original {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @return The resulting record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional() throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting value into a + * custom mapper callback. + * + * @return The custom mapped record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptional(RecordMapper mapper) throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting record as a name/value + * map. + * + * @return The resulting record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + * @see Result#intoMaps() + * @see Record#intoMap() + */ + Optional> fetchOptionalMap() throws DataAccessException, TooManyRowsException; + + /** + * Execute the query and return at most one resulting record as an array. + * + * @return The resulting record + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptionalArray() throws DataAccessException, TooManyRowsException; + + /** + * Map resulting records onto a custom type. + *

+ * This is the same as calling

+     * Optional<E> result = q.fetchOptional().map(r -> r.into(type));
+     * 
. See {@link Record#into(Class)} for more details + * + * @param The generic entity type. + * @param type The entity type. + * @return The resulting record + * @see Record#into(Class) + * @see Result#into(Class) + * @throws DataAccessException if something went wrong executing the query + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @throws TooManyRowsException if the query returned more than one record + * @see DefaultRecordMapper + */ + Optional fetchOptionalInto(Class type) throws DataAccessException, MappingException, TooManyRowsException; + + /** + * Map resulting records onto a custom record. + *

+ * This is the same as calling

+     * Optional<Z> result = q.fetchOptional().map(r -> r.into(table));
+     * 
. See {@link Record#into(Table)} for more details + *

+ * The resulting record is attached to the original {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @param The generic table record type. + * @param table The table type. + * @return The resulting record + * @see Record#into(Table) + * @see Result#into(Table) + * @throws DataAccessException if something went wrong executing the query + * @throws TooManyRowsException if the query returned more than one record + */ + Optional fetchOptionalInto(Table table) throws DataAccessException, TooManyRowsException; + /* [/java-8] */ + /** * Execute the query and return at most one resulting value for a * field from the generated result. diff --git a/jOOQ/src/main/java/org/jooq/UpdateResultStep.java b/jOOQ/src/main/java/org/jooq/UpdateResultStep.java index 876034fadc..dc0504aadb 100644 --- a/jOOQ/src/main/java/org/jooq/UpdateResultStep.java +++ b/jOOQ/src/main/java/org/jooq/UpdateResultStep.java @@ -44,6 +44,8 @@ package org.jooq; import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.POSTGRES; +import java.util.Optional; + import org.jooq.exception.DataAccessException; /** @@ -74,7 +76,7 @@ public interface UpdateResultStep extends Update { /** * The result holding returned values as specified by the - * {@link UpdateReturningStep} + * {@link UpdateReturningStep}. * * @return The returned values as specified by the * {@link UpdateReturningStep}. Note: @@ -92,7 +94,7 @@ public interface UpdateResultStep extends Update { /** * The record holding returned values as specified by the - * {@link UpdateReturningStep} + * {@link UpdateReturningStep}. * * @return The returned value as specified by the * {@link UpdateReturningStep}. This may return null in @@ -103,4 +105,18 @@ public interface UpdateResultStep extends Update { */ @Support({ FIREBIRD, POSTGRES }) R fetchOne() throws DataAccessException; + + /* [java-8] */ + /** + * The record holding returned values as specified by the + * {@link UpdateReturningStep}. + * + * @return The returned value as specified by the + * {@link UpdateReturningStep}. + * @throws DataAccessException if something went wrong executing the query + * @see UpdateQuery#getReturnedRecord() + */ + @Support({ FIREBIRD, POSTGRES }) + Optional fetchOptional() throws DataAccessException; + /* [/java-8] */ } diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index 60edf05d79..94934a5361 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -58,6 +58,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -508,6 +509,98 @@ abstract class AbstractResultQuery extends AbstractQuery imple return record == null ? null : record.into(table); } + /* [java-8] */ + @Override + public final Optional fetchOptional(Field field) { + return Optional.ofNullable(fetchOne(field)); + } + + @Override + public final Optional fetchOptional(Field field, Class type) { + return Optional.ofNullable(fetchOne(field, type)); + } + + @Override + public final Optional fetchOptional(Field field, Converter converter) { + return Optional.ofNullable(fetchOne(field, converter)); + } + + @Override + public final Optional fetchOptional(int fieldIndex) { + return Optional.ofNullable(fetchOne(fieldIndex)); + } + + @Override + public final Optional fetchOptional(int fieldIndex, Class type) { + return Optional.ofNullable(fetchOne(fieldIndex, type)); + } + + @Override + public final Optional fetchOptional(int fieldIndex, Converter converter) { + return Optional.ofNullable(fetchOne(fieldIndex, converter)); + } + + @Override + public final Optional fetchOptional(String fieldName) { + return Optional.ofNullable(fetchOne(fieldName)); + } + + @Override + public final Optional fetchOptional(String fieldName, Class type) { + return Optional.ofNullable(fetchOne(fieldName, type)); + } + + @Override + public final Optional fetchOptional(String fieldName, Converter converter) { + return Optional.ofNullable(fetchOne(fieldName, converter)); + } + + @Override + public final Optional fetchOptional(Name fieldName) { + return Optional.ofNullable(fetchOne(fieldName)); + } + + @Override + public final Optional fetchOptional(Name fieldName, Class type) { + return Optional.ofNullable(fetchOne(fieldName, type)); + } + + @Override + public final Optional fetchOptional(Name fieldName, Converter converter) { + return Optional.ofNullable(fetchOne(fieldName, converter)); + } + + @Override + public final Optional fetchOptional() { + return Optional.ofNullable(fetchOne()); + } + + @Override + public final Optional fetchOptional(RecordMapper mapper) { + return Optional.ofNullable(fetchOne(mapper)); + } + + @Override + public final Optional> fetchOptionalMap() { + return Optional.ofNullable(fetchOneMap()); + } + + @Override + public final Optional fetchOptionalArray() { + return Optional.ofNullable(fetchOneArray()); + } + + @Override + public final Optional fetchOptionalInto(Class type) { + return Optional.ofNullable(fetchOneInto(type)); + } + + @Override + public final Optional fetchOptionalInto(Table table) { + return Optional.ofNullable(fetchOneInto(table)); + } + /* [/java-8] */ + @Override public final T fetchAny(Field field) { R record = fetchAny(); diff --git a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java index b6bf576ccc..c0d4598107 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java @@ -68,6 +68,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Optional; import org.jooq.BindingGetResultSetContext; import org.jooq.Cursor; @@ -83,6 +84,7 @@ import org.jooq.Result; import org.jooq.Row; import org.jooq.Table; import org.jooq.exception.ControlFlowSignal; +import org.jooq.exception.DataAccessException; import org.jooq.tools.JooqLogger; import org.jooq.tools.jdbc.JDBC41ResultSet; import org.jooq.tools.jdbc.JDBCUtils; @@ -202,6 +204,28 @@ class CursorImpl implements Cursor { return null; } + /* [java-8] */ + @Override + public final Optional fetchOptional() throws DataAccessException { + return Optional.ofNullable(fetchOne()); + } + + @Override + public final Optional fetchOptionalInto(Class type) { + return Optional.ofNullable(fetchOneInto(type)); + } + + @Override + public final Optional fetchOptional(RecordMapper mapper) { + return Optional.ofNullable(fetchOne(mapper)); + } + + @Override + public final Optional fetchOptionalInto(Table table) { + return Optional.ofNullable(fetchOneInto(table)); + } + /* [/java-8] */ + @Override public final Result fetch(int number) { // [#1157] This invokes listener.fetchStart(ctx), which has to be called diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 41fab0b45b..5bced3e79e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -69,6 +69,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import javax.annotation.Generated; import javax.sql.DataSource; @@ -551,6 +552,23 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return resultQuery(sql, parts).fetchOne(); } + /* [java-8] */ + @Override + public Optional fetchOptional(String sql) { + return Optional.ofNullable(fetchOne(sql)); + } + + @Override + public Optional fetchOptional(String sql, Object... bindings) { + return Optional.ofNullable(fetchOne(sql, bindings)); + } + + @Override + public Optional fetchOptional(String sql, QueryPart... parts) { + return Optional.ofNullable(fetchOne(sql, parts)); + } + /* [/java-8] */ + @Override public Object fetchValue(String sql) { return fetchValue((ResultQuery) resultQuery(sql)); @@ -566,6 +584,23 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return fetchValue((ResultQuery) resultQuery(sql, parts)); } + /* [java-8] */ + @Override + public Optional fetchOptionalValue(String sql) { + return Optional.ofNullable(fetchValue(sql)); + } + + @Override + public Optional fetchOptionalValue(String sql, Object... bindings) { + return Optional.ofNullable(fetchValue(sql, bindings)); + } + + @Override + public Optional fetchOptionalValue(String sql, QueryPart... parts) { + return Optional.ofNullable(fetchValue(sql, parts)); + } + /* [/java-8] */ + @Override public List fetchValues(String sql) { return fetchValues((ResultQuery) resultQuery(sql)); @@ -655,6 +690,28 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return Utils.fetchOne(fetchLazy(rs, types)); } + /* [java-8] */ + @Override + public Optional fetchOptional(ResultSet rs) { + return Optional.ofNullable(fetchOne(rs)); + } + + @Override + public Optional fetchOptional(ResultSet rs, Field... fields) { + return Optional.ofNullable(fetchOne(rs, fields)); + } + + @Override + public Optional fetchOptional(ResultSet rs, DataType... types) { + return Optional.ofNullable(fetchOne(rs, types)); + } + + @Override + public Optional fetchOptional(ResultSet rs, Class... types) { + return Optional.ofNullable(fetchOne(rs, types)); + } + /* [/java-8] */ + @Override public Object fetchValue(ResultSet rs) { return value1((Record1) fetchOne(rs)); @@ -675,6 +732,28 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return (T) value1((Record1) fetchOne(rs, type)); } + /* [java-8] */ + @Override + public Optional fetchOptionalValue(ResultSet rs) { + return Optional.ofNullable(fetchValue(rs)); + } + + @Override + public Optional fetchOptionalValue(ResultSet rs, Field field) { + return Optional.ofNullable(fetchValue(rs, field)); + } + + @Override + public Optional fetchOptionalValue(ResultSet rs, DataType type) { + return Optional.ofNullable(fetchValue(rs, type)); + } + + @Override + public Optional fetchOptionalValue(ResultSet rs, Class type) { + return Optional.ofNullable(fetchValue(rs, type)); + } + /* [/java-8] */ + @Override public List fetchValues(ResultSet rs) { return fetch(rs).getValues(0); @@ -2323,6 +2402,13 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri } } + /* [java-8] */ + @Override + public Optional fetchOptional(ResultQuery query) { + return Optional.ofNullable(fetchOne(query)); + } + /* [/java-8] */ + @Override public > T fetchValue(ResultQuery query) { final Configuration previous = Utils.getConfiguration(query); @@ -2336,6 +2422,13 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri } } + /* [java-8] */ + @Override + public > Optional fetchOptionalValue(ResultQuery query) { + return Optional.ofNullable(fetchValue(query)); + } + /* [/java-8] */ + @Override public > List fetchValues(ResultQuery query) { return (List) fetch(query).getValues(0); @@ -2423,6 +2516,18 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return Utils.fetchOne(fetchLazy(table, condition)); } + /* [java-8] */ + @Override + public Optional fetchOptional(Table table) { + return Optional.ofNullable(fetchOne(table)); + } + + @Override + public Optional fetchOptional(Table table, Condition condition) { + return Optional.ofNullable(fetchOne(table, condition)); + } + /* [/java-8] */ + @Override public R fetchAny(Table table) { return Utils.filterOne(selectFrom(table).limit(1).fetch()); diff --git a/jOOQ/src/main/java/org/jooq/impl/DeleteImpl.java b/jOOQ/src/main/java/org/jooq/impl/DeleteImpl.java index e6bd8220f2..2cf185320c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DeleteImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/DeleteImpl.java @@ -45,6 +45,7 @@ import static org.jooq.impl.DSL.exists; import static org.jooq.impl.DSL.notExists; import java.util.Collection; +import java.util.Optional; import org.jooq.Condition; import org.jooq.Configuration; @@ -243,4 +244,9 @@ class DeleteImpl getDelegate().execute(); return getDelegate().getReturnedRecord(); } + + @Override + public final Optional fetchOptional() { + return Optional.ofNullable(fetchOne()); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java index 25a37359ac..3ae72fa2f4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java @@ -44,6 +44,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Optional; import javax.annotation.Generated; @@ -663,4 +664,9 @@ class InsertImpl fetchOptional() { + return Optional.ofNullable(fetchOne()); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java index b1ba3ea124..79e7c5bcd5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java @@ -51,6 +51,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -2524,6 +2525,96 @@ class SelectImpl Optional fetchOptional(Field field) { + return getDelegate().fetchOptional(field); + } + + @Override + public final Optional fetchOptional(Field field, Class type) { + return getDelegate().fetchOptional(field, type); + } + + @Override + public final Optional fetchOptional(Field field, Converter converter) { + return getDelegate().fetchOptional(field, converter); + } + + @Override + public final Optional fetchOptional(int fieldIndex) { + return getDelegate().fetchOptional(fieldIndex); + } + + @Override + public final Optional fetchOptional(int fieldIndex, Class type) { + return getDelegate().fetchOptional(fieldIndex, type); + } + + @Override + public final Optional fetchOptional(int fieldIndex, Converter converter) { + return getDelegate().fetchOptional(fieldIndex, converter); + } + + @Override + public final Optional fetchOptional(String fieldName) { + return getDelegate().fetchOptional(fieldName); + } + + @Override + public final Optional fetchOptional(String fieldName, Class type) { + return getDelegate().fetchOptional(fieldName, type); + } + + @Override + public final Optional fetchOptional(String fieldName, Converter converter) { + return getDelegate().fetchOptional(fieldName, converter); + } + + @Override + public final Optional fetchOptional(Name fieldName) { + return getDelegate().fetchOptional(fieldName); + } + + @Override + public final Optional fetchOptional(Name fieldName, Class type) { + return getDelegate().fetchOptional(fieldName, type); + } + + @Override + public final Optional fetchOptional(Name fieldName, Converter converter) { + return getDelegate().fetchOptional(fieldName, converter); + } + + @Override + public final Optional fetchOptional() { + return getDelegate().fetchOptional(); + } + + @Override + public final Optional fetchOptional(RecordMapper mapper) { + return getDelegate().fetchOptional(mapper); + } + + @Override + public final Optional> fetchOptionalMap() { + return getDelegate().fetchOptionalMap(); + } + + @Override + public final Optional fetchOptionalArray() { + return getDelegate().fetchOptionalArray(); + } + + @Override + public final Optional fetchOptionalInto(Class type) { + return getDelegate().fetchOptionalInto(type); + } + + @Override + public final Optional fetchOptionalInto(Table table) { + return getDelegate().fetchOptionalInto(table); + } + @Override public final T fetchAny(Field field) { return getDelegate().fetchAny(field); diff --git a/jOOQ/src/main/java/org/jooq/impl/UpdateImpl.java b/jOOQ/src/main/java/org/jooq/impl/UpdateImpl.java index d9a86bc9cb..6feba2f2ef 100644 --- a/jOOQ/src/main/java/org/jooq/impl/UpdateImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/UpdateImpl.java @@ -47,6 +47,7 @@ import static org.jooq.impl.DSL.table; import java.util.Collection; import java.util.Map; +import java.util.Optional; import javax.annotation.Generated; @@ -672,4 +673,9 @@ final class UpdateImpl getDelegate().execute(); return getDelegate().getReturnedRecord(); } + + @Override + public final Optional fetchOptional() { + return Optional.ofNullable(fetchOne()); + } }