forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
78 lines (65 loc) · 1.47 KB
/
rows.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
package clickhouse
import (
"database/sql/driver"
"encoding/csv"
"io"
"reflect"
"time"
)
func newTextRows(c *conn, body io.ReadCloser, location *time.Location, useDBLocation bool) (*textRows, error) {
tsvReader := csv.NewReader(body)
tsvReader.Comma = '\t'
columns, err := tsvReader.Read()
if err != nil {
return nil, err
}
types, err := tsvReader.Read()
if err != nil {
return nil, err
}
return &textRows{
c: c,
respBody: body,
tsv: tsvReader,
columns: columns,
types: types,
decode: &textDecoder{location: location, useDBLocation: useDBLocation},
}, nil
}
type textRows struct {
c *conn
respBody io.ReadCloser
tsv *csv.Reader
columns []string
types []string
decode decoder
}
func (r *textRows) Columns() []string {
return r.columns
}
func (r *textRows) Close() error {
r.c.cancel = nil
return r.respBody.Close()
}
func (r *textRows) Next(dest []driver.Value) error {
row, err := r.tsv.Read()
if err != nil {
return err
}
for i, s := range row {
v, err := r.decode.Decode(r.types[i], []byte(s))
if err != nil {
return err
}
dest[i] = v
}
return nil
}
// ColumnTypeScanType implements the driver.RowsColumnTypeScanType
func (r *textRows) ColumnTypeScanType(index int) reflect.Type {
return columnType(r.types[index])
}
// ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypeDatabaseTypeName
func (r *textRows) ColumnTypeDatabaseTypeName(index int) string {
return r.types[index]
}