Module org.jooq
Package org.jooq

Interface ResultQuery<R extends Record>

All Superinterfaces:
Attachable, AttachableQueryPart, Fields, Flow.Publisher<R>, Iterable<R>, Publisher<R>, org.reactivestreams.Publisher<R>, Query, QueryPart, Serializable, Statement
All Known Subinterfaces:
CloseableResultQuery<R>, DeleteResultStep<R>, InsertResultStep<R>, Select<R>, SelectConditionStep<R>, SelectConnectByAfterStartWithConditionStep<R>, SelectConnectByConditionStep<R>, SelectConnectByStep<R>, SelectCorrelatedSubqueryStep<R>, SelectDistinctOnStep<R>, SelectFinalStep<R>, SelectForJSONCommonDirectivesStep<R>, SelectForStep<R>, SelectForUpdateOfStep<R>, SelectForUpdateStep<R>, SelectForUpdateWaitStep<R>, SelectForXMLCommonDirectivesStep<R>, SelectForXMLPathDirectivesStep<R>, SelectForXMLRawDirectivesStep<R>, SelectFromStep<R>, SelectGroupByStep<R>, SelectHavingConditionStep<R>, SelectHavingStep<R>, SelectIntoStep<R>, SelectJoinStep<R>, SelectLimitAfterOffsetStep<R>, SelectLimitPercentAfterOffsetStep<R>, SelectLimitPercentStep<R>, SelectLimitStep<R>, SelectOffsetStep<R>, SelectOnConditionStep<R>, SelectOptionalOnStep<R>, SelectOptionStep<R>, SelectOrderByStep<R>, SelectQualifyConditionStep<R>, SelectQualifyStep<R>, SelectQuery<R>, SelectSeekLimitStep<R>, SelectSeekStep1<R,T1>, SelectSeekStep10<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10>, SelectSeekStep11<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11>, SelectSeekStep12<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12>, SelectSeekStep13<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13>, SelectSeekStep14<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14>, SelectSeekStep15<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15>, SelectSeekStep16<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16>, SelectSeekStep17<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17>, SelectSeekStep18<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18>, SelectSeekStep19<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19>, SelectSeekStep2<R,T1,T2>, SelectSeekStep20<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20>, SelectSeekStep21<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21>, SelectSeekStep22<R,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21,T22>, SelectSeekStep3<R,T1,T2,T3>, SelectSeekStep4<R,T1,T2,T3,T4>, SelectSeekStep5<R,T1,T2,T3,T4,T5>, SelectSeekStep6<R,T1,T2,T3,T4,T5,T6>, SelectSeekStep7<R,T1,T2,T3,T4,T5,T6,T7>, SelectSeekStep8<R,T1,T2,T3,T4,T5,T6,T7,T8>, SelectSeekStep9<R,T1,T2,T3,T4,T5,T6,T7,T8,T9>, SelectSeekStepN<R>, SelectSelectStep<R>, SelectStartWithStep<R>, SelectUnionStep<R>, SelectWhereStep<R>, SelectWindowStep<R>, SelectWithTiesAfterOffsetStep<R>, SelectWithTiesStep<R>, UpdateResultStep<R>

public interface ResultQuery<R extends Record> extends Fields, Query, Iterable<R>, Publisher<R>
A query that can return results.

jOOQ distinguishes between ordinary Query types, such as Insert, Update, Delete, and any DDLQuery, which are meant to produce side effects in a database, and the ResultQuery, which is meant to produce a Result through various means.

The most common way to create a result is by calling fetch(), or by using the query's iterator() method in a foreach loop:

 
 Result<TRecord> result = ctx.select(T.A, T.B).from(T).fetch();

 for (TRecord record : ctx.select(T.A, T.B).from(T)) {
   // ...
 }
 
 

Most approaches to fetching results in ResultQuery (including the above), fetch the entire JDBC ResultSet eagerly into memory, which allows for closing the underlying JDBC resources as quickly as possible. Such operations are not resourceful, i.e. users do not need to worry about closing any resources. None of the many ways of fetching data will affect the SQL query projection (SELECT clause). Hence, users must make sure not to fetch any unnecessary columns, themselves.

There are, however, some ways of fetching results lazily, and thus in a resourceful way. These include:

  • fetchLazy() and related methods, which produce a Cursor for imperative style consumption of resulting records.
  • fetchStream() and related methods, which produce a Java Stream for functional style consumption of resulting records.

In both cases, it is recommended to explicitly close the underlying resources (i.e. JDBC ResultSet) using try-with-resources:

 
 try (Cursor<TRecord> cursor = ctx.select(T.A, T.B).from(T).fetchLazy()) {
   for (;;) {
     TRecord record = cursor.fetchNext();
     if (record == null)
       break;

     // ...
   }
 }

 try (Stream<TRecord> stream = ctx.select(T.A, T.B).from(T).fetchStream()) {
   stream.forEach(record -> {
     // ...
   });
 }
 
 

While most instances of ResultQuery implement Select, there also exist other types of ResultQuery constructed e.g. from plain SQL APIs, such as DSLContext.resultQuery(String).

Author:
Lukas Eder