-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
input_tbln.go
132 lines (116 loc) · 2.51 KB
/
input_tbln.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
package trdsql
import (
"errors"
"io"
"strconv"
"github.com/noborus/tbln"
)
// TBLNRead provides methods of the Reader interface.
type TBLNRead struct {
reader tbln.Reader
inNULL string
preRead [][]any
limitRead bool
needNULL bool
}
// NewTBLNReader returns TBLNRead and error.
func NewTBLNReader(reader io.Reader, opts *ReadOpts) (*TBLNRead, error) {
r := &TBLNRead{}
r.reader = tbln.NewReader(reader)
r.limitRead = opts.InLimitRead
r.needNULL = opts.InNeedNULL
r.inNULL = opts.InNULL
rec, err := r.reader.ReadRow()
if err != nil {
if !errors.Is(err, io.EOF) {
return r, err
}
debug.Printf(err.Error())
return r, nil
}
// SetNames if there is no names header.
d := r.reader.GetDefinition()
names := d.Names()
if len(names) == 0 {
names = make([]string, len(rec))
for i := range rec {
names[i] = "c" + strconv.Itoa(i+1)
}
if err := d.SetNames(names); err != nil {
return r, err
}
}
// SetTypes if there is no types header.
types := d.Types()
if len(types) == 0 {
types = make([]string, len(rec))
for i := range rec {
types[i] = DefaultDBType
}
if err := d.SetTypes(types); err != nil {
return r, err
}
}
r.preRead = make([][]any, 0, opts.InPreRead)
r.preRead = append(r.preRead, r.recToRow(rec))
for n := 1; n < opts.InPreRead; n++ {
rec, err := r.reader.ReadRow()
if err != nil {
if !errors.Is(err, io.EOF) {
return r, err
}
debug.Printf(err.Error())
return r, nil
}
r.preRead = append(r.preRead, r.recToRow(rec))
}
return r, nil
}
// Names returns column names.
func (r *TBLNRead) Names() ([]string, error) {
reader := r.reader
if reader == nil {
return nil, ErrNonDefinition
}
d := reader.GetDefinition()
return d.Names(), nil
}
// Types returns column types.
func (r *TBLNRead) Types() ([]string, error) {
reader := r.reader
if reader == nil {
return nil, ErrNonDefinition
}
d := reader.GetDefinition()
return d.Types(), nil
}
// PreReadRow is returns only columns that store preread rows.
func (r *TBLNRead) PreReadRow() [][]any {
return r.preRead
}
// ReadRow is read the rest of the row.
func (r *TBLNRead) ReadRow(row []any) ([]any, error) {
if r.limitRead {
return nil, io.EOF
}
rec, err := r.reader.ReadRow()
if err != nil {
return row, err
}
row = r.recToRow(rec)
return row, nil
}
func (r *TBLNRead) recToRow(rec []string) []any {
row := make([]any, len(rec))
for i, c := range rec {
if c != "" {
row[i] = c
}
if r.needNULL {
if row[i] == r.inNULL {
row[i] = nil
}
}
}
return row
}