下面列出了org.springframework.data.domain.Sort#unsorted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private <T, F> Sort createSpringSort(Query<T, F> q) {
List<QuerySortOrder> sortOrders;
if (q.getSortOrders().isEmpty()) {
sortOrders = getDefaultSortOrders();
} else {
sortOrders = q.getSortOrders();
}
List<Order> orders = sortOrders.stream()
.map(PageableDataProvider::queryOrderToSpringOrder)
.collect(Collectors.toList());
if (orders.isEmpty()) {
return Sort.unsorted();
} else {
return Sort.by(orders);
}
}
/**
* 构建排序
* @param sortDOS s
* @return r
*/
public static Sort buildSort(List<SearchSortDO> sortDOS){
Sort sort = null;
//构建排序
if(!CollectionUtils.isEmpty(sortDOS)){
List<Sort.Order> orders = new ArrayList<>();
for (SearchSortDO sortDO: sortDOS) {
Sort.Order order = null;
if(sortDO.getDirection() == SearchSortDO.Direction.ASC){
order = Sort.Order.asc(sortDO.getProperty());
}else{
order = Sort.Order.desc(sortDO.getProperty());
}
orders.add(order);
}
sort = Sort.by(orders);
}else{
sort = Sort.unsorted();
}
return sort;
}
/**
* Creates a 'LIMIT .. OFFSET .. ORDER BY ..' clause for the given {@link DataTablesInput}.
*
* @return a {@link Pageable}, must not be {@literal null}.
*/
public Pageable createPageable() {
List<Sort.Order> orders = new ArrayList<>();
for (org.springframework.data.jpa.datatables.mapping.Order order : input.getOrder()) {
Column column = input.getColumns().get(order.getColumn());
if (column.getOrderable()) {
String sortColumn = column.getData();
Sort.Direction sortDirection = Sort.Direction.fromString(order.getDir());
orders.add(new Sort.Order(sortDirection, sortColumn));
}
}
Sort sort = orders.isEmpty() ? Sort.unsorted() : Sort.by(orders);
if (input.getLength() == -1) {
input.setStart(0);
input.setLength(Integer.MAX_VALUE);
}
return new DataTablesPageRequest(input.getStart(), input.getLength(), sort);
}
@Override
public Sort getSort() {
List<io.micronaut.data.model.Sort.Order> orderBy = delegate.getSort().getOrderBy();
if (CollectionUtils.isEmpty(orderBy)) {
return Sort.unsorted();
} else {
return new SortDelegate(delegate.getSort());
}
}
@Override
public BindingResult<Pageable> bind(ArgumentConversionContext<Pageable> context, HttpRequest<?> source) {
HttpParameters parameters = source.getParameters();
int page = Math.max(parameters.getFirst(configuration.getPageParameterName(), Integer.class)
.orElse(0), 0);
final int configuredMaxSize = configuration.getMaxPageSize();
final int defaultSize = configuration.getDefaultPageSize();
int size = Math.min(parameters.getFirst(configuration.getSizeParameterName(), Integer.class)
.orElse(defaultSize), configuredMaxSize);
String sortParameterName = configuration.getSortParameterName();
boolean hasSort = parameters.contains(sortParameterName);
Pageable pageable;
Sort sort;
if (hasSort) {
List<String> sortParams = parameters.getAll(sortParameterName);
List<Sort.Order> orders = sortParams.stream()
.map(sortMapper)
.collect(Collectors.toList());
sort = Sort.by(orders);
} else {
sort = Sort.unsorted();
}
if (size < 1) {
if (page == 0 && configuredMaxSize < 1 && sort.isUnsorted()) {
pageable = Pageable.unpaged();
} else {
pageable = PageRequest.of(page, defaultSize, sort);
}
} else {
pageable = PageRequest.of(page, size, sort);
}
return () -> Optional.of(pageable);
}
@Test
public void testFindAllUnSorted() {
final Sort sort = Sort.unsorted();
final List<SortedProject> projects = Lists.newArrayList(this.repository.findAll(sort));
PROJECTS.sort(Comparator.comparing(SortedProject::getId));
projects.sort(Comparator.comparing(SortedProject::getId));
Assert.assertEquals(PROJECTS.size(), projects.size());
Assert.assertEquals(PROJECTS, projects);
}
/**
* 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;
default:
}
}
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;
default:
}
}
sql.append(", ");
}
sql.delete(sql.length() - 2, sql.length());
}
return sql;
}
/**
* <p>
* Sort on a sequence of fields, possibly none.
* </P>
*
* @param query If not null, will contain one of more {@link Sort.Order} objects.
* @return A sequence of comparators or {@code null}
*/
public Comparator<Entry<?, ?>> resolve(KeyValueQuery<?> query) {
if (query == null || query.getSort() == Sort.unsorted()) {
return null;
}
Comparator hazelcastPropertyComparator = null;
for (Order order : query.getSort()) {
if (order.getProperty().indexOf('.') > -1) {
throw new UnsupportedOperationException("Embedded fields not implemented: " + order);
}
if (order.isIgnoreCase()) {
throw new UnsupportedOperationException("Ignore case not implemented: " + order);
}
if (NullHandling.NATIVE != order.getNullHandling()) {
throw new UnsupportedOperationException("Null handling not implemented: " + order);
}
if (hazelcastPropertyComparator == null) {
hazelcastPropertyComparator = new HazelcastPropertyComparator(order.getProperty(),
order.isAscending());
} else {
hazelcastPropertyComparator = hazelcastPropertyComparator.thenComparing(
new HazelcastPropertyComparator(order.getProperty(),
order.isAscending()));
}
}
return hazelcastPropertyComparator;
}
protected PageRequest wrapPageable(Sort sort) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
int pageSize = ServletRequestUtils.getIntParameter(request, "pageSize", 10);
int pageNo = ServletRequestUtils.getIntParameter(request, "pageNo", 1);
if (null == sort) {
sort = Sort.unsorted();
}
return PageRequest.of(pageNo - 1, pageSize, sort);
}
protected <T> TypedQuery<T> getQuery(Class<T> clazz, @Nullable Specification<T> spec, Pageable pageable) {
Sort sort = pageable.isPaged() ? pageable.getSort() : Sort.unsorted();
return getQuery(clazz, spec, sort);
}
@Override
public Object getDefaultValue() {
return Sort.unsorted();
}
public CosmosPageRequest(int page, int size, String requestContinuation) {
super(page, size, Sort.unsorted());
this.requestContinuation = requestContinuation;
}
private CosmosPageRequest(long offset, int page, int size, String requestContinuation) {
super(page, size, Sort.unsorted());
this.offset = offset;
this.requestContinuation = requestContinuation;
}
/**
* Creates a new {@link OffsetBasedPageRequest}. Offsets are zero indexed,
* thus providing 0 for {@code offset} will return the first entry.
*
* @param offset
* zero-based offset index.
* @param limit
* the limit of the page to be returned.
*/
public OffsetBasedPageRequest(final long offset, final int limit) {
this(offset, limit, Sort.unsorted());
}