-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_import_db.go
61 lines (58 loc) · 1.63 KB
/
mysql_import_db.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
package mysql
import (
"context"
"path/filepath"
"strings"
"github.com/admpub/errors"
"github.com/admpub/nging/v5/application/library/notice"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver/mysql/utils"
"github.com/webx-top/com"
)
func (m *mySQL) importDB(c context.Context, noticer *notice.NoticeAndProgress,
cfg *driver.DbAuth, cacheDir string, files []string) (err error) {
if !com.InSlice(cfg.Charset, Charsets) {
return errors.New(`字符集charset值无效`)
}
names := make([]string, len(files))
for i, file := range files {
names[i] = filepath.Base(file)
}
noticer.Success(`开始导入: ` + strings.Join(names, ", "))
var ifi *utils.ImportFile
ifi, err = utils.ParseImportFile(cacheDir, files)
if err != nil {
return
}
for _, sqlStr := range []string{
`SET NAMES ` + cfg.Charset + `;`,
`SET FOREIGN_KEY_CHECKS=0;`,
`SET UNIQUE_CHECKS=0;`,
} {
_, err := m.exec(sqlStr)
if err != nil {
noticer.Failure(`[FAILURE] ` + err.Error() + `: ` + sqlStr)
} else {
noticer.Success(`[SUCCESS] ` + sqlStr)
}
}
noticer.Add(int64(ifi.AllSqlFileNum()))
if len(ifi.StructFiles) > 0 {
err = m.importDBStruct(c, noticer, cfg, ifi.StructFiles)
}
if err == nil && len(ifi.DataFiles) > 0 {
err = m.importDBData(c, noticer, cfg, ifi.StructFiles)
}
for _, sqlStr := range []string{
`SET FOREIGN_KEY_CHECKS=1;`,
`SET UNIQUE_CHECKS=1;`,
} {
_, err := m.exec(sqlStr)
if err != nil {
noticer.Failure(`[FAILURE] ` + err.Error() + `: ` + sqlStr)
} else {
noticer.Success(`[SUCCESS] ` + sqlStr)
}
}
return
}