Example usage for org.hibernate.engine.spi RowSelection getMaxRows

List of usage examples for org.hibernate.engine.spi RowSelection getMaxRows

Introduction

In this page you can find the example usage for org.hibernate.engine.spi RowSelection getMaxRows.

Prototype

public Integer getMaxRows() 

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

private QueryParamEntry createQueryParameters(EntityManager em, List<Query> participatingQueries,
        List<String> queryStrings, Set<String> querySpaces) {
    List<ParameterSpecification> parameterSpecifications = new ArrayList<ParameterSpecification>();

    List<Type> types = new ArrayList<Type>();
    List<Object> values = new ArrayList<Object>();
    Map<String, TypedValue> namedParams = new LinkedHashMap<String, TypedValue>();
    Serializable collectionKey = null;
    LockOptions lockOptions = new LockOptions();
    RowSelection rowSelection = new RowSelection();
    boolean readOnly = false; // TODO: readonly?
    boolean cacheable = false; // TODO: cacheable?
    String cacheRegion = null;//from  w  w w .  j ava2s  . c o m
    String comment = null;
    List<String> queryHints = null;

    for (QueryParamEntry queryParamEntry : getQueryParamEntries(em, participatingQueries, querySpaces)) {
        queryStrings.add(queryParamEntry.queryString);

        QueryParameters participatingQueryParameters = queryParamEntry.queryParameters;
        // Merge parameters
        Collections.addAll(types, participatingQueryParameters.getPositionalParameterTypes());
        Collections.addAll(values, participatingQueryParameters.getPositionalParameterValues());
        namedParams.putAll(participatingQueryParameters.getNamedParameters());
        parameterSpecifications.addAll(queryParamEntry.specifications);

        // Merge row selections
        if (participatingQueryParameters.hasRowSelection()) {
            RowSelection original = queryParamEntry.queryParameters.getRowSelection();
            // Check for defaults

            /***************************************************************************
             * TODO: Either we do it like this, or let these values be passed in separately
             **************************************************************************/

            if (rowSelection.getFirstRow() == null || rowSelection.getFirstRow() < 1) {
                rowSelection.setFirstRow(original.getFirstRow());
            } else if (original.getFirstRow() != null && original.getFirstRow() > 0
                    && !original.getFirstRow().equals(rowSelection.getFirstRow())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
            if (rowSelection.getMaxRows() == null || rowSelection.getMaxRows() == Integer.MAX_VALUE) {
                rowSelection.setMaxRows(original.getMaxRows());
            } else if (original.getMaxRows() != null && original.getMaxRows() != Integer.MAX_VALUE
                    && !original.getMaxRows().equals(rowSelection.getMaxRows())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }

            if (rowSelection.getFetchSize() == null) {
                rowSelection.setFetchSize(original.getFetchSize());
            } else if (original.getFetchSize() != null
                    && !original.getFetchSize().equals(rowSelection.getFetchSize())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
            if (rowSelection.getTimeout() == null) {
                rowSelection.setTimeout(original.getTimeout());
            } else if (original.getTimeout() != null
                    && !original.getTimeout().equals(rowSelection.getTimeout())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
        }

        // Merge lock options
        LockOptions originalLockOptions = participatingQueryParameters.getLockOptions();
        if (originalLockOptions.getScope()) {
            lockOptions.setScope(true);
        }
        if (originalLockOptions.getLockMode() != LockMode.NONE) {
            if (lockOptions.getLockMode() != LockMode.NONE
                    && lockOptions.getLockMode() != originalLockOptions.getLockMode()) {
                throw new IllegalStateException("Multiple different lock modes!");
            }
            lockOptions.setLockMode(originalLockOptions.getLockMode());
        }
        if (originalLockOptions.getTimeOut() != -1) {
            if (lockOptions.getTimeOut() != -1
                    && lockOptions.getTimeOut() != originalLockOptions.getTimeOut()) {
                throw new IllegalStateException("Multiple different lock timeouts!");
            }
            lockOptions.setTimeOut(originalLockOptions.getTimeOut());
        }
        @SuppressWarnings("unchecked")
        Iterator<Map.Entry<String, LockMode>> aliasLockIter = participatingQueryParameters.getLockOptions()
                .getAliasLockIterator();
        while (aliasLockIter.hasNext()) {
            Map.Entry<String, LockMode> entry = aliasLockIter.next();
            lockOptions.setAliasSpecificLockMode(entry.getKey(), entry.getValue());
        }
    }

    QueryParameters queryParameters = hibernateAccess.createQueryParameters(
            types.toArray(new Type[types.size()]), values.toArray(new Object[values.size()]), namedParams,
            lockOptions, rowSelection, true, readOnly, cacheable, cacheRegion, comment, queryHints,
            collectionKey == null ? null : new Serializable[] { collectionKey });

    return new QueryParamEntry(null, queryParameters, parameterSpecifications);
}

From source file:com.blazebit.persistence.integration.hibernate.Hibernate43LimitHandler.java

License:Apache License

public Hibernate43LimitHandler(Dialect dialect, DbmsDialect dbmsDialect, String sql, RowSelection selection) {
    this.limitHandler = dbmsDialect.createLimitHandler();
    this.sql = sql;

    if (selection == null || selection.getMaxRows() == null
            || selection.getMaxRows().intValue() == Integer.MAX_VALUE) {
        this.limit = null;
    } else {//from  w  w  w . jav  a  2  s. c  o m
        this.limit = selection.getMaxRows();
    }
    if (selection == null || selection.getFirstRow() == null || selection.getFirstRow().intValue() < 1) {
        this.offset = null;
    } else {
        this.offset = selection.getFirstRow();
    }
}

From source file:com.blazebit.persistence.integration.hibernate.Hibernate4LimitHandler.java

License:Apache License

public Hibernate4LimitHandler(Dialect dialect, DbmsDialect dbmsDialect, String sql, RowSelection selection) {
    this.limitHandler = dbmsDialect.createLimitHandler();
    this.sql = sql;

    if (selection == null || selection.getMaxRows() == null
            || selection.getMaxRows().intValue() == Integer.MAX_VALUE) {
        this.limit = null;
    } else {//  w  ww . j ava2s  . co  m
        this.limit = selection.getMaxRows();
    }
    if (selection == null || selection.getFirstRow() == null || selection.getFirstRow().intValue() < 1) {
        this.offset = null;
    } else {
        this.offset = selection.getFirstRow();
    }
}

From source file:com.blazebit.persistence.integration.hibernate.Hibernate52LimitHandler.java

License:Apache License

@Override
public String processSql(String sql, RowSelection selection) {
    if (selection == null || selection.getMaxRows() == null
            || selection.getMaxRows().intValue() == Integer.MAX_VALUE) {
        this.limit = null;
    } else {/*from   www  .  j av a 2s  . co  m*/
        this.limit = selection.getMaxRows();
    }
    if (selection == null || selection.getFirstRow() == null || selection.getFirstRow().intValue() < 1) {
        this.offset = null;
    } else {
        this.offset = selection.getFirstRow();
    }
    return limitHandler.applySql(sql, false, limit, offset);
}

From source file:com.sam.moca.server.db.translate.SQLServerLimitHandler.java

License:Open Source License

private String handlePostProcessing(SqlServerPagingHandler handler, RowSelection selection, BindList args) {
    StringBuilder sql = new StringBuilder(handler.getProcessedSql());

    // At this point we have the Hibernate generated SQL that uses bindings like:
    // where __hibernate_row_nr__ >= ? and hibernate_row_nr__ < ?
    // We want to replace those ? tokens with bind variables MOCA can work with in the form
    // :<bind_variable_name> - then add this to our bind list

    // First replace the start row inequality
    int tokenIndex = sql.lastIndexOf(HIBERNATE_START_INQUALITY) + HIBERNATE_START_INQUALITY.length();
    sql.replace(tokenIndex - TOKEN.length(), tokenIndex, ROW_START_TOKEN);
    args.add(ROW_START_IDENTIFIER, selection.getFirstRow());

    // Next replace the max rows inequality if needed
    if (selection.getMaxRows() > 0) {
        // Handle the end row bind variable replacement
        // Find the row limit inequality: __hibernate_row_nr__ < ?
        // Replace the ? with our bind variable and the calculated end row
        tokenIndex = sql.lastIndexOf(HIBERNATE_END_INEQUALITY) + HIBERNATE_END_INEQUALITY.length();
        sql.replace(tokenIndex - TOKEN.length(), tokenIndex, ROW_END_TOKEN);
        args.add(ROW_END_IDENTIFER, selection.getFirstRow() + selection.getMaxRows());
    } else {//from   w  w w. j  av a 2 s .co  m
        // Max rows is 0 (unlimited) - remove the row limit inequality
        tokenIndex = sql.lastIndexOf(HIBERNATE_END_INEQUALITY);
        sql.replace(tokenIndex - " AND ".length(), sql.length(), "");
    }

    return sql.toString();
}