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;
}
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());
}
Aggregations