Search in sources :

Example 11 with Sort

use of io.micronaut.data.model.Sort in project micronaut-data by micronaut-projects.

the class DefaultMongoPreparedQuery method applyPageable.

private int applyPageable(Pageable pageable, List<Bson> pipeline) {
    int limit = 0;
    if (pageable != Pageable.UNPAGED) {
        int skip = (int) pageable.getOffset();
        limit = pageable.getSize();
        Sort pageableSort = pageable.getSort();
        if (pageableSort.isSorted()) {
            Bson sort = pageableSort.getOrderBy().stream().map(order -> order.isAscending() ? Sorts.ascending(order.getProperty()) : Sorts.descending(order.getProperty())).collect(Collectors.collectingAndThen(Collectors.toList(), Sorts::orderBy));
            BsonDocument sortStage = new BsonDocument().append("$sort", sort.toBsonDocument());
            addStageToPipelineBefore(pipeline, sortStage, "$limit", "$skip");
        }
        if (skip > 0) {
            pipeline.add(new BsonDocument().append("$skip", new BsonInt32(skip)));
        }
        if (limit > 0) {
            pipeline.add(new BsonDocument().append("$limit", new BsonInt32(limit)));
        }
    }
    return limit;
}
Also used : DelegateStoredQuery(io.micronaut.data.runtime.query.internal.DelegateStoredQuery) Sort(io.micronaut.data.model.Sort) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) DefaultPreparedQuery(io.micronaut.data.runtime.query.internal.DefaultPreparedQuery) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) List(java.util.List) Sorts(com.mongodb.client.model.Sorts) Pageable(io.micronaut.data.model.Pageable) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) BsonInt32(org.bson.BsonInt32) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Sort(io.micronaut.data.model.Sort) Bson(org.bson.conversions.Bson)

Example 12 with Sort

use of io.micronaut.data.model.Sort in project micronaut-data by micronaut-projects.

the class AbstractSqlLikeQueryBuilder method buildOrderBy.

/**
 * Encode the given query into the encoded query instance.
 *
 * @param query The query
 * @param entity The root entity
 * @param sort The sort
 * @return The encoded query
 */
@NonNull
public QueryResult buildOrderBy(String query, @NonNull PersistentEntity entity, @NonNull Sort sort) {
    ArgumentUtils.requireNonNull("entity", entity);
    ArgumentUtils.requireNonNull("sort", sort);
    List<Sort.Order> orders = sort.getOrderBy();
    if (CollectionUtils.isEmpty(orders)) {
        throw new IllegalArgumentException("Sort is empty");
    }
    StringBuilder buff = new StringBuilder(ORDER_BY_CLAUSE);
    Iterator<Sort.Order> i = orders.iterator();
    while (i.hasNext()) {
        Sort.Order order = i.next();
        String property = order.getProperty();
        PersistentPropertyPath path = entity.getPropertyPath(property);
        if (path == null) {
            throw new IllegalArgumentException("Cannot sort on non-existent property path: " + property);
        }
        boolean ignoreCase = order.isIgnoreCase();
        if (ignoreCase) {
            buff.append("LOWER(");
        }
        if (path.getAssociations().isEmpty()) {
            buff.append(getAliasName(entity));
        } else {
            StringJoiner joiner = new StringJoiner(".");
            for (Association association : path.getAssociations()) {
                joiner.add(association.getName());
            }
            String joinAlias = getAliasName(new JoinPath(joiner.toString(), path.getAssociations().toArray(new Association[0]), Join.Type.DEFAULT, null));
            if (!computePropertyPaths()) {
                if (!query.contains(" " + joinAlias + " ") && !query.endsWith(" " + joinAlias)) {
                    // Special hack case for JPA, Hibernate can join the relation with cross join automatically when referenced by the property path
                    // This probably should be removed in the future major version
                    buff.append(getAliasName(entity)).append(DOT);
                    StringJoiner pathJoiner = new StringJoiner(".");
                    for (Association association : path.getAssociations()) {
                        pathJoiner.add(association.getName());
                    }
                    buff.append(pathJoiner);
                } else {
                    buff.append(joinAlias);
                }
            } else {
                buff.append(joinAlias);
            }
        }
        buff.append(DOT);
        if (!computePropertyPaths()) {
            buff.append(path.getProperty().getName());
        } else {
            buff.append(getColumnName(path.getProperty()));
        }
        if (ignoreCase) {
            buff.append(")");
        }
        buff.append(SPACE).append(order.getDirection());
        if (i.hasNext()) {
            buff.append(",");
        }
    }
    return QueryResult.of(buff.toString(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap());
}
Also used : JoinPath(io.micronaut.data.model.query.JoinPath) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) Association(io.micronaut.data.model.Association) Sort(io.micronaut.data.model.Sort) StringJoiner(java.util.StringJoiner) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

Sort (io.micronaut.data.model.Sort)12 Pageable (io.micronaut.data.model.Pageable)7 ArrayList (java.util.ArrayList)5 NonNull (io.micronaut.core.annotation.NonNull)4 PreparedQuery (io.micronaut.data.model.runtime.PreparedQuery)3 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Sorts (com.mongodb.client.model.Sorts)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 Internal (io.micronaut.core.annotation.Internal)2 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)2 Map (java.util.Map)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Expression (javax.persistence.criteria.Expression)2 Order (javax.persistence.criteria.Order)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 EachBean (io.micronaut.context.annotation.EachBean)1 Parameter (io.micronaut.context.annotation.Parameter)1