DATACMNS-867 - Second draft.

This commit is contained in:
Oliver Gierke
2016-06-21 16:52:28 +02:00
parent 57ed50a730
commit d4811e29d9
222 changed files with 2297 additions and 2138 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.data.querydsl;
import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import com.querydsl.core.types.OrderSpecifier;
@@ -39,7 +40,7 @@ public class QPageRequest extends AbstractPageRequest {
* @param size
*/
public QPageRequest(int page, int size) {
this(page, size, (QSort) null);
this(page, size, QSort.unsorted());
}
/**
@@ -71,7 +72,7 @@ public class QPageRequest extends AbstractPageRequest {
* @see org.springframework.data.domain.Pageable#getSort()
*/
@Override
public QSort getSort() {
public Sort getSort() {
return sort;
}
@@ -81,7 +82,7 @@ public class QPageRequest extends AbstractPageRequest {
*/
@Override
public Pageable next() {
return new QPageRequest(getPageNumber() + 1, getPageSize(), getSort());
return new QPageRequest(getPageNumber() + 1, getPageSize(), sort);
}
/*
@@ -90,7 +91,7 @@ public class QPageRequest extends AbstractPageRequest {
*/
@Override
public Pageable previous() {
return new QPageRequest(getPageNumber() - 1, getPageSize(), getSort());
return new QPageRequest(getPageNumber() - 1, getPageSize(), sort);
}
/*
@@ -99,6 +100,6 @@ public class QPageRequest extends AbstractPageRequest {
*/
@Override
public Pageable first() {
return new QPageRequest(0, getPageSize(), getSort());
return new QPageRequest(0, getPageSize(), sort);
}
}

View File

@@ -19,6 +19,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
@@ -36,13 +37,14 @@ import com.querydsl.core.types.Path;
public class QSort extends Sort implements Serializable {
private static final long serialVersionUID = -6701117396842171930L;
private static final QSort UNSORTED = new QSort();
private final List<OrderSpecifier<?>> orderSpecifiers;
/**
* Creates a new {@link QSort} instance with the given {@link OrderSpecifier}s.
*
* @param orderSpecifiers must not be {@literal null} or empty.
* @param orderSpecifiers must not be {@literal null} .
*/
public QSort(OrderSpecifier<?>... orderSpecifiers) {
this(Arrays.asList(orderSpecifiers));
@@ -51,16 +53,24 @@ public class QSort extends Sort implements Serializable {
/**
* Creates a new {@link QSort} instance with the given {@link OrderSpecifier}s.
*
* @param orderSpecifiers must not be {@literal null} or empty.
* @param orderSpecifiers must not be {@literal null}.
*/
public QSort(List<OrderSpecifier<?>> orderSpecifiers) {
super(toOrders(orderSpecifiers));
Assert.notEmpty(orderSpecifiers, "Order specifiers must not be null or empty!");
this.orderSpecifiers = orderSpecifiers;
}
public static QSort by(OrderSpecifier<?>... orderSpecifiers) {
return new QSort(orderSpecifiers);
}
public static QSort unsorted() {
return UNSORTED;
}
/**
* Converts the given {@link OrderSpecifier}s into a list of {@link Order}s.
*
@@ -69,14 +79,9 @@ public class QSort extends Sort implements Serializable {
*/
private static List<Order> toOrders(List<OrderSpecifier<?>> orderSpecifiers) {
Assert.notEmpty(orderSpecifiers, "Order specifiers must not be null or empty!");
Assert.notNull(orderSpecifiers, "Order specifiers must not be null!");
List<Order> orders = new ArrayList<Sort.Order>();
for (OrderSpecifier<?> orderSpecifier : orderSpecifiers) {
orders.add(toOrder(orderSpecifier));
}
return orders;
return orderSpecifiers.stream().map(QSort::toOrder).collect(Collectors.toList());
}
/**

View File

@@ -57,18 +57,18 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker {
this.predicate = predicate;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
* @see org.springframework.data.repository.support.RepositoryInvoker#invokePagedFindAll(org.springframework.data.domain.Pageable)
*/
@Override
public Iterable<Object> invokeFindAll(Pageable pageable) {
return executor.findAll(predicate, pageable);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeSortedFindAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<Object> invokeFindAll(Sort sort) {
@@ -129,7 +129,7 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker {
return delegate.invokeFindOne(id);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, org.springframework.util.MultiValueMap, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
*/

View File

@@ -38,8 +38,8 @@ public interface MultiValueBinding<T extends Path<? extends S>, S> {
*
* @param path {@link Path} to the property. Will not be {@literal null}.
* @param value the value that should be bound. Will not be {@literal null} or empty.
* @return can be {@literal null}, in which case the binding will not be incorporated in the overall {@link Predicate}
* .
* @return can be {@literal null}, in which case the binding will not be incorporated in the overall
* {@link Predicate}.
*/
Optional<Predicate> bind(T path, Collection<? extends S> value);
}

View File

@@ -81,9 +81,9 @@ public class QuerydslBindings {
this.pathSpecs = new LinkedHashMap<String, PathAndBinding<?, ?>>();
this.typeSpecs = new LinkedHashMap<Class<?>, PathAndBinding<?, ?>>();
this.whiteList = new HashSet<String>();
this.blackList = new HashSet<String>();
this.aliases = new HashSet<String>();
this.whiteList = new HashSet<>();
this.blackList = new HashSet<>();
this.aliases = new HashSet<>();
}
@@ -115,7 +115,7 @@ public class QuerydslBindings {
* @return
*/
public final <T> TypeBinder<T> bind(Class<T> type) {
return new TypeBinder<T>(type);
return new TypeBinder<>(type);
}
/**
@@ -502,7 +502,7 @@ public class QuerydslBindings {
Assert.notNull(binding, "Binding must not be null!");
QuerydslBindings.this.typeSpecs.put(type, PathAndBinding.<T, P> withoutPath().with(binding));
QuerydslBindings.this.typeSpecs.put(type, PathAndBinding.<T, P>withoutPath().with(binding));
}
}

View File

@@ -148,7 +148,37 @@ public class QuerydslPredicateBuilder {
Optional<Path<?>> resolvedPath = bindings.getExistingPath(path);
return resolvedPath.orElseGet(() -> paths.computeIfAbsent(path, it -> path.reifyPath(resolver)));
return resolvedPath.orElseGet(() -> paths.computeIfAbsent(path, it -> reifyPath(path, Optional.empty())));
}
/**
* Tries to reify a Querydsl {@link Path} from the given {@link PropertyPath} and base.
*
* @param path must not be {@literal null}.
* @param base can be empty.
* @return
*/
private Path<?> reifyPath(PropertyPath path, Optional<Path<?>> base) {
Optional<Path<?>> map = base.filter(it -> it instanceof CollectionPathBase)
.map(it -> CollectionPathBase.class.cast(it))//
.map(CollectionPathBase::any)//
.map(it -> Path.class.cast(it))//
.map(it -> reifyPath(path, Optional.of(it)));
return map.orElseGet(() -> {
Path<?> entityPath = base.orElseGet(() -> resolver.createPath(path.getOwningType().getType()));
Field field = ReflectionUtils.findField(entityPath.getClass(), path.getSegment());
Object value = ReflectionUtils.getField(field, entityPath);
if (path.hasNext()) {
return reifyPath(path.next(), Optional.of((Path<?>) value));
}
return (Path<?>) value;
});
}
/**
@@ -162,13 +192,13 @@ public class QuerydslPredicateBuilder {
*/
private Collection<Object> convertToPropertyPathSpecificType(List<String> source, PathInformation path) {
Class<?> targetType = path.getLeafType();
Class<?> targetType = path.getLeafProperty().getType();
if (source.isEmpty() || isSingleElementCollectionWithoutText(source)) {
return Collections.emptyList();
}
Collection<Object> target = new ArrayList<Object>(source.size());
Collection<Object> target = new ArrayList<>(source.size());
for (String value : source) {