-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_export_doc_markdown.go
62 lines (56 loc) · 1.94 KB
/
mysql_export_doc_markdown.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
package mysql
import (
"github.com/webx-top/echo"
)
func newMarkdownDocExportor(dbName string) DocExportor {
return &mysqlExportMarkdownDoc{
dbName: dbName,
}
}
type mysqlExportMarkdownDoc struct {
dbName string
}
func (a *mysqlExportMarkdownDoc) Open(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEEventStream)
encodedName := echo.URLEncode(a.dbName+`_doc.md`, true)
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename="+encodedName+"; filename*=utf-8''"+encodedName)
c.Response().Write([]byte(`# Table Documention` + "\n"))
return nil
}
func (a *mysqlExportMarkdownDoc) Write(c echo.Context, table *TableStatus, fields []*Field) error {
c.Response().Write([]byte(`## ` + table.Name.String + "\n\n"))
c.Response().Write([]byte(`> ` + table.Comment.String + "\n\n"))
c.Response().Write([]byte(`| ` + c.T(`字段名`) + ` | ` + c.T(`数据类型`) + ` | ` + c.T(`默认值`) + ` | ` + c.T(`必要字段`) + ` | ` + c.T(`说明`) + ` |` + "\n"))
c.Response().Write([]byte(`| :------------ | :------------ | :------------ | :------------ | :------------ |` + "\n"))
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 {
if len(v.Default.String) > 0 {
defaultValue = `[**` + v.Default.String + `**]`
} else {
defaultValue = `[]`
}
}
required := `**` + c.T(`是`) + `**`
if !v.IsRequired() {
required = c.T(`否`)
}
c.Response().Write([]byte(`| ` + v.Field + ` | ` + dataType + ` | ` + defaultValue + ` | ` + required + ` | ` + v.Comment + ` |` + "\n"))
}
c.Response().Write([]byte("\n"))
return nil
}
func (a *mysqlExportMarkdownDoc) Close(c echo.Context) error {
c.Response().Write([]byte("\n\n"))
return nil
}