-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
input_gw.go
113 lines (103 loc) · 2.24 KB
/
input_gw.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
package trdsql
import (
"errors"
"io"
"github.com/noborus/guesswidth"
)
// GWReader provides methods of the Reader interface.
type GWReader struct {
reader *guesswidth.GuessWidth
scanNum int
preRead int
inNULL string
names []string
types []string
limitRead bool
needNULL bool
}
// NewGWReader returns GWReader and error.
func NewGWReader(reader io.Reader, opts *ReadOpts) (*GWReader, error) {
r := &GWReader{}
r.reader = guesswidth.NewReader(reader)
r.reader.TrimSpace = true
r.limitRead = opts.InLimitRead
r.reader.Header = opts.InSkip
r.scanNum = 1000
r.needNULL = opts.InNeedNULL
r.inNULL = opts.InNULL
r.preRead = opts.InPreRead
if r.preRead > r.scanNum {
r.scanNum = r.preRead
}
r.reader.Scan(r.scanNum)
for i := 0; i < opts.InSkip; i++ {
if _, err := r.reader.Read(); err != nil {
if errors.Is(err, io.EOF) {
return r, nil
}
}
}
names, err := r.reader.Read()
if err != nil {
if errors.Is(err, io.EOF) {
return r, nil
}
return nil, err
}
r.names = names
r.setColumnType()
return r, nil
}
func (r *GWReader) setColumnType() {
if r.names == nil {
return
}
r.types = make([]string, len(r.names))
for i := 0; i < len(r.names); i++ {
r.types[i] = DefaultDBType
}
}
// Names returns column names.
func (r *GWReader) Names() ([]string, error) {
return r.names, nil
}
// Types returns column types.
// All GW types return the DefaultDBType.
func (r *GWReader) Types() ([]string, error) {
return r.types, nil
}
// PreReadRow is returns only columns that store preread rows.
func (r *GWReader) PreReadRow() [][]any {
rows := make([][]any, r.preRead)
for n := 0; n < r.preRead; n++ {
record, err := r.reader.Read()
if err != nil {
return rows
}
rows[n] = make([]any, len(r.names))
for i := 0; i < len(r.names); i++ {
rows[n][i] = record[i]
if r.needNULL {
rows[n][i] = replaceNULL(r.inNULL, rows[n][i])
}
}
}
return rows
}
// ReadRow is read the rest of the row.
func (r *GWReader) ReadRow(row []any) ([]any, error) {
if r.limitRead {
return nil, io.EOF
}
record, err := r.reader.Read()
if err != nil {
return row, err
}
for i := 0; i < len(row); i++ {
row[i] = record[i]
if r.needNULL {
row[i] = replaceNULL(r.inNULL, row[i])
}
}
return row, nil
}