Java源码示例:org.hibernate.dialect.pagination.LimitHandler
示例1
default CompletionStage<ResultSet> executeReactiveQueryStatement(
String sqlStatement,
QueryParameters queryParameters,
List<AfterLoadAction> afterLoadActions,
SessionImplementor session) {
// Processing query filters.
queryParameters.processFilters( sqlStatement, session );
// Applying LIMIT clause.
final LimitHandler limitHandler = limitHandler( queryParameters.getRowSelection(), session );
String sql = limitHandler.processSql( queryParameters.getFilteredSQL(), queryParameters.getRowSelection() );
// Adding locks and comments.
sql = preprocessSQL( sql, queryParameters, session.getSessionFactory(), afterLoadActions );
return session.unwrap(ReactiveSession.class)
.getReactiveConnection()
.selectJdbc( sql, toParameterArray(queryParameters, session) );
}
示例2
/**
* Execute given <tt>PreparedStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
*/
protected final ResultSet getResultSet(
final PreparedStatement st,
final RowSelection selection,
final LimitHandler limitHandler,
final boolean autodiscovertypes,
final SharedSessionContractImplementor session) throws SQLException, HibernateException {
try {
ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
return processResultSet(rs, selection, limitHandler, autodiscovertypes, session);
}
catch (SQLException | HibernateException e) {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw e;
}
}
示例3
/**
* Execute given <tt>CallableStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
*/
protected final ResultSet getResultSet(
final CallableStatement st,
final RowSelection selection,
final LimitHandler limitHandler,
final boolean autodiscovertypes,
final SharedSessionContractImplementor session) throws SQLException, HibernateException {
try {
ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
return processResultSet(rs, selection, limitHandler, autodiscovertypes, session);
}
catch (SQLException | HibernateException e) {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw e;
}
}
示例4
private ResultSet processResultSet(
ResultSet rs,
final RowSelection selection,
final LimitHandler limitHandler,
final boolean autodiscovertypes,
final SharedSessionContractImplementor session
) throws SQLException, HibernateException {
rs = wrapResultSetIfEnabled( rs, session );
if ( !limitHandler.supportsLimitOffset() || !LimitHelper.useLimit( limitHandler, selection ) ) {
advance( rs, selection );
}
if ( autodiscovertypes ) {
autoDiscoverTypes( rs );
}
return rs;
}
示例5
@Override
public void bindToPreparedStatement(PreparedStatement adaptor,
QueryParameters queryParameters,
LimitHandler limitHandler,
SharedSessionContractImplementor session) throws SQLException {
super.bindPreparedStatement(adaptor, queryParameters, limitHandler, session);
}
示例6
@Override
public void bindToPreparedStatement(PreparedStatement adaptor,
QueryParameters queryParameters,
LimitHandler limitHandler,
SharedSessionContractImplementor session) throws SQLException {
super.bindPreparedStatement(adaptor, queryParameters, limitHandler, session);
}
示例7
protected SqlStatementWrapper executeQueryStatement(
String sqlStatement,
QueryParameters queryParameters,
boolean scroll,
List<AfterLoadAction> afterLoadActions,
SharedSessionContractImplementor session) throws SQLException {
// Processing query filters.
queryParameters.processFilters( sqlStatement, session );
// Applying LIMIT clause.
final LimitHandler limitHandler = getLimitHandler(
queryParameters.getRowSelection()
);
String sql = limitHandler.processSql( queryParameters.getFilteredSQL(), queryParameters.getRowSelection() );
// Adding locks and comments.
sql = session.getJdbcServices().getJdbcEnvironment().getDialect()
.addSqlHintOrComment(
sql,
queryParameters,
session.getFactory().getSessionFactoryOptions().isCommentsEnabled()
);
final PreparedStatement st = prepareQueryStatement( sql, queryParameters, limitHandler, scroll, session );
return new SqlStatementWrapper( st, getResultSet( st, queryParameters.getRowSelection(), limitHandler, queryParameters.hasAutoDiscoverScalarTypes(), session ) );
}
示例8
/**
* Execute given <tt>PreparedStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
*/
protected final ResultSet getResultSet(
final PreparedStatement st,
final RowSelection selection,
final LimitHandler limitHandler,
final boolean autodiscovertypes,
final SharedSessionContractImplementor session)
throws SQLException, HibernateException {
try {
ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
rs = wrapResultSetIfEnabled( rs , session );
if ( !limitHandler.supportsLimitOffset() || !LimitHelper.useLimit( limitHandler, selection ) ) {
advance( rs, selection );
}
if ( autodiscovertypes ) {
autoDiscoverTypes( rs );
}
return rs;
}
catch (SQLException | HibernateException ex) {
session.getJdbcCoordinator().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw ex;
}
}
示例9
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return LEGACY_LIMIT_HANDLER;
}
else {
return LIMIT_HANDLER;
}
}
示例10
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return LegacyFirstLimitHandler.INSTANCE;
}
return FirstLimitHandler.INSTANCE;
}
示例11
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return LegacyFirstLimitHandler.INSTANCE;
}
return getDefaultLimitHandler();
}
示例12
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return super.getLimitHandler();
}
return limitHandler;
}
示例13
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return LegacyFirstLimitHandler.INSTANCE;
}
return FirstLimitHandler.INSTANCE;
}
示例14
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return new LegacyLimitHandler( this );
}
return getDefaultLimitHandler();
}
示例15
@Override
public LimitHandler getLimitHandler() {
if ( isLegacyLimitHandlerBehaviorEnabled() ) {
return LEGACY_LIMIT_HANDLER;
}
return LIMIT_HANDLER;
}
示例16
@Test
public void testLimit() {
final RowSelection rowSelection = new RowSelection();
rowSelection.setMaxRows(getMaxRows());
LimitHandler limitHandler = ((SessionFactoryImpl) sessionFactory()).getDialect().getLimitHandler();
String limitStatement = limitHandler.processSql(SELECT_POST_COMMENT, rowSelection);
long startNanos = System.nanoTime();
doInJDBC(connection -> {
try (PreparedStatement statement = connection.prepareStatement(limitStatement)) {
limitHandler.bindLimitParametersAtEndOfQuery(rowSelection, statement, 1);
statement.setInt(1, getMaxRows());
statement.execute();
int count = 0;
ResultSet resultSet = statement.getResultSet();
while (resultSet.next()) {
resultSet.getLong(1);
count++;
}
assertEquals(getMaxRows(), count);
} catch (SQLException e) {
fail(e.getMessage());
}
});
LOGGER.info("{} Result Set with limit took {} millis",
dataSourceProvider().database(),
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos));
}
示例17
void bindToPreparedStatement(PreparedStatement adaptor,
QueryParameters queryParameters,
LimitHandler limitHandler,
SharedSessionContractImplementor session) throws SQLException;
示例18
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例19
protected SqlStatementWrapper executeQueryStatement(
String sqlStatement,
QueryParameters queryParameters,
boolean scroll,
List<AfterLoadAction> afterLoadActions,
SharedSessionContractImplementor session) throws SQLException {
// Processing query filters.
queryParameters.processFilters( sqlStatement, session );
// Applying LIMIT clause.
final LimitHandler limitHandler = getLimitHandler(
queryParameters.getRowSelection()
);
String sql = limitHandler.processSql( queryParameters.getFilteredSQL(), queryParameters.getRowSelection() );
// Adding locks and comments.
sql = preprocessSQL( sql, queryParameters, getFactory(), afterLoadActions );
final PreparedStatement st = prepareQueryStatement( sql, queryParameters, limitHandler, scroll, session );
final ResultSet rs;
if( queryParameters.isCallable() && isTypeOf( st, CallableStatement.class ) ) {
final CallableStatement cs = st.unwrap( CallableStatement.class );
rs = getResultSet(
cs,
queryParameters.getRowSelection(),
limitHandler,
queryParameters.hasAutoDiscoverScalarTypes(),
session
);
}
else {
rs = getResultSet(
st,
queryParameters.getRowSelection(),
limitHandler,
queryParameters.hasAutoDiscoverScalarTypes(),
session
);
}
return new SqlStatementWrapper(
st,
rs
);
}
示例20
/**
* Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
* Bind JDBC-style <tt>?</tt> parameters, named parameters, and
* limit parameters.
*/
protected final PreparedStatement prepareQueryStatement(
String sql,
final QueryParameters queryParameters,
final LimitHandler limitHandler,
final boolean scroll,
final SharedSessionContractImplementor session) throws SQLException, HibernateException {
final Dialect dialect = getFactory().getDialect();
final RowSelection selection = queryParameters.getRowSelection();
final boolean useLimit = LimitHelper.useLimit( limitHandler, selection );
final boolean hasFirstRow = LimitHelper.hasFirstRow( selection );
final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
final boolean callable = queryParameters.isCallable();
final ScrollMode scrollMode = getScrollMode( scroll, hasFirstRow, useLimitOffset, queryParameters );
PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareQueryStatement(
sql,
callable,
scrollMode
);
try {
int col = 1;
//TODO: can we limit stored procedures ?!
col += limitHandler.bindLimitParametersAtStartOfQuery( selection, st, col );
if ( callable ) {
col = dialect.registerResultSetOutParameter( (CallableStatement) st, col );
}
col += bindParameterValues( st, queryParameters, col, session );
col += limitHandler.bindLimitParametersAtEndOfQuery( selection, st, col );
limitHandler.setMaxRows( selection, st );
if ( selection != null ) {
if ( selection.getTimeout() != null ) {
st.setQueryTimeout( selection.getTimeout() );
}
if ( selection.getFetchSize() != null ) {
st.setFetchSize( selection.getFetchSize() );
}
}
// handle lock timeout...
LockOptions lockOptions = queryParameters.getLockOptions();
if ( lockOptions != null ) {
if ( lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER ) {
if ( !dialect.supportsLockTimeouts() ) {
if ( LOG.isDebugEnabled() ) {
LOG.debugf(
"Lock timeout [%s] requested but dialect reported to not support lock timeouts",
lockOptions.getTimeOut()
);
}
}
else if ( dialect.isLockTimeoutParameterized() ) {
st.setInt( col++, lockOptions.getTimeOut() );
}
}
}
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Bound [{0}] parameters total", col );
}
}
catch (SQLException | HibernateException e) {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw e;
}
return st;
}
示例21
/**
* Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
* Bind JDBC-style <tt>?</tt> parameters, named parameters, and
* limit parameters.
*/
protected final PreparedStatement prepareQueryStatement(
final String sql,
final QueryParameters queryParameters,
final LimitHandler limitHandler,
final boolean scroll,
final SharedSessionContractImplementor session) throws SQLException, HibernateException {
final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
final RowSelection selection = queryParameters.getRowSelection();
final boolean useLimit = LimitHelper.useLimit( limitHandler, selection );
final boolean hasFirstRow = LimitHelper.hasFirstRow( selection );
final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
final boolean callable = queryParameters.isCallable();
final ScrollMode scrollMode = getScrollMode( scroll, hasFirstRow, useLimitOffset, queryParameters );
final PreparedStatement st = session.getJdbcCoordinator()
.getStatementPreparer().prepareQueryStatement( sql, callable, scrollMode );
try {
int col = 1;
//TODO: can we limit stored procedures ?!
col += limitHandler.bindLimitParametersAtStartOfQuery( selection, st, col );
if (callable) {
col = dialect.registerResultSetOutParameter( (CallableStatement)st, col );
}
col += bindParameterValues( st, queryParameters, col, session );
col += limitHandler.bindLimitParametersAtEndOfQuery( selection, st, col );
limitHandler.setMaxRows( selection, st );
if ( selection != null ) {
if ( selection.getTimeout() != null ) {
st.setQueryTimeout( selection.getTimeout() );
}
if ( selection.getFetchSize() != null ) {
st.setFetchSize( selection.getFetchSize() );
}
}
// handle lock timeout...
final LockOptions lockOptions = queryParameters.getLockOptions();
if ( lockOptions != null ) {
if ( lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER ) {
if ( !dialect.supportsLockTimeouts() ) {
if ( log.isDebugEnabled() ) {
log.debugf(
"Lock timeout [%s] requested but dialect reported to not support lock timeouts",
lockOptions.getTimeOut()
);
}
}
else if ( dialect.isLockTimeoutParameterized() ) {
st.setInt( col++, lockOptions.getTimeOut() );
}
}
}
if ( log.isTraceEnabled() ) {
log.tracev( "Bound [{0}] parameters total", col );
}
}
catch ( SQLException sqle ) {
session.getJdbcCoordinator().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw sqle;
}
catch ( HibernateException he ) {
session.getJdbcCoordinator().getResourceRegistry().release( st );
session.getJdbcCoordinator().afterStatementExecution();
throw he;
}
return st;
}
示例22
@Override
public LimitHandler getLimitHandler() {
return limitHandler;
}
示例23
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例24
@Override
public LimitHandler getDefaultLimitHandler() {
return LIMIT_HANDLER;
}
示例25
@Override
public LimitHandler getLimitHandler() {
return limitHandler;
}
示例26
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例27
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例28
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例29
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}
示例30
@Override
public LimitHandler getLimitHandler() {
return LIMIT_HANDLER;
}