forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_go18.go
87 lines (74 loc) · 2.14 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
78
79
80
81
82
83
84
85
86
87
// +build go1.8
package clickhouse
import (
"context"
"database/sql/driver"
"fmt"
"io/ioutil"
"strings"
)
// pingExpectedPrefix represents expected answer for "SELECT 1" query
const pingExpectedPrefix = "1"
// Ping implements the driver.Pinger
func (c *conn) Ping(ctx context.Context) error {
if c.transport == nil {
return ErrTransportNil
}
req, err := c.buildRequest(ctx, "select 1", nil)
if err != nil {
return err
}
respBody, err := c.doRequest(ctx, req)
defer func() {
c.cancel = nil
}()
if err != nil {
return err
}
// Close response body to enable connection reuse
defer respBody.Close()
// drain the response body to check if we got expected `1`
resp, err := ioutil.ReadAll(respBody)
if err != nil {
return fmt.Errorf("ping: failed to read the response: %w", err)
}
if !strings.HasPrefix(string(resp), pingExpectedPrefix) {
return fmt.Errorf("ping: failed to get expected result (1), got '%s' instead", string(resp))
}
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
}