-
Notifications
You must be signed in to change notification settings - Fork 5
/
connection.go
191 lines (169 loc) · 4.36 KB
/
connection.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
package godatabend
import (
"context"
"database/sql/driver"
"fmt"
"log"
"os"
"sync/atomic"
"github.com/google/uuid"
)
const (
accept = "Accept"
authorization = "Authorization"
contentType = "Content-Type"
jsonContentType = "application/json; charset=utf-8"
)
type DatabendConn struct {
ctx context.Context
cfg *Config
cancel context.CancelFunc
closed int32
logger *log.Logger
rest *APIClient
batchMode bool
batchInsert func() error
}
func (dc *DatabendConn) exec(ctx context.Context, query string, args ...driver.Value) (driver.Result, error) {
ctx = checkQueryID(ctx)
_, err := dc.rest.QuerySync(ctx, query, args)
if err != nil {
return emptyResult, err
}
return emptyResult, nil
}
func (dc *DatabendConn) query(ctx context.Context, query string, args ...driver.Value) (rows driver.Rows, err error) {
ctx = checkQueryID(ctx)
r0, err := dc.rest.StartQuery(ctx, query, args)
if err != nil {
return nil, fmt.Errorf("query failed: %w", err)
}
defer func() {
if err != nil {
_ = dc.rest.CloseQuery(ctx, r0)
}
}()
if r0.Error != nil {
return nil, fmt.Errorf("query error: %+v", r0.Error)
}
response, err := waitForData(ctx, dc, r0)
if err != nil {
return nil, err
}
return newNextRows(ctx, dc, response)
}
func (dc *DatabendConn) Begin() (driver.Tx, error) {
return dc.BeginTx(dc.ctx, driver.TxOptions{})
}
func (dc *DatabendConn) BeginTx(
ctx context.Context,
_ driver.TxOptions) (
driver.Tx, error) {
if dc.rest == nil {
return nil, driver.ErrBadConn
}
if _, err := dc.exec(ctx, "BEGIN"); err != nil {
return nil, err
}
return &databendTx{dc}, nil
}
func (dc *DatabendConn) cleanup() {
// must flush log buffer while the process is running.
dc.rest = nil
dc.cfg = nil
}
func (dc *DatabendConn) Ping(ctx context.Context) error {
_, err := dc.exec(ctx, "SELECT 1")
if err != nil {
return err
}
return nil
}
func (dc *DatabendConn) Prepare(query string) (driver.Stmt, error) {
return dc.PrepareContext(dc.ctx, query)
}
func (dc *DatabendConn) prepare(ctx context.Context, query string) (*databendStmt, error) {
logger.WithContext(dc.ctx).Infoln("Prepare")
if dc.rest == nil {
return nil, driver.ErrBadConn
}
batch, err := dc.prepareBatch(ctx, query)
if err != nil {
return nil, err
}
dc.batchInsert = batch.BatchInsert
dc.batchMode = true
stmt := &databendStmt{
dc: dc,
query: query,
batch: batch,
}
return stmt, nil
}
func (dc *DatabendConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
ctx = checkQueryID(ctx)
return dc.prepare(ctx, query)
}
func buildDatabendConn(ctx context.Context, config Config) (*DatabendConn, error) {
dc := &DatabendConn{
ctx: ctx,
cfg: &config,
rest: NewAPIClientFromConfig(&config),
}
if config.Debug {
dc.logger = log.New(os.Stderr, "databend: ", log.LstdFlags)
}
return dc, nil
}
func (dc *DatabendConn) log(msg ...interface{}) {
if dc.logger != nil {
dc.logger.Println(msg...)
}
}
// Close invalidates and potentially stops any current
// prepared statements and transactions, marking this
// connection as no longer in use.
func (dc *DatabendConn) Close() error {
if atomic.CompareAndSwapInt32(&dc.closed, 0, 1) {
dc.log("close connection", dc.rest)
cancel := dc.cancel
dc.cancel = nil
if cancel != nil {
cancel()
}
dc.cleanup()
}
return nil
}
func (dc *DatabendConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
values := make([]driver.Value, len(args))
for i, arg := range args {
values[i] = arg.Value
}
return dc.exec(ctx, query, values...)
}
func (dc *DatabendConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
values := make([]driver.Value, len(args))
for i, arg := range args {
values[i] = arg.Value
}
return dc.query(ctx, query, values...)
}
// ExecuteBatch applies batch prepared statement if it exists
func (dc *DatabendConn) ExecuteBatch() (err error) {
if dc.batchInsert == nil {
return nil
}
defer func() {
dc.batchInsert = nil
}()
return dc.batchInsert()
}
// checkQueryID checks if query_id exists in context, if not, generate a new one
func checkQueryID(ctx context.Context) context.Context {
if _, ok := ctx.Value(ContextKeyQueryID).(string); !ok {
queryId := uuid.NewString()
ctx = context.WithValue(ctx, ContextKeyQueryID, queryId)
}
return ctx
}