Skip to content

Commit a37722a

Browse files
committed
Merge branch 'datacouch_1288_querydsl_support' of github.com:spring-projects/spring-data-couchbase into datacouch_1288_querydsl_support
2 parents 64724cd + 5a8d65c commit a37722a

30 files changed

+954
-430
lines changed

pom.xml

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
<couchbase.osgi>3.2.5</couchbase.osgi>
2323
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
2424
<java-module-name>spring.data.couchbase</java-module-name>
25-
<apt>1.1.3</apt>
26-
<querydsl>5.0.0</querydsl>
27-
<mysema.querydsl>3.7.4</mysema.querydsl>
2825
</properties>
2926

3027
<dependencyManagement>
@@ -172,8 +169,6 @@
172169
<version>${javax-annotation-api}</version>
173170
</dependency>
174171

175-
<!-- -addmods java.annotations.common -->
176-
177172
<dependency>
178173
<groupId>org.apache.openwebbeans</groupId>
179174
<artifactId>openwebbeans-se</artifactId>
@@ -223,6 +218,12 @@
223218
<version>4.0.3</version>
224219
<scope>test</scope>
225220
</dependency>
221+
<dependency>
222+
<groupId>ch.qos.logback</groupId>
223+
<artifactId>logback-classic</artifactId>
224+
<version>1.2.10</version>
225+
<scope>test</scope>
226+
</dependency>
226227

