-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_csv.go
154 lines (141 loc) · 3.14 KB
/
output_csv.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package trdsql
import (
"bufio"
"strings"
"unicode"
"unicode/utf8"
)
// CSVWriter provides methods of the Writer interface.
type CSVWriter struct {
writer *bufio.Writer
needQuotes string
endLine string
outNULL string
outDelimiter rune
outQuote rune
outHeader bool
outAllQuote bool
outUseCRLF bool
needNULL bool
}
// NewCSVWriter returns CSVWriter.
func NewCSVWriter(writeOpts *WriteOpts) *CSVWriter {
w := &CSVWriter{}
w.writer = bufio.NewWriter(writeOpts.OutStream)
d, err := delimiter(writeOpts.OutDelimiter)
if err != nil {
debug.Printf("%s\n", err)
}
w.outDelimiter = d
w.outQuote = 0
if len(writeOpts.OutQuote) > 0 {
w.outQuote = ([]rune(writeOpts.OutQuote))[0]
}
w.outAllQuote = writeOpts.OutAllQuotes
w.outUseCRLF = writeOpts.OutUseCRLF
w.outHeader = writeOpts.OutHeader
w.needQuotes = string(w.outDelimiter) + string(w.outQuote) + "\r\n"
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 *CSVWriter) PreWrite(columns []string, types []string) error {
if !w.outHeader {
return nil
}
for n, column := range columns {
if n > 0 {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
}
if err := w.writeColumnString(column); err != nil {
return err
}
}
_, err := w.writer.WriteString(w.endLine)
return err
}
// WriteRow is row write.
func (w *CSVWriter) WriteRow(values []any, _ []string) error {
for n, column := range values {
if n > 0 {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
}
if err := w.writeColumn(column); err != nil {
return err
}
}
_, err := w.writer.WriteString(w.endLine)
return err
}
func (w *CSVWriter) writeColumn(column any) error {
if column == nil {
var err error
if w.needNULL {
_, err = w.writer.WriteString(w.outNULL)
} else {
_, err = w.writer.WriteString("")
}
return err
}
str := ValString(column)
return w.writeColumnString(str)
}
func (w *CSVWriter) writeColumnString(column string) error {
if !w.fieldNeedsQuotes(column) {
_, err := w.writer.WriteString(column)
return err
}
if _, err := w.writer.WriteRune(w.outQuote); err != nil {
return err
}
var err error
for _, r1 := range column {
switch r1 {
case w.outQuote:
_, err = w.writer.WriteString(string([]rune{w.outQuote, w.outQuote}))
case '\r':
if !w.outUseCRLF {
err = w.writer.WriteByte('\r')
}
case '\n':
if w.outUseCRLF {
_, err = w.writer.WriteString("\r\n")
} else {
err = w.writer.WriteByte('\n')
}
default:
_, err = w.writer.WriteRune(r1)
}
if err != nil {
return err
}
}
_, err = w.writer.WriteRune(w.outQuote)
return err
}
func (w *CSVWriter) fieldNeedsQuotes(field string) bool {
if w.outAllQuote {
return true
}
if field == "" {
return false
}
if field == `\.` || strings.ContainsAny(field, w.needQuotes) {
return true
}
r1, _ := utf8.DecodeRuneInString(field)
return unicode.IsSpace(r1)
}
// PostWrite is flush.
func (w *CSVWriter) PostWrite() error {
return w.writer.Flush()
}