Permalink
Browse files

allow deletion of optional nodes and relationships in cypher

  • Loading branch information...
jexp committed May 12, 2012
2 parents c85350a + 2b0bddf commit a55c5b7050ca5f48f7dfa5b29f165a328bd1ec0e
@@ -107,6 +107,18 @@ case class DeleteEntityAction(elementToDelete: Expression)
def exec(context: ExecutionContext, state: QueryState) = {
elementToDelete(context) match {
+ case n: Node => delete(n, state)
+ case r: Relationship => delete(r, state)
+ case null =>
+ case p:Path => p.iterator().asScala.foreach( pc => delete(pc, state))
+ case x => throw new CypherTypeException("Expression `" + elementToDelete.toString() + "` yielded `" + x.toString + "`. Don't know how to delete that.")
+ }
+
+ Stream(context)
+ }
+
+ private def delete(x: PropertyContainer, state: QueryState) {
+ x match {
case n: Node => {
state.deletedNodes.increase()
n.delete()
@@ -115,15 +127,12 @@ case class DeleteEntityAction(elementToDelete: Expression)
state.deletedRelationships.increase()
r.delete()
}
- case x => throw new CypherTypeException("Expression `" + elementToDelete.toString() + "` yielded `" + x.toString + "`. Don't know how to delete that.")
}
-
- Stream(context)
}
def identifier = None
- def dependencies = elementToDelete.dependencies(MapType())
+ def dependencies = elementToDelete.dependencies(AnyType())
def rewrite(f: (Expression) => Expression) = DeleteEntityAction(elementToDelete.rewrite(f))
@@ -369,4 +369,25 @@ return distinct center""")
val result = parseAndExecute( """start n=node(0) set n.x=[1,2,3] return extract (i in n.x : i/2) as x""")
assert(result.toList == List(Map("x" -> List(0.5, 1.0, 1.5))))
}
+
+ @Test
+ def delete_optionals() {
+ createNode()
+ val a = createNode()
+ val b = createNode()
+ relate(a,b)
+
+ parseAndExecute("""start n=node(*) match n-[r?]-() delete n,r""")
+ assert(graph.getAllNodes.asScala.size === 0)
+ }
+
+ @Test
+ def delete_path() {
+ val a = createNode()
+ val b = createNode()
+ relate(a,b)
+
+ parseAndExecute("""start n=node(1) match p=n-->() delete p""")
+ assert(graph.getAllNodes.asScala.size === 1)
+ }
}

0 comments on commit a55c5b7

Please sign in to comment.