Refine search
/** * 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; }
/** * 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; }
private Sort convertToSort(Map<String, Sort.Direction> sorts) { List<Sort.Order> sortValues = new ArrayList<>(); for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) { sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey())); } return Sort.by(sortValues); }
@Override public Sort deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { final ArrayNode node = jsonParser.getCodec().readTree(jsonParser); final Order[] orders = new Order[node.size()]; int i = 0; for (JsonNode jsonNode : node) { orders[i] = new Order(Direction.valueOf(jsonNode.get("direction").asText()), jsonNode.get("property").asText()); i++; } return new Sort(orders); }
public static StringBuilder applySort(Sort sort, StringBuilder sql, Function<Order, String> sortedPropertyNameFunction) { if (sort == null || sort.isUnsorted()) { return sql; } sql.append(" ORDER BY "); StringJoiner sj = new StringJoiner(" , "); sort.iterator().forEachRemaining((o) -> { String sortedPropertyName = sortedPropertyNameFunction.apply(o); String sortedProperty = o.isIgnoreCase() ? "LOWER(" + sortedPropertyName + ")" : sortedPropertyName; sj.add(sortedProperty + (o.isAscending() ? " ASC" : " DESC")); }); return sql.append(sj); }
/** * Translates {@link Sort} orders from Jackson-mapped field names to {@link PersistentProperty} names. Properties * that cannot be resolved are dropped. * * @param input must not be {@literal null}. * @param rootEntity must not be {@literal null}. * @return {@link Sort} with translated field names or {@literal null} if translation dropped all sort fields. */ public Sort translateSort(Sort input, PersistentEntity<?, ?> rootEntity) { Assert.notNull(input, "Sort must not be null!"); Assert.notNull(rootEntity, "PersistentEntity must not be null!"); List<Order> filteredOrders = new ArrayList<Order>(); for (Order order : input) { List<String> iteratorSource = new ArrayList<String>(); Matcher matcher = SPLITTER.matcher("_" + order.getProperty()); while (matcher.find()) { iteratorSource.add(matcher.group(1)); } String mappedPropertyPath = getMappedPropertyPath(rootEntity, iteratorSource); if (mappedPropertyPath != null) { filteredOrders.add(order.withProperty(mappedPropertyPath)); } } return filteredOrders.isEmpty() ? Sort.unsorted() : Sort.by(filteredOrders); }
/** * Add a {@link Sort} to the {@link Query} instance. * * @param sort must not be {@literal null}. * @return a new {@link Query} object containing the former settings with {@link Sort} applied. */ public Query sort(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;" + " Apache Cassandra does not support sorting ignoring case currently", order.getProperty())); } } return new Query(this.criteriaDefinitions, this.columns, this.sort.and(sort), this.pagingState, this.queryOptions, this.limit, this.allowFiltering); }
@Override public Page<ContractEventDetails> getContractEventsForSignature(String eventSignature, PageRequest pagination) { final Sort.Order firstOrder = pagination.getSort().iterator().next(); return client.getContractEvents(pagination.getPageNumber(), pagination.getPageSize(), firstOrder.getProperty(), firstOrder.getDirection(), eventSignature); }
/** * @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; }
private static Direction toDirection(Sort sort, String property) { if (sort.isUnsorted()) { return Direction.DESC; } Order order = sort.getOrderFor(property); return order == null ? Direction.DESC : order.isAscending() ? Direction.ASC : Direction.DESC; } }
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)); }
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; }
private String getSortOrder(Sort sort) { return sort.stream() .map(order -> order.getProperty() + " " + order.getDirection()) .collect(Collectors.joining(", ")); }
@Override public DBObject toDBObject(AggregationOperationContext context) { BasicDBObject object = new BasicDBObject(); sort.forEach(it -> object.put(it.getProperty(), it.isAscending() ? 1 : -1)); return new BasicDBObject("$sort", object); }
/** * Transforms a plain {@link Order} into a QueryDsl specific {@link OrderSpecifier}. * * @param order must not be {@literal null}. * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private OrderSpecifier<?> toOrderSpecifier(Order order) { return new OrderSpecifier( order.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC, buildOrderPropertyPathFrom(order), toQueryDslNullHandling(order.getNullHandling())); }
private static List<Order> combine(List<Order> orders, @Nullable Direction direction, List<Path<?, ?>> paths) { List<Order> result = new ArrayList<Sort.Order>(orders); for (Path<?, ?> path : paths) { result.add(new Order(direction, path.toString())); } return result; }
private static Document getSortObject(Sort sort) { Document document = new Document(); for (Order order : sort) { document.put(order.getProperty(), order.isAscending() ? 1 : -1); } return document; }
/** * 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); }
@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); } }
@Override public boolean isIgnoreCase() { return super.isIgnoreCase() || ignoreCase; }