-
Notifications
You must be signed in to change notification settings - Fork 562
/
clickhouse_std.go
269 lines (229 loc) · 5.82 KB
/
clickhouse_std.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
package clickhouse
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"io"
"reflect"
"regexp"
"strings"
"sync/atomic"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
)
func init() {
sql.Register("clickhouse", &stdDriver{})
}
type stdDriver struct {
conn *connect
commit func() error
connID int64
}
func (d *stdDriver) Open(dsn string) (_ driver.Conn, err error) {
var (
opt Options
conn *connect
connID = int(atomic.AddInt64(&d.connID, 1))
)
if err = opt.fromDSN(dsn); err != nil {
return nil, err
}
for num := range opt.Addr {
if opt.ConnOpenStrategy == ConnOpenRoundRobin {
num = int(connID) % len(opt.Addr)
}
if conn, err = dial(opt.Addr[num], connID, &opt); err == nil {
return &stdDriver{
conn: conn,
}, nil
}
}
return nil, err
}
func (std *stdDriver) ResetSession(ctx context.Context) error {
if std.conn.isBad() {
return driver.ErrBadConn
}
return nil
}
func (std *stdDriver) Ping(ctx context.Context) error { return std.conn.ping(ctx) }
func (std *stdDriver) Begin() (driver.Tx, error) { return std, nil }
func (std *stdDriver) Commit() error {
if std.commit == nil {
return nil
}
defer func() {
std.commit = nil
}()
return std.commit()
}
func (std *stdDriver) Rollback() error {
std.commit = nil
std.conn.close()
return nil
}
func (std *stdDriver) CheckNamedValue(nv *driver.NamedValue) error { return nil }
func (std *stdDriver) Close() error { return std.conn.close() }
func (std *stdDriver) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
r, err := std.conn.query(ctx, query, rebind(args)...)
if err != nil {
return nil, err
}
return &stdRows{
rows: r,
}, nil
}
func (std *stdDriver) Prepare(query string) (driver.Stmt, error) {
return std.PrepareContext(context.Background(), query)
}
func (std *stdDriver) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
if isInsert(query) {
return std.insert(ctx, query)
}
return &stmt{
conn: std.conn,
query: query,
}, nil
}
func (std *stdDriver) insert(ctx context.Context, query string) (driver.Stmt, error) {
batch, err := std.conn.prepareBatch(ctx, query, func(c *connect) {})
if err != nil {
return nil, err
}
std.commit = batch.Send
return &stdBatch{
batch: batch,
}, nil
}
type stdBatch struct {
batch *batch
}
func (s *stdBatch) NumInput() int { return -1 }
func (s *stdBatch) Exec(args []driver.Value) (driver.Result, error) {
values := make([]interface{}, 0, len(args))
for _, v := range args {
values = append(values, v)
}
if err := s.batch.Append(values...); err != nil {
return nil, err
}
return driver.RowsAffected(0), nil
}
func (s *stdBatch) Query(args []driver.Value) (driver.Rows, error) {
return nil, errors.New("only Exec method supported in batch mode")
}
func (s *stdBatch) Close() error { return nil }
type stmt struct {
conn *connect
query string
}
func (s *stmt) NumInput() int { return -1 }
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), driverValueToNamedValue(args))
}
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if err := s.conn.exec(ctx, s.query, rebind(args)...); err != nil {
return nil, err
}
return driver.RowsAffected(0), nil
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), driverValueToNamedValue(args))
}
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
r, err := s.conn.query(ctx, s.query, rebind(args)...)
if err != nil {
return nil, err
}
return &stdRows{
rows: r,
}, nil
}
func (s *stmt) Close() error {
return nil
}
type stdRows struct {
rows *rows
}
func (r *stdRows) Columns() []string {
return r.rows.Columns()
}
func (r *stdRows) ColumnTypeScanType(idx int) reflect.Type {
return r.rows.block.Columns[idx].ScanType()
}
func (r *stdRows) ColumnTypeDatabaseTypeName(idx int) string {
return string(r.rows.block.Columns[idx].Type())
}
func (r *stdRows) ColumnTypeNullable(idx int) (nullable, ok bool) {
_, ok = r.rows.block.Columns[idx].(*column.Nullable)
return ok, true
}
func (r *stdRows) ColumnTypePrecisionScale(idx int) (precision, scale int64, ok bool) {
switch col := r.rows.block.Columns[idx].(type) {
case *column.Decimal:
return col.Precision(), col.Scale(), true
case interface{ Base() column.Interface }:
switch col := col.Base().(type) {
case *column.Decimal:
return col.Precision(), col.Scale(), true
}
}
return 0, 0, false
}
func (r *stdRows) Next(dest []driver.Value) error {
if r.rows.Next() {
for i := range dest {
nullable, ok := r.ColumnTypeNullable(i)
switch value := r.rows.block.Columns[i].Row(r.rows.row-1, nullable && ok).(type) {
case driver.Valuer:
v, err := value.Value()
if err != nil {
return err
}
dest[i] = v
default:
dest[i] = value
}
}
return nil
}
if err := r.rows.Err(); err != nil {
return err
}
return io.EOF
}
func (r *stdRows) HasNextResultSet() bool {
return r.rows.totals != nil
}
func (r *stdRows) NextResultSet() error {
switch {
case r.rows.totals != nil:
r.rows.block = r.rows.totals
r.rows.totals = nil
default:
return io.EOF
}
return nil
}
func (r *stdRows) Close() error {
return r.rows.Close()
}
var selectRe = regexp.MustCompile(`\s+SELECT\s+`)
func isInsert(query string) bool {
if f := strings.Fields(query); len(f) > 2 {
return strings.EqualFold("INSERT", f[0]) &&
strings.EqualFold("INTO", f[1]) &&
!selectRe.MatchString(strings.ToUpper(query))
}
return false
}
func driverValueToNamedValue(args []driver.Value) []driver.NamedValue {
named := make([]driver.NamedValue, 0, len(args))
for i, v := range args {
named = append(named, driver.NamedValue{
Ordinal: i + 1,
Value: v,
})
}
return named
}