Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FluentQuery support to QuerydslPredicateExecutor and QueryByExampleExecutor #2421

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.6.0-SNAPSHOT</version>
<version>2.6.0-2228-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package org.springframework.data.querydsl;

import java.util.Optional;
import java.util.function.Function;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
Expand Down Expand Up @@ -108,4 +110,15 @@ public interface QuerydslPredicateExecutor<T> {
* @return {@literal true} if the data store contains elements that match the given {@link Predicate}.
*/
boolean exists(Predicate predicate);

/**
* Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param predicate must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Predicate}.
* @since 2.6
*/
<S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not defaulting it?

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Function;

import org.reactivestreams.Publisher;

import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
Expand Down Expand Up @@ -127,4 +132,16 @@ public interface ReactiveQuerydslPredicateExecutor<T> {
* @throws IllegalArgumentException if the required parameter is {@literal null}.
*/
Mono<Boolean> exists(Predicate predicate);

/**
* Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param predicate must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Predicate}.
* @since 2.6
*/
<S extends T, R, P extends Publisher<R>> P findBy(Predicate predicate,
Function<FluentQuery.ReactiveFluentQuery<S>, P> queryFunction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.query;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;

/**
* Fluent interface to define a query along with projection and sorting.
*
* @author Mark Paluch
* @since 2.6
*/
public interface FluentQuery<T> {

/**
* Define the sort order.
*
* @param sort must not be {@code null}.
* @return a new instance of {@link FluentQuery}.
* @throws IllegalArgumentException if resultType is {@code null}.
*/
FluentQuery<T> sortBy(Sort sort);

/**
* Define the target type the result should be mapped to. Skip this step if you are only interested in the original
* domain type.
*
* @param resultType must not be {@code null}.
* @param <R> result type.
* @return a new instance of {@link FluentQuery}.
* @throws IllegalArgumentException if resultType is {@code null}.
*/
<R> FluentQuery<R> as(Class<R> resultType);

/**
* Define which properties or property paths to include in the query.
*
* @param properties must not be {@code null}.
* @return a new instance of {@link FluentQuery}.
* @throws IllegalArgumentException if fields is {@code null}.
*/
default FluentQuery<T> project(String... properties) {
return project(Arrays.asList(properties));
}

/**
* Define which properties or property paths to include in the query.
*
* @param properties must not be {@code null}.
* @return a new instance of {@link FluentQuery}.
* @throws IllegalArgumentException if fields is {@code null}.
*/
FluentQuery<T> project(Collection<String> properties);

/**
* Fetchable extension {@link FluentQuery} allowing to materialize results from the underlying query.
*
* @author Mark Paluch
* @since 2.6
*/
interface FetchableFluentQuery<T> extends FluentQuery<T> {

@Override
FetchableFluentQuery<T> sortBy(Sort sort);

@Override
<R> FetchableFluentQuery<R> as(Class<R> resultType);

@Override
default FetchableFluentQuery<T> project(String... properties) {
return project(Arrays.asList(properties));
}

@Override
FetchableFluentQuery<T> project(Collection<String> properties);

/**
* Get exactly zero or one result.
*
* @return {@code null} if no match found.
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
*/
@Nullable
T one();

/**
* Get the first or no result.
*
* @return {@code null} if no match found.
*/
@Nullable
T first();

/**
* Get all matching elements.
*
* @return
*/
List<T> all();

/**
* Get a page of matching elements for {@link Pageable}.
*
* @param pageable must not be {@code null}. The given {@link Pageable} will override any previously specified
* {@link Sort sort} if the {@link Sort} object is not {@link Sort#isUnsorted()}.
* @return
*/
Page<T> page(Pageable pageable);

/**
* Stream all matching elements.
*
* @return a {@link Stream} wrapping cursors that need to be closed.
*/
Stream<T> stream();

/**
* Get the number of matching elements.
*
* @return total number of matching elements.
*/
long count();

/**
* Check for the presence of matching elements.
*
* @return {@literal true} if at least one matching element exists.
*/
boolean exists();
}

/**
* Reactive extension {@link FluentQuery} allowing to materialize results from the underlying query.
*
* @author Mark Paluch
* @since 2.6
*/
interface ReactiveFluentQuery<T> extends FluentQuery<T> {

@Override
ReactiveFluentQuery<T> sortBy(Sort sort);

@Override
<R> ReactiveFluentQuery<R> as(Class<R> resultType);

@Override
default ReactiveFluentQuery<T> project(String... properties) {
return project(Arrays.asList(properties));
}

@Override
ReactiveFluentQuery<T> project(Collection<String> properties);

/**
* Get exactly zero or one result.
*
* @return {@code null} if no match found.
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
*/
Mono<T> one();

/**
* Get the first or no result.
*
* @return {@code null} if no match found.
*/
Mono<T> first();

/**
* Get all matching elements.
*
* @return
*/
Flux<T> all();

/**
* Get a page of matching elements for {@link Pageable}.
*
* @param pageable must not be {@code null}. The given {@link Pageable} will override any previously specified
* {@link Sort sort} if the {@link Sort} object is not {@link Sort#isUnsorted()}.
* @return
*/
Mono<Page<T>> page(Pageable pageable);

/**
* Get the number of matching elements.
*
* @return total number of matching elements.
*/
Mono<Long> count();

/**
* Check for the presence of matching elements.
*
* @return {@literal true} if at least one matching element exists.
*/
Mono<Boolean> exists();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.repository.query;

import java.util.Optional;
import java.util.function.Function;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -86,4 +87,15 @@ public interface QueryByExampleExecutor<T> {
* @return {@literal true} if the data store contains elements that match the given {@link Example}.
*/
<S extends T> boolean exists(Example<S> example);

/**
* Returns entities matching the given {@link Example} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param example must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Example}.
* @since 2.6
*/
<S extends T, R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Function;

import org.reactivestreams.Publisher;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;

Expand Down Expand Up @@ -75,4 +79,16 @@ public interface ReactiveQueryByExampleExecutor<T> {
* @return {@literal true} if the data store contains elements that match the given {@link Example}.
*/
<S extends T> Mono<Boolean> exists(Example<S> example);

/**
* Returns entities matching the given {@link Example} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param example must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Example}.
* @since 2.6
*/
<S extends T, R, P extends Publisher<R>> P findBy(Example<S> example,
Function<FluentQuery.ReactiveFluentQuery<S>, P> queryFunction);
}