-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_raw.go
79 lines (72 loc) · 1.61 KB
/
output_raw.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
package trdsql
import (
"bufio"
"strconv"
)
// RAWWriter provides methods of the Writer interface.
type RAWWriter struct {
writer *bufio.Writer
delimiter string
endLine string
outNULL string
outHeader bool
needNULL bool
}
// NewRAWWriter returns RAWWriter.
func NewRAWWriter(writeOpts *WriteOpts) *RAWWriter {
delimiter, err := strconv.Unquote(`"` + writeOpts.OutDelimiter + `"`)
if err != nil {
debug.Printf("%s\n", err)
}
w := &RAWWriter{}
w.writer = bufio.NewWriter(writeOpts.OutStream)
w.delimiter = delimiter
w.outHeader = writeOpts.OutHeader
w.endLine = "\n"
if writeOpts.OutUseCRLF {
w.endLine = "\r\n"
}
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
return w
}
// PreWrite is output of header and preparation.
func (w *RAWWriter) PreWrite(columns []string, types []string) error {
if !w.outHeader {
return nil
}
for n, col := range columns {
if n > 0 {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
}
if _, err := w.writer.WriteString(col); err != nil {
return err
}
}
_, err := w.writer.WriteString(w.endLine)
return err
}
// WriteRow is row write.
func (w *RAWWriter) WriteRow(values []any, _ []string) error {
for n, col := range values {
if n > 0 {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
}
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
}
if _, err := w.writer.WriteString(str); err != nil {
return err
}
}
return w.writer.WriteByte('\n')
}
// PostWrite is flush.
func (w *RAWWriter) PostWrite() error {
return w.writer.Flush()
}