39
39
import org .neo4j .cypherdsl .core .Conditions ;
40
40
import org .neo4j .cypherdsl .core .Cypher ;
41
41
import org .neo4j .cypherdsl .core .Expression ;
42
+ import org .neo4j .cypherdsl .core .FunctionInvocation ;
42
43
import org .neo4j .cypherdsl .core .Functions ;
43
44
import org .neo4j .cypherdsl .core .MapProjection ;
44
45
import org .neo4j .cypherdsl .core .Node ;
54
55
import org .neo4j .cypherdsl .core .SymbolicName ;
55
56
import org .neo4j .cypherdsl .core .renderer .Configuration ;
56
57
import org .neo4j .cypherdsl .core .renderer .Renderer ;
58
+ import org .neo4j .cypherdsl .core .utils .Assertions ;
57
59
import org .springframework .data .domain .Sort ;
58
60
import org .springframework .data .mapping .MappingException ;
59
61
import org .springframework .data .mapping .PersistentProperty ;
62
64
import org .springframework .lang .Nullable ;
63
65
import org .springframework .util .Assert ;
64
66
67
+ import javax .lang .model .SourceVersion ;
68
+
65
69
/**
66
70
* A generator based on the schema defined by node and relationship descriptions. Most methods return renderable Cypher
67
71
* statements.
@@ -348,7 +352,7 @@ public Statement prepareSaveOf(NodeDescription<?> nodeDescription,
348
352
Property versionProperty = rootNode .property (((Neo4jPersistentEntity <?>) nodeDescription ).getRequiredVersionProperty ().getName ());
349
353
350
354
createIfNew = updateDecorator .apply (optionalMatch (possibleExistingNode )
351
- .where (possibleExistingNode . internalId ( ).isEqualTo (idParameter ))
355
+ .where (idFunction ( possibleExistingNode ).isEqualTo (idParameter ))
352
356
.with (possibleExistingNode )
353
357
.where (possibleExistingNode .isNull ())
354
358
.create (rootNode .withProperties (versionProperty , literalOf (0 )))
@@ -358,7 +362,7 @@ public Statement prepareSaveOf(NodeDescription<?> nodeDescription,
358
362
.build ();
359
363
360
364
updateIfExists = updateDecorator .apply (match (rootNode )
361
- .where (rootNode . internalId ( ).isEqualTo (idParameter ))
365
+ .where (idFunction ( rootNode ).isEqualTo (idParameter ))
362
366
.and (versionProperty .isEqualTo (parameter (Constants .NAME_OF_VERSION_PARAM ))) // Initial check
363
367
.set (versionProperty .to (versionProperty .add (literalOf (1 )))) // Acquire lock
364
368
.with (rootNode )
@@ -368,19 +372,23 @@ public Statement prepareSaveOf(NodeDescription<?> nodeDescription,
368
372
.returning (rootNode ).build ();
369
373
} else {
370
374
createIfNew = updateDecorator
371
- .apply (optionalMatch (possibleExistingNode ).where (possibleExistingNode . internalId ( ).isEqualTo (idParameter ))
375
+ .apply (optionalMatch (possibleExistingNode ).where (idFunction ( possibleExistingNode ).isEqualTo (idParameter ))
372
376
.with (possibleExistingNode ).where (possibleExistingNode .isNull ()).create (rootNode )
373
377
.set (rootNode , parameter (Constants .NAME_OF_PROPERTIES_PARAM )))
374
378
.returning (rootNode ).build ();
375
379
376
- updateIfExists = updateDecorator .apply (match (rootNode ).where (rootNode . internalId ( ).isEqualTo (idParameter ))
380
+ updateIfExists = updateDecorator .apply (match (rootNode ).where (idFunction ( rootNode ).isEqualTo (idParameter ))
377
381
.mutate (rootNode , parameter (Constants .NAME_OF_PROPERTIES_PARAM ))).returning (rootNode ).build ();
378
382
}
379
383
380
384
return Cypher .union (createIfNew , updateIfExists );
381
385
}
382
386
}
383
387
388
+ private static FunctionInvocation idFunction (Node rootNode ) {
389
+ return Functions .id (rootNode );
390
+ }
391
+
384
392
public Statement prepareSaveOfMultipleInstancesOf (NodeDescription <?> nodeDescription ) {
385
393
386
394
Assert .isTrue (!nodeDescription .isUsingInternalIds (),
@@ -398,7 +406,7 @@ public Statement prepareSaveOfMultipleInstancesOf(NodeDescription<?> nodeDescrip
398
406
return Cypher .unwind (parameter (Constants .NAME_OF_ENTITY_LIST_PARAM )).as (row )
399
407
.merge (rootNode .withProperties (nameOfIdProperty , Cypher .property (row , Constants .NAME_OF_ID )))
400
408
.mutate (rootNode , Cypher .property (row , Constants .NAME_OF_PROPERTIES_PARAM ))
401
- .returning (rootNode . internalId ( ).as (Constants .NAME_OF_INTERNAL_ID ), rootNode .property (nameOfIdProperty ).as (Constants .NAME_OF_ID ))
409
+ .returning (idFunction ( rootNode ).as (Constants .NAME_OF_INTERNAL_ID ), rootNode .property (nameOfIdProperty ).as (Constants .NAME_OF_ID ))
402
410
.build ();
403
411
}
404
412
@@ -420,9 +428,9 @@ public Statement prepareSaveOfRelationship(Neo4jPersistentEntity<?> neo4jPersist
420
428
startNode .relationshipFrom (endNode , type )).named (RELATIONSHIP_NAME );
421
429
422
430
return match (startNode )
423
- .where (neo4jPersistentEntity .isUsingInternalIds () ? startNode . internalId ( ).isEqualTo (idParameter )
431
+ .where (neo4jPersistentEntity .isUsingInternalIds () ? idFunction ( startNode ).isEqualTo (idParameter )
424
432
: startNode .property (idPropertyName ).isEqualTo (idParameter ))
425
- .match (endNode ).where (endNode . internalId ( ).isEqualTo (parameter (Constants .TO_ID_PARAMETER_NAME )))
433
+ .match (endNode ).where (idFunction ( endNode ).isEqualTo (parameter (Constants .TO_ID_PARAMETER_NAME )))
426
434
.merge (relationshipFragment )
427
435
.returning (Functions .id (relationshipFragment ))
428
436
.build ();
@@ -450,9 +458,9 @@ public Statement prepareSaveOfRelationships(Neo4jPersistentEntity<?> neo4jPersis
450
458
.with (row )
451
459
.match (startNode )
452
460
.where (neo4jPersistentEntity .isUsingInternalIds ()
453
- ? startNode . internalId ( ).isEqualTo (idProperty )
461
+ ? idFunction ( startNode ).isEqualTo (idProperty )
454
462
: startNode .property (idPropertyName ).isEqualTo (idProperty ))
455
- .match (endNode ).where (endNode . internalId ( ).isEqualTo (Cypher .property (row , Constants .TO_ID_PARAMETER_NAME )))
463
+ .match (endNode ).where (idFunction ( endNode ).isEqualTo (Cypher .property (row , Constants .TO_ID_PARAMETER_NAME )))
456
464
.merge (relationshipFragment )
457
465
.returning (Functions .id (relationshipFragment ))
458
466
.build ();
@@ -482,9 +490,9 @@ public Statement prepareSaveOfRelationshipWithProperties(Neo4jPersistentEntity<?
482
490
.named (RELATIONSHIP_NAME );
483
491
484
492
StatementBuilder .OngoingReadingWithWhere startAndEndNodeMatch = match (startNode )
485
- .where (neo4jPersistentEntity .isUsingInternalIds () ? startNode . internalId ( ).isEqualTo (idParameter )
493
+ .where (neo4jPersistentEntity .isUsingInternalIds () ? idFunction ( startNode ).isEqualTo (idParameter )
486
494
: startNode .property (idPropertyName ).isEqualTo (idParameter ))
487
- .match (endNode ).where (endNode . internalId ( ).isEqualTo (parameter (Constants .TO_ID_PARAMETER_NAME )));
495
+ .match (endNode ).where (idFunction ( endNode ).isEqualTo (parameter (Constants .TO_ID_PARAMETER_NAME )));
488
496
489
497
StatementBuilder .ExposesSet createOrMatch = isNew
490
498
? startAndEndNodeMatch .create (relationshipFragment )
@@ -527,10 +535,10 @@ public Statement prepareUpdateOfRelationshipsWithProperties(Neo4jPersistentEntit
527
535
.match (startNode )
528
536
.where (
529
537
neo4jPersistentEntity .isUsingInternalIds ()
530
- ? startNode . internalId ( ).isEqualTo (idProperty )
538
+ ? idFunction ( startNode ).isEqualTo (idProperty )
531
539
: startNode .property (idPropertyName ).isEqualTo (idProperty )
532
540
)
533
- .match (endNode ).where (endNode . internalId ( ).isEqualTo (Cypher .property (row , Constants .TO_ID_PARAMETER_NAME )))
541
+ .match (endNode ).where (idFunction ( endNode ).isEqualTo (Cypher .property (row , Constants .TO_ID_PARAMETER_NAME )))
534
542
.create (relationshipFragment )
535
543
.mutate (RELATIONSHIP_NAME , relationshipProperties )
536
544
.returning (Functions .id (relationshipFragment )).build ();
@@ -566,7 +574,7 @@ public Statement prepareDeleteOf(
566
574
567
575
Parameter <?> idParameter = parameter (Constants .FROM_ID_PARAMETER_NAME );
568
576
return match (relationship )
569
- .where (neo4jPersistentEntity .isUsingInternalIds () ? startNode . internalId ( ).isEqualTo (idParameter )
577
+ .where (neo4jPersistentEntity .isUsingInternalIds () ? idFunction ( startNode ).isEqualTo (idParameter )
570
578
: startNode .property (idPropertyName ).isEqualTo (idParameter ))
571
579
.and (Functions .id (relationship ).in (Cypher .parameter (Constants .NAME_OF_KNOWN_RELATIONSHIPS_PARAM )).not ())
572
580
.delete (relationship .getRequiredSymbolicName ())
@@ -613,6 +621,7 @@ public Collection<Expression> createReturnStatementForMatch(Neo4jPersistentEntit
613
621
}
614
622
expression = Cypher .property (property .substring (0 , firstDot ), tail );
615
623
} else {
624
+ Assertions .isTrue (SourceVersion .isIdentifier (property ), "Name must be a valid identifier." );
616
625
expression = Cypher .name (property );
617
626
}
618
627
if (order .isIgnoreCase ()) {
0 commit comments