public class SQLServer2005LimitHandler extends AbstractLimitHandler
LIMIT clause handler compatible with SQL Server 2005 and later.
| Constructor and Description |
|---|
SQLServer2005LimitHandler()
Constructs a SQLServer2005LimitHandler
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addTopExpression(StringBuilder sql,
int offset)
Adds
TOP expression. |
int |
bindLimitParametersAtEndOfQuery(RowSelection selection,
PreparedStatement statement,
int index)
Bind parameter values needed by the LIMIT clause after original SELECT statement.
|
int |
bindLimitParametersAtStartOfQuery(RowSelection selection,
PreparedStatement statement,
int index)
Bind parameter values needed by the LIMIT clause before original SELECT statement.
|
int |
convertToFirstRowValue(int zeroBasedFirstResult)
Hibernate APIs explicitly state that setFirstResult() should be a zero-based offset.
|
protected void |
encloseWithOuterQuery(StringBuilder sql,
int offset)
Encloses original SQL statement with outer query that provides hibernate_row_nr column.
|
protected String |
fillAliasInSelectClause(StringBuilder sb,
int offset)
Adds missing aliases in provided SELECT clause and returns coma-separated list of them.
|
String |
processSql(String sql,
RowSelection selection)
Add a LIMIT clause to the given SQL SELECT (HHH-2655: ROW_NUMBER for Paging)
|
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 <tt>LIMIT</tt> clause take a "maximum" row number instead
of a total number of returned rows?
<p/>
This is easiest understood via an example.
|
bindLimitParameters, bindLimitParametersFirst, bindLimitParametersInReverseOrder, forceLimitUsage, getMaxOrLimit, setMaxRowspublic SQLServer2005LimitHandler()
Constructs a SQLServer2005LimitHandler
public boolean supportsLimit()
LimitHandlerDoes this handler support some form of limiting query results via a SQL clause?
supportsLimit in interface LimitHandlersupportsLimit in class AbstractLimitHandlerpublic boolean useMaxForLimit()
AbstractLimitHandlerDoes the <tt>LIMIT</tt> clause take a "maximum" row number instead of a total number of returned rows? <p/> 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) <p/> So essentially, is limit relative from offset? Or is limit absolute?
useMaxForLimit in class AbstractLimitHandlerpublic boolean supportsLimitOffset()
LimitHandlerDoes this handler’s LIMIT support (if any) additionally support specifying an offset?
supportsLimitOffset in interface LimitHandlersupportsLimitOffset in class AbstractLimitHandlerpublic boolean supportsVariableLimit()
AbstractLimitHandlerDoes this handler support bind variables (i.e., prepared statement parameters) for its limit/offset?
supportsVariableLimit in class AbstractLimitHandlerpublic int convertToFirstRowValue(int zeroBasedFirstResult)
AbstractLimitHandlerHibernate 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.
<p/>
NOTE: what gets passed into AbstractLimitHandler.processSql(String, org.hibernate.engine.spi.RowSelection)
is the zero-based offset. Dialects which do not AbstractLimitHandler.supportsVariableLimit() should take care to perform
any needed first-row-conversion calls prior to injecting the limit values into the SQL string.
convertToFirstRowValue in class AbstractLimitHandlerzeroBasedFirstResult - The user-supplied, zero-based first row offset.Query.setFirstResult(int),
Criteria.setFirstResult(int)public String processSql(String sql, RowSelection selection)
Add a LIMIT clause to the given SQL SELECT (HHH-2655: ROW_NUMBER for Paging)
The LIMIT SQL will look like:
<pre> WITH query AS ( SELECT inner_query.* , ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as hibernate_row_nr FROM ( original_query_with_top_if_order_by_present_and_all_aliased_columns ) inner_query ) SELECT alias_list FROM query WHERE hibernate_row_nr >= offset AND hibernate_row_nr < offset + last </pre>
When offset equals 0, only <code>TOP(?)</code> expression is added to the original query.
processSql in interface LimitHandlerprocessSql in class AbstractLimitHandlersql - the SQL query to process.selection - the selection criteria for rows.public int bindLimitParametersAtStartOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException
LimitHandlerBind parameter values needed by the LIMIT clause before original SELECT statement.
bindLimitParametersAtStartOfQuery in interface LimitHandlerbindLimitParametersAtStartOfQuery in class AbstractLimitHandlerselection - the selection criteria for rows.statement - Statement to which to bind limit parameter values.index - Index from which to start binding.SQLException - Indicates problems binding parameter values.public int bindLimitParametersAtEndOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException
LimitHandlerBind parameter values needed by the LIMIT clause after original SELECT statement.
bindLimitParametersAtEndOfQuery in interface LimitHandlerbindLimitParametersAtEndOfQuery in class AbstractLimitHandlerselection - the selection criteria for rows.statement - Statement to which to bind limit parameter values.index - Index from which to start binding.SQLException - Indicates problems binding parameter values.protected String fillAliasInSelectClause(StringBuilder sb, int offset)
Adds missing aliases in provided SELECT clause and returns coma-separated list of them. If query takes advantage of expressions like * or {table.*} inside SELECT clause, method returns *.
sb - SQL query.offset - the starting offset.protected void encloseWithOuterQuery(StringBuilder sql, int offset)
Encloses original SQL statement with outer query that provides hibernate_row_nr column.
sql - SQL query.offset - SQL query offset.protected void addTopExpression(StringBuilder sql, int offset)
Adds TOP expression. Parameter value is bind in
bindLimitParametersAtStartOfQuery(RowSelection, PreparedStatement, int) method.
sql - SQL query.offset - the offset where top expression pattern matching should begin.Copyright © 2001-2018 Red Hat, Inc. All Rights Reserved.