forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
152 lines (140 loc) · 3.13 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
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
package internal
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"reflect"
"regexp"
"strings"
)
// Version version
const Version = "0.4"
// AppURL site
const AppURL = "https://github.com/admpub/mysql-schema-sync/"
const timeFormatStd string = "2006-01-02 15:04:05"
// loadJsonFile load json
func loadJSONFile(jsonPath string, val interface{}) error {
bs, err := ioutil.ReadFile(jsonPath)
if err != nil {
return err
}
return ParseJSON(string(bs), val)
}
// ParseJSON parse json
func ParseJSON(content string, val interface{}) error {
lines := strings.Split(content, "\n")
var bf bytes.Buffer
for _, line := range lines {
lineNew := strings.TrimSpace(line)
if (len(lineNew) > 0 && lineNew[0] == '#') || (len(lineNew) > 1 && lineNew[0:2] == "//") {
continue
}
bf.WriteString(lineNew)
}
return json.Unmarshal(bf.Bytes(), &val)
}
func inStringSlice(str string, strSli []string) bool {
for _, v := range strSli {
if str == v {
return true
}
}
return false
}
func simpleMatch(patternStr string, str string, msg ...string) bool {
str = strings.TrimSpace(str)
patternStr = strings.TrimSpace(patternStr)
if patternStr == str {
log.Println("simple_match:suc,equal", msg, "patternStr:", patternStr, "str:", str)
return true
}
pattern := "^" + strings.Replace(patternStr, "*", `.*`, -1) + "$"
match, err := regexp.MatchString(pattern, str)
if err != nil {
log.Println("simple_match:error", msg, "patternStr:", patternStr, "pattern:", pattern, "str:", str, "err:", err)
}
if match {
//log.Println("simple_match:suc", msg, "patternStr:", patternStr, "pattern:", pattern, "str:", str)
}
return match
}
func htmlPre(str string) string {
return "<pre>" + html.EscapeString(str) + "</pre>"
}
func dsnSort(dsn string) string {
i := strings.Index(dsn, "@")
if i < 1 {
return dsn
}
return dsn[i+1:]
}
func maxMapKeyLen(data interface{}, ext int) string {
l := 0
for _, k := range reflect.ValueOf(data).MapKeys() {
if k.Len() > l {
l = k.Len()
}
}
return fmt.Sprintf("%d", l+ext)
}
func Exec(mydb *sql.DB, query string) (sql.Result, error) {
var sqlStr string
var ret sql.Result
tx, err := mydb.Begin()
if err != nil {
return nil, err
}
execute := func(line string) (rErr error) {
if strings.HasPrefix(line, `--`) {
return nil
}
line = strings.TrimRight(line, "\r ")
if strings.HasPrefix(line, `/*`) && strings.HasSuffix(line, `*/;`) {
return nil
}
sqlStr += line
if strings.HasSuffix(line, `;`) {
defer func() {
sqlStr = ``
}()
if sqlStr == `;` {
return nil
}
ret, err = tx.Exec(sqlStr)
log.Println("exec_one:[", sqlStr, "]", err)
return err
}
sqlStr += "\n"
return nil
}
for _, line := range strings.Split(query, "\n") {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
err = execute(line)
if err != nil {
break
}
}
if err == nil {
sqlStr = strings.TrimSpace(sqlStr)
if len(sqlStr) > 0 {
if sqlStr != `;` {
ret, err = tx.Exec(sqlStr)
log.Println("exec_one:[", sqlStr, "]", err)
}
sqlStr = ``
}
}
if err == nil {
err = tx.Commit()
} else {
tx.Rollback()
}
return ret, err
}