Skip to content

[Fix] Allow CallProcedure to specify mode #75

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 26 additions & 12 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ import (
"github.com/gomodule/redigo/redis"
)

// QueryMode is the query mode
type QueryMode string

const (
// ReadQuery is a read query
ReadQuery QueryMode = "READ"
// WriteQuery is a write query
WriteQuery QueryMode = "WRITE"
)

// QueryOptions are a set of additional arguments to be emitted with a query.
type QueryOptions struct {
timeout int
timeout int
}

// Graph represents a graph, which is a collection of nodes and edges.
Expand Down Expand Up @@ -110,7 +120,7 @@ func (g *Graph) Commit() (*QueryResult, error) {
// NewQueryOptions instantiates a new QueryOptions struct.
func NewQueryOptions() *QueryOptions {
return &QueryOptions{
timeout: -1,
timeout: -1,
}
}

Expand Down Expand Up @@ -147,17 +157,17 @@ func (g *Graph) ROQuery(q string) (*QueryResult, error) {
}

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

// QueryWithOptions issues a query with the given timeout
func (g *Graph) QueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
var r interface{}
var err error
if(options.timeout >= 0) {
if options.timeout >= 0 {
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact", "timeout", options.timeout)
} else {
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
Expand All @@ -171,17 +181,17 @@ func (g *Graph) QueryWithOptions(q string, options *QueryOptions) (*QueryResult,

// ParameterizedQueryWithOptions issues a parameterized query with the given timeout
func (g *Graph) ParameterizedQueryWithOptions(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
if(params != nil){
if params != nil {
q = BuildParamsHeader(params) + q
}
return g.QueryWithOptions(q, options);
return g.QueryWithOptions(q, options)
}

// ROQueryWithOptions issues a read-only query with the given timeout
func (g *Graph) ROQueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
var r interface{}
var err error
if(options.timeout >= 0) {
if options.timeout >= 0 {
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact", "timeout", options.timeout)
} else {
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact")
Expand Down Expand Up @@ -263,7 +273,7 @@ func (g *Graph) getProperty(propIdx int) string {
// Procedures

// CallProcedure invokes procedure.
func (g *Graph) CallProcedure(procedure string, yield []string, args ...interface{}) (*QueryResult, error) {
func (g *Graph) CallProcedure(procedure string, yield []string, mode QueryMode, args ...interface{}) (*QueryResult, error) {
q := fmt.Sprintf("CALL %s(", procedure)

tmp := make([]string, 0, len(args))
Expand All @@ -276,12 +286,16 @@ func (g *Graph) CallProcedure(procedure string, yield []string, args ...interfac
q += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
}

if mode == ReadQuery {
return g.ROQuery(q)
}

return g.Query(q)
}

// Labels, retrieves all node labels.
func (g *Graph) Labels() []string {
qr, _ := g.CallProcedure("db.labels", nil)
qr, _ := g.CallProcedure("db.labels", nil, ReadQuery)

l := make([]string, len(qr.results))

Expand All @@ -293,7 +307,7 @@ func (g *Graph) Labels() []string {

// RelationshipTypes, retrieves all edge relationship types.
func (g *Graph) RelationshipTypes() []string {
qr, _ := g.CallProcedure("db.relationshipTypes", nil)
qr, _ := g.CallProcedure("db.relationshipTypes", nil, ReadQuery)

rt := make([]string, len(qr.results))

Expand All @@ -305,7 +319,7 @@ func (g *Graph) RelationshipTypes() []string {

// PropertyKeys, retrieves all properties names.
func (g *Graph) PropertyKeys() []string {
qr, _ := g.CallProcedure("db.propertyKeys", nil)
qr, _ := g.CallProcedure("db.propertyKeys", nil, ReadQuery)

p := make([]string, len(qr.results))

Expand Down