neo4j :: CypherQuery :: defaultinit
# A Cypher query for Neo4j REST API
#
# The Neo4j REST API allows querying with Cypher.
# The results are returned as a list of string headers (columns), and a data part,
# consisting of a list of all rows, every row consisting of a list of REST representations
# of the field value - Node, Relationship, Path or any simple value like String.
#
# Example:
#
# var client = new Neo4jClient("http://neo4j:7474")
# var query = new CypherQuery
# query.nmatch("(n)-[r:LOVES]->(m)")
# query.nwhere("n.name=\"Andres\"")
# query.nreturn("m.name")
# var res = client.cypher(query).as(JsonObject)
# assert res["data"].as(JsonArray).first.as(JsonArray).first == "Kate"
#
# For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
class CypherQuery
# Query string to perform
private var query: String = ""
# `params` to embed in the query like in prepared statements
var params = new JsonObject
# init the query from a query string
init from_string(query: String) do
self.query = query
end
# init the query with parameters
init with_params(params: JsonObject) do
self.params = params
end
# Pass the argument `value` as the parameter `key`.
#
# SEE: `set`
fun []=(key: String, value: nullable Serializable) do
params[key] = value
end
# Add a `CREATE` statement to the query
fun ncreate(query: String): CypherQuery do
self.query = "{self.query}CREATE {query} "
return self
end
# Add a `START` statement to the query
fun nstart(query: String): CypherQuery do
self.query = "{self.query}START {query} "
return self
end
# Add a `MATCH` statement to the query
fun nmatch(query: String): CypherQuery do
self.query = "{self.query}MATCH {query} "
return self
end
# Add a `WHERE` statement to the query
fun nwhere(query: String): CypherQuery do
self.query = "{self.query}WHERE {query} "
return self
end
# Add a `AND` statement to the query
fun nand(query: String): CypherQuery do
self.query = "{self.query}AND {query} "
return self
end
# Add a `RETURN` statement to the query
fun nreturn(query: String): CypherQuery do
self.query = "{self.query}RETURN {query} "
return self
end
# Pass the argument `value` as the parameter `key`.
#
# Return `self`.
#
# ```
# var query = (new CypherQuery).
# nmatch("(n)").
# nwhere("n.key = \{key\}").
# set("key", "foo")
#
# assert query.params["key"] == "foo"
# ```
#
# SEE: `[]=`
fun set(key: String, value: nullable Serializable): SELF do
self[key] = value
return self
end
# Translate the query to the body of a corresponding Neo4j REST request.
fun to_rest: JsonObject do
var obj = new JsonObject
obj["query"] = query
if not params.is_empty then
obj["params"] = params
end
return obj
end
redef fun to_s do return to_rest.to_s
end
lib/neo4j/neo4j.nit:345,1--453,3