-
Notifications
You must be signed in to change notification settings - Fork 19
/
crate.go
278 lines (220 loc) · 5.55 KB
/
crate.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package crate
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
// Crate conn structure
type CrateDriver struct {
Url string // Crate http endpoint url
username string
password string
httpClient *http.Client
}
// Init a new "Connection" to a Crate Data Storage instance.
// Note that the connection is not tested until the first query.
func (c *CrateDriver) Open(crate_url string) (driver.Conn, error) {
u, err := url.Parse(crate_url)
if err != nil {
return nil, err
}
sanUrl := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
c.Url = sanUrl
c.httpClient = &http.Client{}
if u.User != nil {
username := u.User.Username()
password, _ := u.User.Password()
c.username = username
c.password = password
}
return c, nil
}
// JSON endpoint response struct
// We expect error to be null or ommited
type endpointResponse struct {
Error struct {
Message string
Code int
} `json:"error"`
Cols []string `json:"cols"`
Duration float64 `json:"duration"`
ColumnTypes []interface{} `json:"col_types"`
Rowcount int64 `json:"rowcount"`
Rows [][]interface{} `json:"rows"`
}
// JSON endpoint request struct
type endpointQuery struct {
Stmt string `json:"stmt"`
Args []driver.Value `json:"args,omitempty"`
}
// Query the database using prepared statements.
// Read: https://crate.io/docs/stable/sql/rest.html for more information about the returned response.
// Example: crate.Query("SELECT * FROM sys.cluster LIMIT ?", 10)
// "Parameter Substitution" is also supported, read, https://crate.io/docs/stable/sql/rest.html#parameter-substitution
// This is the internal query function
func (c *CrateDriver) query(stmt string, args []driver.Value) (*endpointResponse, error) {
endpoint := c.Url + "/_sql?types"
query := &endpointQuery{
Stmt: stmt,
}
if len(args) > 0 {
query.Args = args
}
buf, err := json.Marshal(query)
if err != nil {
return nil, err
}
data := bytes.NewReader(buf)
req, err := http.NewRequest("POST", endpoint, data)
if err != nil {
return nil, err
}
if c.username != "" {
req.SetBasicAuth(c.username, c.password)
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// Parse response
res := &endpointResponse{}
d := json.NewDecoder(resp.Body)
// We need to set this, or long integers will be interpreted as floats
d.UseNumber()
err = d.Decode(res)
if err != nil {
return nil, err
}
// Check for db errors
if res.Error.Code != 0 {
err = &CrateErr{
Code: res.Error.Code,
Message: res.Error.Message,
}
return nil, err
}
return res, nil
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
msg := fmt.Sprintf("Invalid http status code %d, body: %s", resp.StatusCode, string(body))
return nil, errors.New(msg)
}
}
// Queries the database
func (c *CrateDriver) Query(stmt string, args []driver.Value) (driver.Rows, error) {
res, err := c.query(stmt, args)
if err != nil {
return nil, err
}
// Rows reader
rows := &Rows{
columns: res.Cols,
values: res.Rows,
rowcount: res.Rowcount,
}
return rows, nil
}
// Exec queries on the dataabase
func (c *CrateDriver) Exec(stmt string, args []driver.Value) (result driver.Result, err error) {
res, err := c.query(stmt, args)
if err != nil {
return nil, err
}
result = &Result{res.Rowcount}
return result, nil
}
// Result interface
type Result struct {
affectedRows int64
}
// Last inserted id
func (r *Result) LastInsertId() (int64, error) {
err := errors.New("LastInsertId() not supported.")
return 0, err
}
// # of affected rows on exec
func (r *Result) RowsAffected() (int64, error) {
return r.affectedRows, nil
}
// Rows reader
type Rows struct {
columns []string
values [][]interface{}
rowcount int64
pos int64 // index position on the values array
}
// Row columns
func (r *Rows) Columns() []string {
return r.columns
}
// Get the next row
func (r *Rows) Next(dest []driver.Value) error {
if r.pos >= r.rowcount {
return io.EOF
}
for i := range dest {
dest[i] = r.values[r.pos][i]
}
r.pos++
return nil
}
// Close
func (r *Rows) Close() error {
r.pos = r.rowcount // Set to end of list
return nil
}
// Yet not supported
func (c *CrateDriver) Begin() (driver.Tx, error) {
err := errors.New("Transactions are not supported by this driver.")
return nil, err
}
// Nothing to close, crate is stateless
func (c *CrateDriver) Close() error {
return nil
}
// Prepared stmt interface
type CrateStmt struct {
stmt string // Query stmt
driver *CrateDriver
}
// Driver method that initiates the prepared stmt interface
func (c *CrateDriver) Prepare(query string) (driver.Stmt, error) {
stmt := &CrateStmt{
stmt: query,
driver: c,
}
return stmt, nil
}
// Just pass it to the driver's' default Query() function
func (s *CrateStmt) Query(args []driver.Value) (driver.Rows, error) {
return s.driver.Query(s.stmt, args)
}
// Just pass it to the driver's default Exec() function
func (s *CrateStmt) Exec(args []driver.Value) (driver.Result, error) {
return s.driver.Exec(s.stmt, args)
}
// No need to implement close
func (s *CrateStmt) Close() error {
return nil
}
// The NumInput method is not supported, return -1 so the database/sql packages knows.
func (s *CrateStmt) NumInput() int {
return -1
}
// Register the driver
func init() {
sql.Register("crate", &CrateDriver{})
}