-
Notifications
You must be signed in to change notification settings - Fork 545
/
conn_batch.go
141 lines (130 loc) · 2.68 KB
/
conn_batch.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
package clickhouse
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)
var splitInsertRe = regexp.MustCompile(`(?i)\sVALUES\s*\(`)
func (c *connect) prepareBatch(ctx context.Context, query string, release func(*connect)) (*batch, error) {
query = splitInsertRe.Split(query, -1)[0]
if !strings.HasSuffix(strings.TrimSpace(strings.ToUpper(query)), "VALUES") {
query += " VALUES"
}
options := queryOptions(ctx)
if deadline, ok := ctx.Deadline(); ok {
c.conn.SetDeadline(deadline)
defer c.conn.SetDeadline(time.Time{})
}
if c.err = c.sendQuery(query, &options); c.err != nil {
release(c)
return nil, c.err
}
var (
onProcess = options.onProcess()
block, err = c.firstBlock(ctx, onProcess)
)
if err != nil {
release(c)
return nil, err
}
return &batch{
ctx: ctx,
conn: c,
block: block,
release: func(err error) {
c.err = err
release(c)
},
onProcess: onProcess,
}, nil
}
type batch struct {
err error
ctx context.Context
conn *connect
sent bool
block *proto.Block
release func(error)
onProcess *onProcess
}
func (b *batch) Append(v ...interface{}) error {
if b.sent {
return ErrBatchAlreadySent
}
if err := b.block.Append(v...); err != nil {
b.release(err)
return err
}
return nil
}
func (b *batch) Column(idx int) driver.BatchColumn {
if len(b.block.Columns) <= idx {
b.release(nil)
return &batchColumn{
err: &OpError{
Op: "batch.Column",
Err: fmt.Errorf("invalid column index %d", idx),
},
}
}
return &batchColumn{
batch: b,
column: b.block.Columns[idx],
release: func(err error) {
b.err = err
b.release(err)
},
}
}
func (b *batch) Send() (err error) {
defer func() {
b.sent = true
b.release(err)
}()
if b.sent {
return ErrBatchAlreadySent
}
if b.err != nil {
return b.err
}
if err = b.conn.sendData(b.block, ""); err != nil {
return err
}
if err = b.conn.sendData(&proto.Block{}, ""); err != nil {
return err
}
if err = b.conn.encoder.Flush(); err != nil {
return err
}
if err = b.conn.process(b.ctx, b.onProcess); err != nil {
return err
}
return nil
}
type batchColumn struct {
err error
batch *batch
column column.Interface
release func(error)
}
func (b *batchColumn) Append(v interface{}) (err error) {
if b.batch.sent {
return ErrBatchAlreadySent
}
if b.err != nil {
b.release(b.err)
return b.err
}
if _, err = b.column.Append(v); err != nil {
b.release(err)
return err
}
return nil
}
var _ (driver.Batch) = (*batch)(nil)
var _ (driver.BatchColumn) = (*batchColumn)(nil)