Skip to content

Commit

Permalink
Add support for ON MATCH and ON CREATE
Browse files Browse the repository at this point in the history
Added support for ON MATCH and ON CREATE which can follow a merge clause.  Added some
supporting test cases.
  • Loading branch information
William Gorder authored and jexp committed Feb 23, 2015
1 parent 9e00922 commit c0643b7
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/CypherQuery.java
Expand Up @@ -1922,6 +1922,73 @@ public UpdateNext set( Iterable<SetProperty> setProperties )
return this;
}


// On Create ------------------------------------------------------
@Override
public UpdateNext onCreate(SetProperty... setProperties) {
Clause clause = query.lastClause(Clause.class);
if (clause instanceof MergeClause || clause instanceof OnMatchClause)
{
query.add(new OnCreateClause(Arrays.asList(setProperties)));
}
else
{
throw new IllegalArgumentException("ON CREATE must follow a MERGE or ON MATCH Clause");
}


return this;
}

@Override
public UpdateNext onCreate(Iterable<SetProperty> setProperties) {
Clause clause = query.lastClause(Clause.class);
if (clause instanceof MergeClause || clause instanceof OnMatchClause)
{
query.add( new OnCreateClause( setProperties ) );
}
else
{
throw new IllegalArgumentException("ON CREATE must follow a MERGE or ON MATCH Clause");
}


return this;
}

// On Match ------------------------------------------------------
@Override
public UpdateNext onMatch(SetProperty... setProperties) {
Clause clause = query.lastClause(Clause.class);
if (clause instanceof MergeClause || clause instanceof OnCreateClause)
{
query.add( new OnMatchClause( Arrays.asList( setProperties ) ) );
}
else
{
throw new IllegalArgumentException("ON MATCH must follow a MERGE or ON CREATE Clause");
}


return this;
}

@Override
public UpdateNext onMatch(Iterable<SetProperty> setProperties) {
Clause clause = query.lastClause(Clause.class);
if (clause instanceof MergeClause || clause instanceof OnCreateClause)
{
query.add( new OnMatchClause( setProperties ) );
}
else
{
throw new IllegalArgumentException("ON MATCH must follow a MERGE or ON CREATE Clause");
}


return this;
}

// Delete -------------------------------------------------------
@Override
public UpdateNext delete( ReferenceExpression... expressions )
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/grammar/OnCreate.java
@@ -0,0 +1,13 @@
package org.neo4j.cypherdsl.grammar;

import org.neo4j.cypherdsl.SetProperty;

/**
* Represents the ON CREATE clause
*/
public interface OnCreate
{
UpdateNext onCreate( SetProperty... propertyValues );

UpdateNext onCreate( Iterable<SetProperty> propertyValues );
}
13 changes: 13 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/grammar/OnMatch.java
@@ -0,0 +1,13 @@
package org.neo4j.cypherdsl.grammar;

import org.neo4j.cypherdsl.SetProperty;

/**
* Represents the ON MATCH clause
*/
public interface OnMatch
{
UpdateNext onMatch( SetProperty... propertyValues );

UpdateNext onMatch( Iterable<SetProperty> propertyValues );
}
2 changes: 1 addition & 1 deletion src/main/java/org/neo4j/cypherdsl/grammar/UpdateNext.java
Expand Up @@ -24,6 +24,6 @@
* This specifies what can come after an update clause
*/
public interface UpdateNext
extends Update, ForEach, With, Return
extends Update, ForEach, With, Return, OnCreate, OnMatch
{
}
31 changes: 31 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/query/clause/OnCreateClause.java
@@ -0,0 +1,31 @@
package org.neo4j.cypherdsl.query.clause;

import org.neo4j.cypherdsl.SetProperty;

import java.util.ArrayList;

/**
* ON CREATE clause
*/
public class OnCreateClause
extends Clause
{
private final ArrayList<SetProperty> expressions = new ArrayList<SetProperty>();

public OnCreateClause( Iterable<SetProperty> expressions )
{
for ( SetProperty expression : expressions )
{
this.expressions.add( expression );
}
}

@Override
public void asString( StringBuilder builder )
{
String name = " ON CREATE";
builder.append(name);
clauseAsString( builder, "SET", expressions, "," );
}
}

30 changes: 30 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/query/clause/OnMatchClause.java
@@ -0,0 +1,30 @@
package org.neo4j.cypherdsl.query.clause;

import org.neo4j.cypherdsl.SetProperty;

import java.util.ArrayList;

/**
* ON CREATE clause
*/
public class OnMatchClause
extends Clause
{
private final ArrayList<SetProperty> expressions = new ArrayList<SetProperty>();

public OnMatchClause( Iterable<SetProperty> expressions )
{
for ( SetProperty expression : expressions )
{
this.expressions.add( expression );
}
}

@Override
public void asString( StringBuilder builder )
{
String name = " ON MATCH";
builder.append(name);
clauseAsString( builder, "SET", expressions, "," );
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/neo4j/cypherdsl/CypherReferenceTest.java
Expand Up @@ -1309,4 +1309,41 @@ public void test_9_7_1_UnionAll()
toString() );
}

@Test
public void test_13_2_1_MergeOnCreate()
{
assertQueryEquals(CYPHER + "MERGE (keanu:Person {name:\"Keanu Reeves\"}) " +
"ON CREATE SET keanu.movie=\"Matrix\"",
merge(node(identifier("keanu")).label("Person").values(value("name", "Keanu Reeves")))
.onCreate(property(identifier("keanu").property("movie"),
literal("Matrix"))).toString());
}

@Test
public void test_13_2_1_MergeOnMatch()
{
assertQueryEquals(CYPHER + "MERGE (keanu:Person {name:\"Keanu Reeves\"}) " +
"ON MATCH SET keanu.movie=\"Matrix\"",
merge(node(identifier("keanu")).label("Person").values(value("name", "Keanu Reeves")))
.onMatch(property(identifier("keanu").property("movie"),
literal("Matrix"))).toString());
}

@Test
public void test_13_2_1_MergeOnMatchAndOnCreate()
{
assertQueryEquals(CYPHER + "MERGE (keanu:Person {name:\"Keanu Reeves\"}) " +
"ON CREATE SET keanu.movie=\"Matrix\" " +
"ON MATCH SET keanu.movie=\"Matrix\",keanu.found=true " +
"RETURN keanu",
merge(node(identifier("keanu")).label("Person").values(value("name", "Keanu Reeves")))
.onCreate(property(identifier("keanu").property("movie"),
literal("Matrix")))
.onMatch(property(identifier("keanu").property("movie"),
literal("Matrix")), property(identifier("keanu").property("found"),
literal(true)))
.returns(identifier("keanu"))
.toString());
}

}

0 comments on commit c0643b7

Please sign in to comment.