-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
input_text.go
65 lines (56 loc) · 1.16 KB
/
input_text.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
package trdsql
import (
"bufio"
"io"
"strings"
)
// TextReader provides a reader for text format.
type TextReader struct {
reader *bufio.Reader
num int
maxNum int
}
// NewTextReader returns a new TextReader.
func NewTextReader(reader io.Reader, opts *ReadOpts) (*TextReader, error) {
r := &TextReader{
reader: bufio.NewReader(reader),
}
if opts.InSkip > 0 {
skipRead(r, opts.InSkip)
}
if opts.InLimitRead {
r.maxNum = opts.InPreRead
}
return r, nil
}
// Names returns column names.
func (r *TextReader) Names() ([]string, error) {
return []string{"text"}, nil
}
// Types returns column types.
func (r *TextReader) Types() ([]string, error) {
return []string{"text"}, nil
}
// PreReadRow returns pre-read rows.
func (r *TextReader) PreReadRow() [][]any {
return nil
}
// ReadRow reads a row.
func (r *TextReader) ReadRow([]any) ([]any, error) {
var builder strings.Builder
for {
if r.maxNum > 0 && r.num >= r.maxNum {
return []any{""}, io.EOF
}
line, isPrefix, err := r.reader.ReadLine()
if err != nil {
return []any{""}, err
}
builder.Write(line)
if isPrefix {
continue
}
r.num++
return []any{builder.String()}, nil
}
}