|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.repository.support;
|
17 | 17 |
|
| 18 | +import java.util.Collections; |
18 | 19 | import java.util.List;
|
19 | 20 | import java.util.Optional;
|
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.stream.Stream; |
20 | 23 |
|
| 24 | +import org.bson.Document; |
21 | 25 | import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
22 | 26 | import org.springframework.data.domain.Page;
|
23 | 27 | import org.springframework.data.domain.Pageable;
|
24 | 28 | import org.springframework.data.domain.Sort;
|
25 | 29 | import org.springframework.data.mongodb.core.MongoOperations;
|
| 30 | +import org.springframework.data.mongodb.core.query.BasicQuery; |
26 | 31 | import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
|
27 | 32 | import org.springframework.data.querydsl.EntityPathResolver;
|
28 | 33 | import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
29 | 34 | import org.springframework.data.querydsl.SimpleEntityPathResolver;
|
| 35 | +import org.springframework.data.repository.query.FluentQuery; |
30 | 36 | import org.springframework.data.support.PageableExecutionUtils;
|
31 | 37 | import org.springframework.util.Assert;
|
32 | 38 |
|
@@ -184,6 +190,21 @@ public boolean exists(Predicate predicate) {
|
184 | 190 | return createQueryFor(predicate).fetchCount() > 0;
|
185 | 191 | }
|
186 | 192 |
|
| 193 | + /* |
| 194 | + * (non-Javadoc) |
| 195 | + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findBy(com.querydsl.core.types.Predicate, java.util.function.Function) |
| 196 | + */ |
| 197 | + @Override |
| 198 | + @SuppressWarnings("unchecked") |
| 199 | + public <S extends T, R> R findBy(Predicate predicate, |
| 200 | + Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) { |
| 201 | + |
| 202 | + Assert.notNull(predicate, "Predicate must not be null!"); |
| 203 | + Assert.notNull(queryFunction, "Query function must not be null!"); |
| 204 | + |
| 205 | + return queryFunction.apply(new FluentQuerydsl<>(predicate, (Class<S>) typeInformation().getJavaType())); |
| 206 | + } |
| 207 | + |
187 | 208 | /**
|
188 | 209 | * Creates a {@link SpringDataMongodbQuery} for the given {@link Predicate}.
|
189 | 210 | *
|
@@ -232,4 +253,113 @@ private SpringDataMongodbQuery<T> applySorting(SpringDataMongodbQuery<T> query,
|
232 | 253 | toOrderSpecifiers(sort).forEach(query::orderBy);
|
233 | 254 | return query;
|
234 | 255 | }
|
| 256 | + |
| 257 | + /** |
| 258 | + * {@link org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery} using Querydsl |
| 259 | + * {@link Predicate}. |
| 260 | + * |
| 261 | + * @author Mark Paluch |
| 262 | + * @since 3.3 |
| 263 | + */ |
| 264 | + class FluentQuerydsl<T> extends FetchableFluentQuerySupport<Predicate, T> { |
| 265 | + |
| 266 | + FluentQuerydsl(Predicate predicate, Class<T> resultType) { |
| 267 | + this(predicate, Sort.unsorted(), resultType, Collections.emptyList()); |
| 268 | + } |
| 269 | + |
| 270 | + FluentQuerydsl(Predicate predicate, Sort sort, Class<T> resultType, List<String> fieldsToInclude) { |
| 271 | + super(predicate, sort, resultType, fieldsToInclude); |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + protected <R> FluentQuerydsl<R> create(Predicate predicate, Sort sort, Class<R> resultType, |
| 276 | + List<String> fieldsToInclude) { |
| 277 | + return new FluentQuerydsl<>(predicate, sort, resultType, fieldsToInclude); |
| 278 | + } |
| 279 | + |
| 280 | + /* |
| 281 | + * (non-Javadoc) |
| 282 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#oneValue() |
| 283 | + */ |
| 284 | + @Override |
| 285 | + public T oneValue() { |
| 286 | + return createQuery().fetchOne(); |
| 287 | + } |
| 288 | + |
| 289 | + /* |
| 290 | + * (non-Javadoc) |
| 291 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#firstValue() |
| 292 | + */ |
| 293 | + @Override |
| 294 | + public T firstValue() { |
| 295 | + return createQuery().fetchFirst(); |
| 296 | + } |
| 297 | + |
| 298 | + /* |
| 299 | + * (non-Javadoc) |
| 300 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all() |
| 301 | + */ |
| 302 | + @Override |
| 303 | + public List<T> all() { |
| 304 | + return createQuery().fetch(); |
| 305 | + } |
| 306 | + |
| 307 | + /* |
| 308 | + * (non-Javadoc) |
| 309 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#page(org.springframework.data.domain.Pageable) |
| 310 | + */ |
| 311 | + @Override |
| 312 | + public Page<T> page(Pageable pageable) { |
| 313 | + |
| 314 | + Assert.notNull(pageable, "Pageable must not be null!"); |
| 315 | + |
| 316 | + return createQuery().fetchPage(pageable); |
| 317 | + } |
| 318 | + |
| 319 | + /* |
| 320 | + * (non-Javadoc) |
| 321 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#stream() |
| 322 | + */ |
| 323 | + @Override |
| 324 | + public Stream<T> stream() { |
| 325 | + return createQuery().stream(); |
| 326 | + } |
| 327 | + |
| 328 | + /* |
| 329 | + * (non-Javadoc) |
| 330 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#count() |
| 331 | + */ |
| 332 | + @Override |
| 333 | + public long count() { |
| 334 | + return createQuery().fetchCount(); |
| 335 | + } |
| 336 | + |
| 337 | + /* |
| 338 | + * (non-Javadoc) |
| 339 | + * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#exists() |
| 340 | + */ |
| 341 | + @Override |
| 342 | + public boolean exists() { |
| 343 | + return count() > 0; |
| 344 | + } |
| 345 | + |
| 346 | + private SpringDataMongodbQuery<T> createQuery() { |
| 347 | + return new SpringDataMongodbQuery<>(mongoOperations, typeInformation().getJavaType(), getResultType(), |
| 348 | + mongoOperations.getCollectionName(typeInformation().getJavaType()), this::customize).where(getPredicate()); |
| 349 | + } |
| 350 | + |
| 351 | + private void customize(BasicQuery query) { |
| 352 | + |
| 353 | + List<String> fieldsToInclude = getFieldsToInclude(); |
| 354 | + if (!fieldsToInclude.isEmpty()) { |
| 355 | + Document fields = new Document(); |
| 356 | + fieldsToInclude.forEach(field -> fields.put(field, 1)); |
| 357 | + query.setFieldsObject(fields); |
| 358 | + } |
| 359 | + |
| 360 | + if (getSort().isSorted()) { |
| 361 | + query.with(getSort()); |
| 362 | + } |
| 363 | + } |
| 364 | + } |
235 | 365 | }
|
0 commit comments