16
16
package org .springframework .data .neo4j .core .mapping ;
17
17
18
18
import static org .neo4j .cypherdsl .core .Cypher .anyNode ;
19
+ import static org .neo4j .cypherdsl .core .Cypher .coalesce ;
20
+ import static org .neo4j .cypherdsl .core .Cypher .collect ;
19
21
import static org .neo4j .cypherdsl .core .Cypher .listBasedOn ;
20
22
import static org .neo4j .cypherdsl .core .Cypher .literalOf ;
21
23
import static org .neo4j .cypherdsl .core .Cypher .match ;
22
24
import static org .neo4j .cypherdsl .core .Cypher .node ;
23
25
import static org .neo4j .cypherdsl .core .Cypher .optionalMatch ;
24
26
import static org .neo4j .cypherdsl .core .Cypher .parameter ;
25
- import static org .neo4j .cypherdsl .core .Functions .coalesce ;
26
27
27
28
import java .util .ArrayList ;
28
29
import java .util .Arrays ;
39
40
40
41
import org .apiguardian .api .API ;
41
42
import org .neo4j .cypherdsl .core .Condition ;
42
- import org .neo4j .cypherdsl .core .Conditions ;
43
43
import org .neo4j .cypherdsl .core .Cypher ;
44
44
import org .neo4j .cypherdsl .core .Expression ;
45
45
import org .neo4j .cypherdsl .core .FunctionInvocation ;
46
- import org .neo4j .cypherdsl .core .Functions ;
47
46
import org .neo4j .cypherdsl .core .IdentifiableElement ;
48
47
import org .neo4j .cypherdsl .core .MapProjection ;
49
48
import org .neo4j .cypherdsl .core .Named ;
@@ -87,9 +86,9 @@ public enum CypherGenerator {
87
86
// default elementId function
88
87
private Function <Named , FunctionInvocation > elementIdOrIdFunction = named -> {
89
88
if (named instanceof Node node ) {
90
- return Functions .elementId (node );
89
+ return Cypher .elementId (node );
91
90
} else if (named instanceof Relationship relationship ) {
92
- return Functions .elementId (relationship );
91
+ return Cypher .elementId (relationship );
93
92
} else {
94
93
throw new IllegalArgumentException ("Unsupported CypherDSL type: " + named .getClass ());
95
94
}
@@ -140,7 +139,8 @@ public StatementBuilder.OrderableOngoingReadingAndWith prepareMatchOf(NodeDescri
140
139
List <IdentifiableElement > expressions = new ArrayList <>();
141
140
expressions .add (rootNode .getRequiredSymbolicName ());
142
141
if (nodeDescription instanceof Neo4jPersistentEntity <?> entity && entity .isUsingDeprecatedInternalId ()) {
143
- expressions .add (Functions .id (rootNode ).as (Constants .NAME_OF_INTERNAL_ID ));
142
+ //noinspection deprecation
143
+ expressions .add (rootNode .internalId ().as (Constants .NAME_OF_INTERNAL_ID ));
144
144
}
145
145
expressions .add (elementIdOrIdFunction .apply (rootNode ).as (Constants .NAME_OF_ELEMENT_ID ));
146
146
@@ -155,7 +155,7 @@ public StatementBuilder.OngoingReading prepareMatchOf(NodeDescription<?> nodeDes
155
155
StatementBuilder .OngoingReadingWithoutWhere match = prepareMatchOfRootNode (rootNode , initialMatchOn );
156
156
157
157
List <IdentifiableElement > expressions = new ArrayList <>();
158
- expressions .add (Functions .collect (elementIdOrIdFunction .apply (rootNode )).as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ));
158
+ expressions .add (Cypher .collect (elementIdOrIdFunction .apply (rootNode )).as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ));
159
159
160
160
return match
161
161
.where (conditionOrNoCondition (condition ))
@@ -192,9 +192,9 @@ public StatementBuilder.OngoingReading prepareMatchOf(NodeDescription<?> nodeDes
192
192
193
193
relationship = relationship .named (Constants .NAME_OF_SYNTHESIZED_RELATIONS );
194
194
List <IdentifiableElement > expressions = new ArrayList <>();
195
- expressions .add (Functions .collect (elementIdOrIdFunction .apply (rootNode )).as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ));
196
- expressions .add (Functions .collect (elementIdOrIdFunction .apply (targetNode )).as (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ));
197
- expressions .add (Functions .collect (elementIdOrIdFunction .apply (relationship )).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ));
195
+ expressions .add (Cypher .collect (elementIdOrIdFunction .apply (rootNode )).as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ));
196
+ expressions .add (Cypher .collect (elementIdOrIdFunction .apply (targetNode )).as (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ));
197
+ expressions .add (Cypher .collect (elementIdOrIdFunction .apply (relationship )).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ));
198
198
199
199
return match
200
200
.where (conditionOrNoCondition (condition ))
@@ -251,14 +251,14 @@ public Statement createStatementReturningDynamicLabels(NodeDescription<?> nodeDe
251
251
versionCondition = rootNode .property (versionProperty .getName ())
252
252
.isEqualTo (coalesce (parameter (Constants .NAME_OF_VERSION_PARAM ), literalOf (0 )));
253
253
} else {
254
- versionCondition = Conditions .noCondition ();
254
+ versionCondition = Cypher .noCondition ();
255
255
}
256
256
257
257
return match (rootNode )
258
258
.where (idDescription .asIdExpression ().isEqualTo (parameter (Constants .NAME_OF_ID )))
259
259
.and (versionCondition ).unwind (rootNode .labels ()).as ("label" ).with (Cypher .name ("label" ))
260
260
.where (Cypher .name ("label" ).in (parameter (Constants .NAME_OF_STATIC_LABELS_PARAM )).not ())
261
- .returning (Functions . collect (Cypher .name ("label" )).as (Constants .NAME_OF_LABELS )).build ();
261
+ .returning (collect (Cypher .name ("label" )).as (Constants .NAME_OF_LABELS )).build ();
262
262
}
263
263
264
264
public Statement prepareDeleteOf (NodeDescription <?> nodeDescription ) {
@@ -276,7 +276,7 @@ public Statement prepareDeleteOf(NodeDescription<?> nodeDescription, @Nullable C
276
276
.named (Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription ));
277
277
OngoingUpdate ongoingUpdate = match (rootNode ).where (conditionOrNoCondition (condition )).detachDelete (rootNode );
278
278
if (count ) {
279
- return ongoingUpdate .returning (Functions .count (rootNode )).build ();
279
+ return ongoingUpdate .returning (Cypher .count (rootNode )).build ();
280
280
}
281
281
return ongoingUpdate .build ();
282
282
}
@@ -289,7 +289,7 @@ public Condition createCompositePropertyCondition(GraphPropertyDescription idPro
289
289
290
290
Neo4jPersistentProperty property = (Neo4jPersistentProperty ) idProperty ;
291
291
292
- Condition result = Conditions .noCondition ();
292
+ Condition result = Cypher .noCondition ();
293
293
for (String key : property .getOptionalConverter ().write (null ).keys ()) {
294
294
Property expression = Cypher .property (containerName , key );
295
295
result = result .and (expression .isEqualTo (actualParameter .property (key )));
@@ -420,7 +420,7 @@ public Statement prepareSaveOfMultipleInstancesOf(NodeDescription<?> nodeDescrip
420
420
421
421
List <Expression > expressions = new ArrayList <>();
422
422
if (nodeDescription instanceof Neo4jPersistentEntity <?> entity && entity .isUsingDeprecatedInternalId ()) {
423
- Functions . id ( rootNode ).as (Constants .NAME_OF_INTERNAL_ID );
423
+ rootNode . internalId ( ).as (Constants .NAME_OF_INTERNAL_ID );
424
424
}
425
425
expressions .add (elementIdOrIdFunction .apply (rootNode ).as (Constants .NAME_OF_ELEMENT_ID ));
426
426
expressions .add (rootNode .property (nameOfIdProperty ).as (Constants .NAME_OF_ID ));
@@ -465,9 +465,9 @@ private static Function<Node, Expression> getNodeIdFunction(@Nullable Neo4jPersi
465
465
var idProperty = entity .getRequiredIdProperty ();
466
466
if (entity .isUsingInternalIds ()) {
467
467
if (entity .isUsingDeprecatedInternalId () || !canUseElementId ) {
468
- startNodeIdFunction = Functions :: id ;
468
+ startNodeIdFunction = Node :: internalId ;
469
469
} else {
470
- startNodeIdFunction = Functions ::elementId ;
470
+ startNodeIdFunction = Cypher ::elementId ;
471
471
}
472
472
} else {
473
473
startNodeIdFunction = node -> node .property (idProperty .getPropertyName ());
@@ -479,25 +479,29 @@ private static Function<Node, Expression> getEndNodeIdFunction(@Nullable Neo4jPe
479
479
480
480
Function <Node , Expression > startNodeIdFunction ;
481
481
if (entity == null ) {
482
- return Functions ::elementId ;
482
+ return Cypher ::elementId ;
483
483
}
484
484
if (!entity .isUsingDeprecatedInternalId () && canUseElementId ) {
485
- startNodeIdFunction = Functions ::elementId ;
485
+ startNodeIdFunction = Cypher ::elementId ;
486
486
} else {
487
- startNodeIdFunction = Functions :: id ;
487
+ startNodeIdFunction = Node :: internalId ;
488
488
}
489
489
return startNodeIdFunction ;
490
490
}
491
491
492
+ static Expression relId (Relationship r ) {
493
+ return FunctionInvocation .create (() -> "id" , r .getRequiredSymbolicName ());
494
+ }
495
+
492
496
private static Function <Relationship , Expression > getRelationshipIdFunction (RelationshipDescription relationshipDescription , boolean canUseElementId ) {
493
497
494
- Function <Relationship , Expression > result = canUseElementId ? Functions ::elementId : Functions :: id ;
498
+ Function <Relationship , Expression > result = canUseElementId ? Cypher ::elementId : CypherGenerator :: relId ;
495
499
if (relationshipDescription .hasRelationshipProperties ()) {
496
500
Neo4jPersistentEntity <?> entity = (Neo4jPersistentEntity <?>) relationshipDescription .getRelationshipPropertiesEntity ();
497
501
if ((entity != null && entity .isUsingDeprecatedInternalId ()) || !canUseElementId ) {
498
- result = Functions :: id ;
502
+ result = CypherGenerator :: relId ;
499
503
} else {
500
- result = Functions ::elementId ;
504
+ result = Cypher ::elementId ;
501
505
}
502
506
}
503
507
return result ;
@@ -623,7 +627,7 @@ public Statement prepareUpdateOfRelationshipsWithProperties(Neo4jPersistentEntit
623
627
private List <Expression > getReturnedIdExpressionsForRelationship (RelationshipDescription relationship , Relationship relationshipFragment ) {
624
628
List <Expression > result = new ArrayList <>();
625
629
if (relationship .hasRelationshipProperties () && relationship .getRelationshipPropertiesEntity () instanceof Neo4jPersistentEntity <?> entity && entity .isUsingDeprecatedInternalId ()) {
626
- result .add (Functions . id (relationshipFragment ).as (Constants .NAME_OF_INTERNAL_ID ));
630
+ result .add (relId (relationshipFragment ).as (Constants .NAME_OF_INTERNAL_ID ));
627
631
}
628
632
result .add (elementIdOrIdFunction .apply (relationshipFragment ).as (Constants .NAME_OF_ELEMENT_ID ));
629
633
return result ;
@@ -661,7 +665,7 @@ public Statement prepareDeleteOf(
661
665
662
666
public Collection <Expression > createReturnStatementForExists (Neo4jPersistentEntity <?> nodeDescription ) {
663
667
664
- return Collections .singleton (Functions .count (Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription )));
668
+ return Collections .singleton (Cypher .count (Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription )));
665
669
}
666
670
667
671
public Collection <Expression > createReturnStatementForMatch (Neo4jPersistentEntity <?> nodeDescription ) {
@@ -711,7 +715,7 @@ public Collection<Expression> createReturnStatementForMatch(Neo4jPersistentEntit
711
715
}
712
716
}
713
717
if (order .isIgnoreCase ()) {
714
- expression = Functions .toLower (expression );
718
+ expression = Cypher .toLower (expression );
715
719
}
716
720
return order .isAscending () ? expression .ascending () : expression .descending ();
717
721
}).toArray (SortItem []::new ))
@@ -804,10 +808,11 @@ private List<Object> projectNodeProperties(PropertyFilter.RelaxedPropertyPath pa
804
808
}
805
809
806
810
nodePropertiesProjection .add (Constants .NAME_OF_LABELS );
807
- nodePropertiesProjection .add (Functions .labels (node ));
811
+ nodePropertiesProjection .add (Cypher .labels (node ));
808
812
if (nodeDescription instanceof Neo4jPersistentEntity <?> entity && entity .isUsingDeprecatedInternalId ()) {
809
813
nodePropertiesProjection .add (Constants .NAME_OF_INTERNAL_ID );
810
- nodePropertiesProjection .add (Functions .id (node ));
814
+ //noinspection deprecation
815
+ nodePropertiesProjection .add (node .internalId ());
811
816
}
812
817
nodePropertiesProjection .add (Constants .NAME_OF_ELEMENT_ID );
813
818
nodePropertiesProjection .add (elementIdOrIdFunction .apply (node ));
@@ -876,7 +881,7 @@ private void generateListFor(PropertyFilter.RelaxedPropertyPath parentPath, Neo4
876
881
877
882
addMapProjection (relationshipTargetName ,
878
883
listBasedOn (relationship ).returning (mapProjection
879
- .and (RelationshipDescription .NAME_OF_RELATIONSHIP_TYPE , Functions .type (relationship ))),
884
+ .and (RelationshipDescription .NAME_OF_RELATIONSHIP_TYPE , Cypher .type (relationship ))),
880
885
mapProjectionLists );
881
886
882
887
} else {
@@ -902,6 +907,6 @@ private void addMapProjection(String name, Object projection, List<Object> proje
902
907
}
903
908
904
909
private static Condition conditionOrNoCondition (@ Nullable Condition condition ) {
905
- return condition == null ? Conditions .noCondition () : condition ;
910
+ return condition == null ? Cypher .noCondition () : condition ;
906
911
}
907
912
}
0 commit comments