Search in sources :

Example 1 with Limit

use of org.hibernate.query.spi.Limit in project hibernate-orm by hibernate.

the class JdbcSelectExecutorStandardImpl method getScrollContext.

/*
		When `Query#scroll()` is call the query is not executed immediately, a new ExecutionContext with the values of the `persistenceContext.isDefaultReadOnly()` and of the `queryOptions.isReadOnly()`
		set at the moment of the Query#scroll() call is created in order to use it when the query will be executed.
	 */
private ExecutionContext getScrollContext(ExecutionContext context, PersistenceContext persistenceContext) {
    final QueryOptions queryOptions = context.getQueryOptions();
    final Boolean readOnly;
    if (queryOptions.isReadOnly() == null) {
        readOnly = persistenceContext.isDefaultReadOnly();
    } else {
        readOnly = queryOptions.isReadOnly();
    }
    final Integer timeout = queryOptions.getTimeout();
    final FlushMode flushMode = queryOptions.getFlushMode();
    final AppliedGraph appliedGraph = queryOptions.getAppliedGraph();
    final TupleTransformer<?> tupleTransformer = queryOptions.getTupleTransformer();
    final ResultListTransformer<?> resultListTransformer = queryOptions.getResultListTransformer();
    final Boolean resultCachingEnabled = queryOptions.isResultCachingEnabled();
    final CacheRetrieveMode cacheRetrieveMode = queryOptions.getCacheRetrieveMode();
    final CacheStoreMode cacheStoreMode = queryOptions.getCacheStoreMode();
    final String resultCacheRegionName = queryOptions.getResultCacheRegionName();
    final LockOptions lockOptions = queryOptions.getLockOptions();
    final String comment = queryOptions.getComment();
    final List<String> databaseHints = queryOptions.getDatabaseHints();
    final Integer fetchSize = queryOptions.getFetchSize();
    final Limit limit = queryOptions.getLimit();
    return new ExecutionContext() {

        @Override
        public QueryOptions getQueryOptions() {
            return new QueryOptions() {

                @Override
                public Integer getTimeout() {
                    return timeout;
                }

                @Override
                public FlushMode getFlushMode() {
                    return flushMode;
                }

                @Override
                public Boolean isReadOnly() {
                    return readOnly;
                }

                @Override
                public AppliedGraph getAppliedGraph() {
                    return appliedGraph;
                }

                @Override
                public TupleTransformer<?> getTupleTransformer() {
                    return tupleTransformer;
                }

                @Override
                public ResultListTransformer<?> getResultListTransformer() {
                    return resultListTransformer;
                }

                @Override
                public Boolean isResultCachingEnabled() {
                    return resultCachingEnabled;
                }

                @Override
                public CacheRetrieveMode getCacheRetrieveMode() {
                    return cacheRetrieveMode;
                }

                @Override
                public CacheStoreMode getCacheStoreMode() {
                    return cacheStoreMode;
                }

                @Override
                public String getResultCacheRegionName() {
                    return resultCacheRegionName;
                }

                @Override
                public LockOptions getLockOptions() {
                    return lockOptions;
                }

                @Override
                public String getComment() {
                    return comment;
                }

                @Override
                public List<String> getDatabaseHints() {
                    return databaseHints;
                }

                @Override
                public Integer getFetchSize() {
                    return fetchSize;
                }

                @Override
                public Limit getLimit() {
                    return limit;
                }
            };
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return context.getQueryParameterBindings();
        }

        @Override
        public Callback getCallback() {
            return context.getCallback();
        }

        @Override
        public SharedSessionContractImplementor getSession() {
            return context.getSession();
        }
    };
}
Also used : CacheRetrieveMode(jakarta.persistence.CacheRetrieveMode) LockOptions(org.hibernate.LockOptions) CacheStoreMode(jakarta.persistence.CacheStoreMode) QueryOptions(org.hibernate.query.spi.QueryOptions) AppliedGraph(org.hibernate.graph.spi.AppliedGraph) FlushMode(org.hibernate.FlushMode) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Limit(org.hibernate.query.spi.Limit)

Example 2 with Limit

use of org.hibernate.query.spi.Limit in project hibernate-orm by hibernate.

the class AggregatedSelectQueryPlanImpl method performList.

@Override
public List<R> performList(DomainQueryExecutionContext executionContext) {
    final Limit effectiveLimit = executionContext.getQueryOptions().getEffectiveLimit();
    final int maxRowsJpa = effectiveLimit.getMaxRowsJpa();
    if (maxRowsJpa == 0) {
        return Collections.emptyList();
    }
    int elementsToSkip = effectiveLimit.getFirstRowJpa();
    final List<R> overallResults = new ArrayList<>();
    for (SelectQueryPlan<R> aggregatedQueryPlan : aggregatedQueryPlans) {
        final List<R> list = aggregatedQueryPlan.performList(executionContext);
        final int size = list.size();
        if (size <= elementsToSkip) {
            // More elements to skip than the collection size
            elementsToSkip -= size;
            continue;
        }
        final int availableElements = size - elementsToSkip;
        if (overallResults.size() + availableElements >= maxRowsJpa) {
            // This result list is the last one i.e. fulfills the limit
            final int end = elementsToSkip + (maxRowsJpa - overallResults.size());
            for (int i = elementsToSkip; i < end; i++) {
                overallResults.add(list.get(i));
            }
            break;
        } else if (elementsToSkip > 0) {
            // We can skip a part of this result list
            for (int i = availableElements; i < size; i++) {
                overallResults.add(list.get(i));
            }
            elementsToSkip = 0;
        } else {
            overallResults.addAll(list);
        }
    }
    return overallResults;
}
Also used : ArrayList(java.util.ArrayList) Limit(org.hibernate.query.spi.Limit)

