Package org.hibernate.dialect.pagination
Class AbstractLimitHandler
- java.lang.Object
-
- org.hibernate.dialect.pagination.AbstractLimitHandler
-
- All Implemented Interfaces:
LimitHandler
- Direct Known Subclasses:
AbstractNoOffsetLimitHandler
,AbstractSimpleLimitHandler
,LegacyDB2LimitHandler
,LegacyOracleLimitHandler
,NoopLimitHandler
,OffsetFetchLimitHandler
,Oracle12LimitHandler
,SQLServer2005LimitHandler
public abstract class AbstractLimitHandler extends Object implements LimitHandler
Default implementation ofLimitHandler
interface.
-
-
Field Summary
Fields Modifier and Type Field Description static LimitHandler
NO_LIMIT
-
Constructor Summary
Constructors Constructor Description AbstractLimitHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
bindLimitParameters(Limit limit, PreparedStatement statement, int index)
Default implementation of binding parameter values needed by the LIMIT clause.int
bindLimitParametersAtEndOfQuery(Limit limit, PreparedStatement statement, int index)
int
bindLimitParametersAtStartOfQuery(Limit limit, PreparedStatement statement, int index)
boolean
bindLimitParametersFirst()
Does the offset/limit clause come at the start of theSELECT
statement, or at the end of the query?boolean
bindLimitParametersInReverseOrder()
Usually, the offset comes before the limit, but occasionally the offset is specified after the limit.int
convertToFirstRowValue(int zeroBasedFirstResult)
The API methodQuery.setFirstResult(int)
accepts a zero-based offset.boolean
forceLimitUsage()
Generally, if there is no limit applied to a Hibernate query we do not apply any limits to the SQL query.protected int
getFirstRow(Limit limit)
Retrieve the indicated first row for paginationprotected Pattern
getForUpdatePattern()
The offset/limit clauses typically must come before theFOR UPDATE
ish clauses, so we need a way to identify these clauses in the text of the whole query.protected int
getMaxOrLimit(Limit limit)
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).static boolean
hasFirstRow(Limit limit)
Is a first row limit indicated?static boolean
hasMaxRows(Limit limit)
Is a max row limit indicated?protected static String
insertAfterDistinct(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right afterSELECT
,SELECT DISTINCT
, orSELECT ALL
.protected static String
insertAfterSelect(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right afterSELECT
, but beforeDISTINCT
orALL
.protected String
insertAtEnd(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right at the very end of the query.protected String
insertBeforeForUpdate(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right before theFOR UPDATE
ish clauses at the end of the query.String
processSql(String sql, Limit limit)
void
setMaxRows(Limit limit, PreparedStatement statement)
boolean
supportsLimit()
Does this handler support limiting query results?boolean
supportsLimitOffset()
Does this handler support combinations of limit and offset?boolean
supportsOffset()
Does this handler support offsetting query results without also specifying a limit?boolean
supportsVariableLimit()
Does this handler support bind variables (JDBC prepared statement parameters) for its limit/offset?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'sQuery.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 thanQuery.setFirstResult(int)
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hibernate.dialect.pagination.LimitHandler
processSql
-
-
-
-
Field Detail
-
NO_LIMIT
public static LimitHandler NO_LIMIT
-
-
Method Detail
-
supportsLimit
public boolean supportsLimit()
Description copied from interface:LimitHandler
Does this handler support limiting query results?- Specified by:
supportsLimit
in interfaceLimitHandler
- 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 interfaceLimitHandler
- 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 interfaceLimitHandler
- 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 theSELECT
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'sQuery.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 thanQuery.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 methodQuery.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(String, Limit)
has a zero-based offset. Handlers which do notsupportsVariableLimit()
should take care to perform any needed first-row-conversion calls prior to injecting the limit values into the SQL string.
-
processSql
public String processSql(String sql, Limit limit)
- Specified by:
processSql
in interfaceLimitHandler
-
bindLimitParametersAtStartOfQuery
public int bindLimitParametersAtStartOfQuery(Limit limit, PreparedStatement statement, int index) throws SQLException
- Specified by:
bindLimitParametersAtStartOfQuery
in interfaceLimitHandler
- Throws:
SQLException
-
bindLimitParametersAtEndOfQuery
public int bindLimitParametersAtEndOfQuery(Limit limit, PreparedStatement statement, int index) throws SQLException
- Specified by:
bindLimitParametersAtEndOfQuery
in interfaceLimitHandler
- Throws:
SQLException
-
setMaxRows
public void setMaxRows(Limit limit, PreparedStatement statement) throws SQLException
- Specified by:
setMaxRows
in interfaceLimitHandler
- Throws:
SQLException
-
bindLimitParameters
protected final int bindLimitParameters(Limit limit, PreparedStatement statement, int index) throws SQLException
Default implementation of binding parameter values needed by the LIMIT clause.- Parameters:
limit
- the limit.statement
- Statement to which to bind limit parameter values.index
- Index from which to start binding.- Returns:
- The number of parameter values bound.
- Throws:
SQLException
- Indicates problems binding parameter values.
-
hasMaxRows
public static boolean hasMaxRows(Limit limit)
Is a max row limit indicated?- Parameters:
limit
- The limit- Returns:
- Whether a max row limit was indicated
-
hasFirstRow
public static boolean hasFirstRow(Limit limit)
Is a first row limit indicated?- Parameters:
limit
- The limit- Returns:
- Whether a first row limit was indicated
-
getMaxOrLimit
protected final int getMaxOrLimit(Limit limit)
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:
limit
- The limit- Returns:
- The appropriate value to bind into the limit clause.
-
getFirstRow
protected final int getFirstRow(Limit limit)
Retrieve the indicated first row for pagination- Parameters:
limit
- The limit- Returns:
- The first row
-
insertAfterSelect
protected static String insertAfterSelect(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right afterSELECT
, but beforeDISTINCT
orALL
.
-
insertAfterDistinct
protected static String insertAfterDistinct(String limitOffsetClause, String sqlStatement)
Insert a fragment of SQL right afterSELECT
,SELECT DISTINCT
, orSELECT 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 theFOR UPDATE
ish clauses, so we need a way to identify these clauses in the text of the whole query.
-
-