-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
70 lines (57 loc) · 1.28 KB
/
exec.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
package corroclient
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
func (c *CorroClient) Exec(ctx context.Context, stmts []Statement) (*ExecResult, error) {
payload, err := json.Marshal(stmts)
if err != nil {
return nil, err
}
buffer := bytes.NewBuffer(payload)
request, err := http.NewRequestWithContext(ctx, "POST", c.getURL("/v1/transactions"), buffer)
if err != nil {
return nil, err
}
resp, err := c.request(request)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
bodyErr, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("corroclient: invalid status code: %d, body: %s", resp.StatusCode, string(bodyErr))
}
var execResult ExecResult
err = json.NewDecoder(resp.Body).Decode(&execResult)
if err != nil {
return nil, err
}
return &execResult, nil
}
type ExecResult struct {
Results []Result `json:"results"`
}
func (e *ExecResult) Errors() []error {
var errs []error
for _, res := range e.Results {
err := res.Err()
errs = append(errs, err)
}
return errs
}
type Result struct {
Error string `json:"error"`
RowAffected int `json:"rows_affected"`
Time float64 `json:"time"`
}
func (r *Result) Err() error {
if r.Error != "" {
return errors.New(r.Error)
}
return nil
}