forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
62 lines (57 loc) · 1.68 KB
/
util.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 sync
import (
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
var (
sqlCreateTableRegexp = regexp.MustCompile(`(?i)^CREATE TABLE `)
)
// ReplaceCharset 替换DDL语句中的字符集
func ReplaceCharset(sqlStr string, srcCharset string, destCharset string, checkCreateDDL ...bool) string {
if destCharset == srcCharset {
return sqlStr
}
if len(checkCreateDDL) > 0 && checkCreateDDL[0] {
if !sqlCreateTableRegexp.MatchString(sqlStr) {
return sqlStr
}
}
srcCharsetQuoted := regexp.QuoteMeta(srcCharset)
sqlCharsetRegexp := regexp.MustCompile(`(?i) (CHARACTER SET |CHARSET=)` + srcCharsetQuoted + ` `)
sqlCollateRegexp := regexp.MustCompile(`(?i) (COLLATE[= ])` + srcCharsetQuoted + `([\w]+)`)
sqlStr = sqlCharsetRegexp.ReplaceAllString(sqlStr, ` ${1}`+destCharset+` `)
sqlStr = sqlCollateRegexp.ReplaceAllString(sqlStr, ` ${1}`+destCharset+`${2}`)
return sqlStr
}
func GetSQLFileContent(filePattern string) (string, error) {
var content string
sourceSQLFiles, err := filepath.Glob(filePattern)
if err != nil {
return content, err
}
for _, sourceSQLFile := range sourceSQLFiles {
if strings.HasSuffix(sourceSQLFile, `.sql`) {
b, err := ioutil.ReadFile(sourceSQLFile)
if err != nil {
return content, err
}
content += "\n" + string(b)
}
}
return content, nil
}
var HTMLDocTemplate = `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://b.webx.top/public/assets/backend/js/bootstrap/dist/css/bootstrap.min.css?t=20211101213650" />
</head>
<body>
%s
</body>
</html>`