-
Notifications
You must be signed in to change notification settings - Fork 546
/
clickhouse_std.go
284 lines (247 loc) · 6.85 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package clickhouse
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync/atomic"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
)
var globalConnID int64
type stdConnOpener struct {
err error
opt *Options
}
func (o *stdConnOpener) Driver() driver.Driver {
return &stdDriver{}
}
func (o *stdConnOpener) Connect(ctx context.Context) (_ driver.Conn, err error) {
if o.err != nil {
return nil, o.err
}
var (
conn *connect
connID = int(atomic.AddInt64(&globalConnID, 1))
)
for num := range o.opt.Addr {
if o.opt.ConnOpenStrategy == ConnOpenRoundRobin {
num = int(connID) % len(o.opt.Addr)
}
if conn, err = dial(ctx, o.opt.Addr[num], connID, o.opt); err == nil {
return &stdDriver{
conn: conn,
}, nil
}
}
return nil, err
}
func init() {
sql.Register("clickhouse", &stdDriver{})
}
func OpenDB(opt *Options) *sql.DB {
var settings []string
if opt.MaxIdleConns > 0 {
settings = append(settings, "SetMaxIdleConns")
}
if opt.MaxOpenConns > 0 {
settings = append(settings, "SetMaxOpenConns")
}
if opt.ConnMaxLifetime > 0 {
settings = append(settings, "SetConnMaxLifetime")
}
if len(settings) != 0 {
return sql.OpenDB(&stdConnOpener{
err: fmt.Errorf("cannot connect. invalid settings. use %s (see https://pkg.go.dev/database/sql)", strings.Join(settings, ",")),
})
}
opt.setDefaults()
return sql.OpenDB(&stdConnOpener{
opt: opt,
})
}
type stdDriver struct {
conn *connect
commit func() error
}
func (d *stdDriver) Open(dsn string) (_ driver.Conn, err error) {
var opt Options
if err := opt.fromDSN(dsn); err != nil {
return nil, err
}
return (&stdConnOpener{opt: &opt}).Connect(context.Background())
}
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) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if options := queryOptions(ctx); options.async.ok {
if len(args) != 0 {
return nil, errors.New("clickhouse: you can't use parameters in an asynchronous insert")
}
return driver.RowsAffected(0), std.conn.asyncInsert(ctx, query, options.async.wait)
}
if err := std.conn.exec(ctx, query, rebind(args)...); err != nil {
return nil, err
}
return driver.RowsAffected(0), nil
}
func (std *stdDriver) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
r, err := std.conn.query(ctx, func(*connect, error) {}, 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) {
batch, err := std.conn.prepareBatch(ctx, query, func(*connect, error) {})
if err != nil {
return nil, err
}
std.commit = batch.Send
return &stdBatch{
batch: batch,
}, nil
}
func (std *stdDriver) Close() error { return std.conn.close() }
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) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
values := make([]driver.Value, 0, len(args))
for _, v := range args {
values = append(values, v.Value)
}
return s.Exec(values)
}
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 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 len(r.rows.block.Columns) != len(dest) {
return &OpError{
Op: "Next",
Err: fmt.Errorf("expected %d destination arguments in Next, not %d", len(r.rows.block.Columns), len(dest)),
}
}
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()
}