Skip to content

Commit 7d06f2b

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1848 - Use imported Querydsl support for Document API MongoDB.
Original Pull Request: spring-projects#579
1 parent b7755e7 commit 7d06f2b

File tree

9 files changed

+206
-88
lines changed

9 files changed

+206
-88
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDbFactory.java

+3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public interface MongoDbFactory extends CodecRegistryProvider, MongoSessionProvi
6262
* Get the legacy database entry point. Please consider {@link #getDb()} instead.
6363
*
6464
* @return
65+
* @deprecated since 2.1, use {@link #getDb()}. This method will be removed with a future version as it works only
66+
* with the legacy MongoDB driver.
6567
*/
68+
@Deprecated
6669
DB getLegacyDb();
6770

6871
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/AbstractMongodbQuery.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected Document createProjection(Expression<?> projection) {
146146
}
147147
return obj;
148148
}
149-
return null;
149+
return new Document();
150150
}
151151

152152
protected Document createQuery(@Nullable Predicate predicate) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/FetchableMongodbQuery.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
* @param <Q> concrete subtype
4848
* @author Mark Paluch
4949
*/
50-
class FetchableMongodbQuery<K> extends AbstractMongodbQuery<FetchableMongodbQuery<K>> implements Fetchable<K> {
50+
abstract class FetchableMongodbQuery<K, Q extends FetchableMongodbQuery<K, Q>> extends AbstractMongodbQuery<Q>
51+
implements Fetchable<K> {
5152

5253
private final Class<K> entityClass;
5354
private final String collection;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutor.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.querydsl.core.types.OrderSpecifier;
4040
import com.querydsl.core.types.Predicate;
4141
import com.querydsl.core.types.dsl.PathBuilder;
42-
import com.querydsl.mongodb.AbstractMongodbQuery;
4342

4443
/**
4544
* MongoDB-specific {@link QuerydslPredicateExecutor} that allows execution {@link Predicate}s in various forms.
@@ -166,7 +165,7 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
166165
Assert.notNull(predicate, "Predicate must not be null!");
167166
Assert.notNull(pageable, "Pageable must not be null!");
168167

169-
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query = createQueryFor(predicate);
168+
SimpleFetchableQuery<T> query = createQueryFor(predicate);
170169

171170
return PageableExecutionUtils.getPage(applyPagination(query, pageable).fetch(), pageable, query::fetchCount);
172171
}
@@ -201,7 +200,7 @@ public boolean exists(Predicate predicate) {
201200
* @param predicate
202201
* @return
203202
*/
204-
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQueryFor(Predicate predicate) {
203+
private SimpleFetchableQuery<T> createQueryFor(Predicate predicate) {
205204
return createQuery().where(predicate);
206205
}
207206

@@ -210,7 +209,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQueryFor(Predic
210209
*
211210
* @return
212211
*/
213-
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQuery() {
212+
private SimpleFetchableQuery<T> createQuery() {
214213
return new SpringDataMongodbQuery<>(mongoOperations, entityInformation.getJavaType());
215214
}
216215

@@ -221,8 +220,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQuery() {
221220
* @param pageable
222221
* @return
223222
*/
224-
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applyPagination(
225-
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query, Pageable pageable) {
223+
private SimpleFetchableQuery<T> applyPagination(SimpleFetchableQuery<T> query, Pageable pageable) {
226224

227225
query = query.offset(pageable.getOffset()).limit(pageable.getPageSize());
228226
return applySorting(query, pageable.getSort());
@@ -235,8 +233,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applyPagination(
235233
* @param sort
236234
* @return
237235
*/
238-
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applySorting(
239-
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query, Sort sort) {
236+
private SimpleFetchableQuery<T> applySorting(SimpleFetchableQuery<T> query, Sort sort) {
240237

241238
// TODO: find better solution than instanceof check
242239
if (sort instanceof QSort) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QuerydslRepositorySupport.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.util.Assert;
2222

2323
import com.querydsl.core.types.EntityPath;
24-
import com.querydsl.mongodb.AbstractMongodbQuery;
2524

2625
/**
2726
* Base class to create repository implementations based on Querydsl.
@@ -54,7 +53,7 @@ public QuerydslRepositorySupport(MongoOperations operations) {
5453
* @param path
5554
* @return
5655
*/
57-
protected <T> AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> from(final EntityPath<T> path) {
56+
protected <T> SpringDataMongodbQuery<T> from(final EntityPath<T> path) {
5857

5958
Assert.notNull(path, "EntityPath must not be null!");
6059
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(path.getType());
@@ -68,7 +67,7 @@ protected <T> AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> from(final Enti
6867
* @param collection must not be blank or {@literal null}
6968
* @return
7069
*/
71-
protected <T> AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> from(final EntityPath<T> path, String collection) {
70+
protected <T> SpringDataMongodbQuery<T> from(final EntityPath<T> path, String collection) {
7271

7372
Assert.notNull(path, "EntityPath must not be null!");
7473
Assert.hasText(collection, "Collection name must not be null or empty!");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.support;
17+
18+
import com.querydsl.core.Fetchable;
19+
import com.querydsl.core.SimpleQuery;
20+
21+
/**
22+
* Interface that combines {@link Fetchable} and {@link SimpleQuery}.
23+
*
24+
* @author Mark Paluch
25+
* @since 2.1
26+
*/
27+
public interface SimpleFetchableQuery<K> extends Fetchable<K>, SimpleQuery<SimpleFetchableQuery<K>> {}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbQuery.java

+142-21
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18-
import org.bson.Document;
18+
import java.util.List;
19+
1920
import org.springframework.data.mongodb.core.MongoOperations;
20-
import org.springframework.data.mongodb.core.MongoTemplate;
21-
import org.springframework.lang.Nullable;
2221

23-
import com.google.common.base.Function;
24-
import com.mongodb.BasicDBObject;
25-
import com.mongodb.DBCollection;
26-
import com.mongodb.DBObject;
22+
import com.mysema.commons.lang.CloseableIterator;
23+
import com.querydsl.core.NonUniqueResultException;
24+
import com.querydsl.core.QueryModifiers;
25+
import com.querydsl.core.QueryResults;
26+
import com.querydsl.core.types.OrderSpecifier;
27+
import com.querydsl.core.types.ParamExpression;
28+
import com.querydsl.core.types.Predicate;
2729
import com.querydsl.mongodb.AbstractMongodbQuery;
2830

2931
/**
@@ -32,9 +34,9 @@
3234
* @author Oliver Gierke
3335
* @author Mark Paluch
3436
*/
35-
public class SpringDataMongodbQuery<T> extends AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> {
37+
public class SpringDataMongodbQuery<T> implements SimpleFetchableQuery<T> {
3638

37-
private final MongoOperations operations;
39+
private final OperationsMongodbQuery<T> query;
3840

3941
/**
4042
* Creates a new {@link SpringDataMongodbQuery}.
@@ -56,25 +58,144 @@ public SpringDataMongodbQuery(final MongoOperations operations, final Class<? ex
5658
public SpringDataMongodbQuery(final MongoOperations operations, final Class<? extends T> type,
5759
String collectionName) {
5860

59-
super(((MongoTemplate) operations).getMongoDbFactory().getLegacyDb().getCollection(collectionName),
60-
new Function<DBObject, T>() {
61+
query = new OperationsMongodbQuery<>(new SpringDataMongodbSerializer(operations.getConverter()), type,
62+
collectionName, operations);
63+
}
6164

62-
@Override
63-
public T apply(@Nullable DBObject input) {
64-
return operations.getConverter().read(type, new Document((BasicDBObject) input));
65-
}
66-
}, new SpringDataMongodbSerializer(operations.getConverter()));
65+
/*
66+
* (non-Javadoc)
67+
* @see com.querydsl.core.Fetchable#fetch()
68+
*/
69+
@Override
70+
public List<T> fetch() {
71+
return query.fetch();
72+
}
6773

68-
this.operations = operations;
74+
/*
75+
* (non-Javadoc)
76+
* @see com.querydsl.core.Fetchable#fetchFirst()
77+
*/
78+
@Override
79+
public T fetchFirst() {
80+
return query.fetchFirst();
81+
}
82+
83+
/*
84+
* (non-Javadoc)
85+
* @see com.querydsl.core.Fetchable#fetchOne()
86+
*/
87+
@Override
88+
public T fetchOne() throws NonUniqueResultException {
89+
return query.fetchOne();
90+
}
91+
92+
/*
93+
* (non-Javadoc)
94+
* @see com.querydsl.core.Fetchable#iterate()
95+
*/
96+
@Override
97+
public CloseableIterator<T> iterate() {
98+
return query.iterate();
99+
}
100+
101+
/*
102+
* (non-Javadoc)
103+
* @see com.querydsl.core.Fetchable#fetchResults()
104+
*/
105+
@Override
106+
public QueryResults<T> fetchResults() {
107+
return query.fetchResults();
108+
}
109+
110+
/*
111+
* (non-Javadoc)
112+
* @see com.querydsl.core.Fetchable#fetchCount()
113+
*/
114+
@Override
115+
public long fetchCount() {
116+
return query.fetchCount();
117+
}
118+
119+
/*
120+
* (non-Javadoc)
121+
* @see com.querydsl.core.SimpleQuery#limit(long)
122+
*/
123+
@Override
124+
public SpringDataMongodbQuery<T> limit(long limit) {
125+
query.limit(limit);
126+
return this;
127+
}
128+
129+
/*
130+
* (non-Javadoc)
131+
* @see com.querydsl.core.SimpleQuery#offset(long)
132+
*/
133+
@Override
134+
public SpringDataMongodbQuery<T> offset(long offset) {
135+
query.offset(offset);
136+
return this;
137+
}
138+
139+
/*
140+
* (non-Javadoc)
141+
* @see com.querydsl.core.SimpleQuery#restrict(com.querydsl.core.QueryModifiers)
142+
*/
143+
@Override
144+
public SpringDataMongodbQuery<T> restrict(QueryModifiers modifiers) {
145+
query.restrict(modifiers);
146+
return this;
69147
}
70148

71149
/*
72150
* (non-Javadoc)
73-
* @see com.querydsl.mongodb.AbstractMongodbQuery#getCollection(java.lang.Class)
151+
* @see com.querydsl.core.SimpleQuery#orderBy(com.querydsl.core.types.OrderSpecifier[])
152+
*/
153+
@Override
154+
public SpringDataMongodbQuery<T> orderBy(OrderSpecifier<?>... o) {
155+
query.orderBy(o);
156+
return this;
157+
}
158+
159+
/*
160+
* (non-Javadoc)
161+
* @see com.querydsl.core.SimpleQuery#set(com.querydsl.core.types.ParamExpression, java.lang.Object)
74162
*/
75163
@Override
76-
protected DBCollection getCollection(Class<?> type) {
77-
return ((MongoTemplate) operations).getMongoDbFactory().getLegacyDb()
78-
.getCollection(operations.getCollectionName(type));
164+
public <V> SpringDataMongodbQuery<T> set(ParamExpression<V> param, V value) {
165+
query.set(param, value);
166+
return this;
167+
}
168+
169+
/*
170+
* (non-Javadoc)
171+
* @see com.querydsl.core.SimpleQuery#distinct()
172+
*/
173+
@Override
174+
public SpringDataMongodbQuery<T> distinct() {
175+
query.distinct();
176+
return this;
177+
}
178+
179+
/*
180+
* (non-Javadoc)
181+
* @see com.querydsl.core.FilteredClause#where(com.querydsl.core.types.Predicate[])
182+
*/
183+
@Override
184+
public SpringDataMongodbQuery<T> where(Predicate... o) {
185+
query.where(o);
186+
return this;
187+
}
188+
189+
/**
190+
* Concrete implementation of {@link FetchableMongodbQuery}.
191+
*
192+
* @param <T>
193+
*/
194+
static class OperationsMongodbQuery<T> extends FetchableMongodbQuery<T, OperationsMongodbQuery<T>> {
195+
196+
public OperationsMongodbQuery(MongodbDocumentSerializer serializer, Class<? extends T> entityClass,
197+
String collection, MongoOperations mongoOperations) {
198+
super(serializer, entityClass, collection, mongoOperations);
199+
}
79200
}
80201
}

0 commit comments

Comments
 (0)