-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrequest.go
164 lines (141 loc) · 5.81 KB
/
request.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package gorqlite
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
// RequestResult holds the result of a single statement sent to Unified Endpoint.
//
// If statement failed, Err contains the error, neither Query nor Write is set.
// If statement succeeded, either of Query or Write is set — depending on the type of the statement.
// Query.Err and Write.Err are never set.
type RequestResult struct {
Err error
Query *QueryResult
Write *WriteResult
}
// Request is used to access Unified Endpoint to send read and writes requests in one operation.
func (conn *Connection) Request(sqlStatements []string) (results []RequestResult, err error) {
parameterizedStatements := make([]ParameterizedStatement, 0, len(sqlStatements))
for _, sqlStatement := range sqlStatements {
parameterizedStatements = append(parameterizedStatements, ParameterizedStatement{
Query: sqlStatement,
})
}
return conn.RequestParameterized(parameterizedStatements)
}
// RequestContext is used to access Unified Endpoint to send read and writes requests in one operation.
//
// To use RequestContext with parameterized queries, use RequestParameterizedContext.
func (conn *Connection) RequestContext(ctx context.Context, sqlStatements []string) (results []RequestResult, err error) {
parameterizedStatements := make([]ParameterizedStatement, 0, len(sqlStatements))
for _, sqlStatement := range sqlStatements {
parameterizedStatements = append(parameterizedStatements, ParameterizedStatement{
Query: sqlStatement,
})
}
return conn.RequestParameterizedContext(ctx, parameterizedStatements)
}
// RequestParameterized is used to access Unified Endpoint to send read and writes requests in one operation.
//
// It takes an array of parameterized SQL statements and executes them in a single transaction,
// returning an array of RequestResult vars.
// RequestParameterized returns an error if one is encountered during its operation.
// If it's something like a call to the rqlite API, then it'll return that error.
// If one statement out of several has an error, you can look at the individual statement's Err for more info.
//
// RequestParameterized uses context.Background() internally; to specify the context, use RequestParameterizedContext.
func (conn *Connection) RequestParameterized(sqlStatements []ParameterizedStatement) (results []RequestResult, err error) {
return conn.RequestParameterizedContext(context.Background(), sqlStatements)
}
// RequestParameterizedContext is used to access Unified Endpoint to send read and writes requests in one operation.
//
// It takes an array of parameterized SQL statements and executes them in a single transaction,
// returning an array of RequestResult vars.
// RequestParameterizedContext returns an error if one is encountered during its operation.
// If it's something like a call to the rqlite API, then it'll return that error.
// If one statement out of several has an error, you can look at the individual statement's Err for more info.
func (conn *Connection) RequestParameterizedContext(ctx context.Context, sqlStatements []ParameterizedStatement) (results []RequestResult, err error) {
results = make([]RequestResult, 0)
if conn.hasBeenClosed {
var errResult RequestResult
errResult.Err = ErrClosed
results = append(results, errResult)
return results, ErrClosed
}
trace("%s: Request() for %d statements", conn.ID, len(sqlStatements))
before := time.Now()
// if we get an error POSTing, that's a showstopper
response, err := conn.rqliteApiPost(ctx, api_REQUEST, sqlStatements)
after := time.Now()
if err != nil {
trace("%s: rqliteApiCall() ERROR: %s", conn.ID, err.Error())
var errResult RequestResult
errResult.Err = err
results = append(results, errResult)
return results, err
}
trace("%s: rqliteApiCall() OK, duration: %s", conn.ID, after.Sub(before))
// if we get an error Unmarshaling, that's a showstopper
var sections map[string]interface{}
err = json.Unmarshal(response, §ions)
if err != nil {
trace("%s: json.Unmarshal() ERROR: %s", conn.ID, err.Error())
var errResult RequestResult
errResult.Err = err
results = append(results, errResult)
return results, err
}
// if we got an error from the api, that's a showstopper
if errMsg, ok := sections["error"].(string); ok && errMsg != "" {
trace("%s: api ERROR: %s", conn.ID, errMsg)
var errResult RequestResult
errResult.Err = fmt.Errorf("%s", errMsg)
results = append(results, errResult)
return results, errResult.Err
}
// at this point, we have a "results" section and
// a "time" section. we can ignore the latter.
resultsArray, ok := sections["results"].([]interface{})
if !ok {
err = errors.New("result key is missing from response")
trace("%s: sections[\"results\"] ERROR: %s", conn.ID, err)
var errResult RequestResult
errResult.Err = err
results = append(results, errResult)
return results, err
}
var errs []error
for n, r := range resultsArray {
trace("%s: parsing result %d", conn.ID, n)
var thisR RequestResult
// r is a hash with columns, types, values, and time
thisResult := r.(map[string]interface{})
// did we get an error?
_, ok := thisResult["error"]
if ok {
trace("%s: have an error on this result: %s", conn.ID, thisResult["error"].(string))
thisR.Err = errors.New(thisResult["error"].(string))
results = append(results, thisR)
errs = append(errs, thisR.Err)
continue
}
_, hasValues := thisResult["values"]
_, hasColumns := thisResult["columns"]
if hasValues || hasColumns {
// Presence of these keys means this is a query result
qr := conn.parseQueryResult(thisResult)
qr.conn = conn
thisR.Query = &qr
} else {
wr := conn.parseWriteResult(thisResult)
wr.conn = conn
thisR.Write = &wr
}
results = append(results, thisR)
}
trace("%s: finished parsing, returning %d results", conn.ID, len(results))
return results, joinErrors(errs...)
}