Skip to content

Commit fad4e25

Browse files
Add API for creating list comprehensions based on named paths.
1 parent 515605a commit fad4e25

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/Cypher.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public static <T> Literal<T> literalOf(Object object) {
326326
}
327327

328328
@SuppressWarnings("unchecked") // See above
329-
ListLiteral listLiteral = new ListLiteral((Iterable<Literal<?>>) object);
329+
ListLiteral listLiteral = new ListLiteral((Iterable<Literal<?>>) object);
330330
return (Literal<T>) listLiteral;
331331
}
332332
if (object instanceof Boolean) {
@@ -368,12 +368,26 @@ public static StatementBuilder.OngoingReadingAndReturn returning(Expression... e
368368
return Statement.builder().returning(expressions);
369369
}
370370

371-
public static OngoingDefinitionWithPattern listBasedOn(Relationship pattern) {
372-
return PatternComprehension.basedOn(pattern);
371+
/**
372+
* Creates a list comprehension starting with a {@link Relationship} or a {@link RelationshipChain chain of relationships}.
373+
*
374+
* @param relationshipPattern The relationship pattern on which the new list comprehension is based on.
375+
* @return An ongoing definition.
376+
* @since 2020.0.0
377+
*/
378+
public static OngoingDefinitionWithPattern listBasedOn(RelationshipPattern relationshipPattern) {
379+
return PatternComprehension.basedOn(relationshipPattern);
373380
}
374381

375-
public static OngoingDefinitionWithPattern listBasedOn(RelationshipChain pattern) {
376-
return PatternComprehension.basedOn(pattern);
382+
/**
383+
* Creates a list comprehension starting with a {@link NamedPath named path}.
384+
*
385+
* @param namedPath The named path on which the new list comprehension is based on.
386+
* @return An ongoing definition.
387+
* @since 2020.1.1
388+
*/
389+
public static OngoingDefinitionWithPattern listBasedOn(NamedPath namedPath) {
390+
return PatternComprehension.basedOn(namedPath);
377391
}
378392

379393
/**

neo4j-cypher-dsl/src/main/java/org/neo4j/cypherdsl/core/PatternComprehension.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
@API(status = EXPERIMENTAL, since = "1.0")
3636
public final class PatternComprehension implements Expression {
3737

38-
private final RelationshipPattern pattern;
38+
private final PatternElement pattern;
3939
private final Where where;
4040
private final Expression listDefinition;
4141

42-
static OngoingDefinitionWithPattern basedOn(Relationship pattern) {
42+
static OngoingDefinitionWithPattern basedOn(RelationshipPattern pattern) {
4343

4444
Assertions.notNull(pattern, "A pattern is required");
4545
return new Builder(pattern);
4646
}
4747

48-
static OngoingDefinitionWithPattern basedOn(RelationshipChain pattern) {
48+
static OngoingDefinitionWithPattern basedOn(NamedPath pattern) {
4949

5050
Assertions.notNull(pattern, "A pattern is required");
5151
return new Builder(pattern);
@@ -90,10 +90,10 @@ public interface OngoingDefinitionWithPatternAndWhere extends OngoingDefinitionW
9090
* Ongoing definition of a pattern comprehension. Can be defined without a where-clause now.
9191
*/
9292
private static class Builder implements OngoingDefinitionWithPattern, OngoingDefinitionWithPatternAndWhere {
93-
private final RelationshipPattern pattern;
93+
private final PatternElement pattern;
9494
private final DefaultStatementBuilder.ConditionBuilder conditionBuilder = new DefaultStatementBuilder.ConditionBuilder();
9595

96-
private Builder(RelationshipPattern pattern) {
96+
private Builder(PatternElement pattern) {
9797
this.pattern = pattern;
9898
}
9999

@@ -122,7 +122,7 @@ public PatternComprehension returning(Expression... expressions) {
122122
}
123123
}
124124

125-
private PatternComprehension(RelationshipPattern pattern, Where where, Expression listDefinition) {
125+
private PatternComprehension(PatternElement pattern, Where where, Expression listDefinition) {
126126
this.pattern = pattern;
127127
this.where = where;
128128
this.listDefinition = listDefinition;

neo4j-cypher-dsl/src/test/java/org/neo4j/cypherdsl/core/CypherIT.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3221,11 +3221,22 @@ void doc3148() {
32213221
NamedPath p = Cypher.path("p").definedBy(
32223222
Cypher.anyNode("michael").withProperties("name", Cypher.literalOf("Michael Douglas"))
32233223
.relationshipTo(Cypher.anyNode()));
3224-
Statement statement = Cypher.match(p).returning(p.getRequiredSymbolicName()).build();
3224+
Statement statement = Cypher.match(p).returning(p).build();
32253225

32263226
assertThat(cypherRenderer.render(statement))
32273227
.isEqualTo("MATCH p = (michael {name: 'Michael Douglas'})-->() RETURN p");
32283228
}
3229+
3230+
@Test
3231+
void shouldWorkInListComprehensions() {
3232+
3233+
NamedPath p = Cypher.path("p").definedBy(
3234+
Cypher.anyNode("n").relationshipTo(Cypher.anyNode(), "LIKES", "OWNS").unbounded());
3235+
Statement statement = Cypher.returning(Cypher.listBasedOn(p).returning(p)).build();
3236+
3237+
assertThat(cypherRenderer.render(statement))
3238+
.isEqualTo("RETURN [p = (n)-[:`LIKES`|`OWNS`*]->() | p]");
3239+
}
32293240
}
32303241

32313242
@Nested

0 commit comments

Comments
 (0)