-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
97 lines (83 loc) · 1.57 KB
/
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
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
package clickhouse
import (
"errors"
"strings"
)
type External struct {
Name string
Structure string
Data []byte
}
type Func struct {
Name string
Args interface{}
}
type Query struct {
Stmt string
args []interface{}
externals []External
}
func (q *Query) AddExternal(name string, structure string, data []byte) {
q.externals = append(q.externals, External{Name: name, Structure: structure, Data: data})
}
func (q Query) Iter(conn *Conn) *Iter {
if conn == nil {
return &Iter{err: errors.New("Connection pointer is nil")}
}
resp, err := conn.transport.Exec(conn, q, false)
if err != nil {
return &Iter{err: err}
}
err = errorFromResponse(resp)
if err != nil {
return &Iter{err: err}
}
return &Iter{text: resp}
}
func (q Query) Exec(conn *Conn) (err error) {
if conn == nil {
return errors.New("Connection pointer is nil")
}
resp, err := conn.transport.Exec(conn, q, false)
if err == nil {
err = errorFromResponse(resp)
}
return err
}
type Iter struct {
err error
text string
}
func (r *Iter) Error() error {
return r.err
}
func (r *Iter) Scan(vars ...interface{}) bool {
row := r.fetchNext()
if len(row) == 0 {
return false
}
a := strings.Split(row, "\t")
if len(a) < len(vars) {
return false
}
for i, v := range vars {
err := unmarshal(v, a[i])
if err != nil {
r.err = err
return false
}
}
return true
}
func (r *Iter) fetchNext() string {
var res string
pos := strings.Index(r.text, "\n")
if pos == -1 {
res = r.text
r.text = ""
} else {
res = r.text[:pos]
r.text = r.text[pos+1:]
}
return res
}