/** * Is a max row limit indicated? * * @param selection The row selection options * * @return Whether a max row limit was indicated */ public static boolean hasMaxRows(RowSelection selection) { return selection != null && selection.getMaxRows() != null && selection.getMaxRows() > 0; }
@Override public int getMaxResults() { getProducer().checkOpen(); // to be JPA compliant this method returns an int - specifically the "magic number" Integer.MAX_VALUE defined by the spec. // For access to the Integer (for checking), use #getQueryOptions#getMaxRows instead return queryOptions.getMaxRows() == null ? Integer.MAX_VALUE : queryOptions.getMaxRows(); }
/** * The maximum number of query results to be retrieved, previously set by * {@link #setMaxResults(int)} or {@link #setHibernateMaxResults(int)}. * <p/> * If the value was not initialized by {@link #setMaxResults(int)} or * {@link #setHibernateMaxResults(int)}, then {@code null} is returned * <p/> * If {@link #setHibernateMaxResults(int)} was called with a value less than * or equal to 0, the value is considered to be uninitialized, and {@code null} * is returned, resulting in no limit on the number of results. * * @return the maximum number of query results, or {@code null} if uninitialized. * * @see #setMaxResults(int) (int) * @see #setHibernateMaxResults(int) (int) * * @deprecated {@link #getMaxResults()} should be used instead. */ @Deprecated default Integer getHibernateMaxResults() { return getQueryOptions().getMaxRows(); }
/** * If we're able to guess a likely size of the results we can optimize allocation * of our datastructures. * Essentially if we detect the user is not using pagination, we attempt to use the FetchSize * as a reasonable hint. If fetch size is not being set either, it is reasonable to expect * that we're going to have a single hit. In such a case it would be tempting to return a constant * of value one, but that's dangerous as it doesn't scale up appropriately for example * with an ArrayList if the guess is wrong. * * @param rowSelection * @return a reasonable size to use for allocation */ @SuppressWarnings("UnnecessaryUnboxing") private int guessResultSize(RowSelection rowSelection) { if ( rowSelection != null ) { final int maxReasonableAllocation = rowSelection.getFetchSize() != null ? rowSelection.getFetchSize().intValue() : 100; if ( rowSelection.getMaxRows() != null && rowSelection.getMaxRows().intValue() > 0 ) { return Math.min( maxReasonableAllocation, rowSelection.getMaxRows().intValue() ); } else if ( rowSelection.getFetchSize() != null && rowSelection.getFetchSize().intValue() > 0 ) { return rowSelection.getFetchSize().intValue(); } } return 7;//magic number guessed as a reasonable default. }
@Override public String processSql(String sql, RowSelection selection) { if ( selection.getMaxRows() == null ) { return sql; } int top = getMaxOrLimit( selection ); if ( top == Integer.MAX_VALUE ) { return sql; } Matcher selectDistinctMatcher = SELECT_DISTINCT_PATTERN.matcher( sql ); if ( selectDistinctMatcher.matches() ) { return insertTop( selectDistinctMatcher, sql, top ); } Matcher selectMatcher = SELECT_PATTERN.matcher( sql ); if ( selectMatcher.matches() ) { return insertTop( selectMatcher, sql, top ); } return sql; }
/** * 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; } } }
@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 ); } } } }
if ( selection != null ) { firstRow = selection.getFirstRow(); maxRows = selection.getMaxRows();
private void doTheLoad(String sql, QueryParameters queryParameters, SharedSessionContractImplementor session) throws SQLException { final RowSelection selection = queryParameters.getRowSelection(); final int maxRows = LimitHelper.hasMaxRows( selection ) ? selection.getMaxRows() : Integer.MAX_VALUE; final List<AfterLoadAction> afterLoadActions = Collections.emptyList(); final SqlStatementWrapper wrapper = executeQueryStatement( sql, queryParameters, false, afterLoadActions, session ); final ResultSet rs = wrapper.getResultSet(); final Statement st = wrapper.getStatement(); try { processResultSet( rs, queryParameters, session, true, null, maxRows, afterLoadActions ); } finally { session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st ); session.getJdbcCoordinator().afterStatementExecution(); } }
final RowSelection selection = queryParameters.getRowSelection(); if ( LimitHelper.hasMaxRows( selection ) ) { maxRows = selection.getMaxRows(); LOG.tracef( "Limiting ResultSet processing to just %s rows", maxRows );
private List doTheLoad(String sql, QueryParameters queryParameters, SharedSessionContractImplementor session) throws SQLException { final RowSelection selection = queryParameters.getRowSelection(); final int maxRows = LimitHelper.hasMaxRows( selection ) ? selection.getMaxRows() : Integer.MAX_VALUE; final List<AfterLoadAction> afterLoadActions = new ArrayList<>(); final SqlStatementWrapper wrapper = executeQueryStatement( sql, queryParameters, false, afterLoadActions, session ); final ResultSet rs = wrapper.getResultSet(); final Statement st = wrapper.getStatement(); try { return processResultSet( rs, queryParameters, session, false, null, maxRows, afterLoadActions ); } finally { session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st ); session.getJdbcCoordinator().afterStatementExecution(); } } }
selection.getMaxRows() : Integer.MAX_VALUE;
? 0 : queryParameters.getRowSelection().getFirstRow(); final int max = queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows(); for ( final Object result : tmp ) { if ( !distinction.add( result ) ) {
? 0 : queryParameters.getRowSelection().getFirstRow(); int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows(); List tmp = new ArrayList(); IdentitySet distinction = new IdentitySet();
private void fillInNamedQueryBuilder(NamedQueryDefinitionBuilder builder, org.hibernate.query.Query query) { builder.setQuery( query.getQueryString() ) .setComment( query.getComment() ) .setCacheable( query.isCacheable() ) .setCacheRegion( query.getCacheRegion() ) .setCacheMode( query.getCacheMode() ) .setReadOnly( query.isReadOnly() ) .setFlushMode( query.getHibernateFlushMode() ); if ( query.getQueryOptions().getFirstRow() != null ) { builder.setFirstResult( query.getQueryOptions().getFirstRow() ); } if ( query.getQueryOptions().getMaxRows() != null ) { builder.setMaxResults( query.getQueryOptions().getMaxRows() ); } if ( query.getQueryOptions().getTimeout() != null ) { builder.setTimeout( query.getQueryOptions().getTimeout() ); } if ( query.getQueryOptions().getFetchSize() != null ) { builder.setFetchSize( query.getQueryOptions().getFetchSize() ); } }
/** * Is a max row limit indicated? * * @param selection The row selection options * * @return Whether a max row limit was indicated */ public static boolean hasMaxRows(RowSelection selection) { return selection != null && selection.getMaxRows() != null && selection.getMaxRows() > 0; }
/** * Use JDBC API to limit the number of rows returned by the SQL query if necessary */ private void setMaxRows( final PreparedStatement st, final RowSelection selection) throws SQLException { if ( hasMaxRows( selection ) ) { st.setMaxRows( selection.getMaxRows() + interpretFirstRow( getFirstRow( selection ) ) ); } }
/** * Use JDBC API to limit the number of rows returned by the SQL query if necessary */ private void setMaxRows( final PreparedStatement st, final RowSelection selection) throws SQLException { if ( hasMaxRows( selection ) ) { st.setMaxRows( selection.getMaxRows() + interpretFirstRow( getFirstRow( selection ) ) ); } }
public JoinFetchUsage(QueryParameters queryParameters) { boolean collect = containsCollectionFetches(); boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits(); if (collect && hasLimit) { firstRow = queryParameters.getRowSelection().getFirstRow(); maxRows = queryParameters.getRowSelection().getMaxRows(); time = System.currentTimeMillis(); } }
@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 ); } } } }