/** * Should limit be applied? * * @param limitHandler The limit handler * @param selection The row selection * * @return Whether limiting is indicated */ public static boolean useLimit(LimitHandler limitHandler, RowSelection selection) { return limitHandler.supportsLimit() && hasMaxRows( selection ); }
@Override public int bindLimitParametersAtEndOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException { return LimitHelper.hasFirstRow( selection ) ? super.bindLimitParametersAtEndOfQuery( selection, statement, index ) : 0; }
@Override public String processSql(String sql, RowSelection selection) { // SQLServer mandates the following rules to use OFFSET/LIMIT // * An 'ORDER BY' is required // * The 'OFFSET ...' clause is mandatory, cannot use 'FETCH ...' by itself. // * The 'TOP' clause isn't permitted with LIMIT/OFFSET. if ( hasOrderBy( sql ) ) { if ( !LimitHelper.useLimit( this, selection ) ) { return sql; } return applyOffsetFetch( selection, sql, getInsertPosition( sql ) ); } return super.processSql( sql, selection ); }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow( selection ); return sql + (hasOffset ? " limit ?, ?" : " limit ?"); }
@Override public int bindLimitParametersAtEndOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException { if ( usedOffsetFetch && !LimitHelper.hasFirstRow( selection ) ) { // apply just the max value when offset fetch applied statement.setInt( index, getMaxOrLimit( selection ) ); return 1; } return super.bindLimitParametersAtEndOfQuery( selection, statement, index ); }
@Override public String processSql(String sql, RowSelection selection) { if (LimitHelper.useLimit( this, selection )) { return sql + (LimitHelper.hasFirstRow( selection ) ? " offset ? rows fetch next ? rows only" : " fetch first ? rows only"); } else { // or return unaltered SQL return sql; } }
@Override public int bindLimitParametersAtStartOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException { return bindLimitParametersFirst() ? bindLimitParameters( selection, statement, index ) : 0; }
@Override public boolean useMaxForLimit() { // when using the offset fetch clause, the max value is passed as-is. // SQLServer2005LimitHandler uses start + max values. return usedOffsetFetch ? false : super.useMaxForLimit(); }
/** * Does this handler support bind variables (i.e., prepared statement * parameters) for its limit/offset? * * @return True if bind variables can be used; false otherwise. */ public boolean supportsVariableLimit() { return supportsLimit(); }
/** * Get the select start position. * * @param sb the string builder sql. * @param offset the starting offset in buffer. * @return the position where {@code select} is found. */ private int getSelectStartPosition(StringBuilder sb, int offset) { return shallowIndexOfPattern( sb, SELECT_PATTERN, offset ); }
/** * Is a first row limit indicated? * * @param selection The row selection options * * @return Whether a first row limit in indicated */ public static boolean hasFirstRow(RowSelection selection) { return getFirstRow( selection ) > 0; }
@Override public int convertToFirstRowValue(int zeroBasedFirstResult) { // When using the offset/fetch clause, the first row is passed as-is // SQLServer2005LimitHandler uses zeroBasedFirstResult + 1 if ( usedOffsetFetch ) { return zeroBasedFirstResult; } return super.convertToFirstRowValue( zeroBasedFirstResult ); }
@Override protected LimitHandler getDefaultLimitHandler() { return new SQLServer2012LimitHandler(); } }
/** * Returns the delegate managing LIMIT clause. * * @return LIMIT clause delegate. */ public LimitHandler getLimitHandler() { return new LegacyLimitHandler( this ); }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow( selection ); return sql + ( hasOffset ? " limit ? offset ?" : " limit ?" ); }
@Override public int bindLimitParametersAtEndOfQuery(RowSelection selection, PreparedStatement statement, int index) throws SQLException { return !bindLimitParametersFirst() ? bindLimitParameters( selection, statement, index ) : 0; }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow( selection ); return sql + (hasOffset ? " limit ? offset ?" : " limit ?"); }
private String getOffsetFetch(RowSelection selection) { if ( !LimitHelper.hasFirstRow( selection ) ) { return " offset 0 rows fetch next ? rows only"; } return " offset ? rows fetch next ? rows only"; }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow( selection ); return hasOffset ? sql + " rows ? to ?" : sql + " rows ?"; }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow( selection ); return sql + (hasOffset ? " limit ? offset ?" : " limit ?"); }