Class AbstractLimitHandler

java.lang.Object
org.hibernate.dialect.pagination.spi.AbstractLimitHandler
All Implemented Interfaces:
LimitHandler
Direct Known Subclasses:
AbstractNoOffsetLimitHandler, AbstractSimpleLimitHandler, LegacyDB2LimitHandler, NoopLimitHandler, OffsetFetchLimitHandler, Oracle12LimitHandler

@SPI({USE,IMPLEMENT}) public abstract class AbstractLimitHandler extends Object implements LimitHandler
Default implementation of LimitHandler interface.
Since:
8.0
  • Field Details

  • Constructor Details

    • AbstractLimitHandler

      @SPI(IMPLEMENT) protected AbstractLimitHandler()
  • Method Details

    • supportsLimit

      public boolean supportsLimit()
      Description copied from interface: LimitHandler
      Does this handler support limiting query results?
      Specified by:
      supportsLimit in interface LimitHandler
      Returns:
      True if this handler supports limit alone.
    • supportsOffset

      public boolean supportsOffset()
      Description copied from interface: LimitHandler
      Does this handler support offsetting query results without also specifying a limit?
      Specified by:
      supportsOffset in interface LimitHandler
      Returns:
      True if this handler supports offset alone.
    • supportsLimitOffset

      public boolean supportsLimitOffset()
      Description copied from interface: LimitHandler
      Does this handler support combinations of limit and offset?
      Specified by:
      supportsLimitOffset in interface LimitHandler
      Returns:
      True if the handler supports an offset within the limit support.
    • supportsVariableLimit

      public boolean supportsVariableLimit()
      Does this handler support bind variables (JDBC prepared statement parameters) for its limit/offset?
      Returns:
      true if bind variables can be used
    • bindLimitParametersInReverseOrder

      public boolean bindLimitParametersInReverseOrder()
      Usually, the offset comes before the limit, but occasionally the offset is specified after the limit. Does this dialect require us to bind the parameters in reverse order?
      Returns:
      true if the correct order is limit then offset
    • bindLimitParametersFirst

      public boolean bindLimitParametersFirst()
      Does the offset/limit clause come at the start of the SELECT statement, or at the end of the query?
      Returns:
      true if limit parameters come before other parameters
    • useMaxForLimit

      public boolean useMaxForLimit()
      Does the limit clause expect the number of the last row, or the "page size", the maximum number of rows we want to receive? Hibernate's Query.setMaxResults(int) accepts the page size, so the number of the last row is obtained by adding the number of the first row, which is one greater than Query.setFirstResult(int).
      Returns:
      true if the limit clause expects the number of the last row, false if it expects the page size
    • forceLimitUsage

      public boolean forceLimitUsage()
      Generally, if there is no limit applied to a Hibernate query we do not apply any limits to the SQL query. This option forces that the limit be written to the SQL query.
      Returns:
      true to force limit into SQL query even if none specified in Hibernate query; false otherwise.
    • convertToFirstRowValue

      public int convertToFirstRowValue(int zeroBasedFirstResult)
      The API method Query.setFirstResult(int) accepts a zero-based offset. Does this dialect require a one-based offset to be specified in the offset clause?
      Parameters:
      zeroBasedFirstResult - The user-supplied, zero-based first row offset.
      Returns:
      The resulting offset, adjusted to one-based if necessary.
      Implementation Note:
      The value passed into processSql(PaginationRequest) has a zero-based offset. Handlers which do not supportsVariableLimit() should take care to perform any needed first-row-conversion calls prior to injecting the limit values into the SQL string.
    • processSql

      public PaginationResult processSql(PaginationRequest request)
      Description copied from interface: LimitHandler

      Apply pagination to a complete SQL string.

      Return PaginationResult.unchanged(String) when the request needs no SQL or JDBC pagination. The returned JDBC instructions must describe every pagination parameter introduced by the rewritten SQL.

      Specified by:
      processSql in interface LimitHandler
      Parameters:
      request - immutable completed-SQL pagination input
      Returns:
      rewritten SQL and the JDBC work needed to execute it
    • result

      protected final PaginationResult result(PaginationRequest request, String sql)
    • jdbcInstructions

      protected PaginationJdbcInstructions jdbcInstructions(PaginationRequest request)
    • jdbcMaxRows

      protected Integer jdbcMaxRows(PaginationRequest request)
    • rowsToSkip

      protected int rowsToSkip(PaginationRequest request)
    • parameterPositionStart

      public int parameterPositionStart(PaginationRequest request)
      Description copied from interface: LimitHandler

      Determine the one-based position of the first original query parameter after any pagination parameters inserted at the beginning of the SQL.

      This method must agree with PaginationJdbcInstructions.parametersAtStart returned by LimitHandler.processSql(PaginationRequest) and must not mutate this handler.

      Specified by:
      parameterPositionStart in interface LimitHandler
    • bindLimitParameterValues

      protected List<Integer> bindLimitParameterValues(PaginationRequest request)
    • hasMaxRows

      public static boolean hasMaxRows(PaginationRequest request)
      Is a max row limit indicated?
      Parameters:
      request - the immutable pagination request
      Returns:
      Whether a max row limit was indicated
    • hasFirstRow

      public static boolean hasFirstRow(PaginationRequest request)
      Is a first row limit indicated?
      Parameters:
      request - the immutable pagination request
      Returns:
      Whether a first row limit was indicated
    • getMaxOrLimit

      protected final int getMaxOrLimit(PaginationRequest request)
      Some dialect-specific LIMIT clauses require the maximum last row number (aka, first_row_number + total_row_count), while others require the maximum returned row count (the total maximum number of rows to return).
      Parameters:
      request - the immutable pagination request
      Returns:
      The appropriate value to bind into the limit clause.
    • getFirstRow

      protected final int getFirstRow(PaginationRequest request)
      Retrieve the indicated first row for pagination
      Parameters:
      request - the immutable pagination request
      Returns:
      The first row
    • insertAfterSelect

      protected static String insertAfterSelect(String limitOffsetClause, String sqlStatement)
      Insert a fragment of SQL right after SELECT, but before DISTINCT or ALL.
    • insertAfterDistinct

      protected static String insertAfterDistinct(String limitOffsetClause, String sqlStatement)
      Insert a fragment of SQL right after SELECT, SELECT DISTINCT, or SELECT ALL.
    • insertAtEnd

      protected String insertAtEnd(String limitOffsetClause, String sqlStatement)
      Insert a fragment of SQL right at the very end of the query.
    • getForUpdatePattern

      protected Pattern getForUpdatePattern()
      The offset/limit clauses typically must come before the FOR UPDATEish clauses, so we need a way to identify these clauses in the text of the whole query.
    • insertBeforeForUpdate

      protected String insertBeforeForUpdate(String limitOffsetClause, String sqlStatement)
      Insert a fragment of SQL right before the FOR UPDATEish clauses at the end of the query.