org.hibernate.dialect.pagination
Class AbstractLimitHandler

java.lang.Object
  extended by org.hibernate.dialect.pagination.AbstractLimitHandler
All Implemented Interfaces:
LimitHandler
Direct Known Subclasses:
LegacyLimitHandler, NoopLimitHandler, SQLServer2005LimitHandler

public abstract class AbstractLimitHandler
extends Object
implements LimitHandler

Default implementation of LimitHandler interface.


Field Summary
protected  RowSelection selection
           
protected  String sql
           
 
Constructor Summary
AbstractLimitHandler(String sql, RowSelection selection)
          Default constructor.
 
Method Summary
protected  int bindLimitParameters(PreparedStatement statement, int index)
          Default implementation of binding parameter values needed by the LIMIT clause.
 int bindLimitParametersAtEndOfQuery(PreparedStatement statement, int index)
          Bind parameter values needed by the LIMIT clause after original SELECT statement.
 int bindLimitParametersAtStartOfQuery(PreparedStatement statement, int index)
          Bind parameter values needed by the LIMIT clause before original SELECT statement.
 boolean bindLimitParametersFirst()
          Does the LIMIT clause come at the start of the SELECT statement, rather than at the end?
 boolean bindLimitParametersInReverseOrder()
          ANSI SQL defines the LIMIT clause to be in the form LIMIT offset, limit.
 int convertToFirstRowValue(int zeroBasedFirstResult)
          Hibernate APIs explicitly state that setFirstResult() should be 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 getMaxOrLimit()
          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).
 String getProcessedSql()
          Return processed SQL query.
 void setMaxRows(PreparedStatement statement)
          Use JDBC API to limit the number of rows returned by the SQL query.
 boolean supportsLimit()
          Does this handler support some form of limiting query results via a SQL clause?
 boolean supportsLimitOffset()
          Does this handler's LIMIT support (if any) additionally support specifying an offset?
 boolean supportsVariableLimit()
          Does this handler support bind variables (i.e., prepared statement parameters) for its limit/offset?
 boolean useMaxForLimit()
          Does the LIMIT clause take a "maximum" row number instead of a total number of returned rows?

This is easiest understood via an example.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sql

protected final String sql

selection

protected final RowSelection selection
Constructor Detail

AbstractLimitHandler

public AbstractLimitHandler(String sql,
                            RowSelection selection)
Default constructor. SQL query and selection criteria required to allow LIMIT clause pre-processing.

Parameters:
sql - SQL query.
selection - Selection criteria. null in case of unlimited number of rows.
Method Detail

supportsLimit

public boolean supportsLimit()
Description copied from interface: LimitHandler
Does this handler support some form of limiting query results via a SQL clause?

Specified by:
supportsLimit in interface LimitHandler
Returns:
True if this handler supports some form of LIMIT.

supportsLimitOffset

public boolean supportsLimitOffset()
Description copied from interface: LimitHandler
Does this handler's LIMIT support (if any) additionally support specifying an 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 (i.e., prepared statement parameters) for its limit/offset?

Returns:
True if bind variables can be used; false otherwise.

bindLimitParametersInReverseOrder

public boolean bindLimitParametersInReverseOrder()
ANSI SQL defines the LIMIT clause to be in the form LIMIT offset, limit. Does this dialect require us to bind the parameters in reverse order?

Returns:
true if the correct order is limit, offset

bindLimitParametersFirst

public boolean bindLimitParametersFirst()
Does the LIMIT clause come at the start of the SELECT statement, rather than at the end?

Returns:
true if limit parameters should come before other parameters

useMaxForLimit

public boolean useMaxForLimit()
Does the LIMIT clause take a "maximum" row number instead of a total number of returned rows?

This is easiest understood via an example. Consider you have a table with 20 rows, but you only want to retrieve rows number 11 through 20. Generally, a limit with offset would say that the offset = 11 and the limit = 10 (we only want 10 rows at a time); this is specifying the total number of returned rows. Some dialects require that we instead specify offset = 11 and limit = 20, where 20 is the "last" row we want relative to offset (i.e. total number of rows = 20 - 11 = 9)

So essentially, is limit relative from offset? Or is limit absolute?

Returns:
True if limit is relative from offset; false otherwise.

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)
Hibernate APIs explicitly state that setFirstResult() should be a zero-based offset. Here we allow the Dialect a chance to convert that value based on what the underlying db or driver will expect.

NOTE: what gets passed into AbstractLimitHandler(String, RowSelection) is the zero-based offset. Dialects 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.

Parameters:
zeroBasedFirstResult - The user-supplied, zero-based first row offset.
Returns:
The corresponding db/dialect specific offset.
See Also:
Query.setFirstResult(int), Criteria.setFirstResult(int)

getProcessedSql

public String getProcessedSql()
Description copied from interface: LimitHandler
Return processed SQL query.

Specified by:
getProcessedSql in interface LimitHandler
Returns:
Query statement with LIMIT clause applied.

bindLimitParametersAtStartOfQuery

public int bindLimitParametersAtStartOfQuery(PreparedStatement statement,
                                             int index)
                                      throws SQLException
Description copied from interface: LimitHandler
Bind parameter values needed by the LIMIT clause before original SELECT statement.

Specified by:
bindLimitParametersAtStartOfQuery in interface LimitHandler
Parameters:
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.

bindLimitParametersAtEndOfQuery

public int bindLimitParametersAtEndOfQuery(PreparedStatement statement,
                                           int index)
                                    throws SQLException
Description copied from interface: LimitHandler
Bind parameter values needed by the LIMIT clause after original SELECT statement.

Specified by:
bindLimitParametersAtEndOfQuery in interface LimitHandler
Parameters:
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.

setMaxRows

public void setMaxRows(PreparedStatement statement)
                throws SQLException
Description copied from interface: LimitHandler
Use JDBC API to limit the number of rows returned by the SQL query. Typically handlers that do not support LIMIT clause should implement this method.

Specified by:
setMaxRows in interface LimitHandler
Parameters:
statement - Statement which number of returned rows shall be limited.
Throws:
SQLException - Indicates problems while limiting maximum rows returned.

bindLimitParameters

protected int bindLimitParameters(PreparedStatement statement,
                                  int index)
                           throws SQLException
Default implementation of binding parameter values needed by the LIMIT clause.

Parameters:
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.

getMaxOrLimit

protected int getMaxOrLimit()
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).

Returns:
The appropriate value to bind into the limit clause.


Copyright © 2001-2013 Red Hat, Inc. All Rights Reserved.