Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays as parameters,- how does it work for Neoism? #104

Closed
tkandal opened this issue May 28, 2017 · 2 comments
Closed

Arrays as parameters,- how does it work for Neoism? #104

tkandal opened this issue May 28, 2017 · 2 comments
Labels

Comments

@tkandal
Copy link

tkandal commented May 28, 2017

Hello

We are doing a project in Node.js,- but I plan to do the same in Go.
I do this to measure the effort going into doing this in Go vs Node.js and also difference in performance.
The Node.js implementation uses a BOLT-driver and I have mostly kind of copied the queries.
This query works fine for Node.js and BOLT,- but not for Go and Neoism. I have also ran the query manually in the browser directly against a Neo4J-instance (of course with replacements for the parameters).

result := []struct {
    Hierarchy neoism.Node
    Ous       []neoism.Node
}{}

query := neoism.CypherQuery{
		Statement: ``MERGE (hierarchy:Hierarchy {id: {hierarchyId}})
                SET hierarchy.user = {user}
                SET hierarchy.timestamp = {timestamp}
                WITH hierarchy, {ouIds} AS ids
                MATCH (hierarchy)-[h:has]->(ouId:OuId) WHERE NOT ouId.ouId IN ids
                    DELETE h
                WITH hierarchy, ids
                UNWIND ids AS id
                MERGE (ouId:OuId {ouId: id})
                WITH hierarchy, ouId
                MATCH (hierarchy) WHERE NOT (hierarchy)-[:has]->(ouId)
                    CREATE (hierarchy)-[:has]->(ouId)
                RETURN
                    hierarchy,
                    COLLECT(DISTINCT ouId) AS ous;``,
		Parameters: neoism.Props{
			"hierarchyId": h, // String
			"user":        hierarchy.LastModified.User, // String
			"timestamp":   modified.UnixNano(), // int64
			"ouIds":       hierarchy.Ous, // [int64, int64, ... ]
		},
		Result: &result,
	}

qs := []*neoism.CypherQuery{}

var tx *neoism.Tx
if tx, err = db.Begin(qs); err != nil {
    w.WriteHeader(http.StatusInternalServerError)
    json.NewEncoder(w).Encode(model.ErrorResponse{Error: err})
    return
}

if err = db.Cypher(&query); err != nil {
    w.WriteHeader(http.StatusInternalServerError)
    json.NewEncoder(w).Encode(model.ErrorResponse{Error: errors.New("Query failed!")})
    return
}
if err = tx.Commit(); err != nil {
    w.WriteHeader(http.StatusInternalServerError)
    json.NewEncoder(w).Encode(model.ErrorResponse{Error: err})
    return
}
if len(result) == 0 {
    log.Printf("No results!\n")
    w.WriteHeader(http.StatusInternalServerError)
    return
}

The hierarchy-node is created in the database, but the rest is ignored. Commit also seems to work.
On the Go side the only symptom I get is that result is empty.

Am I doing something very wrong?

@tkandal
Copy link
Author

tkandal commented Jun 3, 2017

Hello again.

Sorry for replying on my own question,- but I have seemed to have found a solution to my problem.
There was probably a corner-case, and adding OPTIONAL MATCH solved my problem.

query := neoism.CypherQuery{
		Statement: `MERGE (hierarchy:Hierarchy {id: {hierarchyId}})
                SET hierarchy.user = {user}
                SET hierarchy.timestamp = {timestamp}
                WITH hierarchy, {ouIds} AS ids
                OPTIONAL MATCH (hierarchy)-[h:has]->(ouId:OuId) WHERE NOT ouId.ouId IN ids
                    DELETE h
                WITH hierarchy, ids
                UNWIND ids AS id
                MERGE (ouId:OuId {ouId: id})
                WITH hierarchy, ouId
                OPTIONAL MATCH (hierarchy) WHERE NOT (hierarchy)-[:has]->(ouId)
                    CREATE (hierarchy)-[:has]->(ouId)
                RETURN
                    hierarchy,
                    COLLECT(DISTINCT ouId) AS ous;`,
		Parameters: neoism.Props{
			"hierarchyId": h,
			"user":        hierarchy.LastModified.User,
			"timestamp":   modified.UnixNano(),
			"ouIds":       hierarchy.Ous,
		},
		Result: &result,
	}

@jmcvetta
Copy link
Owner

jmcvetta commented Jun 3, 2017

Great to hear you found a solution. And thanks for updating the Issue!

@jmcvetta jmcvetta closed this as completed Jun 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants