forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickhouse.go
238 lines (218 loc) · 5.09 KB
/
clickhouse.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
package clickhouse
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"time"
)
const (
ClientHelloPacket = 0
ClientQueryPacket = 1
ClientDataPacket = 2
ClientCancelPacket = 3
ClientPingPacket = 4
)
const (
StateComplete = 2
)
const (
ServerHelloPacket = 0
ServerDataPacket = 1
ServerExceptionPacket = 2
ServerProgressPacket = 3
ServerPongPacket = 4
ServerEndOfStreamPacket = 5
ServerProfileInfoPacket = 6
ServerTotalsPacket = 7
ServerExtremesPacket = 8
)
var (
ErrInsertInNotBatchMode = errors.New("insert statement supported only in the batch mode (use begin/commit)")
ErrLimitDataRequestInTx = errors.New("data request has already been prepared in transaction")
)
var (
splitInsertRe = regexp.MustCompile(`(?i)\sVALUES\s*\(`)
)
type logger func(format string, v ...interface{})
type clickhouse struct {
logf logger
conn *connect
serverName string
serverRevision uint64
serverVersionMinor uint64
serverVersionMajor uint64
serverTimezone *time.Location
data *block
blockSize int
inTransaction bool
}
func (ch *clickhouse) Prepare(query string) (driver.Stmt, error) {
return ch.prepareContext(context.Background(), query)
}
func (ch *clickhouse) prepareContext(ctx context.Context, query string) (driver.Stmt, error) {
ch.logf("[prepare] %s", query)
if ch.data != nil {
return nil, ErrLimitDataRequestInTx
}
if isInsert(query) {
if !ch.inTransaction {
return nil, ErrInsertInNotBatchMode
}
return ch.insert(query)
}
return &stmt{
ch: ch,
query: query,
numInput: numInput(query),
}, nil
}
func (ch *clickhouse) insert(query string) (driver.Stmt, error) {
if err := ch.sendQuery(splitInsertRe.Split(query, -1)[0] + " VALUES "); err != nil {
return nil, err
}
for {
packet, err := readUvarint(ch.conn)
if err != nil {
return nil, err
}
switch packet {
case ServerDataPacket:
var block block
if err := block.read(ch.serverRevision, ch.conn); err != nil {
return nil, err
}
ch.data = &block
return &stmt{
ch: ch,
isInsert: true,
numInput: numInput(query),
}, nil
case ServerExceptionPacket:
return nil, ch.exception()
default:
return nil, fmt.Errorf("unexpected packet [%d] from server", packet)
}
}
}
func (ch *clickhouse) Begin() (driver.Tx, error) {
return ch.beginTx(context.Background(), txOptions{})
}
type txOptions struct {
Isolation int
ReadOnly bool
}
func (ch *clickhouse) beginTx(ctx context.Context, opts txOptions) (driver.Tx, error) {
ch.logf("[begin] tx=%t, data=%t", ch.inTransaction, ch.data != nil)
if ch.inTransaction {
return nil, sql.ErrTxDone
}
if finish := ch.watchCancel(ctx); finish != nil {
defer finish()
}
ch.data = nil
ch.inTransaction = true
return ch, nil
}
func (ch *clickhouse) Commit() error {
ch.logf("[commit] tx=%t, data=%t", ch.inTransaction, ch.data != nil)
if !ch.inTransaction {
return sql.ErrTxDone
}
defer func() {
ch.data.reset()
ch.data = nil
ch.inTransaction = false
}()
if ch.data != nil {
if err := ch.data.write(ch.serverRevision, ch.conn); err != nil {
return err
}
if err := ch.ping(); err != nil {
return err
}
if err := ch.gotPacket(ServerEndOfStreamPacket); err != nil {
return err
}
}
return nil
}
func (ch *clickhouse) Rollback() error {
ch.logf("[rollback] tx=%t, data=%t", ch.inTransaction, ch.data != nil)
if !ch.inTransaction {
return sql.ErrTxDone
}
ch.data = nil
ch.inTransaction = false
return ch.cancel()
}
func (ch *clickhouse) Close() error {
ch.data = nil
return ch.conn.Close()
}
func (ch *clickhouse) gotPacket(p uint64) error {
packet, err := readUvarint(ch.conn)
if err != nil {
return err
}
for packet != p {
switch packet {
case ServerExceptionPacket:
ch.logf("[got packet] <- exception")
return ch.exception()
case ServerProgressPacket:
progress, err := ch.progress()
if err != nil {
return err
}
ch.logf("[got packet] <- progress: rows=%d, bytes=%d, total rows=%d",
progress.bytes,
progress.rows,
progress.totalRows,
)
case ServerDataPacket:
var block block
if err := block.read(ch.serverRevision, ch.conn); err != nil {
return err
}
ch.logf("[got packet] <- data: columns=%d, rows=%d", block.numColumns, block.numRows)
default:
return fmt.Errorf("unexpected packet [%d] from server", packet)
}
if packet, err = readUvarint(ch.conn); err != nil {
return err
}
}
return nil
}
func (ch *clickhouse) cancel() error {
ch.logf("cancel request")
if err := writeUvarint(ch.conn, ClientCancelPacket); err != nil {
return err
}
return ch.conn.Close()
}
func (ch *clickhouse) watchCancel(ctx context.Context) func() {
if done := ctx.Done(); done != nil {
finished := make(chan struct{})
go func() {
select {
case <-done:
_ = ch.cancel()
finished <- struct{}{}
ch.logf("[ch] watchCancel <- done")
case <-finished:
ch.logf("[ch] watchCancel <- finished")
}
}()
return func() {
select {
case <-finished:
case finished <- struct{}{}:
}
}
}
return nil
}