-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump.go
158 lines (143 loc) · 2.92 KB
/
dump.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
package sqlbless
import (
"context"
"database/sql"
"encoding/csv"
"fmt"
"io"
"strings"
"time"
"unicode/utf8"
)
type RowToCsv struct {
Comma rune
UseCRLF bool
Null string
PrintType bool
TimeLayout string
}
type _RowsI interface {
Close() error
ColumnTypes() ([]*sql.ColumnType, error)
Columns() ([]string, error)
Err() error
Next() bool
Scan(dest ...any) error
}
type _UnreadRows struct {
*sql.Rows
unread bool
}
func rowsHasNext(r *sql.Rows) *_UnreadRows {
if !r.Next() {
return nil
}
return &_UnreadRows{
Rows: r,
unread: true,
}
}
func (r *_UnreadRows) Next() bool {
if r.unread {
r.unread = false
return true
}
return r.Rows.Next()
}
func (cfg RowToCsv) Dump(ctx context.Context, rows _RowsI, w io.Writer) error {
csvw := csv.NewWriter(w)
defer csvw.Flush()
csvw.Comma = cfg.Comma
csvw.UseCRLF = cfg.UseCRLF
return rowsToCsv(ctx, rows, cfg.Null, cfg.TimeLayout, cfg.PrintType, csvw)
}
func rowsToCsv(ctx context.Context, rows _RowsI, null, timeLayout string, printType bool, csvw *csv.Writer) error {
columns, err := rows.Columns()
if err != nil {
return fmt.Errorf("(sql.Rows) Columns: %w", err)
}
if err := csvw.Write(columns); err != nil {
return err
}
itemAny := make([]any, len(columns))
itemStr := make([]string, len(columns))
for i := range itemAny {
itemAny[i] = new(any)
}
if printType {
ct, err := rows.ColumnTypes()
if err != nil {
return err
}
for i, c := range ct {
if c != nil {
var buffer strings.Builder
buffer.WriteString(c.DatabaseTypeName())
if st := c.ScanType(); st != nil {
buffer.WriteByte('(')
buffer.WriteString(st.String())
buffer.WriteByte(')')
}
itemStr[i] = buffer.String()
} else {
itemStr[i] = ""
}
}
csvw.Write(itemStr)
}
for rows.Next() {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := rows.Scan(itemAny...); err != nil {
return fmt.Errorf("(sql.Rows) Scan: %w", err)
}
if printType {
for i, a := range itemAny {
if p, ok := a.(*any); ok {
itemStr[i] = fmt.Sprintf("%T", *p)
} else {
itemStr[i] = ""
}
}
csvw.Write(itemStr)
printType = false
}
for i, a := range itemAny {
if p, ok := a.(*any); ok {
if *p == nil {
itemStr[i] = null
continue
}
if tm, ok := (*p).(sql.NullTime); ok {
if tm.Valid {
itemStr[i] = tm.Time.Format(timeLayout)
} else {
itemStr[i] = null
}
continue
}
if tm, ok := (*p).(time.Time); ok {
itemStr[i] = tm.Format(timeLayout)
continue
}
if b, ok := (*p).([]byte); ok && utf8.Valid(b) {
itemStr[i] = string(b)
continue
}
itemStr[i] = fmt.Sprint(*p)
} else {
itemStr[i] = ""
}
}
if err := csvw.Write(itemStr); err != nil {
return fmt.Errorf("(csv.Writer).Write: %w", err)
}
}
if err := rows.Err(); err != nil {
return fmt.Errorf("(sql.Rows) Err: %w", err)
}
return nil
}