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

Introduced properties

fun []=(key: String, value: nullable Serializable)

neo4j :: CypherQuery :: []=

Pass the argument value as the parameter key.
init from_string(query: String)

neo4j :: CypherQuery :: from_string

init the query from a query string
fun nand(query: String): CypherQuery

neo4j :: CypherQuery :: nand

Add a AND statement to the query
fun ncreate(query: String): CypherQuery

neo4j :: CypherQuery :: ncreate

Add a CREATE statement to the query
fun nmatch(query: String): CypherQuery

neo4j :: CypherQuery :: nmatch

Add a MATCH statement to the query
fun nreturn(query: String): CypherQuery

neo4j :: CypherQuery :: nreturn

Add a RETURN statement to the query
fun nstart(query: String): CypherQuery

neo4j :: CypherQuery :: nstart

Add a START statement to the query
fun nwhere(query: String): CypherQuery

neo4j :: CypherQuery :: nwhere

Add a WHERE statement to the query
fun params: JsonObject

neo4j :: CypherQuery :: params

params to embed in the query like in prepared statements
protected fun params=(params: JsonObject)

neo4j :: CypherQuery :: params=

params to embed in the query like in prepared statements
fun set(key: String, value: nullable Serializable): SELF

neo4j :: CypherQuery :: set

Pass the argument value as the parameter key.
fun to_rest: JsonObject

neo4j :: CypherQuery :: to_rest

Translate the query to the body of a corresponding Neo4j REST request.
init with_params(params: JsonObject)

neo4j :: CypherQuery :: with_params

init the query with parameters

Redefined properties

redef type SELF: CypherQuery

neo4j $ CypherQuery :: SELF

Type of this instance, automatically specialized in every class
redef fun to_s: String

neo4j $ CypherQuery :: to_s

User readable representation of self.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun []=(key: String, value: nullable Serializable)

neo4j :: CypherQuery :: []=

Pass the argument value as the parameter key.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init from_string(query: String)

neo4j :: CypherQuery :: from_string

init the query from a query string
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun nand(query: String): CypherQuery

neo4j :: CypherQuery :: nand

Add a AND statement to the query
fun ncreate(query: String): CypherQuery

neo4j :: CypherQuery :: ncreate

Add a CREATE statement to the query
fun nmatch(query: String): CypherQuery

neo4j :: CypherQuery :: nmatch

Add a MATCH statement to the query
fun nreturn(query: String): CypherQuery

neo4j :: CypherQuery :: nreturn

Add a RETURN statement to the query
fun nstart(query: String): CypherQuery

neo4j :: CypherQuery :: nstart

Add a START statement to the query
fun nwhere(query: String): CypherQuery

neo4j :: CypherQuery :: nwhere

Add a WHERE statement to the query
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun params: JsonObject

neo4j :: CypherQuery :: params

params to embed in the query like in prepared statements
protected fun params=(params: JsonObject)

neo4j :: CypherQuery :: params=

params to embed in the query like in prepared statements
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun set(key: String, value: nullable Serializable): SELF

neo4j :: CypherQuery :: set

Pass the argument value as the parameter key.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_rest: JsonObject

neo4j :: CypherQuery :: to_rest

Translate the query to the body of a corresponding Neo4j REST request.
fun to_s: String

core :: Object :: to_s

User readable representation of self.
init with_params(params: JsonObject)

neo4j :: CypherQuery :: with_params

init the query with parameters
package_diagram neo4j::CypherQuery CypherQuery core::Object Object neo4j::CypherQuery->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

neo4j $ CypherQuery
# 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