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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Package tblfmt provides streaming table encoders for result sets (ie, from a
// database).
package tblfmt
import (
"io"
"runtime"
)
// Encoder is the shared interface for encoders.
type Encoder interface {
Encode(io.Writer) error
EncodeAll(io.Writer) error
}
// ResultSet is the shared interface for a result set.
type ResultSet interface {
Next() bool
Scan(...any) error
Columns() ([]string, error)
Close() error
Err() error
NextResultSet() bool
}
// Encode encodes the result set to the writer using the supplied map params
// and any additional options.
func Encode(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error {
f, opts := FromMap(params)
enc, err := f(resultSet, append(opts, options...)...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeAll encodes all result sets to the writer using the supplied map
// params and any additional options.
func EncodeAll(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error {
f, opts := FromMap(params)
enc, err := f(resultSet, append(opts, options...)...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeTable encodes result set to the writer as a table using the supplied
// encoding options.
func EncodeTable(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewTableEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeTableAll encodes all result sets to the writer as a table using the
// supplied encoding options.
func EncodeTableAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewTableEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeExpanded encodes result set to the writer as a table using the supplied
// encoding options.
func EncodeExpanded(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewExpandedEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeExpandedAll encodes all result sets to the writer as a table using the
// supplied encoding options.
func EncodeExpandedAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewExpandedEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeJSON encodes the result set to the writer as JSON using the supplied
// encoding options.
func EncodeJSON(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewJSONEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeJSONAll encodes all result sets to the writer as JSON using the
// supplied encoding options.
func EncodeJSONAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewJSONEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeUnaligned encodes the result set to the writer unaligned using the
// supplied encoding options.
func EncodeUnaligned(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewUnalignedEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeUnalignedAll encodes all result sets to the writer unaligned using the
// supplied encoding options.
func EncodeUnalignedAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewUnalignedEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeCSV encodes the result set to the writer unaligned using the
// supplied encoding options.
func EncodeCSV(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewCSVEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeCSVAll encodes all result sets to the writer unaligned using the
// supplied encoding options.
func EncodeCSVAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewCSVEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeTemplate encodes the result set to the writer using a template from
// the supplied encoding options.
func EncodeTemplate(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewTemplateEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeTemplateAll encodes all result sets to the writer using a template
// from the supplied encoding options.
func EncodeTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewTemplateEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeHTML encodes the result set to the writer using the html template and
// the supplied encoding options.
func EncodeHTML(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewHTMLEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeHTML encodes the result set to the writer using the html template and
// the supplied encoding options.
func EncodeHTMLAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewHTMLEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeAsciiDoc encodes the result set to the writer using the asciidoc
// template and the supplied encoding options.
func EncodeAsciiDoc(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewAsciiDocEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeAsciiDoc encodes the result set to the writer using the asciidoc
// template and the supplied encoding options.
func EncodeAsciiDocAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewAsciiDocEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// EncodeVertical encodes the result set to the writer using the vertical
// template and the supplied encoding options.
func EncodeVertical(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewVerticalEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.Encode(w)
}
// EncodeVertical encodes the result set to the writer using the vertical
// template and the supplied encoding options.
func EncodeVerticalAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
enc, err := NewVerticalEncoder(resultSet, opts...)
if err != nil {
return err
}
return enc.EncodeAll(w)
}
// Error is an error.
type Error string
// Error satisfies the error interface.
func (err Error) Error() string {
return string(err)
}
// Error values.
const (
// ErrResultSetIsNil is the result set is nil error.
ErrResultSetIsNil Error = "result set is nil"
// ErrResultSetHasNoColumnTypes is the result set has no column types error.
ErrResultSetHasNoColumnTypes Error = "result set has no column types"
// ErrResultSetHasNoColumns is the result set has no columns error.
ErrResultSetHasNoColumns Error = "result set has no columns"
// ErrResultSetReturnedInvalidColumnTypes is the result set returned invalid column types error.
ErrResultSetReturnedInvalidColumnTypes Error = "result set returned invalid column types"
// ErrInvalidFormat is the invalid format error.
ErrInvalidFormat Error = "invalid format"
// ErrInvalidLineStyle is the invalid line style error.
ErrInvalidLineStyle Error = "invalid line style"
// ErrInvalidTemplate is the invalid template error.
ErrInvalidTemplate Error = "invalid template"
// ErrInvalidFieldSeparator is the invalid field separator error.
ErrInvalidFieldSeparator Error = "invalid field separator"
// ErrInvalidCSVFieldSeparator is the invalid csv field separator error.
ErrInvalidCSVFieldSeparator Error = "invalid csv field separator"
// ErrInvalidColumnParams is the invalid column params error.
ErrInvalidColumnParams Error = "invalid column params"
// ErrCrosstabResultMustHaveAtLeast3Columns is the crosstab result must
// have at least 3 columns error.
ErrCrosstabResultMustHaveAtLeast3Columns Error = "crosstab result must have at least 3 columns"
// ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumnsA
// is the data column must be specified when query returns more than three
// columns error.
ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumns Error = "data column must be specified when query returns more than three columns"
// ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame is the crosstab
// vertical and horizontal columns must not be same error.
ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame Error = "crosstab vertical and horizontal columns must not be same"
// ErrCrosstabVerticalColumnNotInResult is the crosstab vertical column not
// in result error.
ErrCrosstabVerticalColumnNotInResult Error = "crosstab vertical column not in result"
// ErrCrosstabHorizontalColumnNotInResult is the crosstab horizontal column
// not in result error.
ErrCrosstabHorizontalColumnNotInResult Error = "crosstab horizontal column not in result"
// ErrCrosstabDataColumnNotInResult is the crosstab data column not in
// result error.
ErrCrosstabDataColumnNotInResult Error = "crosstab data column not in result"
// ErrCrosstabHorizontalSortColumnNotInResult is the crosstab horizontal
// sort column not in result error.
ErrCrosstabHorizontalSortColumnNotInResult Error = "crosstab horizontal sort column not in result"
// ErrCrosstabDuplicateVerticalAndHorizontalValue is the crosstab duplicate
// vertical and horizontal value error.
ErrCrosstabDuplicateVerticalAndHorizontalValue Error = "crosstab duplicate vertical and horizontal value"
// ErrCrosstabHorizontalSortColumnIsNotANumber is the crosstab horizontal
// sort column is not a number error.
ErrCrosstabHorizontalSortColumnIsNotANumber Error = "crosstab horizontal sort column is not a number"
)
// newline is the default newline used by the system.
var newline []byte
func init() {
if runtime.GOOS == "windows" {
newline = []byte("\r\n")
} else {
newline = []byte("\n")
}
}
|