-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_tbln.go
86 lines (76 loc) · 1.92 KB
/
output_tbln.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
package trdsql
import (
"strings"
"github.com/noborus/tbln"
)
// TBLNWriter provides methods of the Writer interface.
type TBLNWriter struct {
writer *tbln.Writer
outNULL string
results []string
needNULL bool
}
// NewTBLNWriter returns TBLNWriter.
func NewTBLNWriter(writeOpts *WriteOpts) *TBLNWriter {
w := &TBLNWriter{}
w.writer = tbln.NewWriter(writeOpts.OutStream)
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
return w
}
// PreWrite is prepare tbln definition body.
func (w *TBLNWriter) PreWrite(columns []string, types []string) error {
d := tbln.NewDefinition()
if err := d.SetNames(columns); err != nil {
return err
}
if err := d.SetTypes(ConvertTypes(types)); err != nil {
return err
}
if err := w.writer.WriteDefinition(d); err != nil {
return err
}
w.results = make([]string, len(columns))
return nil
}
// WriteRow is row write.
func (w *TBLNWriter) WriteRow(values []any, columns []string) error {
for i, col := range values {
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
}
w.results[i] = strings.ReplaceAll(str, "\n", "\\n")
}
return w.writer.WriteRow(w.results)
}
// PostWrite is nil.
func (w *TBLNWriter) PostWrite() error {
return nil
}
// ConvertTypes is converts database types to common types.
func ConvertTypes(dbTypes []string) []string {
ret := make([]string, len(dbTypes))
for i, t := range dbTypes {
ret[i] = convertType(t)
}
return ret
}
func convertType(dbType string) string {
switch strings.ToLower(dbType) {
case "smallint", "integer", "int", "int2", "int4", "smallserial", "serial":
return "int"
case "bigint", "int8", "bigserial":
return "bigint"
case "float", "decimal", "numeric", "real", "double precision":
return "numeric"
case "bool":
return "bool"
case "timestamp", "timestamptz", "date", "time":
return "timestamp"
case "string", "text", "char", "varchar":
return "text"
default:
return "text"
}
}