Debian Codegraph @

tblfmt.go

raw
1 // Package tblfmt provides streaming table encoders for result sets (ie, from a
2 // database).
3 package tblfmt
4
5 import (
6 "io"
7 "runtime"
8 )
9
10 // Encoder is the shared interface for encoders.
11 type Encoder interface {
12 Encode(io.Writer) error
13 EncodeAll(io.Writer) error
14 }
15
16 // ResultSet is the shared interface for a result set.
17 type ResultSet interface {
18 Next() bool
19 Scan(...any) error
20 Columns() ([]string, error)
21 Close() error
22 Err() error
23 NextResultSet() bool
24 }
25
26 // Encode encodes the result set to the writer using the supplied map params
27 // and any additional options.
28 func Encode(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error {
29 f, opts := FromMap(params)
30 enc, err := f(resultSet, append(opts, options...)...)
31 if err != nil {
32 return err
33 }
34 return enc.Encode(w)
35 }
36
37 // EncodeAll encodes all result sets to the writer using the supplied map
38 // params and any additional options.
39 func EncodeAll(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error {
40 f, opts := FromMap(params)
41 enc, err := f(resultSet, append(opts, options...)...)
42 if err != nil {
43 return err
44 }
45 return enc.EncodeAll(w)
46 }
47
48 // EncodeTable encodes result set to the writer as a table using the supplied
49 // encoding options.
50 func EncodeTable(w io.Writer, resultSet ResultSet, opts ...Option) error {
51 enc, err := NewTableEncoder(resultSet, opts...)
52 if err != nil {
53 return err
54 }
55 return enc.Encode(w)
56 }
57
58 // EncodeTableAll encodes all result sets to the writer as a table using the
59 // supplied encoding options.
60 func EncodeTableAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
61 enc, err := NewTableEncoder(resultSet, opts...)
62 if err != nil {
63 return err
64 }
65 return enc.EncodeAll(w)
66 }
67
68 // EncodeExpanded encodes result set to the writer as a table using the supplied
69 // encoding options.
70 func EncodeExpanded(w io.Writer, resultSet ResultSet, opts ...Option) error {
71 enc, err := NewExpandedEncoder(resultSet, opts...)
72 if err != nil {
73 return err
74 }
75 return enc.Encode(w)
76 }
77
78 // EncodeExpandedAll encodes all result sets to the writer as a table using the
79 // supplied encoding options.
80 func EncodeExpandedAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
81 enc, err := NewExpandedEncoder(resultSet, opts...)
82 if err != nil {
83 return err
84 }
85 return enc.EncodeAll(w)
86 }
87
88 // EncodeJSON encodes the result set to the writer as JSON using the supplied
89 // encoding options.
90 func EncodeJSON(w io.Writer, resultSet ResultSet, opts ...Option) error {
91 enc, err := NewJSONEncoder(resultSet, opts...)
92 if err != nil {
93 return err
94 }
95 return enc.Encode(w)
96 }
97
98 // EncodeJSONAll encodes all result sets to the writer as JSON using the
99 // supplied encoding options.
100 func EncodeJSONAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
101 enc, err := NewJSONEncoder(resultSet, opts...)
102 if err != nil {
103 return err
104 }
105 return enc.EncodeAll(w)
106 }
107
108 // EncodeUnaligned encodes the result set to the writer unaligned using the
109 // supplied encoding options.
110 func EncodeUnaligned(w io.Writer, resultSet ResultSet, opts ...Option) error {
111 enc, err := NewUnalignedEncoder(resultSet, opts...)
112 if err != nil {
113 return err
114 }
115 return enc.Encode(w)
116 }
117
118 // EncodeUnalignedAll encodes all result sets to the writer unaligned using the
119 // supplied encoding options.
120 func EncodeUnalignedAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
121 enc, err := NewUnalignedEncoder(resultSet, opts...)
122 if err != nil {
123 return err
124 }
125 return enc.EncodeAll(w)
126 }
127
128 // EncodeCSV encodes the result set to the writer unaligned using the
129 // supplied encoding options.
130 func EncodeCSV(w io.Writer, resultSet ResultSet, opts ...Option) error {
131 enc, err := NewCSVEncoder(resultSet, opts...)
132 if err != nil {
133 return err
134 }
135 return enc.Encode(w)
136 }
137
138 // EncodeCSVAll encodes all result sets to the writer unaligned using the
139 // supplied encoding options.
140 func EncodeCSVAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
141 enc, err := NewCSVEncoder(resultSet, opts...)
142 if err != nil {
143 return err
144 }
145 return enc.EncodeAll(w)
146 }
147
148 // EncodeTemplate encodes the result set to the writer using a template from
149 // the supplied encoding options.
150 func EncodeTemplate(w io.Writer, resultSet ResultSet, opts ...Option) error {
151 enc, err := NewTemplateEncoder(resultSet, opts...)
152 if err != nil {
153 return err
154 }
155 return enc.Encode(w)
156 }
157
158 // EncodeTemplateAll encodes all result sets to the writer using a template
159 // from the supplied encoding options.
160 func EncodeTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
161 enc, err := NewTemplateEncoder(resultSet, opts...)
162 if err != nil {
163 return err
164 }
165 return enc.EncodeAll(w)
166 }
167
168 // EncodeHTML encodes the result set to the writer using the html template and
169 // the supplied encoding options.
170 func EncodeHTML(w io.Writer, resultSet ResultSet, opts ...Option) error {
171 enc, err := NewHTMLEncoder(resultSet, opts...)
172 if err != nil {
173 return err
174 }
175 return enc.Encode(w)
176 }
177
178 // EncodeHTMLAll encodes the result set to the writer using the html template
179 // and the supplied encoding options.
180 func EncodeHTMLAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
181 enc, err := NewHTMLEncoder(resultSet, opts...)
182 if err != nil {
183 return err
184 }
185 return enc.EncodeAll(w)
186 }
187
188 // EncodeAsciiDoc encodes the result set to the writer using the asciidoc
189 // template and the supplied encoding options.
190 func EncodeAsciiDoc(w io.Writer, resultSet ResultSet, opts ...Option) error {
191 enc, err := NewAsciiDocEncoder(resultSet, opts...)
192 if err != nil {
193 return err
194 }
195 return enc.Encode(w)
196 }
197
198 // EncodeAsciiDocAll encodes the result set to the writer using the asciidoc
199 // template and the supplied encoding options.
200 func EncodeAsciiDocAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
201 enc, err := NewAsciiDocEncoder(resultSet, opts...)
202 if err != nil {
203 return err
204 }
205 return enc.EncodeAll(w)
206 }
207
208 // EncodeVertical encodes the result set to the writer using the vertical
209 // template and the supplied encoding options.
210 func EncodeVertical(w io.Writer, resultSet ResultSet, opts ...Option) error {
211 enc, err := NewVerticalEncoder(resultSet, opts...)
212 if err != nil {
213 return err
214 }
215 return enc.Encode(w)
216 }
217
218 // EncodeVerticalAll encodes the result set to the writer using the vertical
219 // template and the supplied encoding options.
220 func EncodeVerticalAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
221 enc, err := NewVerticalEncoder(resultSet, opts...)
222 if err != nil {
223 return err
224 }
225 return enc.EncodeAll(w)
226 }
227
228 // Error is an error.
229 type Error string
230
231 // Error satisfies the error interface.
232 func (err Error) Error() string {
233 return string(err)
234 }
235
236 // Error values.
237 const (
238 // ErrResultSetIsNil is the result set is nil error.
239 ErrResultSetIsNil Error = "result set is nil"
240 // ErrResultSetHasNoColumnTypes is the result set has no column types error.
241 ErrResultSetHasNoColumnTypes Error = "result set has no column types"
242 // ErrResultSetHasNoColumns is the result set has no columns error.
243 ErrResultSetHasNoColumns Error = "result set has no columns"
244 // ErrResultSetReturnedInvalidColumnTypes is the result set returned invalid column types error.
245 ErrResultSetReturnedInvalidColumnTypes Error = "result set returned invalid column types"
246 // ErrInvalidFormat is the invalid format error.
247 ErrInvalidFormat Error = "invalid format"
248 // ErrInvalidLineStyle is the invalid line style error.
249 ErrInvalidLineStyle Error = "invalid line style"
250 // ErrInvalidTemplate is the invalid template error.
251 ErrInvalidTemplate Error = "invalid template"
252 // ErrInvalidFieldSeparator is the invalid field separator error.
253 ErrInvalidFieldSeparator Error = "invalid field separator"
254 // ErrInvalidCSVFieldSeparator is the invalid csv field separator error.
255 ErrInvalidCSVFieldSeparator Error = "invalid csv field separator"
256 // ErrInvalidColumnParams is the invalid column params error.
257 ErrInvalidColumnParams Error = "invalid column params"
258 // ErrCrosstabResultMustHaveAtLeast3Columns is the crosstab result must
259 // have at least 3 columns error.
260 ErrCrosstabResultMustHaveAtLeast3Columns Error = "crosstab result must have at least 3 columns"
261 // ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumns
262 // is the data column must be specified when query returns more than three
263 // columns error.
264 ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumns Error = "data column must be specified when query returns more than three columns"
265 // ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame is the crosstab
266 // vertical and horizontal columns must not be same error.
267 ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame Error = "crosstab vertical and horizontal columns must not be same"
268 // ErrCrosstabVerticalColumnNotInResult is the crosstab vertical column not
269 // in result error.
270 ErrCrosstabVerticalColumnNotInResult Error = "crosstab vertical column not in result"
271 // ErrCrosstabHorizontalColumnNotInResult is the crosstab horizontal column
272 // not in result error.
273 ErrCrosstabHorizontalColumnNotInResult Error = "crosstab horizontal column not in result"
274 // ErrCrosstabDataColumnNotInResult is the crosstab data column not in
275 // result error.
276 ErrCrosstabDataColumnNotInResult Error = "crosstab data column not in result"
277 // ErrCrosstabHorizontalSortColumnNotInResult is the crosstab horizontal
278 // sort column not in result error.
279 ErrCrosstabHorizontalSortColumnNotInResult Error = "crosstab horizontal sort column not in result"
280 // ErrCrosstabDuplicateVerticalAndHorizontalValue is the crosstab duplicate
281 // vertical and horizontal value error.
282 ErrCrosstabDuplicateVerticalAndHorizontalValue Error = "crosstab duplicate vertical and horizontal value"
283 // ErrCrosstabHorizontalSortColumnIsNotANumber is the crosstab horizontal
284 // sort column is not a number error.
285 ErrCrosstabHorizontalSortColumnIsNotANumber Error = "crosstab horizontal sort column is not a number"
286 )
287
288 // newline is the default newline used by the system.
289 var newline []byte
290
291 func init() {
292 if runtime.GOOS == "windows" {
293 newline = []byte("\r\n")
294 } else {
295 newline = []byte("\n")
296 }
297 }