Skip to content

Commit b7755e7

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1848 - Import Document-based Querydsl support.
Original Pull Request: #579
1 parent 0ec82e1 commit b7755e7

File tree

6 files changed

+961
-0
lines changed

6 files changed

+961
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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 java.util.Collection;
19+
20+
import javax.annotation.Nullable;
21+
22+
import org.bson.Document;
23+
24+
import com.querydsl.core.DefaultQueryMetadata;
25+
import com.querydsl.core.QueryModifiers;
26+
import com.querydsl.core.SimpleQuery;
27+
import com.querydsl.core.support.QueryMixin;
28+
import com.querydsl.core.types.Expression;
29+
import com.querydsl.core.types.FactoryExpression;
30+
import com.querydsl.core.types.OrderSpecifier;
31+
import com.querydsl.core.types.ParamExpression;
32+
import com.querydsl.core.types.Path;
33+
import com.querydsl.core.types.Predicate;
34+
import com.querydsl.core.types.dsl.CollectionPathBase;
35+
36+
/**
37+
* {@code AbstractMongodbQuery} provides a base class for general Querydsl query implementation.
38+
*
39+
* @author Mark Paluch
40+
* @param <Q> concrete subtype
41+
*/
42+
abstract class AbstractMongodbQuery<Q extends AbstractMongodbQuery<Q>> implements SimpleQuery<Q> {
43+
44+
@SuppressWarnings("serial")
45+
static class NoResults extends RuntimeException {}
46+
47+
private final MongodbDocumentSerializer serializer;
48+
private final QueryMixin<Q> queryMixin;
49+
50+
/**
51+
* Create a new MongodbQuery instance
52+
*
53+
* @param serializer serializer
54+
*/
55+
@SuppressWarnings("unchecked")
56+
public AbstractMongodbQuery(MongodbDocumentSerializer serializer) {
57+
@SuppressWarnings("unchecked") // Q is this plus subclass
58+
Q query = (Q) this;
59+
this.queryMixin = new QueryMixin<Q>(query, new DefaultQueryMetadata(), false);
60+
this.serializer = serializer;
61+
}
62+
63+
/**
64+
* Define a join
65+
*
66+
* @param ref reference
67+
* @param target join target
68+
* @return join builder
69+
*/
70+
public <T> JoinBuilder<Q, T> join(Path<T> ref, Path<T> target) {
71+
return new JoinBuilder<Q, T>(queryMixin, ref, target);
72+
}
73+
74+
/**
75+
* Define a join
76+
*
77+
* @param ref reference
78+
* @param target join target
79+
* @return join builder
80+
*/
81+
public <T> JoinBuilder<Q, T> join(CollectionPathBase<?, T, ?> ref, Path<T> target) {
82+
return new JoinBuilder<Q, T>(queryMixin, ref, target);
83+
}
84+
85+
/**
86+
* Define a constraint for an embedded object
87+
*
88+
* @param collection collection
89+
* @param target target
90+
* @return builder
91+
*/
92+
public <T> AnyEmbeddedBuilder<Q> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
93+
return new AnyEmbeddedBuilder<Q>(queryMixin, collection);
94+
}
95+
96+
@Override
97+
public Q distinct() {
98+
return queryMixin.distinct();
99+
}
100+
101+
public Q where(Predicate e) {
102+
return queryMixin.where(e);
103+
}
104+
105+
@Override
106+
public Q where(Predicate... e) {
107+
return queryMixin.where(e);
108+
}
109+
110+
@Override
111+
public Q limit(long limit) {
112+
return queryMixin.limit(limit);
113+
}
114+
115+
@Override
116+
public Q offset(long offset) {
117+
return queryMixin.offset(offset);
118+
}
119+
120+
@Override
121+
public Q restrict(QueryModifiers modifiers) {
122+
return queryMixin.restrict(modifiers);
123+
}
124+
125+
public Q orderBy(OrderSpecifier<?> o) {
126+
return queryMixin.orderBy(o);
127+
}
128+
129+
@Override
130+
public Q orderBy(OrderSpecifier<?>... o) {
131+
return queryMixin.orderBy(o);
132+
}
133+
134+
@Override
135+
public <T> Q set(ParamExpression<T> param, T value) {
136+
return queryMixin.set(param, value);
137+
}
138+
139+
protected Document createProjection(Expression<?> projection) {
140+
if (projection instanceof FactoryExpression) {
141+
Document obj = new Document();
142+
for (Object expr : ((FactoryExpression) projection).getArgs()) {
143+
if (expr instanceof Expression) {
144+
obj.put((String) serializer.handle((Expression) expr), 1);
145+
}
146+
}
147+
return obj;
148+
}
149+
return null;
150+
}
151+
152+
protected Document createQuery(@Nullable Predicate predicate) {
153+
if (predicate != null) {
154+
return (Document) serializer.handle(predicate);
155+
} else {
156+
return new Document();
157+
}
158+
}
159+
160+
QueryMixin<Q> getQueryMixin() {
161+
return queryMixin;
162+
}
163+
164+
MongodbDocumentSerializer getSerializer() {
165+
return serializer;
166+
}
167+
168+
/**
169+
* Get the where definition as a Document instance
170+
*
171+
* @return
172+
*/
173+
public Document asDocument() {
174+
return createQuery(queryMixin.getMetadata().getWhere());
175+
}
176+
177+
@Override
178+
public String toString() {
179+
return asDocument().toString();
180+
}
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 java.util.Collection;
19+
20+
import com.querydsl.core.support.QueryMixin;
21+
import com.querydsl.core.types.ExpressionUtils;
22+
import com.querydsl.core.types.Path;
23+
import com.querydsl.core.types.Predicate;
24+
import com.querydsl.mongodb.MongodbOps;
25+
26+
/**
27+
* {@code AnyEmbeddedBuilder} is a builder for constraints on embedded objects
28+
*
29+
* @param <Q> query type
30+
* @author Mark Paluch
31+
*/
32+
class AnyEmbeddedBuilder<Q extends AbstractMongodbQuery<Q>> {
33+
34+
private final QueryMixin<Q> queryMixin;
35+
36+
private final Path<? extends Collection<?>> collection;
37+
38+
public AnyEmbeddedBuilder(QueryMixin<Q> queryMixin, Path<? extends Collection<?>> collection) {
39+
this.queryMixin = queryMixin;
40+
this.collection = collection;
41+
}
42+
43+
public Q on(Predicate... conditions) {
44+
return queryMixin
45+
.where(ExpressionUtils.predicate(MongodbOps.ELEM_MATCH, collection, ExpressionUtils.allOf(conditions)));
46+
}
47+
}

0 commit comments

Comments
 (0)