-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_manager_query.go
91 lines (75 loc) · 2.5 KB
/
client_manager_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package goC8
import (
"github.com/marvin-hansen/goC8/requests/query_req"
"github.com/marvin-hansen/goC8/utils"
"time"
)
type QueryManager struct {
client *Client
}
func NewQueryManager(client *Client) *QueryManager {
return &QueryManager{client: client}
}
func (c QueryManager) ExplainQuery(fabric, query string) (res *query_req.ResponseForExplainQuery, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "ExplainQuery")
}
req := query_req.NewRequestForExplainQuery(fabric, query)
res = query_req.NewResponseForExplainQuery()
err = c.client.Request(req, res)
return res, CheckReturnError(err)
}
// ValidateQuery
// validates a C8QL query. To actually query the database, see Query
// https://macrometa.com/docs/api#/operations/parseQuery
func (c QueryManager) ValidateQuery(fabric, query string) (res *query_req.ResponseForValidateQuery, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "ValidateQuery")
}
req := query_req.NewRequestForValidateQuery(fabric, query)
res = query_req.NewResponseForValidateQuery()
err = c.client.Request(req, res)
return res, CheckReturnError(err)
}
// Query
// Queries database / graph
func (c QueryManager) Query(fabric, query string, bindVars map[string]interface{}, options *query_req.CursorOptions) (res *query_req.Cursor, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "Query")
}
if options == nil {
options = query_req.NewDefaultCursorOptions()
}
ttl := c.client.getQueryTTL()
req := query_req.NewRequestForCreateCursor(fabric, query, bindVars, options, ttl)
// Create a cursor for the query from the server
res = query_req.NewResponseForCreateCursor()
if err = c.client.requestJsonResponse(req, res); err != nil {
return nil, err
}
// check for more to come
if res.HasMore {
for {
// request update for the cursor
reqNext := query_req.NewRequestForReadNextCursor(fabric, res.Id)
responseNext := query_req.NewResponseForReadNextCursor()
if err = c.client.requestJsonResponse(reqNext, responseNext); err != nil {
return nil, err
}
// updated cursor with next result
res.Update(responseNext)
// stop when there are no more results, delete cursor and stop
if responseNext.HasMore == false {
// Delete the cursor from the server
reqDel := query_req.NewRequestForDeleteCursor(fabric, res.Id)
resDel := query_req.NewResponseForDeleteCursor()
if err = c.client.Request(reqDel, resDel); err != nil {
return nil, err
}
// stop the loop
break
}
}
}
return res, nil
}