2121import java .util .List ;
2222import java .util .Map ;
2323
24+ import com .querydsl .core .types .dsl .CollectionPathBase ;
2425import org .jetbrains .annotations .Nullable ;
2526import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
2627import org .springframework .data .couchbase .core .query .QueryCriteriaDefinition ;
4748 * @author Michael Reiche
4849 */
4950
50- public abstract class AbstractCouchbaseQueryDSL <Q extends AbstractCouchbaseQueryDSL <Q >> implements SimpleQuery <Q > {
51+ public abstract class AbstractCouchbaseQuery <Q extends AbstractCouchbaseQuery <Q >> implements SimpleQuery <Q > {
5152 private final CouchbaseDocumentSerializer serializer ;
5253 private final QueryMixin <Q > queryMixin ;// = new QueryMixin(this, new DefaultQueryMetadata(), false);
53- // TODO private ReadPreference readPreference;
5454
55- public AbstractCouchbaseQueryDSL (CouchbaseDocumentSerializer serializer ) {
55+ public AbstractCouchbaseQuery (CouchbaseDocumentSerializer serializer ) {
5656 this .serializer = serializer ;
5757 @ SuppressWarnings ("unchecked" ) // Q is this plus subclass
5858 Q query = (Q ) this ;
5959 this .queryMixin = new QueryMixin <Q >(query , new DefaultQueryMetadata (), false );
6060 }
6161
6262 /**
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>
6563 * Couchbase Query has a 'criteria' which is a <br>
6664 * List<QueryCriteriaDefinition> criteria <br>
6765 * so we could create a List<QueryCriteriaDefinition> or an uber QueryCriteria that combines <br>
6866 * all the sub QueryDefinitions in the filter.
6967 */
7068 protected QueryCriteriaDefinition createCriteria (Predicate predicate ) {
71- // mongodb uses createQuery(Predicate filter) where the serializer creates the 'queryObject' of the BasicQuery
7269 return predicate != null ? (QueryCriteriaDefinition ) this .serializer .handle (predicate ) : null ;
7370 }
7471
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+ }
7975
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+ }
8379
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+ }
8783
8884 @ Nullable
8985 protected Predicate createFilter (QueryMetadata metadata ) {
@@ -107,12 +103,13 @@ protected Predicate createJoinFilter(QueryMetadata metadata) {
107103 Path <?> target = (Path ) ((Operation ) join .getTarget ()).getArg (1 );
108104 Predicate extraFilters = (Predicate ) predicates .get (target .getRoot ());
109105 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 );
111107 if (ids .isEmpty ()) {
112- throw new AbstractCouchbaseQueryDSL .NoResults ();
108+ throw new AbstractCouchbaseQuery .NoResults ();
113109 }
114110
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" );
116113 predicates .merge (source .getRoot (),
117114 ExpressionUtils .in (path , (Collection ) ids /* TODO was just ids without casting to Collection */ ),
118115 ExpressionUtils ::and );
@@ -126,7 +123,10 @@ private Predicate allOf(Collection<Predicate> predicates) {
126123 return predicates != null ? ExpressionUtils .allOf (predicates ) : null ;
127124 }
128125
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+
130130
131131 public Q distinct () {
132132 return this .queryMixin .distinct ();
@@ -182,13 +182,9 @@ protected Map<String, String> createProjection(Expression<?> projection) {
182182 }
183183
184184 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 ();
186186 }
187187
188- // public void setReadPreference(ReadPreference readPreference) {
189- // this.readPreference = readPreference;
190- // }
191-
192188 protected QueryMixin <Q > getQueryMixin () {
193189 return this .queryMixin ;
194190 }
@@ -197,10 +193,6 @@ protected CouchbaseDocumentSerializer getSerializer() {
197193 return this .serializer ;
198194 }
199195
200- // protected ReadPreference getReadPreference() {
201- // return this.readPreference;
202- // }
203-
204196 public CouchbaseDocument asDocument () {
205197 return this .createQuery (this .queryMixin .getMetadata ().getWhere ());
206198 }
0 commit comments