/** * Add a dynamic part of query for the sorting support. * * @param sql SQL text string. * @param sort Sort method. */ public static StringBuilder addSorting(StringBuilder sql, Sort sort) { if (sort != null) { sql.append(" ORDER BY "); for (Sort.Order order : sort) { sql.append(order.getProperty()).append(" ").append(order.getDirection()); if (order.getNullHandling() != Sort.NullHandling.NATIVE) { sql.append(" ").append("NULL "); switch (order.getNullHandling()) { case NULLS_FIRST: sql.append("FIRST"); break; case NULLS_LAST: sql.append("LAST"); break; } } sql.append(", "); } sql.delete(sql.length() - 2, sql.length()); } return sql; }
/** * Add a dynamic part of query for the sorting support. * * @param sql SQL text string. * @param sort Sort method. * @return Sorting criteria in StringBuilder. */ public static StringBuilder addSorting(StringBuilder sql, Sort sort) { if (sort != null && sort != Sort.unsorted()) { sql.append(" ORDER BY "); for (Sort.Order order : sort) { sql.append(order.getProperty()).append(" ").append(order.getDirection()); if (order.getNullHandling() != Sort.NullHandling.NATIVE) { sql.append(" ").append("NULL "); switch (order.getNullHandling()) { case NULLS_FIRST: sql.append("FIRST"); break; case NULLS_LAST: sql.append("LAST"); break; } } sql.append(", "); } sql.delete(sql.length() - 2, sql.length()); } return sql; }
/** * Creates a new {@link SortModifier} instance given {@link Sort}. * * @param sort must not be {@literal null}. */ SortModifier(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); for (Order order : sort) { if (order.isIgnoreCase()) { throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! " + "MongoDB does not support sorting ignoring case currently!", order.getProperty())); } } this.sort = sort; }
/** * Transforms a plain {@link Order} into a Querydsl specific {@link OrderSpecifier}. * * @param order * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private OrderSpecifier<?> toOrder(Order order) { Expression<Object> property = builder.get(order.getProperty()); return new OrderSpecifier( order.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC, property); } }
private String getSortOrder(Sort sort) { return sort.stream() .map(order -> order.getProperty() + " " + order.getDirection()) .collect(Collectors.joining(", ")); }
private static Document getSortObject(Sort sort) { Document document = new Document(); for (Order order : sort) { document.put(order.getProperty(), order.isAscending() ? 1 : -1); } return document; }
@Override public Document toDocument(AggregationOperationContext context) { Document object = new Document(); for (Order order : sort) { // Check reference FieldReference reference = context.getReference(order.getProperty()); object.put(reference.getRaw(), order.isAscending() ? 1 : -1); } return new Document("$sort", object); } }
/** * @return the sort {@link Document}. */ public Document getSortObject() { if (this.sort.isUnsorted()) { return new Document(); } Document document = new Document(); this.sort.stream()// .forEach(order -> document.put(order.getProperty(), order.isAscending() ? 1 : -1)); return document; }
FieldSortBuilder sort = SortBuilders.fieldSort(order.getProperty()) .order(order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC); if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
public void onCreation(PartTreeMongoQuery query) { PartTree tree = query.getTree(); if (!tree.hasPredicate()) { return; } Index index = new Index(); index.named(query.getQueryMethod().getName()); Sort sort = tree.getSort(); for (Part part : tree.getParts()) { if (GEOSPATIAL_TYPES.contains(part.getType())) { return; } String property = part.getProperty().toDotPath(); Direction order = toDirection(sort, property); index.on(property, order); } // Add fixed sorting criteria to index if (sort.isSorted()) { for (Order order : sort) { index.on(order.getProperty(), order.getDirection()); } } MongoEntityMetadata<?> metadata = query.getQueryMethod().getEntityInformation(); indexOperationsProvider.indexOps(metadata.getCollectionName()).ensureIndex(index); LOG.debug(String.format("Created %s!", index)); }
/** * Returns the order clause for the given {@link Order}. Will prefix the clause with the given alias if the referenced * property refers to a join alias, i.e. starts with {@code $alias.}. * * @param joinAliases the join aliases of the original query. Must not be {@literal null}. * @param alias the alias for the root entity. May be {@literal null}. * @param order the order object to build the clause for. Must not be {@literal null}. * @return a String containing a order clause. Guaranteed to be not {@literal null}. */ private static String getOrderClause(Set<String> joinAliases, Set<String> functionAlias, @Nullable String alias, Order order) { String property = order.getProperty(); checkSortExpression(order); if (functionAlias.contains(property)) { return String.format("%s %s", property, toJpaDirection(order)); } boolean qualifyReference = !property.contains("("); // ( indicates a function for (String joinAlias : joinAliases) { if (property.startsWith(joinAlias.concat("."))) { qualifyReference = false; break; } } String reference = qualifyReference && StringUtils.hasText(alias) ? String.format("%s.%s", alias, property) : property; String wrapped = order.isIgnoreCase() ? String.format("lower(%s)", reference) : reference; return String.format("%s %s", wrapped, toJpaDirection(order)); }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @return */ private Expression<?> buildOrderPropertyPathFrom(Order order) { Assert.notNull(order, "Order must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; } }
/** * Adds a {@link Sort} to the {@link Query} instance. * * @param sort * @return */ public Query with(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); if (sort.isUnsorted()) { return this; } sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> { throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! " + "MongoDB does not support sorting ignoring case currently!", it.getProperty())); }); this.sort = this.sort.and(sort); return this; }
/** * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}. * * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order} * @param from the {@link From} the {@link Order} expression is based on * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order} with * @return */ @SuppressWarnings("unchecked") private static javax.persistence.criteria.Order toJpaOrder(Order order, From<?, ?> from, CriteriaBuilder cb) { PropertyPath property = PropertyPath.from(order.getProperty(), from.getJavaType()); Expression<?> expression = toExpressionRecursively(from, property); if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) { Expression<String> lower = cb.lower((Expression<String>) expression); return order.isAscending() ? cb.asc(lower) : cb.desc(lower); } else { return order.isAscending() ? cb.asc(expression) : cb.desc(expression); } }
private static List<FieldSortBuilder> sort(Query query, ElasticsearchPersistentEntity<?> entity) { if (query.getSort() == null || query.getSort().isUnsorted()) { return Collections.emptyList(); } List<FieldSortBuilder> mappedSort = new ArrayList<>(); for (Sort.Order order : query.getSort()) { ElasticsearchPersistentProperty property = entity.getPersistentProperty(order.getProperty()); String fieldName = property != null ? property.getFieldName() : order.getProperty(); FieldSortBuilder sort = SortBuilders.fieldSort(fieldName) .order(order.getDirection().isDescending() ? SortOrder.DESC : SortOrder.ASC); if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) { sort.missing("_first"); } else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) { sort.missing("_last"); } mappedSort.add(sort); } return mappedSort; }
/** * Check any given {@link JpaOrder#isUnsafe()} order for presence of at least one property offending the * {@link #PUNCTATION_PATTERN} and throw an {@link Exception} indicating potential unsafe order by expression. * * @param order */ private static void checkSortExpression(Order order) { if (order instanceof JpaOrder && ((JpaOrder) order).isUnsafe()) { return; } if (PUNCTATION_PATTERN.matcher(order.getProperty()).find()) { throw new InvalidDataAccessApiUsageException(String.format(UNSAFE_PROPERTY_REFERENCE, order)); } } }
@Override public Document toDocument(AggregationOperationContext context) { Document object = new Document(); for (Order order : sort) { // Check reference FieldReference reference = context.getReference(order.getProperty()); object.put(reference.getRaw(), order.isAscending() ? 1 : -1); } return new Document("$sort", object); } }
/** * Transforms a plain {@link Order} into a Querydsl specific {@link OrderSpecifier}. * * @param order * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected OrderSpecifier<?> toOrder(Order order) { Expression<Object> property = builder.get(order.getProperty()); return new OrderSpecifier( order.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC, property); }
private static Document getSortObject(Sort sort) { Document document = new Document(); for (Order order : sort) { document.put(order.getProperty(), order.isAscending() ? 1 : -1); } return document; }
/** * @return the sort {@link Document}. */ public Document getSortObject() { if (this.sort.isUnsorted()) { return new Document(); } Document document = new Document(); this.sort.stream()// .forEach(order -> document.put(order.getProperty(), order.isAscending() ? 1 : -1)); return document; }