Package org.jooq

Interface Cursor<R extends Record>

  • Type Parameters:
    R - The cursor's record type
    All Superinterfaces:
    java.lang.AutoCloseable, Formattable, java.lang.Iterable<R>

    public interface Cursor<R extends Record>
    extends java.lang.Iterable<R>, Formattable, java.lang.AutoCloseable
    Cursors allow for lazy, sequential access to an underlying JDBC ResultSet. Unlike Result, data can only be accessed sequentially, using an Iterator, or the cursor's hasNext() and fetch() methods.

    Client code must close this Cursor in order to close the underlying PreparedStatement and ResultSet

    The cursor can be consumed in two ways:

    • Record by record: Such methods can be recognised by the term "Next" in the method name, e.g. fetchNext(int).
    • Completely in one go: Such methods do not have the term "Next" in their method names, e.g. fetch().

    Note: Unlike usual implementations of Iterable, a Cursor can only provide one Iterator!

    Author:
    Lukas Eder
    • Method Detail

      • recordType

        RecordType<R> recordType()
        Get this cursor's row type.
      • fieldsRow

        Row fieldsRow()
        Get this cursor's fields as a Row.
      • field

        <T> Field<T> field​(Field<T> field)
        Get a specific field from this Cursor.

        This will return:

        • A field that is the same as the argument field (by identity comparison).
        • A field that is equal to the argument field (exact matching fully qualified name).
        • A field that is equal to the argument field (partially matching qualified name).
        • A field whose name is equal to the name of the argument field.
        • null otherwise.
        If several fields have the same name, the first one is returned and a warning is logged.
        See Also:
        Row.field(Field)
      • field

        Field<?> field​(java.lang.String name)
        Get a specific field from this Cursor.
        See Also:
        Row.field(String)
      • field

        Field<?> field​(int index)
        Get a specific field from this Cursor.
        See Also:
        Row.field(int)
      • fields

        Field<?>[] fields()
        Get all fields from this Cursor.
        See Also:
        Row.fields()
      • fields

        Field<?>[] fields​(Field<?>... fields)
        Get all fields from this Cursor, providing some fields.
        Returns:
        All available fields
        See Also:
        Row.fields(Field...)
      • fields

        Field<?>[] fields​(java.lang.String... fieldNames)
        Get all fields from this Cursor, providing some field names.
        Returns:
        All available fields
        See Also:
        Row.fields(String...)
      • fields

        Field<?>[] fields​(Name... fieldNames)
        Get all fields from this Cursor, providing some field names.
        Returns:
        All available fields
        See Also:
        Row.fields(Name...)
      • fields

        Field<?>[] fields​(int... fieldIndexes)
        Get all fields from this Cursor, providing some field indexes.
        Returns:
        All available fields
        See Also:
        Row.fields(int...)
      • indexOf

        int indexOf​(Field<?> field)
        Get a field's index from this cursor.
        Parameters:
        field - The field to look for
        Returns:
        The field's index or -1 if the field is not contained in this cursor.
      • indexOf

        int indexOf​(java.lang.String fieldName)
        Get a field's index from this cursor.
        Parameters:
        fieldName - The field name to look for
        Returns:
        The field's index or -1 if the field is not contained in this cursor.
      • indexOf

        int indexOf​(Name fieldName)
        Get a field's index from this cursor.
        Parameters:
        fieldName - The field name to look for
        Returns:
        The field's index or -1 if the field is not contained in this cursor
      • hasNext

        boolean hasNext()
                 throws DataAccessException
        Check whether this cursor has a next record.

        This will conveniently close the Cursor, after the last Record was fetched.

        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNext

        Result<R> fetchNext​(int number)
                     throws DataAccessException
        Fetch the next couple of records from the cursor.

        This will conveniently close the Cursor, after the last Record was fetched.

        The result and its contained records are attached to the original Configuration by default. Use Settings.isAttachRecords() to override this behaviour.

        Parameters:
        number - The number of records to fetch. If this is 0 or negative an empty list is returned, the cursor is untouched. If this is greater than the number of remaining records, then all remaining records are returned.
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchInto

        <H extends RecordHandler<? super R>> H fetchInto​(H handler)
                                                  throws DataAccessException
        Fetch results into a custom handler callback.

        The resulting records are attached to the original Configuration by default. Use Settings.isAttachRecords() to override this behaviour.

        Parameters:
        handler - The handler callback
        Returns:
        Convenience result, returning the parameter handler itself
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetch

        <E> java.util.List<E> fetch​(RecordMapper<? super R,​E> mapper)
                             throws DataAccessException
        Fetch results into a custom mapper callback.
        Parameters:
        mapper - The mapper callback
        Returns:
        The custom mapped records
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNext

        R fetchNext()
             throws DataAccessException
        Fetch the next record from the cursor.

        This will conveniently close the Cursor, after the last Record was fetched.

        The resulting record is attached to the original Configuration by default. Use Settings.isAttachRecords() to override this behaviour.

        Returns:
        The next record from the cursor, or null if there is no next record.
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNextInto

        <H extends RecordHandler<? super R>> H fetchNextInto​(H handler)
                                                      throws DataAccessException
        Fetch the next record into a custom handler callback.

        This will conveniently close the Cursor, after the last Record was fetched.

        The resulting record is attached to the original Configuration by default. Use Settings.isAttachRecords() to override this behaviour.

        Parameters:
        handler - The handler callback
        Returns:
        Convenience result, returning the parameter handler itself
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNext

        <E> E fetchNext​(RecordMapper<? super R,​E> mapper)
                 throws DataAccessException
        Fetch the next record into a custom mapper callback.

        This will conveniently close the Cursor, after the last Record was fetched.

        Parameters:
        mapper - The mapper callback
        Returns:
        The custom mapped record
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNextOptional

        java.util.Optional<R> fetchNextOptional()
                                         throws DataAccessException
        Fetch the next record from the cursor.

        This will conveniently close the Cursor, after the last Record was fetched.

        The resulting record is attached to the original Configuration by default. Use Settings.isAttachRecords() to override this behaviour.

        Returns:
        The next record from the cursor
        Throws:
        DataAccessException - if something went wrong executing the query
      • fetchNextOptional

        <E> java.util.Optional<E> fetchNextOptional​(RecordMapper<? super R,​E> mapper)
                                             throws DataAccessException
        Fetch the next record into a custom mapper callback.

        This will conveniently close the Cursor, after the last Record was fetched.

        Parameters:
        mapper - The mapper callback
        Returns:
        The custom mapped record
        Throws:
        DataAccessException - if something went wrong executing the query
      • collect

        <X,​A> X collect​(java.util.stream.Collector<? super R,​A,​X> collector)
                       throws DataAccessException
        Reduce the execution results of this query using a Collector.

        This works in the same way as calling the following code:

         
         cursor.stream().collect(collector);
         
         
        Parameters:
        collector - The collector that collects all records and accumulates them into a result type.
        Returns:
        The result of the collection.
        Throws:
        DataAccessException - if something went wrong executing the query
      • close

        void close()
            throws DataAccessException
        Explicitly close the underlying PreparedStatement and ResultSet.

        If you fetch all records from the underlying ResultSet, jOOQ Cursor implementations will close themselves for you. Calling close() again will have no effect.

        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        DataAccessException - if something went wrong executing the query
      • isClosed

        boolean isClosed()
        Check whether this Cursor has been explicitly or "conveniently" closed.

        Explicit closing can be achieved by calling close() from client code. "Convenient" closing is done by any of the other methods, when the last record was fetched.

      • resultSet

        java.sql.ResultSet resultSet()
        Get the Cursor's underlying ResultSet.

        This will return a ResultSet wrapping the JDBC driver's ResultSet. Closing this ResultSet may close the producing Statement or PreparedStatement, depending on your setting for ResultQuery.keepStatement(boolean).

        Modifying this ResultSet will affect this Cursor.

        Returns:
        The underlying ResultSet. May be null, for instance when the Cursor is closed.