Skip to content

Commit 436a09d

Browse files
committed
batch close
1 parent f8a9f74 commit 436a09d

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

clickhouse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ func (ch *clickhouse) Exec(ctx context.Context, query string, args ...any) error
147147
if err != nil {
148148
return err
149149
}
150+
conn.debugf("[acquired] connection [%d]", conn.id)
151+
150152
if err := conn.exec(ctx, query, args...); err != nil {
151153
ch.release(conn, err)
152154
return err
@@ -345,6 +347,8 @@ func (ch *clickhouse) release(conn *connect, err error) {
345347
return
346348
}
347349
conn.released = true
350+
conn.debugf("[released] connection [%d]", conn.id)
351+
348352
select {
349353
case <-ch.open:
350354
default:

conn_batch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ func (b *batch) closeQuery() error {
320320
return nil
321321
}
322322

323+
func (b *batch) Close() error {
324+
if b.sent || b.released {
325+
return nil
326+
}
327+
328+
if err := b.closeQuery(); err != nil {
329+
return err
330+
}
331+
b.sent = true
332+
333+
b.release(nil)
334+
335+
return nil
336+
}
337+
323338
type batchColumn struct {
324339
err error
325340
batch driver.Batch

conn_http_batch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func (b *httpBatch) Flush() error {
109109
return nil
110110
}
111111

112+
func (b *httpBatch) Close() error {
113+
b.sent = true
114+
return nil
115+
}
116+
112117
func (b *httpBatch) Abort() error {
113118
defer func() {
114119
b.sent = true

examples/clickhouse_api/batch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func BatchInsert() error {
5353
if err != nil {
5454
return err
5555
}
56+
defer batch.Close()
57+
5658
for i := 0; i < 1000; i++ {
5759
err := batch.Append(
5860
uint8(42),

lib/driver/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type (
8787
IsSent() bool
8888
Rows() int
8989
Columns() []column.Interface
90+
Close() error
9091
}
9192
BatchColumn interface {
9293
Append(any) error

tests/abort_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,25 @@ func TestAbort(t *testing.T) {
5656
}
5757
}
5858
}
59+
60+
func TestBatchClose(t *testing.T) {
61+
conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{
62+
Method: clickhouse.CompressionLZ4,
63+
})
64+
require.NoError(t, err)
65+
ctx := context.Background()
66+
67+
batch, err := conn.PrepareBatch(ctx, "INSERT INTO function null('x UInt64') VALUES (1)")
68+
require.NoError(t, err)
69+
require.NoError(t, batch.Close())
70+
require.NoError(t, batch.Close()) // No error on multiple calls
71+
72+
batch, err = conn.PrepareBatch(ctx, "INSERT INTO function null('x UInt64') VALUES (1)")
73+
require.NoError(t, err)
74+
if assert.NoError(t, batch.Append(uint8(1))) && assert.NoError(t, batch.Send()) {
75+
var col1 uint8
76+
if err := conn.QueryRow(ctx, "SELECT 1").Scan(&col1); assert.NoError(t, err) {
77+
assert.Equal(t, uint8(1), col1)
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)