Skip to content

Commit 76eae71

Browse files
committed
Allow CallProcedure to specify mode
1 parent 3b0ad09 commit 76eae71

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

graph.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ import (
88
"github.com/gomodule/redigo/redis"
99
)
1010

11+
// QueryMode is the query mode
12+
type QueryMode string
13+
14+
const (
15+
// ReadQuery is a read query
16+
ReadQuery QueryMode = "READ"
17+
// WriteQuery is a write query
18+
WriteQuery QueryMode = "WRITE"
19+
)
20+
1121
// QueryOptions are a set of additional arguments to be emitted with a query.
1222
type QueryOptions struct {
13-
timeout int
23+
timeout int
1424
}
1525

1626
// Graph represents a graph, which is a collection of nodes and edges.
@@ -110,7 +120,7 @@ func (g *Graph) Commit() (*QueryResult, error) {
110120
// NewQueryOptions instantiates a new QueryOptions struct.
111121
func NewQueryOptions() *QueryOptions {
112122
return &QueryOptions{
113-
timeout: -1,
123+
timeout: -1,
114124
}
115125
}
116126

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

149159
func (g *Graph) ParameterizedQuery(q string, params map[string]interface{}) (*QueryResult, error) {
150-
if(params != nil){
160+
if params != nil {
151161
q = BuildParamsHeader(params) + q
152162
}
153-
return g.Query(q);
163+
return g.Query(q)
154164
}
155165

156166
// QueryWithOptions issues a query with the given timeout
157167
func (g *Graph) QueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
158168
var r interface{}
159169
var err error
160-
if(options.timeout >= 0) {
170+
if options.timeout >= 0 {
161171
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact", "timeout", options.timeout)
162172
} else {
163173
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
@@ -171,17 +181,17 @@ func (g *Graph) QueryWithOptions(q string, options *QueryOptions) (*QueryResult,
171181

172182
// ParameterizedQueryWithOptions issues a parameterized query with the given timeout
173183
func (g *Graph) ParameterizedQueryWithOptions(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
174-
if(params != nil){
184+
if params != nil {
175185
q = BuildParamsHeader(params) + q
176186
}
177-
return g.QueryWithOptions(q, options);
187+
return g.QueryWithOptions(q, options)
178188
}
179189

180190
// ROQueryWithOptions issues a read-only query with the given timeout
181191
func (g *Graph) ROQueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
182192
var r interface{}
183193
var err error
184-
if(options.timeout >= 0) {
194+
if options.timeout >= 0 {
185195
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact", "timeout", options.timeout)
186196
} else {
187197
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact")
@@ -263,7 +273,7 @@ func (g *Graph) getProperty(propIdx int) string {
263273
// Procedures
264274

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

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

289+
if mode == ReadQuery {
290+
return g.ROQuery(q)
291+
}
292+
279293
return g.Query(q)
280294
}
281295

282296
// Labels, retrieves all node labels.
283297
func (g *Graph) Labels() []string {
284-
qr, _ := g.CallProcedure("db.labels", nil)
298+
qr, _ := g.CallProcedure("db.labels", nil, ReadQuery)
285299

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)