Module storm

Class SelectBuilderImpl<T extends Record,R,ID>

java.lang.Object
st.orm.template.QueryBuilder<T,R,ID>
st.orm.template.impl.SelectBuilderImpl<T,R,ID>
Type Parameters:
T - the type of the table being queried.
R - the type of the result.
ID - the type of the primary key.
All Implemented Interfaces:
Subqueryable

public class SelectBuilderImpl<T extends Record,R,ID> extends QueryBuilder<T,R,ID>
SelectBuilderImpl relies on preview features of the Java platform:
  • SelectBuilderImpl refers to one or more preview APIs: StringTemplate.
Programs can only use SelectBuilderImpl when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A query builder for SELECT queries.
  • Field Details

  • Constructor Details

  • Method Details

    • safe

      public QueryBuilder<T,R,ID> safe()
      Returns a query builder that does not require a WHERE clause for UPDATE and DELETE queries.

      This method is used to prevent accidental updates or deletions of all records in a table when a WHERE clause is not provided.

      Specified by:
      safe in class QueryBuilder<T extends Record,R,ID>
      Since:
      1.2
    • supportsJoin

      protected boolean supportsJoin()
      Returns true to indicate that the query supports joins, false otherwise.
      Returns:
      true if the query supports joins, false otherwise.
    • distinct

      public QueryBuilder<T,R,ID> distinct()
      Marks the current query as a distinct query.
      Specified by:
      distinct in class QueryBuilder<T extends Record,R,ID>
      Returns:
      the query builder.
    • getSubquery

      public StringTemplatePREVIEW getSubquery()
      Description copied from interface: Subqueryable
      Returns the template for the query for the subquery.
      Returns:
      the template.
    • offset

      public QueryBuilder<T,R,ID> offset(int offset)
      Adds an OFFSET clause to the query.
      Specified by:
      offset in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      offset - the offset.
      Returns:
      the query builder.
      Since:
      1.2
    • limit

      public QueryBuilder<T,R,ID> limit(int limit)
      Adds a LIMIT clause to the query.
      Specified by:
      limit in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      limit - the maximum number of records to return.
      Returns:
      the query builder.
      Since:
      1.2
    • forShare

      public QueryBuilder<T,R,ID> forShare()
      Locks the selected rows for reading.
      Specified by:
      forShare in class QueryBuilder<T extends Record,R,ID>
      Returns:
      the query builder.
      Throws:
      PersistenceException - if the database does not support the specified lock mode, or if the lock mode is not supported for the current query.
      Since:
      1.2
    • forUpdate

      public QueryBuilder<T,R,ID> forUpdate()
      Locks the selected rows for reading.
      Specified by:
      forUpdate in class QueryBuilder<T extends Record,R,ID>
      Returns:
      the query builder.
      Throws:
      PersistenceException - if the database does not support the specified lock mode, or if the lock mode is not supported for the current query.
      Since:
      1.2
    • forLock

      public QueryBuilder<T,R,ID> forLock(@Nonnull StringTemplatePREVIEW template)
      Locks the selected rows using a custom lock mode.

      Note that this method results in non-portable code, as the lock mode is specific to the underlying database.

      Specified by:
      forLock in class QueryBuilder<T extends Record,R,ID>
      Returns:
      the query builder.
      Throws:
      PersistenceException - if the lock mode is not supported for the current query.
      Since:
      1.2
    • build

      public Query build()
      Builds the query based on the current state of the query builder.
      Specified by:
      build in class QueryBuilder<T extends Record,R,ID>
      Returns:
      the constructed query.
    • getResultStream

      public Stream<R> getResultStream()
      Executes the query and returns a stream of results.

      The resulting stream is lazily loaded, meaning that the records are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of records.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Specified by:
      getResultStream in class QueryBuilder<T extends Record,R,ID>
      Returns:
      a stream of results.
      Throws:
      PersistenceException - if the query operation fails due to underlying database issues, such as connectivity.
    • typed

      public <X> QueryBuilder<T,R,X> typed(@Nonnull Class<X> pkType)
      Returns a typed query builder for the specified primary key type.
      Specified by:
      typed in class QueryBuilder<T extends Record,R,ID>
      Type Parameters:
      X - the type of the primary key.
      Parameters:
      pkType - the primary key type.
      Returns:
      the typed query builder.
      Throws:
      PersistenceException - if the pk type is not valid.
      Since:
      1.2
    • append

      public QueryBuilder<T,R,ID> append(@Nonnull StringTemplatePREVIEW template)
      Append the query with a string template.
      Specified by:
      append in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      template - the string template to append.
      Returns:
      the query builder.
    • crossJoin

      public QueryBuilder<T,R,ID> crossJoin(@Nonnull Class<? extends Record> relation)
      Adds a cross join to the query.
      Specified by:
      crossJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      relation - the relation to join.
      Returns:
      the query builder.
    • innerJoin

      public QueryBuilder.TypedJoinBuilder<T,R,ID> innerJoin(@Nonnull Class<? extends Record> relation)
      Adds an inner join to the query.
      Specified by:
      innerJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      relation - the relation to join.
      Returns:
      the query builder.
    • leftJoin

      public QueryBuilder.TypedJoinBuilder<T,R,ID> leftJoin(@Nonnull Class<? extends Record> relation)
      Adds a left join to the query.
      Specified by:
      leftJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      relation - the relation to join.
      Returns:
      the query builder.
    • rightJoin

      public QueryBuilder.TypedJoinBuilder<T,R,ID> rightJoin(@Nonnull Class<? extends Record> relation)
      Adds a right join to the query.
      Specified by:
      rightJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      relation - the relation to join.
      Returns:
      the query builder.
    • join

      public QueryBuilder.JoinBuilder<T,R,ID> join(@Nonnull JoinType type, @Nonnull StringTemplatePREVIEW template, @Nonnull String alias)
      Adds a join of the specified type to the query using a template.
      Specified by:
      join in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      type - the join type.
      template - the template to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • join

      public QueryBuilder.JoinBuilder<T,R,ID> join(@Nonnull JoinType type, @Nonnull QueryBuilder<?,?,?> subquery, @Nonnull String alias)
      Adds a join of the specified type to the query using a subquery.
      Specified by:
      join in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      type - the join type.
      subquery - the subquery to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • crossJoin

      public QueryBuilder<T,R,ID> crossJoin(@Nonnull StringTemplatePREVIEW template)
      Adds a cross join to the query.
      Specified by:
      crossJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      template - the template to join.
      Returns:
      the query builder.
    • innerJoin

      public QueryBuilder.JoinBuilder<T,R,ID> innerJoin(@Nonnull StringTemplatePREVIEW template, @Nonnull String alias)
      Adds an inner join to the query.
      Specified by:
      innerJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      template - the template to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • leftJoin

      public QueryBuilder.JoinBuilder<T,R,ID> leftJoin(@Nonnull StringTemplatePREVIEW template, @Nonnull String alias)
      Adds a left join to the query.
      Specified by:
      leftJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      template - the template to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • rightJoin

      public QueryBuilder.JoinBuilder<T,R,ID> rightJoin(@Nonnull StringTemplatePREVIEW template, @Nonnull String alias)
      Adds a right join to the query.
      Specified by:
      rightJoin in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      template - the template to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • join

      public QueryBuilder.TypedJoinBuilder<T,R,ID> join(@Nonnull JoinType type, @Nonnull Class<? extends Record> relation, @Nonnull String alias)
      Adds a join of the specified type to the query.
      Specified by:
      join in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      type - the type of the join (e.g., INNER, LEFT, RIGHT).
      relation - the relation to join.
      alias - the alias to use for the joined relation.
      Returns:
      the query builder.
    • where

      public QueryBuilder<T,R,ID> where(@Nonnull Function<QueryBuilder.WhereBuilder<T,R,ID>,QueryBuilder.PredicateBuilder<?,?,?>> predicate)
      Adds a WHERE clause to the query using a QueryBuilder.WhereBuilder.
      Specified by:
      where in class QueryBuilder<T extends Record,R,ID>
      Parameters:
      predicate - the predicate to add.
      Returns:
      the query builder.