Search in sources :

Example 1 with NoDataFoundException

use of org.jooq.exception.NoDataFoundException in project jOOQ by jOOQ.

the class Tools method fetchSingle.

/**
 * Get the only element from a cursor, or throw an exception.
 * <p>
 * [#2373] This method will always close the argument cursor, as it is
 * supposed to be completely consumed by this method.
 *
 * @param cursor The cursor
 * @param hasLimit1 Whether a LIMIT clause is present that guarantees at
 *            most one row
 * @return The only element from the cursor
 * @throws NoDataFoundException Thrown if the cursor did not return any rows
 * @throws TooManyRowsException Thrown if the cursor returns more than one
 *             element
 */
static final <R extends Record> R fetchSingle(Cursor<R> cursor, boolean hasLimit1) throws NoDataFoundException, TooManyRowsException {
    try {
        // [#7001] Fetching at most two rows rather than at most one row
        // (and then checking of additional rows) improves debug logs
        // [#7430] Avoid fetching the second row (additional overhead) if
        // there is a guarantee of at most one row
        Result<R> result = cursor.fetchNext(hasLimit1 ? 1 : 2);
        int size = result.size();
        if (size == 0)
            throw exception(cursor, new NoDataFoundException("Cursor returned no rows"));
        else if (size == 1)
            return result.get(0);
        else
            throw exception(cursor, new TooManyRowsException("Cursor returned more than one result"));
    } finally {
        cursor.close();
    }
}
Also used : TooManyRowsException(org.jooq.exception.TooManyRowsException) K_ERROR(org.jooq.impl.Keywords.K_ERROR) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) K_NVARCHAR(org.jooq.impl.Keywords.K_NVARCHAR) REFLECTION_CACHE_GET_ANNOTATED_GETTER(org.jooq.impl.CacheType.REFLECTION_CACHE_GET_ANNOTATED_GETTER) QUOTE_START_DELIMITER(org.jooq.impl.Identifiers.QUOTE_START_DELIMITER) K_RAISERROR(org.jooq.impl.Keywords.K_RAISERROR) INTEGER(org.jooq.impl.SQLDataType.INTEGER) QUOTE_END_DELIMITER(org.jooq.impl.Identifiers.QUOTE_END_DELIMITER) REFLECTION_CACHE_GET_MATCHING_GETTER(org.jooq.impl.CacheType.REFLECTION_CACHE_GET_MATCHING_GETTER) OTHER(org.jooq.impl.SQLDataType.OTHER) NoDataFoundException(org.jooq.exception.NoDataFoundException)

Aggregations

NoDataFoundException (org.jooq.exception.NoDataFoundException)1 TooManyRowsException (org.jooq.exception.TooManyRowsException)1 REFLECTION_CACHE_GET_ANNOTATED_GETTER (org.jooq.impl.CacheType.REFLECTION_CACHE_GET_ANNOTATED_GETTER)1 REFLECTION_CACHE_GET_MATCHING_GETTER (org.jooq.impl.CacheType.REFLECTION_CACHE_GET_MATCHING_GETTER)1 QUOTE_END_DELIMITER (org.jooq.impl.Identifiers.QUOTE_END_DELIMITER)1 QUOTE_START_DELIMITER (org.jooq.impl.Identifiers.QUOTE_START_DELIMITER)1 K_ERROR (org.jooq.impl.Keywords.K_ERROR)1 K_NVARCHAR (org.jooq.impl.Keywords.K_NVARCHAR)1 K_RAISERROR (org.jooq.impl.Keywords.K_RAISERROR)1 INTEGER (org.jooq.impl.SQLDataType.INTEGER)1 OTHER (org.jooq.impl.SQLDataType.OTHER)1 VARCHAR (org.jooq.impl.SQLDataType.VARCHAR)1