Example 3 with Limit

use of org.hibernate.query.spi.Limit in project hibernate-orm by hibernate.

the class DerbyDialectTestCase method toRowSelection.

private Limit toRowSelection(int firstRow, int maxRows) {
    Limit selection = new Limit();
    selection.setFirstRow(firstRow);
    selection.setMaxRows(maxRows);
    return selection;
}
Also used : Limit(org.hibernate.query.spi.Limit)

Example 4 with Limit

use of org.hibernate.query.spi.Limit in project hibernate-orm by hibernate.

the class SQLServer2005DialectTestCase method testGetLimitStringUsingCTEQueryWithOffset.

@Test
@TestForIssue(jiraKey = "HHH-8916")
public void testGetLimitStringUsingCTEQueryWithOffset() {
    Limit selection = toRowSelection(1, 5);
    // test non-top based CTE with single CTE query_ definition with no odd formatting
    final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a";
    assertEquals("WITH a (c1, c2) AS (SELECT c1, c2 FROM t) , query_ as (select row_.*,row_number() over " + "(order by current_timestamp) as rownumber_ from (SELECT c1 as col0_, c2 as col1_ " + "FROM a) row_) select col0_,col1_ from query_ where rownumber_>=? " + "and rownumber_<?", dialect.getLimitHandler().processSql(query1, selection));
    // test non-top based CTE with single CTE query_ definition and various tab, newline spaces
    final String query2 = "  \n\tWITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT c1, c2 FROM a";
    assertEquals("WITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\n, query_ as (select row_.*,row_number()" + " over (order by current_timestamp) as rownumber_ from (SELECT c1 as col0_, c2 " + "as col1_ FROM a) row_) select col0_,col1_ from query_ where rownumber_>=" + "? and rownumber_<?", dialect.getLimitHandler().processSql(query2, selection));
    // test non-top based CTE with multiple CTE query_ definitions with no odd formatting
    final String query3 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + " SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1";
    assertEquals("WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2)  , query_ as (" + "select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + "SELECT c1 as col0_, c2 as col1_, b1 as col2_, b2 as col3_ FROM t1, t2 WHERE t1.c1 = t2.b1) row_)" + " select col0_,col1_,col2_,col3_ from query_ where rownumber_>=? and rownumber_<?", dialect.getLimitHandler().processSql(query3, selection));
    // test top-based CTE with multiple CTE query_ definitions and various tab, newline spaces
    final String query4 = "  \n\r\tWITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, " + "b2 FROM t2)    SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1";
    assertEquals("WITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, b2 FROM t2)    , query_ as (" + "select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + "SELECT c1 as col0_, c2 as col1_, b1 as col2_, b2 as col3_ FROM t1, t2 WHERE t1.c1 = t2.b1) row_)" + " select col0_,col1_,col2_,col3_ from query_ where rownumber_>=? and rownumber_<?", dialect.getLimitHandler().processSql(query4, selection));
}
Also used : Limit(org.hibernate.query.spi.Limit) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with Limit

use of org.hibernate.query.spi.Limit in project hibernate-orm by hibernate.

the class DB2DialectTestCase method testIntegerOverflowForMaxResults.

@Test
@TestForIssue(jiraKey = "HHH-12369")
public void testIntegerOverflowForMaxResults() {
    Limit rowSelection = new Limit();
    rowSelection.setFirstRow(1);
    rowSelection.setMaxRows(Integer.MAX_VALUE);
    String sql = dialect.getLimitHandler().processSql("select a.id from tbl_a a order by a.id", rowSelection);
    assertTrue("Integer overflow for max rows in: " + sql, sql.contains("fetch first 2147483647 rows only"));
}
Also used : Limit(org.hibernate.query.spi.Limit) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Limit (org.hibernate.query.spi.Limit)13 Test (org.junit.jupiter.api.Test)4 TestForIssue (org.hibernate.testing.TestForIssue)3 Test (org.junit.Test)3 CacheRetrieveMode (jakarta.persistence.CacheRetrieveMode)1 CacheStoreMode (jakarta.persistence.CacheStoreMode)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 FlushMode (org.hibernate.FlushMode)1 LockOptions (org.hibernate.LockOptions)1 AppliedGraph (org.hibernate.graph.spi.AppliedGraph)1 FilterJdbcParameter (org.hibernate.internal.FilterJdbcParameter)1 QueryOptions (org.hibernate.query.spi.QueryOptions)1 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)1 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)1