forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
75 lines (67 loc) · 1.68 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
package clickhouse
import (
"database/sql/driver"
"io"
"reflect"
"time"
)
type textRows struct {
columns []string
types []string
data []byte
decode decoder
}
// Columns returns the columns names
func (r *textRows) Columns() []string {
return r.columns
}
// 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]
}
// Close closes the rows iterator.
func (r *textRows) Close() error {
r.data = nil
return nil
}
// Next is called to populate the next row of data into
func (r *textRows) Next(dest []driver.Value) error {
i, k := 0, 0
var err error
for j, ch := range r.data {
switch ch {
case '\t':
dest[k], err = r.decode.Decode(r.types[k], r.data[i:j])
if err != nil {
return err
}
k++
i = j + 1
case '\n':
if j == 0 {
// totals are separated by empty line
i = j + 1
continue
}
dest[k], err = r.decode.Decode(r.types[k], r.data[i:j])
r.data = r.data[j+1:]
return err
}
}
return io.EOF
}
func newTextRows(data []byte, location *time.Location, useDBLocation bool) (*textRows, error) {
colCount := numOfColumns(data)
if colCount < 0 {
return nil, ErrMalformed
}
columns := make([]string, colCount)
types := make([]string, colCount)
data = data[splitTSV(data, columns):]
data = data[splitTSV(data, types):]
return &textRows{columns: columns, types: types, data: data, decode: &textDecoder{location: location, useDBLocation: useDBLocation}}, nil
}