forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
107 lines (92 loc) · 2.05 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
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
package clickhouse
import (
"database/sql/driver"
"encoding/csv"
"fmt"
"io"
"reflect"
"strings"
"time"
)
func newTextRows(c *conn, body io.ReadCloser, location *time.Location, useDBLocation bool) (*textRows, error) {
tsvReader := csv.NewReader(body)
tsvReader.Comma = '\t'
tsvReader.LazyQuotes = true
columns, err := tsvReader.Read()
if err != nil {
return nil, err
}
types, err := tsvReader.Read()
if err != nil {
return nil, err
}
for i := range types {
types[i], err = readUnquoted(strings.NewReader(types[i]), 0)
if err != nil {
return nil, err
}
}
parsers := make([]DataParser, len(types), len(types))
for i, typ := range types {
desc, err := ParseTypeDesc(typ)
if err != nil {
return nil, err
}
parsers[i], err = NewDataParser(desc, &DataParserOptions{
Location: location,
UseDBLocation: useDBLocation,
})
if err != nil {
return nil, err
}
}
return &textRows{
c: c,
respBody: body,
tsv: tsvReader,
columns: columns,
types: types,
parsers: parsers,
}, nil
}
type textRows struct {
c *conn
respBody io.ReadCloser
tsv *csv.Reader
columns []string
types []string
parsers []DataParser
}
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 {
reader := strings.NewReader(s)
v, err := r.parsers[i].Parse(reader)
if err != nil {
return err
}
if _, _, err := reader.ReadRune(); err != io.EOF {
return fmt.Errorf("trailing data after parsing the value")
}
dest[i] = v
}
return nil
}
// ColumnTypeScanType implements the driver.RowsColumnTypeScanType
func (r *textRows) ColumnTypeScanType(index int) reflect.Type {
return r.parsers[index].Type()
}
// ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypeDatabaseTypeName
func (r *textRows) ColumnTypeDatabaseTypeName(index int) string {
return r.types[index]
}