-
Notifications
You must be signed in to change notification settings - Fork 51
/
source.go
241 lines (208 loc) · 4.58 KB
/
source.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package mysqldump
import (
"bufio"
"database/sql"
"errors"
"fmt"
"io"
"log"
"strings"
"time"
)
type sourceOption struct {
dryRun bool
mergeInsert int
debug bool
}
type SourceOption func(*sourceOption)
func WithDryRun() SourceOption {
return func(o *sourceOption) {
o.dryRun = true
}
}
func WithMergeInsert(size int) SourceOption {
return func(o *sourceOption) {
o.mergeInsert = size
}
}
// WithDebug 打印执行的 SQL
func WithDebug() SourceOption {
return func(o *sourceOption) {
o.debug = true
}
}
type dbWrapper struct {
DB *sql.DB
debug bool
dryRun bool
}
func newDBWrapper(db *sql.DB, dryRun, debug bool) *dbWrapper {
return &dbWrapper{
DB: db,
dryRun: dryRun,
debug: debug,
}
}
func (db *dbWrapper) Exec(query string, args ...interface{}) (sql.Result, error) {
if db.debug {
log.Printf("[debug] [query]\n%s\n", query)
}
if db.dryRun {
return nil, nil
}
return db.DB.Exec(query, args...)
}
// Source 加载
// 禁止 golangci-lint 检查
// nolint: gocyclo
func Source(dsn string, reader io.Reader, opts ...SourceOption) error {
// 打印开始
start := time.Now()
log.Printf("[info] [source] start at %s\n", start.Format("2006-01-02 15:04:05"))
// 打印结束
defer func() {
end := time.Now()
log.Printf("[info] [source] end at %s, cost %s\n", end.Format("2006-01-02 15:04:05"), end.Sub(start))
}()
var err error
var db *sql.DB
var o sourceOption
for _, opt := range opts {
opt(&o)
}
dbName, err := GetDBNameFromDSN(dsn)
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
// Open database
db, err = sql.Open("mysql", dsn)
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
defer db.Close()
// DB Wrapper
dbWrapper := newDBWrapper(db, o.dryRun, o.debug)
// Use database
_, err = dbWrapper.Exec(fmt.Sprintf("USE %s;", dbName))
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
// 设置超时时间1小时
db.SetConnMaxLifetime(3600)
// 一句一句执行
r := bufio.NewReader(reader)
// 关闭事务
_, err = dbWrapper.Exec("SET autocommit=0;")
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
for {
line, err := r.ReadString(';')
if err != nil {
if err == io.EOF {
break
}
log.Printf("[error] %v\n", err)
return err
}
ssql := string(line)
// 删除末尾的换行符
ssql = trim(ssql)
if err != nil {
log.Printf("[error] [trim] %v\n", err)
return err
}
// 如果 INSERT 开始, 并且 mergeInsert 为 true, 则合并 INSERT
if o.mergeInsert > 1 && strings.HasPrefix(ssql, "INSERT INTO") {
var insertSQLs []string
insertSQLs = append(insertSQLs, ssql)
for i := 0; i < o.mergeInsert-1; i++ {
line, err := r.ReadString(';')
if err != nil {
if err == io.EOF {
break
}
log.Printf("[error] %v\n", err)
return err
}
ssql2 := string(line)
ssql2 = trim(ssql2)
if err != nil {
log.Printf("[error] [trim] %v\n", err)
return err
}
if strings.HasPrefix(ssql2, "INSERT INTO") {
insertSQLs = append(insertSQLs, ssql2)
continue
}
break
}
// 合并 INSERT
ssql, err = mergeInsert(insertSQLs)
if err != nil {
log.Printf("[error] [mergeInsert] %v\n", err)
return err
}
}
_, err = dbWrapper.Exec(ssql)
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
}
// 提交事务
_, err = dbWrapper.Exec("COMMIT;")
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
// 开启事务
_, err = dbWrapper.Exec("SET autocommit=1;")
if err != nil {
log.Printf("[error] %v\n", err)
return err
}
return nil
}
/*
将多个 INSERT 合并为一个
输入:
INSERT INTO `test` VALUES (1, 'a');
INSERT INTO `test` VALUES (2, 'b');
输出
INSERT INTO `test` VALUES (1, 'a'), (2, 'b');
*/
func mergeInsert(insertSQLs []string) (string, error) {
if len(insertSQLs) == 0 {
return "", errors.New("no input provided")
}
builder := strings.Builder{}
sql1 := insertSQLs[0]
sql1 = strings.TrimSuffix(sql1, ";")
builder.WriteString(sql1)
for i, insertSQL := range insertSQLs[1:] {
if i < len(insertSQLs)-1 {
builder.WriteString(",")
}
valuesIdx := strings.Index(insertSQL, "VALUES")
if valuesIdx == -1 {
return "", errors.New("invalid SQL: missing VALUES keyword")
}
sqln := insertSQL[valuesIdx:]
sqln = strings.TrimPrefix(sqln, "VALUES")
sqln = strings.TrimSuffix(sqln, ";")
builder.WriteString(sqln)
}
builder.WriteString(";")
return builder.String(), nil
}
// 删除空白符换行符和注释
func trim(s string) string {
s = strings.TrimLeft(s, "\n")
s = strings.TrimSpace(s)
return s
}