-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_export_doc_csv.go
77 lines (69 loc) · 1.8 KB
/
mysql_export_doc_csv.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
package mysql
import (
"encoding/csv"
"github.com/webx-top/echo"
)
func newCSVDocExportor(dbName string) DocExportor {
return &mysqlExportCSVDoc{
dbName: dbName,
}
}
type mysqlExportCSVDoc struct {
dbName string
writer *csv.Writer
}
func (a *mysqlExportCSVDoc) Open(c echo.Context) error {
a.writer = csv.NewWriter(c.Response())
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEEventStream)
encodedName := echo.URLEncode(a.dbName+`_doc.csv`, true)
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename="+encodedName+"; filename*=utf-8''"+encodedName)
c.Response().Write([]byte("\xEF\xBB\xBF")) // BOM
return a.writer.Write([]string{`Table Documention`})
}
func (a *mysqlExportCSVDoc) Write(c echo.Context, table *TableStatus, fields []*Field) error {
title := table.Name.String
if len(table.Comment.String) > 0 {
title += ` (` + table.Comment.String + `)`
}
err := a.writer.Write([]string{title})
if err != nil {
return err
}
err = a.writer.Write([]string{
c.T(`字段名`), c.T(`数据类型`), c.T(`默认值`), c.T(`必要字段`), c.T(`说明`),
})
if err != nil {
return err
}
for _, v := range fields {
dataType := v.Full_type
if v.Null {
dataType += ` NULL`
}
if v.AutoIncrement.Valid {
dataType += ` ` + c.T("自动增量")
}
if len(v.On_update) > 0 {
dataType += ` ON UPDATE ` + v.On_update
}
var defaultValue string
if v.Default.Valid {
defaultValue = ` [` + v.Default.String + `]`
}
required := c.T(`是`)
if !v.IsRequired() {
required = c.T(`否`)
}
err = a.writer.Write([]string{
v.Field, dataType, defaultValue, required, v.Comment,
})
if err != nil {
return err
}
}
return a.writer.Write([]string{``})
}
func (a *mysqlExportCSVDoc) Close(c echo.Context) error {
a.writer.Flush()
return nil
}