Skip to content

added parameters support #21

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

Merged
merged 1 commit into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,19 @@ func TestPath(t *testing.T) {
assert.Equal(t, d.GetProperty("population"), 126800000, "Unexpected property value.")

}

func TestParameterizedQuery(t *testing.T) {
createGraph()
params := []interface{}{1, 2.3, "str", true, false, nil, []interface {}{0, 1, 2}}
q := "RETURN $param"
params_map := make(map[string]interface{})
for index, param := range params {
params_map["param"] = param
res, err := graph.ParameterizedQuery(q, params_map);
if err != nil {
t.Error(err)
}
res.Next()
assert.Equal(t, res.Record().GetByIndex(0), params[index], "Unexpected parameter value")
}
}
8 changes: 8 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (g *Graph) Commit() (*QueryResult, error) {

// Query executes a query against the graph.
func (g *Graph) Query(q string) (*QueryResult, error) {

r, err := g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
if err != nil {
return nil, err
Expand All @@ -107,6 +108,13 @@ func (g *Graph) Query(q string) (*QueryResult, error) {
return QueryResultNew(g, r)
}

func (g *Graph) ParameterizedQuery(q string, params map[string]interface{}) (*QueryResult, error) {
if(params != nil){
q = BuildParamsHeader(params) + q
}
return g.Query(q);
}

// Merge pattern
func (g *Graph) Merge(p string) (*QueryResult, error) {
q := fmt.Sprintf("MERGE %s", p)
Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func arrayToString(arr interface{}) interface{} {
}

func ToString(i interface{}) interface{} {
if(i == nil) {
return "null"
}
v := reflect.ValueOf(i)
switch reflect.TypeOf(i).Kind() {
case reflect.String:
Expand Down Expand Up @@ -67,3 +70,11 @@ func RandomString(n int) string {
}
return string(output)
}

func BuildParamsHeader(params map[string]interface{}) (string) {
header := "CYPHER "
for key, value := range params {
header += fmt.Sprintf("%s=%v ", key, ToString(value))
}
return header
}