Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplified the create syntax
fixes #442
  • Loading branch information
systay committed Apr 25, 2012
1 parent 0ae83fb commit 3ad383f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
Expand Up @@ -30,22 +30,23 @@ trait StartClause extends Base with Expressions {
def createStart = ignoreCase("create") ~> comaList(createRel | createNode) ^^ (x => Start(x: _*))

def properties =
expression ^^ (x => Map[String,Expression]("*" -> x)) |
opt("{" ~> repsep(propertyAssignment, ",") <~ "}") ^^ {
case None => Map[String,Expression]()
case Some(x) => x.toMap
}
expression ^^ (x => Map[String,Expression]("*" -> x)) |
"{" ~> repsep(propertyAssignment, ",") <~ "}" ^^ ( _.toMap)

def createdNodeName = opt(identity <~ "=") ^^ (x => namer.name(x))
def createNode: Parser[StartItem] = identity ~ opt("=" ~> properties) ^^ {
case id ~ props => CreateNodeStartItem(id, getProperties(props))
}

def createNode: Parser[StartItem] = createdNodeName ~ properties ^^ {
case id ~ props => CreateNodeStartItem(id, props)
def createRel: Parser[StartItem] = expression ~ "-" ~ "[" ~ opt(identity) ~ ":" ~identity ~ opt(properties) ~ "]" ~ "->" ~ expression ^^ {
case from ~ "-" ~ "[" ~ id ~ ":" ~ relType ~ props ~ "]" ~ "->" ~ to =>
CreateRelationshipStartItem(namer.name(id), from, to, relType, getProperties(props))
}

def createRel: Parser[StartItem] = expression ~ "-" ~ "[" ~ opt(identity) ~ ":" ~identity ~ properties ~ "]" ~ "->" ~ expression ^^ {
case from ~ "-" ~ "[" ~ id ~ ":" ~ relType ~ props ~ "]" ~ "->" ~ to => CreateRelationshipStartItem(namer.name(id), from, to, relType, props)
private def getProperties(props: Option[Map[String, Expression]]): Map[String, Expression] = props match {
case None => Map[String, Expression]()
case Some(x) => x
}

def propertyAssignment:Parser[(String, Expression)] = identity ~ ":" ~ expression ^^ {
case id ~ ":" ~ exp => (id,exp)
}
Expand Down
Expand Up @@ -271,7 +271,7 @@ public void create_node_from_map() throws Exception

Map<String, Object> params = new HashMap<String, Object>();
params.put( "props", props );
engine.execute( "create {props}", params );
engine.execute( "create n = {props}", params );
// END SNIPPET: create_node_from_map

ExecutionResult result = engine.execute( "start n=node(*) where n.name = 'Andres' and n.position = 'Developer' return n" );
Expand Down
14 changes: 3 additions & 11 deletions cypher/src/test/scala/org/neo4j/cypher/CypherParserTest.scala
Expand Up @@ -1406,23 +1406,15 @@ class CypherParserTest extends JUnitSuite with Assertions {
}

@Test def create_node() {
testFrom_1_8("create a = {} ",
testFrom_1_8("create a",
Query.
start(CreateNodeStartItem("a", Map()))
returns()
)
}

@Test def create_node_without_identifier() {
testFrom_1_8("create {} ",
Query.
start(CreateNodeStartItem(" UNNAMED1", Map()))
returns()
)
}

@Test def create_node_with_a_property() {
testFrom_1_8("create a = {name : 'Andres'} ",
testFrom_1_8("create a = {name : 'Andres'}",
Query.
start(CreateNodeStartItem("a", Map("name" -> Literal("Andres"))))
returns()
Expand All @@ -1438,7 +1430,7 @@ class CypherParserTest extends JUnitSuite with Assertions {
}

@Test def create_two_nodes_with_a_property_and_return_it() {
testFrom_1_8("create a = {name : 'Andres'}, b = {} return a,b",
testFrom_1_8("create a = {name : 'Andres'}, b return a,b",
Query.
start(CreateNodeStartItem("a", Map("name" -> Literal("Andres"))), CreateNodeStartItem("b", Map()))
returns(ReturnItem(Entity("a"), "a"), ReturnItem(Entity("b"), "b"))
Expand Down
Expand Up @@ -202,7 +202,7 @@ class MutatingIntegrationTests extends ExecutionEngineHelper with Assertions {
createNode("Michael")
createNode("Peter")

val result = parseAndExecute("start n=node(1,2,3) with collect(n.name) as names create {name : names}")
val result = parseAndExecute("start n=node(1,2,3) with collect(n.name) as names create new = {name : names}")
val statistics = result.queryStatistics()
assert(statistics === stats.copy(
propertiesSet = 1,
Expand All @@ -216,7 +216,7 @@ class MutatingIntegrationTests extends ExecutionEngineHelper with Assertions {
def set_a_property_to_an_empty_collection() {
createNode("Andres")

val result = parseAndExecute("start n=node(1) with filter(x in collect(n.name) : x = 12) as names create {x : names}")
val result = parseAndExecute("start n=node(1) with filter(x in collect(n.name) : x = 12) as names create new = {x : names}")
val statistics = result.queryStatistics()
assert(statistics === stats.copy(
propertiesSet = 1,
Expand Down
Expand Up @@ -32,7 +32,7 @@ class CreateTest extends DocumentingTestBase {
testQuery(
title = "Create single node",
text = "Creating a single node is done by issuing the following query.",
queryText = "create {}",
queryText = "create n",
returns = "Nothing is returned from this query, except the count of affected nodes.",
assertions = (p) => {})
}
Expand All @@ -41,7 +41,7 @@ class CreateTest extends DocumentingTestBase {
testQuery(
title = "Create single node and set properties",
text = "The values for the properties can be any scalar expressions.",
queryText = "create {name : 'Andres', title : 'Developer'}",
queryText = "create n = {name : 'Andres', title : 'Developer'}",
returns = "Nothing is returned from this query.",
assertions = (p) => {})
}
Expand Down

0 comments on commit 3ad383f

Please sign in to comment.