20
20
21
21
import org .springframework .data .couchbase .core .ReactiveFindByQueryOperationSupport .ReactiveFindByQuerySupport ;
22
22
import org .springframework .data .couchbase .core .query .Query ;
23
+ import org .springframework .util .Assert ;
23
24
24
25
import com .couchbase .client .java .query .QueryScanConsistency ;
25
26
@@ -41,26 +42,28 @@ public ExecutableFindByQueryOperationSupport(final CouchbaseTemplate template) {
41
42
42
43
@ Override
43
44
public <T > ExecutableFindByQuery <T > findByQuery (final Class <T > domainType ) {
44
- return new ExecutableFindByQuerySupport <>(template , domainType , ALL_QUERY , QueryScanConsistency . NOT_BOUNDED ,
45
- "_default._default" );
45
+ return new ExecutableFindByQuerySupport <T >(template , domainType , domainType , ALL_QUERY ,
46
+ QueryScanConsistency . NOT_BOUNDED , null );
46
47
}
47
48
48
49
static class ExecutableFindByQuerySupport <T > implements ExecutableFindByQuery <T > {
49
50
50
51
private final CouchbaseTemplate template ;
51
- private final Class <T > domainType ;
52
+ private final Class <?> domainType ;
53
+ private final Class <T > returnType ;
52
54
private final Query query ;
53
55
private final ReactiveFindByQuerySupport <T > reactiveSupport ;
54
56
private final QueryScanConsistency scanConsistency ;
55
57
private final String collection ;
56
58
57
- ExecutableFindByQuerySupport (final CouchbaseTemplate template , final Class <T > domainType , final Query query ,
58
- final QueryScanConsistency scanConsistency , final String collection ) {
59
+ ExecutableFindByQuerySupport (final CouchbaseTemplate template , final Class <? > domainType , final Class < T > returnType ,
60
+ final Query query , final QueryScanConsistency scanConsistency , final String collection ) {
59
61
this .template = template ;
60
62
this .domainType = domainType ;
63
+ this .returnType = returnType ;
61
64
this .query = query ;
62
- this .reactiveSupport = new ReactiveFindByQuerySupport <T >(template .reactive (), domainType , query , scanConsistency ,
63
- collection );
65
+ this .reactiveSupport = new ReactiveFindByQuerySupport <T >(template .reactive (), domainType , returnType , query ,
66
+ scanConsistency , collection );
64
67
this .scanConsistency = scanConsistency ;
65
68
this .collection = collection ;
66
69
}
@@ -88,17 +91,31 @@ public TerminatingFindByQuery<T> matching(final Query query) {
88
91
} else {
89
92
scanCons = scanConsistency ;
90
93
}
91
- return new ExecutableFindByQuerySupport <>(template , domainType , query , scanCons , collection );
94
+ return new ExecutableFindByQuerySupport <>(template , domainType , returnType , query , scanCons , collection );
92
95
}
93
96
94
97
@ Override
95
98
public FindByQueryConsistentWith <T > consistentWith (final QueryScanConsistency scanConsistency ) {
96
- return new ExecutableFindByQuerySupport <>(template , domainType , query , scanConsistency , collection );
99
+ return new ExecutableFindByQuerySupport <>(template , domainType , returnType , query , scanConsistency , collection );
97
100
}
98
101
99
102
@ Override
100
103
public FindByQueryInCollection <T > inCollection (final String collection ) {
101
- return new ExecutableFindByQuerySupport <>(template , domainType , query , scanConsistency , collection );
104
+ return new ExecutableFindByQuerySupport <>(template , domainType , returnType , query , scanConsistency , collection );
105
+ }
106
+
107
+ @ Override
108
+ public <R > FindWithProjection <R > as (Class <R > returnType ) {
109
+ Assert .notNull (returnType , "returnType must not be null!" );
110
+
111
+ return new ExecutableFindByQuerySupport <R >(template , domainType , returnType , query , scanConsistency , collection );
112
+ }
113
+
114
+ @ Override
115
+ public TerminatingDistinct <Object > distinct (String [] distinctFields ) {
116
+
117
+ Assert .notNull (distinctFields , "distinctFields must not be null!" );
118
+ return new DistinctOperationSupport (this , distinctFields );
102
119
}
103
120
104
121
@ Override
@@ -115,7 +132,83 @@ public long count() {
115
132
public boolean exists () {
116
133
return count () > 0 ;
117
134
}
135
+ }
136
+
137
+ /**
138
+ * @author Michael Reiche
139
+ */
140
+ static class DistinctOperationSupport <T > implements TerminatingDistinct <T > {
141
+
142
+ private final String [] distinctFields ;
143
+ private final ExecutableFindByQuerySupport <T > delegate ;
144
+
145
+ public DistinctOperationSupport (ExecutableFindByQuerySupport <T > delegate , String [] distinctFields ) {
146
+
147
+ this .delegate = delegate ;
148
+ this .distinctFields = distinctFields ;
149
+ }
150
+
151
+ /*
152
+ * (non-Javadoc)
153
+ * @see org.springframework.data.mongodb.core.ExecutableFindOperation.DistinctWithProjection#as(java.lang.Class)
154
+ */
155
+ @ Override
156
+ @ SuppressWarnings ("unchecked" )
157
+ public <R > TerminatingDistinct <R > as (Class <R > resultType ) {
158
+
159
+ Assert .notNull (resultType , "resultType must not be null!" );
160
+
161
+ return new DistinctOperationSupport <>((ExecutableFindByQuerySupport ) delegate .as (resultType ), distinctFields );
162
+ }
163
+
164
+ /*
165
+ * (non-Javadoc)
166
+ * @see org.springframework.data.mongodb.core.ExecutableFindOperation.DistinctWithQuery#matching(org.springframework.data.mongodb.core.query.Query)
167
+ */
168
+ @ Override
169
+ public TerminatingDistinct <T > matching (Query query ) {
170
+
171
+ Assert .notNull (query , "Query must not be null!" );
172
+
173
+ return new DistinctOperationSupport <>((ExecutableFindByQuerySupport <T >) delegate .matching (query ), distinctFields );
174
+ }
175
+
176
+ /*
177
+ * (non-Javadoc)
178
+ * @see org.springframework.data.mongodb.core.ExecutableFindOperation.DistinctWithProjection#as(java.lang.Class)
179
+ */
180
+ @ Override
181
+ @ SuppressWarnings ("unchecked" )
182
+ public TerminatingDistinct <Object > consistentWith (QueryScanConsistency scanConsistency ) {
183
+
184
+ Assert .notNull (scanConsistency , "scanConsistency must not be null!" );
185
+
186
+ return new DistinctOperationSupport <>((ExecutableFindByQuerySupport ) delegate .consistentWith (scanConsistency ),
187
+ distinctFields );
188
+ }
118
189
190
+ /*
191
+ * (non-Javadoc)
192
+ * @see org.springframework.data.mongodb.core.ExecutableFindOperation.TerminatingDistinct#all()
193
+ */
194
+ @ Override
195
+ public List <T > all () {
196
+ ReactiveFindByQueryOperationSupport .TerminatingDistinct <T > query = delegate .reactiveSupport
197
+ .distinct (distinctFields ).as (delegate .returnType );
198
+ return query .all ().collectList ().block ();
199
+ }
200
+
201
+ /**
202
+ * Get the number of matching elements.
203
+ *
204
+ * @return total number of matching elements.
205
+ */
206
+ @ Override
207
+ public long count () {
208
+ ReactiveFindByQueryOperationSupport .TerminatingDistinct <T > query = delegate .reactiveSupport
209
+ .distinct (distinctFields ).as (delegate .returnType );
210
+ return query .count ().block ();
211
+ }
119
212
}
120
213
121
214
}
0 commit comments