lib: remove useless comparison on null that broke tests
[nit.git] / lib / neo4j / neo4j.nit
index 6b3f0f3..385b966 100644 (file)
@@ -246,6 +246,19 @@ class Neo4jClient
        end
 
        # Retrieve all nodes with specified `lbl`
+       #
+       #     var client = new Neo4jClient("http://localhost:7474")
+       #     #
+       #     var andres = new NeoNode
+       #     andres.labels.add_all(["Human", "Male"])
+       #     client.save_node(andres)
+       #     var kate = new NeoNode
+       #     kate.labels.add_all(["Human", "Female"])
+       #     client.save_node(kate)
+       #     #
+       #     var nodes = client.nodes_with_label("Human")
+       #     assert nodes.has(andres)
+       #     assert nodes.has(kate)
        fun nodes_with_label(lbl: String): Array[NeoNode] do
                var res = get("{base_url}/db/data/label/{lbl}/nodes")
                var nodes = new Array[NeoNode]
@@ -258,6 +271,33 @@ class Neo4jClient
                return nodes
        end
 
+       # Retrieve nodes belonging to all the specified `labels`.
+       #
+       #     var client = new Neo4jClient("http://localhost:7474")
+       #     #
+       #     var andres = new NeoNode
+       #     andres.labels.add_all(["Human", "Male"])
+       #     client.save_node(andres)
+       #     var kate = new NeoNode
+       #     kate.labels.add_all(["Human", "Female"])
+       #     client.save_node(kate)
+       #     #
+       #     var nodes = client.nodes_with_labels(["Human", "Male"])
+       #     assert nodes.has(andres)
+       #     assert not nodes.has(kate)
+       fun nodes_with_labels(labels: Array[String]): Array[NeoNode] do
+               assert not labels.is_empty
+               var res = cypher(new CypherQuery.from_string("MATCH (n:{labels.join(":")}) RETURN n"))
+               var nodes = new Array[NeoNode]
+               for json in res.as(JsonObject)["data"].as(JsonArray) do
+                       var obj = json.as(JsonArray).first.as(JsonObject)
+                       var node = load_node(obj["self"].to_s)
+                       node.internal_properties = obj["data"].as(JsonObject)
+                       nodes.add node
+               end
+               return nodes
+       end
+
        # Perform a `CypherQuery`
        # see: CypherQuery
        fun cypher(query: CypherQuery): Jsonable do
@@ -344,14 +384,12 @@ end
 # For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
 class CypherQuery
        # Query string to perform
-       private var query: String
+       private var query: String = ""
 
        # `params` to embed in the query like in prepared statements
        var params = new JsonObject
 
-       init do
-               self.query = ""
-       end
+       init do end
 
        # init the query from a query string
        init from_string(query: String) do
@@ -448,10 +486,10 @@ end
 #     assert node["name"] == "Andres"  # loaded lazily from base
 abstract class NeoEntity
        # Neo4j client connector
-       private var neo: Neo4jClient
+       private var neo: Neo4jClient is noinit
 
        # Entity unique URL in Neo4j REST API
-       var url: nullable String
+       var url: nullable String = null
 
        # Temp id used in batch mode to update the entity
        private var batch_id: nullable Int = null
@@ -805,6 +843,15 @@ class NeoBatch
                end
        end
 
+       # Create a `NeoNode` or a `NeoEdge` in batch mode.
+       fun save_entity(nentity: NeoEntity) do
+               if nentity isa NeoNode then
+                       save_node(nentity)
+               else if nentity isa NeoEdge then
+                       save_edge(nentity)
+               else abort
+       end
+
        # Create a node in batch mode also create labels and edges
        fun save_node(node: NeoNode) do
                if node.id != null or node.batch_id != null then return