forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
146 lines (130 loc) · 2.87 KB
/
stmt.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
package clickhouse
import (
"bytes"
"context"
"database/sql/driver"
)
type stmt struct {
ch *clickhouse
query string
numInput int
isInsert bool
counter int
}
var emptyResult = &result{}
func (stmt *stmt) NumInput() int {
if stmt.numInput < 0 {
return 0
}
return stmt.numInput
}
func (stmt *stmt) Exec(args []driver.Value) (driver.Result, error) {
return stmt.execContext(context.Background(), args)
}
func (stmt *stmt) execContext(ctx context.Context, args []driver.Value) (driver.Result, error) {
if finish := stmt.ch.watchCancel(ctx); finish != nil {
defer finish()
}
if stmt.isInsert {
stmt.counter++
if err := stmt.ch.data.append(args); err != nil {
return nil, err
}
if (stmt.counter % stmt.ch.blockSize) == 0 {
if err := stmt.ch.data.write(stmt.ch.serverRevision, stmt.ch.conn); err != nil {
return nil, err
}
}
return emptyResult, nil
}
if err := stmt.ch.sendQuery(stmt.query); err != nil {
return nil, err
}
if _, err := stmt.ch.receiveData(); err != nil {
return nil, err
}
return emptyResult, nil
}
func (stmt *stmt) Query(args []driver.Value) (driver.Rows, error) {
return stmt.queryContext(context.Background(), convertOldArgs(args))
}
func (stmt *stmt) queryContext(ctx context.Context, args []namedValue) (driver.Rows, error) {
var (
buf bytes.Buffer
index int
keyword bool
)
if finish := stmt.ch.watchCancel(ctx); finish != nil {
defer finish()
}
switch {
case stmt.NumInput() != 0:
reader := bytes.NewReader([]byte(stmt.query))
for {
if char, _, err := reader.ReadRune(); err == nil {
switch char {
case '@':
if param := paramParser(reader); len(param) != 0 {
for _, v := range args {
if len(v.Name) != 0 && v.Name == param {
buf.WriteString(quote(v.Value))
}
}
}
case '?':
if keyword && index < len(args) && len(args[index].Name) == 0 {
buf.WriteString(quote(args[index].Value))
index++
} else {
buf.WriteRune(char)
}
default:
switch {
case
char == '=',
char == '<',
char == '>',
char == '(',
char == ',',
char == '%':
keyword = true
default:
keyword = keyword && (char == ' ' || char == '\t' || char == '\n')
}
buf.WriteRune(char)
}
} else {
break
}
}
default:
buf.WriteString(stmt.query)
}
if err := stmt.ch.sendQuery(buf.String()); err != nil {
return nil, err
}
rows, err := stmt.ch.receiveData()
if err != nil {
return nil, err
}
return rows, nil
}
func (stmt *stmt) Close() error {
stmt.ch.logf("[stmt] close")
return nil
}
type namedValue struct {
Name string
Ordinal int
Value driver.Value
}
func convertOldArgs(args []driver.Value) []namedValue {
dargs := make([]namedValue, len(args))
for i, v := range args {
dargs[i] = namedValue{
Ordinal: i + 1,
Value: v,
}
}
return dargs
}