21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
24
+ import com .querydsl .core .types .dsl .CollectionPathBase ;
24
25
import org .jetbrains .annotations .Nullable ;
25
26
import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
26
27
import org .springframework .data .couchbase .core .query .QueryCriteriaDefinition ;
47
48
* @author Michael Reiche
48
49
*/
49
50
50
- public abstract class AbstractCouchbaseQueryDSL <Q extends AbstractCouchbaseQueryDSL <Q >> implements SimpleQuery <Q > {
51
+ public abstract class AbstractCouchbaseQuery <Q extends AbstractCouchbaseQuery <Q >> implements SimpleQuery <Q > {
51
52
private final CouchbaseDocumentSerializer serializer ;
52
53
private final QueryMixin <Q > queryMixin ;// = new QueryMixin(this, new DefaultQueryMetadata(), false);
53
- // TODO private ReadPreference readPreference;
54
54
55
- public AbstractCouchbaseQueryDSL (CouchbaseDocumentSerializer serializer ) {
55
+ public AbstractCouchbaseQuery (CouchbaseDocumentSerializer serializer ) {
56
56
this .serializer = serializer ;
57
57
@ SuppressWarnings ("unchecked" ) // Q is this plus subclass
58
58
Q query = (Q ) this ;
59
59
this .queryMixin = new QueryMixin <Q >(query , new DefaultQueryMetadata (), false );
60
60
}
61
61
62
62
/**
63
- * mongodb uses createQuery(Predicate filter) where the serializer creates the 'query' <br>
64
- * and then uses the result to create a BasicQuery with queryObject = result <br>
65
63
* Couchbase Query has a 'criteria' which is a <br>
66
64
* List<QueryCriteriaDefinition> criteria <br>
67
65
* so we could create a List<QueryCriteriaDefinition> or an uber QueryCriteria that combines <br>
68
66
* all the sub QueryDefinitions in the filter.
69
67
*/
70
68
protected QueryCriteriaDefinition createCriteria (Predicate predicate ) {
71
- // mongodb uses createQuery(Predicate filter) where the serializer creates the 'queryObject' of the BasicQuery
72
69
return predicate != null ? (QueryCriteriaDefinition ) this .serializer .handle (predicate ) : null ;
73
70
}
74
71
75
- // TODO - need later
76
- // public <T> JoinBuilder<Q, T> join(Path<T> ref, Path<T> target) {
77
- // return new JoinBuilder(this.queryMixin, ref, target);
78
- // }
72
+ public <T > JoinBuilder <Q , T > join (Path <T > ref , Path <T > target ) {
73
+ return new JoinBuilder (this .queryMixin , ref , target );
74
+ }
79
75
80
- // public <T> JoinBuilder<Q, T> join(CollectionPathBase<?, T, ?> ref, Path<T> target) {
81
- // return new JoinBuilder(this.queryMixin, ref, target);
82
- // }
76
+ public <T > JoinBuilder <Q , T > join (CollectionPathBase <?, T , ?> ref , Path <T > target ) {
77
+ return new JoinBuilder (this .queryMixin , ref , target );
78
+ }
83
79
84
- // public <T> AnyEmbeddedBuilder<Q> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
85
- // return new AnyEmbeddedBuilder(this.queryMixin, collection);
86
- // }
80
+ public <T > AnyEmbeddedBuilder <Q > anyEmbedded (Path <? extends Collection <T >> collection , Path <T > target ) {
81
+ return new AnyEmbeddedBuilder (this .queryMixin , collection );
82
+ }
87
83
88
84
@ Nullable
89
85
protected Predicate createFilter (QueryMetadata metadata ) {
@@ -107,12 +103,13 @@ protected Predicate createJoinFilter(QueryMetadata metadata) {
107
103
Path <?> target = (Path ) ((Operation ) join .getTarget ()).getArg (1 );
108
104
Predicate extraFilters = (Predicate ) predicates .get (target .getRoot ());
109
105
Predicate filter = ExpressionUtils .allOf (new Predicate [] { join .getCondition (), extraFilters });
110
- List <? extends Object > ids = this .getIds (target . getType () , filter );
106
+ List <? extends Object > ids = this .getIds (source , target , filter );
111
107
if (ids .isEmpty ()) {
112
- throw new AbstractCouchbaseQueryDSL .NoResults ();
108
+ throw new AbstractCouchbaseQuery .NoResults ();
113
109
}
114
110
115
- Path <?> path = ExpressionUtils .path (String .class , source , "$id" );
111
+ Path <?> path = ExpressionUtils .path (String .class , source , "id" );
112
+ path = ExpressionUtils .path (String .class , "meta().id" );
116
113
predicates .merge (source .getRoot (),
117
114
ExpressionUtils .in (path , (Collection ) ids /* TODO was just ids without casting to Collection */ ),
118
115
ExpressionUtils ::and );
@@ -126,7 +123,10 @@ private Predicate allOf(Collection<Predicate> predicates) {
126
123
return predicates != null ? ExpressionUtils .allOf (predicates ) : null ;
127
124
}
128
125
129
- protected abstract List <Object > getIds (Class <?> var1 , Predicate var2 );
126
+ //protected abstract List<Object> getIds(Class<?> var1, Predicate var2);
127
+
128
+ protected abstract List <Object > getIds (Path <?> source , Path <?> target , Predicate var2 );
129
+
130
130
131
131
public Q distinct () {
132
132
return this .queryMixin .distinct ();
@@ -182,13 +182,9 @@ protected Map<String, String> createProjection(Expression<?> projection) {
182
182
}
183
183
184
184
protected CouchbaseDocument createQuery (@ Nullable Predicate predicate ) {
185
- return predicate != null ? ( CouchbaseDocument ) this .serializer .handle (predicate ) : new CouchbaseDocument ();
185
+ return predicate != null ? new CouchbaseDocument ( this .serializer .handle (predicate ). toString () ) : new CouchbaseDocument ();
186
186
}
187
187
188
- // public void setReadPreference(ReadPreference readPreference) {
189
- // this.readPreference = readPreference;
190
- // }
191
-
192
188
protected QueryMixin <Q > getQueryMixin () {
193
189
return this .queryMixin ;
194
190
}
@@ -197,10 +193,6 @@ protected CouchbaseDocumentSerializer getSerializer() {
197
193
return this .serializer ;
198
194
}
199
195
200
- // protected ReadPreference getReadPreference() {
201
- // return this.readPreference;
202
- // }
203
-
204
196
public CouchbaseDocument asDocument () {
205
197
return this .createQuery (this .queryMixin .getMetadata ().getWhere ());
206
198
}
0 commit comments