-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
writer.go
202 lines (183 loc) · 4.52 KB
/
writer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package trdsql
import (
"io"
"os"
)
// extToOutFormat is a map of file extensions to formats.
var extToOutFormat = map[string]Format{
"CSV": CSV,
"LTSV": LTSV,
"JSON": JSON,
"JSONL": JSONL,
"TBLN": TBLN,
"RAW": RAW,
"MD": MD,
"AT": AT,
"VF": VF,
"YAML": YAML,
"YML": YAML,
}
// Writer is an interface that wraps the Write method that writes from the database to a file.
// Writer is a group of methods called from Export.
type Writer interface {
// PreWrite is called first to write.
// The arguments are a list of column names and a list of type names.
PreWrite(columns []string, types []string) error
// WriteRow is row write.
WriteRow(row []any, columns []string) error
// PostWrite is called last in the write.
PostWrite() error
}
// WriteOpts represents options that determine the behavior of the writer.
type WriteOpts struct {
// OutStream is the output destination.
OutStream io.Writer
// ErrStream is the error output destination.
ErrStream io.Writer
// OutDelimiter is the output delimiter (Use only CSV and Raw).
OutDelimiter string
// OutQuote is the output quote character (Use only CSV).
OutQuote string
// OutNeedNULL is true, replace NULL with OutNULL.
OutNULL string
// OutFormat is the writing format.
OutFormat Format
// OutAllQuotes is true if Enclose all fields (Use only CSV).
OutAllQuotes bool
// True to use \r\n as the line terminator (Use only CSV).
OutUseCRLF bool
// OutHeader is true if it outputs a header(Use only CSV and Raw).
OutHeader bool
// OutNoWrap is true, do not wrap long columns(Use only AT and MD).
OutNoWrap bool
// OutNeedNULL is true, replace NULL with OutNULL.
OutNeedNULL bool
// OutJSONToYAML is true, convert JSON to YAML(Use only YAML).
OutJSONToYAML bool
}
// WriteOpt is a function to set WriteOpts.
type WriteOpt func(*WriteOpts)
// OutFormat sets Format.
func OutFormat(f Format) WriteOpt {
return func(args *WriteOpts) {
args.OutFormat = f
}
}
// OutDelimiter sets delimiter.
func OutDelimiter(d string) WriteOpt {
return func(args *WriteOpts) {
args.OutDelimiter = d
}
}
// OutQuote sets quote.
func OutQuote(q string) WriteOpt {
return func(args *WriteOpts) {
args.OutQuote = q
}
}
// OutUseCRLF sets use CRLF.
func OutUseCRLF(c bool) WriteOpt {
return func(args *WriteOpts) {
args.OutUseCRLF = c
}
}
// OutAllQuotes sets all quotes.
func OutAllQuotes(a bool) WriteOpt {
return func(args *WriteOpts) {
args.OutAllQuotes = a
}
}
// OutHeader sets flag to output header.
func OutHeader(h bool) WriteOpt {
return func(args *WriteOpts) {
args.OutHeader = h
}
}
// OutNoWrap sets flag to output do not wrap long columns.
func OutNoWrap(w bool) WriteOpt {
return func(args *WriteOpts) {
args.OutNoWrap = w
}
}
// OutNeedNULL sets a flag to replace NULL.
func OutNeedNULL(n bool) WriteOpt {
return func(args *WriteOpts) {
args.OutNeedNULL = n
}
}
// OutNULL sets the output NULL string.
func OutNULL(s string) WriteOpt {
return func(args *WriteOpts) {
args.OutNULL = s
}
}
// OutStream sets the output destination.
func OutStream(w io.Writer) WriteOpt {
return func(args *WriteOpts) {
args.OutStream = w
}
}
// ErrStream sets the error output destination.
func ErrStream(w io.Writer) WriteOpt {
return func(args *WriteOpts) {
args.ErrStream = w
}
}
// NewWriter returns a Writer interface.
// The argument is an option of Functional Option Pattern.
//
// usage:
//
// NewWriter(
// trdsql.OutFormat(trdsql.CSV),
// trdsql.OutHeader(true),
// trdsql.OutDelimiter(";"),
// )
func NewWriter(options ...WriteOpt) Writer {
writeOpts := &WriteOpts{
OutFormat: CSV,
OutDelimiter: ",",
OutQuote: "\"",
OutAllQuotes: false,
OutUseCRLF: false,
OutHeader: false,
OutNeedNULL: false,
OutNULL: "",
OutStream: os.Stdout,
ErrStream: os.Stderr,
}
for _, option := range options {
option(writeOpts)
}
switch writeOpts.OutFormat {
case LTSV:
return NewLTSVWriter(writeOpts)
case JSON:
return NewJSONWriter(writeOpts)
case YAML:
return NewYAMLWriter(writeOpts)
case RAW:
return NewRAWWriter(writeOpts)
case MD:
return NewTWWriter(writeOpts, true)
case AT:
return NewTWWriter(writeOpts, false)
case VF:
return NewVFWriter(writeOpts)
case TBLN:
return NewTBLNWriter(writeOpts)
case JSONL:
return NewJSONLWriter(writeOpts)
case CSV:
return NewCSVWriter(writeOpts)
default:
return NewCSVWriter(writeOpts)
}
}
// OutputFormat returns the format from the extension.
func OutputFormat(ext string) Format {
if format, ok := extToOutFormat[ext]; ok {
return format
}
return GUESS
}