-
Notifications
You must be signed in to change notification settings - Fork 562
/
conn_batch.go
170 lines (157 loc) · 3.7 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
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
// 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"
"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, error)) (*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 err := c.sendQuery(query, &options); err != nil {
release(c, err)
return nil, err
}
var (
onProcess = options.onProcess()
block, err = c.firstBlock(ctx, onProcess)
)
if err != nil {
release(c, err)
return nil, err
}
return &batch{
ctx: ctx,
conn: c,
block: block,
release: func(err error) {
release(c, err)
},
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) AppendStruct(v interface{}) error {
values, err := b.conn.structMap.Map("AppendStruct", b.block.ColumnsNames(), v, false)
if err != nil {
return err
}
return b.Append(values...)
}
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 b.block.Rows() == 0 {
return nil
}
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)
_ (driver.BatchColumn) = (*batchColumn)(nil)
)