-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_export_doc_html.go
63 lines (56 loc) · 2.47 KB
/
mysql_export_doc_html.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
package mysql
import (
"strings"
"github.com/admpub/nging/v5/application/library/common"
"github.com/webx-top/echo"
)
func newHTMLDocExportor(dbName string) DocExportor {
return &mysqlExportHTMLDoc{
dbName: dbName,
}
}
type mysqlExportHTMLDoc struct {
dbName string
}
func (a *mysqlExportHTMLDoc) Open(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEEventStream)
encodedName := echo.URLEncode(a.dbName+`_doc.html`, true)
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename="+encodedName+"; filename*=utf-8''"+encodedName)
c.Response().Write([]byte(`<doctype html><html><head><title>Table Documention</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="` + common.BackendURL(c) + `/public/assets/backend/js/bootstrap/dist/css/bootstrap.min.css?t=20220807115920" /></head><body><div class="container"><div class="row"><div class="col-md-12">`))
return nil
}
func (a *mysqlExportHTMLDoc) Write(c echo.Context, table *TableStatus, fields []*Field) error {
c.Response().Write([]byte(`<h2 id="` + strings.ReplaceAll(table.Name.String, `"`, ``) + `">` + table.Name.String + `</h2>`))
c.Response().Write([]byte(`<em>` + table.Comment.String + `</em>`))
c.Response().Write([]byte(`<table class="table table-bordered table-hover table-condensed">`))
c.Response().Write([]byte(`<thead><tr><th>` + c.T(`字段名`) + `</th><th>` + c.T(`数据类型`) + `</th><th>` + c.T(`默认值`) + `</th><th>` + c.T(`必要字段`) + `</th><th>` + c.T(`说明`) + `</th></tr></thead>`))
c.Response().Write([]byte(`<tbody>`))
for _, v := range fields {
dataType := v.Full_type
if v.Null {
dataType += ` <span>NULL</span>`
}
if v.AutoIncrement.Valid {
dataType += ` <em>` + c.T("自动增量") + `</em>`
}
if len(v.On_update) > 0 {
dataType += ` ON UPDATE <b>` + v.On_update + `</b>`
}
var defaultValue string
if v.Default.Valid {
defaultValue = ` [<b>` + v.Default.String + `</b>]`
}
required := `<b>` + c.T(`是`) + `</b>`
if !v.IsRequired() {
required = c.T(`否`)
}
c.Response().Write([]byte(`<tr><td>` + v.Field + `</td><td>` + dataType + `</td><td>` + defaultValue + `</td><td>` + required + `</td><td>` + v.Comment + `</td></tr>`))
}
c.Response().Write([]byte(`</tbody>`))
c.Response().Write([]byte(`</table>`))
return nil
}
func (a *mysqlExportHTMLDoc) Close(c echo.Context) error {
c.Response().Write([]byte(`</div></div></div></body></html>`))
return nil
}