Skip to content

Provide ReactiveCypherdslConditionExecutor. #2577

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

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/main/asciidoc/object-mapping/sdn-extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Additional mixins provided are:
* `QuerydslPredicateExecutor`
* `CypherdslConditionExecutor`
* `CypherdslStatementExecutor`
* `ReactiveQuerydslPredicateExecutor`
* `ReactiveCypherdslConditionExecutor`
* `ReactiveCypherdslStatementExecutor`

[[sdn-mixins.dynamic-conditions]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ public long count(Condition condition) {

@Override
public boolean exists(Condition condition) {
Statement statement = CypherGenerator.INSTANCE.prepareMatchOf(this.metaData, condition)
.returning(Functions.count(asterisk())).build();
return this.neo4jOperations.count(statement, statement.getParameters()) > 0;
return count(condition) > 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2011-2022 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.neo4j.repository.query;

import static org.neo4j.cypherdsl.core.Cypher.asterisk;

import java.util.Arrays;

import org.apiguardian.api.API;
import org.neo4j.cypherdsl.core.Condition;
import org.neo4j.cypherdsl.core.Conditions;
import org.neo4j.cypherdsl.core.Functions;
import org.neo4j.cypherdsl.core.SortItem;
import org.neo4j.cypherdsl.core.Statement;
import org.springframework.data.domain.Sort;
import org.springframework.data.neo4j.core.ReactiveNeo4jOperations;
import org.springframework.data.neo4j.core.mapping.CypherGenerator;
import org.springframework.data.neo4j.core.mapping.Neo4jPersistentEntity;
import org.springframework.data.neo4j.repository.support.ReactiveCypherdslConditionExecutor;
import org.springframework.data.neo4j.repository.support.Neo4jEntityInformation;

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

/**
* @author Niklas Krieger
* @author Michael J. Simons
* @param <T> The returned domain type.
* @since 6.3.3
*/
@API(status = API.Status.INTERNAL, since = "6.3.3")
public final class ReactiveCypherdslConditionExecutorImpl<T> implements ReactiveCypherdslConditionExecutor<T> {

private final Neo4jEntityInformation<T, Object> entityInformation;

private final ReactiveNeo4jOperations neo4jOperations;

private final Neo4jPersistentEntity<T> metaData;

public ReactiveCypherdslConditionExecutorImpl(Neo4jEntityInformation<T, Object> entityInformation,
ReactiveNeo4jOperations neo4jOperations) {

this.entityInformation = entityInformation;
this.neo4jOperations = neo4jOperations;
this.metaData = this.entityInformation.getEntityMetaData();
}

@Override
public Mono<T> findOne(Condition condition) {

return this.neo4jOperations.toExecutableQuery(
this.metaData.getType(),
QueryFragmentsAndParameters.forCondition(this.metaData, condition, null, null)
).flatMap(ReactiveNeo4jOperations.ExecutableQuery::getSingleResult);
}

@Override
public Flux<T> findAll(Condition condition) {

return this.neo4jOperations.toExecutableQuery(
this.metaData.getType(),
QueryFragmentsAndParameters.forCondition(this.metaData, condition, null, null)
).flatMapMany(ReactiveNeo4jOperations.ExecutableQuery::getResults);
}

@Override
public Flux<T> findAll(Condition condition, Sort sort) {

return this.neo4jOperations.toExecutableQuery(
metaData.getType(),
QueryFragmentsAndParameters.forCondition(
this.metaData, condition, null, CypherAdapterUtils.toSortItems(this.metaData, sort)
)
).flatMapMany(ReactiveNeo4jOperations.ExecutableQuery::getResults);
}

@Override
public Flux<T> findAll(Condition condition, SortItem... sortItems) {

return this.neo4jOperations.toExecutableQuery(
this.metaData.getType(),
QueryFragmentsAndParameters.forCondition(
this.metaData, condition, null, Arrays.asList(sortItems)
)
).flatMapMany(ReactiveNeo4jOperations.ExecutableQuery::getResults);
}

@Override
public Flux<T> findAll(SortItem... sortItems) {

return this.neo4jOperations.toExecutableQuery(
this.metaData.getType(),
QueryFragmentsAndParameters.forCondition(this.metaData, Conditions.noCondition(), null,
Arrays.asList(sortItems))
).flatMapMany(ReactiveNeo4jOperations.ExecutableQuery::getResults);
}

@Override
public Mono<Long> count(Condition condition) {

Statement statement = CypherGenerator.INSTANCE.prepareMatchOf(this.metaData, condition)
.returning(Functions.count(asterisk())).build();
return this.neo4jOperations.count(statement, statement.getParameters());
}

@Override
public Mono<Boolean> exists(Condition condition) {
return count(condition).map(count -> count > 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2011-2022 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.neo4j.repository.support;

import org.apiguardian.api.API;
import org.neo4j.cypherdsl.core.Condition;
import org.neo4j.cypherdsl.core.SortItem;
import org.springframework.data.domain.Sort;

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

/**
* An interface that can be added to any repository so that queries can be enriched by {@link Condition conditions} of the
* Cypher-DSL. This interface behaves the same as the {@link org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor}.
*
* @author Niklas Krieger
* @author Michael J. Simons
* @param <T> Type of the domain
* @since 6.3.3
*/
@API(status = API.Status.STABLE, since = "6.3.3")
public interface ReactiveCypherdslConditionExecutor<T> {

Mono<T> findOne(Condition condition);

Flux<T> findAll(Condition condition);

Flux<T> findAll(Condition condition, Sort sort);

Flux<T> findAll(Condition condition, SortItem... sortItems);

Flux<T> findAll(SortItem... sortItems);

Mono<Long> count(Condition condition);

Mono<Boolean> exists(Condition condition);
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.data.neo4j.repository.query.ReactiveNeo4jQueryLookupStrategy;
import org.springframework.data.neo4j.repository.query.ReactiveQuerydslNeo4jPredicateExecutor;
import org.springframework.data.neo4j.repository.query.SimpleReactiveQueryByExampleExecutor;
import org.springframework.data.neo4j.repository.query.ReactiveCypherdslConditionExecutorImpl;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.querydsl.QuerydslUtils;
Expand All @@ -45,6 +46,7 @@
*
* @author Gerrit Meier
* @author Michael J. Simons
* @author Niklas Krieger
* @since 6.0
*/
final class ReactiveNeo4jRepositoryFactory extends ReactiveRepositoryFactorySupport {
Expand Down Expand Up @@ -93,6 +95,11 @@ protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata
fragments = fragments.append(createDSLExecutorFragment(metadata, ReactiveQuerydslNeo4jPredicateExecutor.class));
}

if (ReactiveCypherdslConditionExecutor.class.isAssignableFrom(metadata.getRepositoryInterface())) {

fragments = fragments.append(createDSLExecutorFragment(metadata, ReactiveCypherdslConditionExecutorImpl.class));
}

return fragments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,11 @@ class CypherdslConditionExecutorIT {

protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;

private final Driver driver;
private final BookmarkCapture bookmarkCapture;
private final Node person;
private final Property firstName;
private final Property lastName;

@Autowired
CypherdslConditionExecutorIT(Driver driver, BookmarkCapture bookmarkCapture) {

this.driver = driver;
this.bookmarkCapture = bookmarkCapture;
CypherdslConditionExecutorIT() {

//CHECKSTYLE:OFF
// tag::sdn-mixins.dynamic-conditions.usage[]
Expand All @@ -78,7 +72,6 @@ class CypherdslConditionExecutorIT {
// end::sdn-mixins.dynamic-conditions.usage[]
//CHECKSTYLE:ON

this.person = person;
this.firstName = firstName;
this.lastName = lastName;
}
Expand Down
Loading