-
Notifications
You must be signed in to change notification settings - Fork 562
/
conn_query.go
72 lines (60 loc) · 1.23 KB
/
conn_query.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
package clickhouse
import (
"context"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)
func (c *connect) query(ctx context.Context, query string, args ...interface{}) (*rows, error) {
var (
options = queryOptions(ctx)
onProcess = options.onProcess()
body, err = bind(c.server.Timezone, query, args...)
)
if err != nil {
return nil, err
}
if deadline, ok := ctx.Deadline(); ok {
c.conn.SetDeadline(deadline)
defer c.conn.SetDeadline(time.Time{})
}
if c.err = c.sendQuery(body, &options); c.err != nil {
return nil, c.err
}
init, err := c.firstBlock(ctx, onProcess)
if err != nil {
return nil, err
}
var (
errors = make(chan error)
stream = make(chan *proto.Block, 2)
)
go func() {
onProcess.data = func(b *proto.Block) {
stream <- b
}
err := c.process(ctx, onProcess)
if err != nil {
errors <- err
}
close(errors)
close(stream)
}()
return &rows{
conn: c,
block: init,
stream: stream,
errors: errors,
columns: init.ColumnsNames(),
}, nil
}
func (c *connect) queryRow(ctx context.Context, query string, args ...interface{}) *row {
rows, err := c.query(ctx, query, args...)
if err != nil {
return &row{
err: err,
}
}
return &row{
rows: rows,
}
}