forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_go18.go
77 lines (69 loc) · 1.95 KB
/
conn_go18.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
// +build go1.8
package clickhouse
import (
"context"
"database/sql/driver"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Ping implements the driver.Pinger
func (c *conn) Ping(ctx context.Context) error {
if c.transport == nil {
return driver.ErrBadConn
}
// make request with empty body, response should be "Ok"
u := &url.URL{Scheme: c.url.Scheme, User: c.url.User, Host: c.url.Host, Path: "/"}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}
respBody, err := c.doRequest(ctx, req)
defer func() {
c.cancel = nil
}()
if err != nil {
return driver.ErrBadConn
}
resp, err := ioutil.ReadAll(respBody)
if err != nil || len(resp) != 4 || !strings.HasPrefix(string(resp), "Ok.") {
return driver.ErrBadConn
}
return nil
}
// BeginTx implements the driver.ConnBeginTx
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return c.beginTx(ctx)
}
// PrepareContext implements the driver.ConnPrepareContext
func (c *conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) {
return c.prepare(query)
}
// ExecContext implements the driver.ExecerContext
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
values, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return c.exec(ctx, query, values)
}
// QueryContext implements the driver.QueryerContext
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
values, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return c.query(ctx, query, values)
}
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
dargs := make([]driver.Value, len(named))
for n, param := range named {
if len(param.Name) > 0 {
// TODO: support the use of Named Parameters #561
return nil, ErrNameParams
}
dargs[n] = param.Value
}
return dargs, nil
}