Merge: Ci: move services to specific hostnames
[nit.git] / lib / neo4j / neo4j.nit
index f054423..8c10f6e 100644 (file)
 
 # Neo4j connector through its JSON REST API using curl.
 #
-# For ease of use and testing this module provide a wrapper to the `neo4j` command:
-#
-#     # Start the Neo4j server
-#     var srv = new Neo4jServer
-#     assert srv.start_quiet
-#
 # In order to connect to Neo4j you need a connector:
 #
 #     # Create new Neo4j client
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #     assert client.is_ok
 #
 # The fundamental units that form a graph are nodes and relationships.
@@ -66,39 +60,9 @@ module neo4j
 import curl_json
 import error
 
-# Handles Neo4j server start and stop command
-#
-# `neo4j` binary must be in `PATH` in order to work
-class Neo4jServer
-
-       # Start the local Neo4j server instance
-       fun start: Bool do
-               sys.system("neo4j start console")
-               return true
-       end
-
-       # Like `start` but redirect the console output to `/dev/null`
-       fun start_quiet: Bool do
-               sys.system("neo4j start console > /dev/null")
-               return true
-       end
-
-       # Stop the local Neo4j server instance
-       fun stop: Bool do
-               sys.system("neo4j stop")
-               return true
-       end
-
-       # Like `stop` but redirect the console output to `/dev/null`
-       fun stop_quiet: Bool do
-               sys.system("neo4j stop > /dev/null")
-               return true
-       end
-end
-
 # `Neo4jClient` is needed to communicate through the REST API
 #
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #     assert client.is_ok
 class Neo4jClient
 
@@ -111,8 +75,6 @@ class Neo4jClient
        # REST service to send cypher requests
        private var cypher_url: String
 
-       private var curl = new Curl
-
        init(base_url: String) do
                self.base_url = base_url
                var root = service_root
@@ -142,7 +104,7 @@ class Neo4jClient
 
        # Save the node in base
        #
-       #     var client = new Neo4jClient("http://localhost:7474")
+       #     var client = new Neo4jClient("http://neo4j:7474")
        #
        #     # Create a node
        #     var andres = new NeoNode
@@ -198,7 +160,7 @@ class Neo4jClient
        # Save the edge in base
        # From and to nodes will be created.
        #
-       #     var client = new Neo4jClient("http://localhost:7474")
+       #     var client = new Neo4jClient("http://neo4j:7474")
        #
        #     var andres = new NeoNode
        #     var kate = new NeoNode
@@ -247,7 +209,7 @@ class Neo4jClient
 
        # Retrieve all nodes with specified `lbl`
        #
-       #     var client = new Neo4jClient("http://localhost:7474")
+       #     var client = new Neo4jClient("http://neo4j:7474")
        #
        #     var andres = new NeoNode
        #     andres.labels.add_all(["Human", "Male"])
@@ -273,7 +235,7 @@ class Neo4jClient
 
        # Retrieve nodes belonging to all the specified `labels`.
        #
-       #     var client = new Neo4jClient("http://localhost:7474")
+       #     var client = new Neo4jClient("http://neo4j:7474")
        #
        #     var andres = new NeoNode
        #     andres.labels.add_all(["Human", "Male"])
@@ -290,7 +252,7 @@ class Neo4jClient
 
                # Build the query.
                var buffer = new Buffer
-               buffer.append "match n where \{label_0\} in labels(n)"
+               buffer.append "match (n) where \{label_0\} in labels(n)"
                for i in [1..labels.length[ do
                        buffer.append " and \{label_{i}\} in labels(n)"
                end
@@ -389,7 +351,7 @@ end
 #
 # Example:
 #
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #     var query = new CypherQuery
 #     query.nmatch("(n)-[r:LOVES]->(m)")
 #     query.nwhere("n.name=\"Andres\"")
@@ -415,6 +377,13 @@ class CypherQuery
                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} "
@@ -451,6 +420,25 @@ class CypherQuery
                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
@@ -482,7 +470,7 @@ end
 # Then we can link the entity to the base:
 #
 #     # Init client
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #     client.save_node(andres)
 #     # The node is now linked
 #     assert andres.is_linked
@@ -574,7 +562,7 @@ end
 #
 # Creating new nodes:
 #
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #
 #     var andres = new NeoNode
 #     andres.labels.add "Person"
@@ -687,7 +675,7 @@ end
 #
 # Create a relationship:
 #
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #     # Create nodes
 #     var andres = new NeoNode
 #     andres["name"] = "Andres"
@@ -780,7 +768,7 @@ end
 #
 # Example:
 #
-#     var client = new Neo4jClient("http://localhost:7474")
+#     var client = new Neo4jClient("http://neo4j:7474")
 #
 #     var node1 = new NeoNode
 #     var node2 = new NeoNode
@@ -987,7 +975,7 @@ end
 # This is a representation of a neo job in JSON Format
 #
 # Each job description should contain a `to` attribute, with a value relative to the data API root
-# (so http://localhost:7474/db/data/node becomes just /node), and a `method` attribute containing
+# (so http://neo4j:7474/db/data/node becomes just /node), and a `method` attribute containing
 # HTTP verb to use.
 #
 # Optionally you may provide a `body` attribute, and an `id` attribute to help you keep track