/** * 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; }
/** * Advance the cursor to the first required row of the <tt>ResultSet</tt> */ protected void advance(final ResultSet rs, final RowSelection selection) throws SQLException { final int firstRow = LimitHelper.getFirstRow( selection ); if ( firstRow != 0 ) { if ( getFactory().getSettings().isScrollableResultSetsEnabled() ) { // we can go straight to the first required row rs.absolute( firstRow ); } else { // we need to step through the rows one row at a time (slow) for ( int m = 0; m < firstRow; m++ ) { rs.next(); } } } }
/** * Advance the cursor to the first required row of the <tt>ResultSet</tt> */ private void advance(final ResultSet rs, final RowSelection selection) throws SQLException { final int firstRow = LimitHelper.getFirstRow( selection ); if ( firstRow != 0 ) { if ( getFactory().getSessionFactoryOptions().isScrollableResultSetsEnabled() ) { // we can go straight to the first required row rs.absolute( firstRow ); } else { // we need to step through the rows one row at a time (slow) for ( int m = 0; m < firstRow; m++ ) { rs.next(); } } } }
/** * 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). * * @param selection the selection criteria for rows. * * @return The appropriate value to bind into the limit clause. */ protected final int getMaxOrLimit(RowSelection selection) { final int firstRow = convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); final int lastRow = selection.getMaxRows(); final int maxRows = useMaxForLimit() ? lastRow + firstRow : lastRow; // Use Integer.MAX_VALUE on overflow if ( maxRows < 0 ) { return Integer.MAX_VALUE; } else { return maxRows; } } }
/** * Default implementation of binding parameter values needed by the LIMIT clause. * * @param selection the selection criteria for rows. * @param statement Statement to which to bind limit parameter values. * @param index Index from which to start binding. * @return The number of parameter values bound. * @throws SQLException Indicates problems binding parameter values. */ protected final int bindLimitParameters(RowSelection selection, PreparedStatement statement, int index) throws SQLException { if ( !supportsVariableLimit() || !LimitHelper.hasMaxRows( selection ) ) { return 0; } final int firstRow = convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); final int lastRow = getMaxOrLimit( selection ); final boolean hasFirstRow = supportsLimitOffset() && ( firstRow > 0 || forceLimitUsage() ); final boolean reverse = bindLimitParametersInReverseOrder(); if ( hasFirstRow ) { statement.setInt( index + ( reverse ? 1 : 0 ), firstRow ); } statement.setInt( index + ( reverse || !hasFirstRow ? 0 : 1 ), lastRow ); return hasFirstRow ? 2 : 1; }
@Override public void setMaxRows(RowSelection selection, PreparedStatement statement) throws SQLException { if ( LimitHelper.hasMaxRows( selection ) ) { int maxRows = selection.getMaxRows() + convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); // Use Integer.MAX_VALUE on overflow if ( maxRows < 0 ) { statement.setMaxRows( Integer.MAX_VALUE ); } else { statement.setMaxRows( maxRows ); } } } }
@Override public String processSql(String sql, RowSelection selection) { final boolean useLimitOffset = supportsLimit() && supportsLimitOffset() && LimitHelper.hasFirstRow( selection ) && LimitHelper.hasMaxRows( selection ); return dialect.getLimitString( sql, useLimitOffset ? LimitHelper.getFirstRow( selection ) : 0, getMaxOrLimit( selection ) ); } }
/** * 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; }
/** * 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). * * @param selection the selection criteria for rows. * * @return The appropriate value to bind into the limit clause. */ protected final int getMaxOrLimit(RowSelection selection) { final int firstRow = convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); final int lastRow = selection.getMaxRows(); final int maxRows = useMaxForLimit() ? lastRow + firstRow : lastRow; // Use Integer.MAX_VALUE on overflow if ( maxRows < 0 ) { return Integer.MAX_VALUE; } else { return maxRows; } } }
/** * Default implementation of binding parameter values needed by the LIMIT clause. * * @param selection the selection criteria for rows. * @param statement Statement to which to bind limit parameter values. * @param index Index from which to start binding. * @return The number of parameter values bound. * @throws SQLException Indicates problems binding parameter values. */ protected final int bindLimitParameters(RowSelection selection, PreparedStatement statement, int index) throws SQLException { if ( !supportsVariableLimit() || !LimitHelper.hasMaxRows( selection ) ) { return 0; } final int firstRow = convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); final int lastRow = getMaxOrLimit( selection ); final boolean hasFirstRow = supportsLimitOffset() && ( firstRow > 0 || forceLimitUsage() ); final boolean reverse = bindLimitParametersInReverseOrder(); if ( hasFirstRow ) { statement.setInt( index + ( reverse ? 1 : 0 ), firstRow ); } statement.setInt( index + ( reverse || !hasFirstRow ? 0 : 1 ), lastRow ); return hasFirstRow ? 2 : 1; }
@Override public void setMaxRows(RowSelection selection, PreparedStatement statement) throws SQLException { if ( LimitHelper.hasMaxRows( selection ) ) { int maxRows = selection.getMaxRows() + convertToFirstRowValue( LimitHelper.getFirstRow( selection ) ); // Use Integer.MAX_VALUE on overflow if ( maxRows < 0 ) { statement.setMaxRows( Integer.MAX_VALUE ); } else { statement.setMaxRows( maxRows ); } } } }
@Override public String processSql(String sql, RowSelection selection) { final boolean useLimitOffset = supportsLimit() && supportsLimitOffset() && LimitHelper.hasFirstRow( selection ) && LimitHelper.hasMaxRows( selection ); return dialect.getLimitString( sql, useLimitOffset ? LimitHelper.getFirstRow( selection ) : 0, getMaxOrLimit( selection ) ); } }