Search in sources :

Example 1 with KeysetMode

use of com.blazebit.persistence.impl.keyset.KeysetMode in project blaze-persistence by Blazebit.

the class PaginatedCriteriaBuilderImpl method updateKeysetMode.

private void updateKeysetMode() {
    KeysetMode oldMode = this.keysetMode;
    this.keysetMode = KeysetPaginationHelper.getKeysetMode(keysetPage, entityId, firstResult, maxResults);
    if (keysetMode == KeysetMode.NONE) {
        this.keysetManager.setKeysetLink(null);
    } else if (keysetMode == KeysetMode.NEXT) {
        this.keysetManager.setKeysetLink(new SimpleKeysetLink(keysetPage.getHighest(), keysetMode));
    } else {
        this.keysetManager.setKeysetLink(new SimpleKeysetLink(keysetPage.getLowest(), keysetMode));
    }
    if (keysetMode != oldMode) {
        prepareForModification(ClauseType.WHERE);
    }
}
Also used : KeysetMode(com.blazebit.persistence.impl.keyset.KeysetMode) SimpleKeysetLink(com.blazebit.persistence.impl.keyset.SimpleKeysetLink)

Example 2 with KeysetMode

use of com.blazebit.persistence.impl.keyset.KeysetMode in project blaze-persistence by Blazebit.

the class AbstractFullQueryBuilder method applyPageIdQueryInto.

protected void applyPageIdQueryInto(AbstractCommonQueryBuilder<?, ?, ?, ?, ?> newBuilder, KeysetPage keysetPage, int firstResult, int maxResults, ResolvedExpression[] identifierExpressionsToUse, boolean withAlias) {
    ExpressionCopyContext expressionCopyContext = newBuilder.applyFrom(this, true, false, false, false, ID_QUERY_GROUP_BY_CLAUSE_EXCLUSIONS, getIdentifierExpressionsToUseNonRootJoinNodes(identifierExpressionsToUse), new IdentityHashMap<JoinManager, JoinManager>(), ExpressionCopyContext.EMPTY);
    newBuilder.setFirstResult(firstResult);
    newBuilder.setMaxResults(maxResults);
    // Paginated criteria builders always need the last order by expression to be unique
    List<OrderByExpression> orderByExpressions = orderByManager.getOrderByExpressions(false, whereManager.rootPredicate.getPredicate(), hasGroupBy ? Arrays.asList(getIdentifierExpressions()) : Collections.<ResolvedExpression>emptyList(), null);
    if (!orderByExpressions.get(orderByExpressions.size() - 1).isResultUnique()) {
        throw new IllegalStateException("The order by items of the query builder are not guaranteed to produce unique tuples! Consider also ordering by the entity identifier!");
    }
    if (keysetPage != null) {
        KeysetMode keysetMode = KeysetPaginationHelper.getKeysetMode(keysetPage, null, firstResult, maxResults);
        if (keysetMode == KeysetMode.NONE) {
            newBuilder.keysetManager.setKeysetLink(null);
        } else if (keysetMode == KeysetMode.NEXT) {
            newBuilder.keysetManager.setKeysetLink(new SimpleKeysetLink(keysetPage.getHighest(), keysetMode));
        } else {
            newBuilder.keysetManager.setKeysetLink(new SimpleKeysetLink(keysetPage.getLowest(), keysetMode));
        }
        newBuilder.keysetManager.initialize(orderByExpressions);
    }
    // Applying order by items needs special care for page id queries because we have to re-alias the items to avoid collisions
    Map<String, Integer> identifierExpressionStringMap = new HashMap<>(identifierExpressionsToUse.length);
    for (int i = 0; i < identifierExpressionsToUse.length; i++) {
        identifierExpressionStringMap.put(identifierExpressionsToUse[i].getExpressionString(), i);
    }
    String[] identifierToUseSelectAliases = newBuilder.orderByManager.applyFrom(orderByManager, identifierExpressionStringMap);
    if (withAlias) {
        for (int i = 0; i < identifierExpressionsToUse.length; i++) {
            List<Expression> args = new ArrayList<>(2);
            args.add(identifierExpressionsToUse[i].getExpression().copy(expressionCopyContext));
            args.add(new StringLiteral(ColumnTruncFunction.SYNTHETIC_COLUMN_PREFIX + i));
            newBuilder.selectManager.select(new FunctionExpression(AliasFunction.FUNCTION_NAME, args), identifierToUseSelectAliases[i]);
        }
    } else {
        for (int i = 0; i < identifierExpressionsToUse.length; i++) {
            newBuilder.selectManager.select(identifierExpressionsToUse[i].getExpression().copy(expressionCopyContext), identifierToUseSelectAliases[i]);
        }
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExpressionCopyContext(com.blazebit.persistence.parser.expression.ExpressionCopyContext) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) KeysetMode(com.blazebit.persistence.impl.keyset.KeysetMode) SimpleKeysetLink(com.blazebit.persistence.impl.keyset.SimpleKeysetLink)

Aggregations

KeysetMode (com.blazebit.persistence.impl.keyset.KeysetMode)2 SimpleKeysetLink (com.blazebit.persistence.impl.keyset.SimpleKeysetLink)2 Expression (com.blazebit.persistence.parser.expression.Expression)1 ExpressionCopyContext (com.blazebit.persistence.parser.expression.ExpressionCopyContext)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1 StringLiteral (com.blazebit.persistence.parser.expression.StringLiteral)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1