20
20
import org .neo4j .cypherdsl .core .Conditions ;
21
21
import org .neo4j .cypherdsl .core .Cypher ;
22
22
import org .neo4j .cypherdsl .core .Expression ;
23
+ import org .neo4j .cypherdsl .core .Functions ;
24
+ import org .neo4j .cypherdsl .core .Node ;
23
25
import org .neo4j .cypherdsl .core .PatternElement ;
26
+ import org .neo4j .cypherdsl .core .Relationship ;
24
27
import org .neo4j .cypherdsl .core .SortItem ;
25
28
import org .neo4j .cypherdsl .core .Statement ;
26
29
import org .neo4j .cypherdsl .core .StatementBuilder ;
41
44
import java .util .HashSet ;
42
45
import java .util .List ;
43
46
import java .util .Map ;
47
+ import java .util .Optional ;
44
48
import java .util .Set ;
45
49
46
50
import static org .neo4j .cypherdsl .core .Cypher .parameter ;
@@ -59,7 +63,8 @@ public final class QueryFragmentsAndParameters {
59
63
private final QueryFragments queryFragments ;
60
64
private final String cypherQuery ;
61
65
62
- public QueryFragmentsAndParameters (NodeDescription <?> nodeDescription , QueryFragments queryFragments , Map <String , Object > parameters ) {
66
+ public QueryFragmentsAndParameters (NodeDescription <?> nodeDescription , QueryFragments queryFragments ,
67
+ @ Nullable Map <String , Object > parameters ) {
63
68
this .nodeDescription = nodeDescription ;
64
69
this .queryFragments = queryFragments ;
65
70
this .parameters = parameters ;
@@ -150,8 +155,19 @@ static QueryFragmentsAndParameters forExample(Neo4jMappingContext mappingContext
150
155
Map <String , Object > parameters = predicate .getParameters ();
151
156
Condition condition = predicate .getCondition ();
152
157
153
- Neo4jPersistentEntity <?> entityMetaData = mappingContext .getPersistentEntity (example .getProbeType ());
158
+ return getQueryFragmentsAndParameters (mappingContext .getPersistentEntity (example .getProbeType ()), pageable ,
159
+ sort , parameters , condition );
160
+ }
161
+
162
+ public static QueryFragmentsAndParameters forPageableAndSort (Neo4jPersistentEntity <?> neo4jPersistentEntity ,
163
+ @ Nullable Pageable pageable , @ Nullable Sort sort ) {
154
164
165
+ return getQueryFragmentsAndParameters (neo4jPersistentEntity , pageable , sort , Collections .emptyMap (), null );
166
+ }
167
+
168
+ private static QueryFragmentsAndParameters getQueryFragmentsAndParameters (
169
+ Neo4jPersistentEntity <?> entityMetaData , @ Nullable Pageable pageable , @ Nullable Sort sort ,
170
+ @ Nullable Map <String , Object > parameters , @ Nullable Condition condition ) {
155
171
156
172
Expression [] returnStatement = cypherGenerator .createReturnStatementForMatch (entityMetaData );
157
173
@@ -172,7 +188,6 @@ static QueryFragmentsAndParameters forExample(Neo4jMappingContext mappingContext
172
188
}
173
189
174
190
return new QueryFragmentsAndParameters (entityMetaData , queryFragments , parameters );
175
-
176
191
}
177
192
178
193
/**
@@ -204,8 +219,8 @@ public List<PatternElement> getMatchOn() {
204
219
return matchOn ;
205
220
}
206
221
207
- public void setCondition (Condition condition ) {
208
- this .condition = condition ;
222
+ public void setCondition (@ Nullable Condition condition ) {
223
+ this .condition = Optional . ofNullable ( condition ). orElse ( Conditions . noCondition ()) ;
209
224
}
210
225
211
226
public Condition getCondition () {
@@ -260,6 +275,33 @@ private SortItem[] getOrderBy() {
260
275
return orderBy != null ? orderBy : new SortItem []{};
261
276
}
262
277
278
+ public Statement toGenericStatement () {
279
+ String rootNodeIds = "rootNodeIds" ;
280
+ String relationshipIds = "relationshipIds" ;
281
+ String relatedNodeIds = "relatedNodeIds" ;
282
+ Node rootNodes = Cypher .anyNode (rootNodeIds );
283
+ Node relatedNodes = Cypher .anyNode (relatedNodeIds );
284
+ Relationship relationships = Cypher .anyNode ().relationshipBetween (Cypher .anyNode ()).named (relationshipIds );
285
+ return Cypher .match (rootNodes )
286
+ .where (Functions .id (rootNodes ).in (Cypher .parameter (rootNodeIds )))
287
+ .optionalMatch (relationships )
288
+ .where (Functions .id (relationships ).in (Cypher .parameter (relationshipIds )))
289
+ .optionalMatch (relatedNodes )
290
+ .where (Functions .id (relatedNodes ).in (Cypher .parameter (relatedNodeIds )))
291
+ .with (
292
+ rootNodes .as (Constants .NAME_OF_ROOT_NODE .getValue ()),
293
+ Functions .collectDistinct (relationships ).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ),
294
+ Functions .collectDistinct (relatedNodes ).as (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ))
295
+ .orderBy (getOrderBy ())
296
+ .returning (
297
+ Constants .NAME_OF_ROOT_NODE .as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ),
298
+ Cypher .name (Constants .NAME_OF_SYNTHESIZED_RELATIONS ),
299
+ Cypher .name (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES )
300
+ )
301
+ .skip (skip )
302
+ .limit (limit ).build ();
303
+ }
304
+
263
305
public Statement toStatement () {
264
306
265
307
StatementBuilder .OngoingReadingWithoutWhere match = null ;
0 commit comments