227228
</dependencies>
228229

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
//
2+
// Source code recreated from a .class file by IntelliJ IDEA
3+
// (powered by FernFlower decompiler)
4+
//
5+
6+
package com.querydsl.couchbase;
7+
8+
import com.mysema.commons.lang.CloseableIterator;
9+
import com.querydsl.core.DefaultQueryMetadata;
10+
import com.querydsl.core.Fetchable;
11+
import com.querydsl.core.JoinExpression;
12+
import com.querydsl.core.NonUniqueResultException;
13+
import com.querydsl.core.QueryMetadata;
14+
import com.querydsl.core.QueryModifiers;
15+
import com.querydsl.core.QueryResults;
16+
import com.querydsl.core.SimpleQuery;
17+
import com.querydsl.core.support.QueryMixin;
18+
import com.querydsl.core.types.Expression;
19+
import com.querydsl.core.types.ExpressionUtils;
20+
import com.querydsl.core.types.FactoryExpression;
21+
import com.querydsl.core.types.Operation;
22+
import com.querydsl.core.types.OrderSpecifier;
23+
import com.querydsl.core.types.ParamExpression;
24+
import com.querydsl.core.types.Path;
25+
import com.querydsl.core.types.Predicate;
26+
import com.querydsl.core.types.dsl.CollectionPathBase;
27+
import java.util.ArrayList;
28+
import java.util.Collection;
29+
import java.util.Collections;
30+
import java.util.HashMap;
31+
import java.util.Iterator;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.function.Function;
35+
36+
import com.querydsl.couchbase.document.CouchbaseDocumentSerializer;
37+
import org.jetbrains.annotations.Nullable;
38+
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
39+
40+
public abstract class AbstractCouchbaseQuery<K, Q extends AbstractCouchbaseQuery<K, Q>> implements SimpleQuery<Q>, Fetchable<K> {
41+
private final CouchbaseDocumentSerializer serializer;
42+
private final QueryMixin<Q> queryMixin = new QueryMixin(this, new DefaultQueryMetadata(), false);
43+
private final Collection collection;
44+
private final Function<DBObject, K> transformer;
45+
46+
public AbstractCouchbaseQuery(Collection collection, Function<DBObject, K> transformer, CouchbaseDocumentSerializer serializer) {
47+
this.transformer = transformer;
48+
this.collection = collection;
49+
this.serializer = serializer;
50+
}
51+
52+
public <T> JoinBuilder<Q, K, T> join(Path<T> ref, Path<T> target) {
53+
return new JoinBuilder(this.queryMixin, ref, target);
54+
}
55+
56+
public <T> JoinBuilder<Q, K, T> join(CollectionPathBase<?, T, ?> ref, Path<T> target) {
57+
return new JoinBuilder(this.queryMixin, ref, target);
58+
}
59+
60+
public <T> AnyEmbeddedBuilder<Q, K> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
61+
return new AnyEmbeddedBuilder(this.queryMixin, collection);
62+
}
63+
64+
protected abstract com.couchbase.client.java.Collection getCollection(Class<?> var1);
65+
66+
@Nullable
67+
protected Predicate createFilter(QueryMetadata metadata) {
68+
Predicate filter;
69+
if (!metadata.getJoins().isEmpty()) {
70+
filter = ExpressionUtils.allOf(new Predicate[]{metadata.getWhere(), this.createJoinFilter(metadata)});
71+
} else {
72+
filter = metadata.getWhere();
73+
}
74+
75+
return filter;
76+
}
77+
78+
@Nullable
79+
protected Predicate createJoinFilter(QueryMetadata metadata) {
80+
Map<Expression<?>, Predicate> predicates = new HashMap();
81+
List<JoinExpression> joins = metadata.getJoins();
82+
83+
for(int i = joins.size() - 1; i >= 0; --i) {
84+
JoinExpression join = (JoinExpression)joins.get(i);
85+
Path<?> source = (Path)((Operation)join.getTarget()).getArg(0);
86+
Path<?> target = (Path)((Operation)join.getTarget()).getArg(1);
87+
Predicate extraFilters = (Predicate)predicates.get(target.getRoot());
88+
Predicate filter = ExpressionUtils.allOf(new Predicate[]{join.getCondition(), extraFilters});
89+
List<? extends Object> ids = this.getIds(target.getType(), filter);
90+
if (ids.isEmpty()) {
91+
throw new AbstractCouchbaseQuery.NoResults();
92+
}
93+
94+
Path<?> path = ExpressionUtils.path(String.class, source, "$id");
95+
predicates.merge(source.getRoot(), ExpressionUtils.in((Expression<Object>)path, ids), ExpressionUtils::and);
96+
}
97+
98+
Path<?> source = (Path)((Operation)((JoinExpression)joins.get(0)).getTarget()).getArg(0);
99+
return (Predicate)predicates.get(source.getRoot());
100+
}
101+
/*
102+
protected List<Object> getIds(Class<?> targetType, Predicate condition) {
103+
Collection collection = this.getCollection(targetType);
104+
DBCursor cursor = this.createCursor(collection, condition, (Expression)null, QueryModifiers.EMPTY, Collections.emptyList());
105+
if (!cursor.hasNext()) {
106+
return Collections.emptyList();
107+
} else {
108+
List<Object> ids = new ArrayList(cursor.count());
109+
Iterator var6 = cursor.iterator();
110+
111+
while(var6.hasNext()) {
112+
DBObject obj = (DBObject)var6.next();
113+
ids.add(obj.get("_id"));
114+
}
115+
116+
return ids;
117+
}
118+
}
119+
*/
120+
121+
public Q distinct() {
122+
return this.queryMixin.distinct();
123+
}
124+
125+
public Q where(Predicate e) {
126+
return this.queryMixin.where(e);
127+
}
128+
129+
public Q where(Predicate... e) {
130+
return this.queryMixin.where(e);
131+
}
132+
133+
public Q limit(long limit) {
134+
return this.queryMixin.limit(limit);
135+
}
136+
137+
public Q offset(long offset) {
138+
return this.queryMixin.offset(offset);
139+
}
140+
141+
public Q restrict(QueryModifiers modifiers) {
142+
return this.queryMixin.restrict(modifiers);
143+
}
144+
145+
public Q orderBy(OrderSpecifier<?> o) {
146+
return this.queryMixin.orderBy(o);
147+
}
148+
149+
public Q orderBy(OrderSpecifier<?>... o) {
150+
return this.queryMixin.orderBy(o);
151+
}
152+
153+
public <T> Q set(ParamExpression<T> param, T value) {
154+
return this.queryMixin.set(param, value);
155+
}
156+
157+
public CloseableIterator<K> iterate(Path<?>... paths) {
158+
this.queryMixin.setProjection(paths);
159+
return this.iterate();
160+
}
161+
162+
public CloseableIterator<K> iterate() {
163+
final DBCursor cursor = this.createCursor();
164+
return new CloseableIterator<K>() {
165+
public boolean hasNext() {
166+
return cursor.hasNext();
167+
}
168+
169+
public K next() {
170+
return AbstractCouchbaseQuery.this.transformer.apply(cursor.next());
171+
}
172+
173+
public void remove() {
174+
}
175+
176+
public void close() {
177+
}
178+
};
179+
}
180+
181+
public List<K> fetch(Path<?>... paths) {
182+
this.queryMixin.setProjection(paths);
183+
return this.fetch();
184+
}
185+
186+
public List<K> fetch() {
187+
try {
188+
DBCursor cursor = this.createCursor();
189+
List<K> results = new ArrayList();
190+
Iterator var3 = cursor.iterator();
191+
192+
while(var3.hasNext()) {
193+
DBObject dbObject = (DBObject)var3.next();
194+
results.add(this.transformer.apply(dbObject));
195+
}
196+
197+
return results;
198+
} catch (AbstractMongodbQuery.NoResults var5) {
199+
return Collections.emptyList();
200+
}
201+
}
202+
203+
protected DBCursor createCursor() {
204+
QueryMetadata metadata = this.queryMixin.getMetadata();
205+
Predicate filter = this.createFilter(metadata);
206+
return this.createCursor(this.collection, filter, metadata.getProjection(), metadata.getModifiers(), metadata.getOrderBy());
207+
}
208+
209+
protected DBCursor createCursor(DBCollection collection, @Nullable Predicate where, Expression<?> projection, QueryModifiers modifiers, List<OrderSpecifier<?>> orderBy) {
210+
DBCursor cursor = collection.find(this.createQuery(where), this.createProjection(projection));
211+
Integer limit = modifiers.getLimitAsInteger();
212+
Integer offset = modifiers.getOffsetAsInteger();
213+
if (limit != null) {
214+
cursor.limit(limit);
215+
}
216+
217+
if (offset != null) {
218+
cursor.skip(offset);
219+
}
220+
221+
if (orderBy.size() > 0) {
222+
cursor.sort(this.serializer.toSort(orderBy));
223+
}
224+
225+
return cursor;
226+
}
227+
228+
private DBObject createProjection(Expression<?> projection) {
229+
if (projection instanceof FactoryExpression) {
230+
DBObject obj = new CouchbaseDocument();
231+
Iterator var3 = ((FactoryExpression)projection).getArgs().iterator();
232+
233+
while(var3.hasNext()) {
234+
Object expr = var3.next();
235+
if (expr instanceof Expression) {
236+
obj.put((String)this.serializer.handle((Expression)expr), 1);
237+
}
238+
}
239+
240+
return obj;
241+
} else {
242+
return null;
243+
}
244+
}
245+
246+
public K fetchFirst(Path<?>... paths) {
247+
this.queryMixin.setProjection(paths);
248+
return this.fetchFirst();
249+
}
250+
251+
public K fetchFirst() {
252+
try {
253+
DBCursor c = this.createCursor().limit(1);
254+
return c.hasNext() ? this.transformer.apply(c.next()) : null;
255+
} catch (AbstractCouchbaseQuery.NoResults var2) {
256+
return null;
257+
}
258+
}
259+
260+
public K fetchOne(Path<?>... paths) {
261+
this.queryMixin.setProjection(paths);
262+
return this.fetchOne();
263+
}
264+
265+
public K fetchOne() throws NonUniqueResultException {
266+
try {
267+
Long limit = this.queryMixin.getMetadata().getModifiers().getLimit();
268+
if (limit == null) {
269+
limit = 2L;
270+
}
271+
272+
DBCursor c = this.createCursor().limit(limit.intValue());
273+
if (c.hasNext()) {
274+
K rv = this.transformer.apply(c.next());
275+
if (c.hasNext()) {
276+
throw new NonUniqueResultException();
277+
} else {
278+
return rv;
279+
}
280+
} else {
281+
return null;
282+
}
283+
} catch (AbstractCouchbaseQuery.NoResults var4) {
284+
return null;
285+
}
286+
}
287+
288+
public QueryResults<K> fetchResults(Path<?>... paths) {
289+
this.queryMixin.setProjection(paths);
290+
return this.fetchResults();
291+
}
292+
293+
public QueryResults<K> fetchResults() {
294+
try {
295+
long total = this.fetchCount();
296+
return total > 0L ? new QueryResults(this.fetch(), this.queryMixin.getMetadata().getModifiers(), total) : QueryResults.emptyResults();
297+
} catch (AbstractCouchbaseQuery.NoResults var3) {
298+
return QueryResults.emptyResults();
299+
}
300+
}
301+
302+
public long fetchCount() {
303+
try {
304+
Predicate filter = this.createFilter(this.queryMixin.getMetadata());
305+
return this.collection.count(this.createQuery(filter));
306+
} catch (AbstractCouchbaseQuery.NoResults var2) {
307+
return 0L;
308+
}
309+
}
310+
311+
private DBObject createQuery(@Nullable Predicate predicate) {
312+
return (DBObject)(predicate != null ? (DBObject)this.serializer.handle(predicate) : new CouchbaseDocument());
313+
}
314+
315+
public DBObject asDBObject() {
316+
return this.createQuery(this.queryMixin.getMetadata().getWhere());
317+
}
318+
319+
public String toString() {
320+
return this.asDBObject().toString();
321+
}
322+
323+
private static class NoResults extends RuntimeException {
324+
private NoResults() {
325+
}
326+
}
327+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Source code recreated from a .class file by IntelliJ IDEA
3+
// (powered by FernFlower decompiler)
4+
//
5+
6+
package com.querydsl.couchbase;
7+
8+
import com.querydsl.core.support.QueryMixin;
9+
import com.querydsl.core.types.Expression;
10+
import com.querydsl.core.types.ExpressionUtils;
11+
import com.querydsl.core.types.Path;
12+
import com.querydsl.core.types.Predicate;
13+
import com.querydsl.couchbase.document.CouchbaseOps;
14+
15+
import java.util.Collection;
16+
17+
public class AnyEmbeddedBuilder<Q extends AbstractCouchbaseQuery<K, Q>, K> {
18+
private final QueryMixin<Q> queryMixin;
19+
private final Path<? extends Collection<?>> collection;
20+
21+
public AnyEmbeddedBuilder(QueryMixin<Q> queryMixin, Path<? extends Collection<?>> collection) {
22+
this.queryMixin = queryMixin;
23+
this.collection = collection;
24+
}
25+
26+
public Q on(Predicate... conditions) {
27+
return this.queryMixin.where(ExpressionUtils.predicate(CouchbaseOps.ELEM_MATCH, new Expression[]{this.collection, ExpressionUtils.allOf(conditions)}));
28+
}
29+
}

0 commit comments

Comments
 (0)