Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvanstack committed Oct 16, 2023
2 parents 2cb17ff + e29cec8 commit 36db92f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 100 deletions.
16 changes: 8 additions & 8 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

[English](./README.md)

golang 实现的零依赖、高性能、并发 mysqldump 工具。
golang 实现的零依赖、支持所有类型、高性能、并发 mysqldump 工具。


## Features
Expand Down Expand Up @@ -80,19 +80,19 @@ import (

func main() {

dns := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
dsn := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"

f, _ := os.Create("dump.sql")

_ = mysqldump.Dump(
dns, // DNS
dsn, // DSN
mysqldump.WithDropTable(), // Option: Delete table before create (Default: Not delete table)
mysqldump.WithData(), // Option: Dump Data (Default: Only dump table schema)
mysqldump.WithTables("test"), // Option: Dump Tables (Default: All tables)
mysqldump.WithWriter(f), // Option: Writer (Default: os.Stdout)
mysqldump.WithDBs("dc3"), // Option: Dump Dbs (Default: db in dns)
)
}

```

### Output File dump.sql
Expand Down Expand Up @@ -172,14 +172,14 @@ import (

func main() {

dns := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
dsn := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
f, _ := os.Open("dump.sql")

_ = mysqldump.Source(
dns,
dsn,
f,
mysqldump.WithMergeInsert(1000),// Option: Merge insert 1000 (Default: Not merge insert)
mysqldump.WithDebug(), // Option: Print execute sql (Default: Not print execute sql)
mysqldump.WithMergeInsert(1000), // Option: Merge insert 1000 (Default: Not merge insert)
mysqldump.WithDebug(), // Option: Print execute sql (Default: Not print execute sql)
)
}
```
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

[简体中文](README-zh.md)

A zero-dependency, high-performance, concurrent mysqldump tool implemented in golang.
A zero-dependency,all data types are supported, high-performance, concurrent mysqldump tool implemented in golang.

## Features

Expand Down Expand Up @@ -80,17 +80,16 @@ import (

func main() {

dns := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
dsn := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"

f, _ := os.Create("dump.sql")

_ = mysqldump.Dump(
dns, // DNS
dsn, // DSN
mysqldump.WithDropTable(), // Option: Delete table before create (Default: Not delete table)
mysqldump.WithData(), // Option: Dump Data (Default: Only dump table schema)
mysqldump.WithTables("test"), // Option: Dump Tables (Default: All tables)
mysqldump.WithWriter(f), // Option: Writer (Default: os.Stdout)
mysqldump.WithDBs("dc3"), // Option: Dump Dbs (Default: db in dns)
)
}
```
Expand Down Expand Up @@ -172,14 +171,14 @@ import (

func main() {

dns := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
dsn := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
f, _ := os.Open("dump.sql")

_ = mysqldump.Source(
dns,
dsn,
f,
mysqldump.WithMergeInsert(1000),// Option: Merge insert 1000 (Default: Not merge insert)
mysqldump.WithDebug(), // Option: Print execute sql (Default: Not print execute sql)
mysqldump.WithMergeInsert(1000), // Option: Merge insert 1000 (Default: Not merge insert)
mysqldump.WithDebug(), // Option: Print execute sql (Default: Not print execute sql)
)
}
```
Expand Down
5 changes: 2 additions & 3 deletions example/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (

func main() {

dns := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
dsn := "root:rootpasswd@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"

f, _ := os.Create("dump.sql")

_ = mysqldump.Dump(
dns, // DNS
dsn, // DSN
mysqldump.WithDropTable(), // Option: Delete table before create (Default: Not delete table)
mysqldump.WithData(), // Option: Dump Data (Default: Only dump table schema)
mysqldump.WithTables("test"), // Option: Dump Tables (Default: All tables)
mysqldump.WithWriter(f), // Option: Writer (Default: os.Stdout)
mysqldump.WithDBs("dc3"), // Option: Dump Dbs (Default: db in dns)
)
}
4 changes: 2 additions & 2 deletions example/source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
_ = mysqldump.Source(
dns,
f,
mysqldump.WithMergeInsert(1000),
mysqldump.WithDebug(),
mysqldump.WithMergeInsert(1000), // Option: Merge insert 1000 (Default: Not merge insert)
mysqldump.WithDebug(), // Option: Print execute sql (Default: Not print execute sql)
)
}
106 changes: 33 additions & 73 deletions mysqldump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func init() {
type dumpOption struct {
// 导出表数据
isData bool
// 导出指定数据库, 与 WithAllDatabases 互斥, WithAllDatabases 优先级高
dbs []string
// 导出全部数据库
isAllDB bool

// 导出指定表, 与 isAllTables 互斥, isAllTables 优先级高
tables []string
// 导出全部表
Expand Down Expand Up @@ -52,20 +49,6 @@ func WithData() DumpOption {
}
}

// 导出全部数据库
func WithAllDatabases() DumpOption {
return func(option *dumpOption) {
option.isAllDB = true
}
}

// 导出指定数据库, 与 WithAllDatabases 互斥, WithAllDatabases 优先级高
func WithDBs(databases ...string) DumpOption {
return func(option *dumpOption) {
option.dbs = databases
}
}

// 导出指定表, 与 WithAllTables 互斥, WithAllTables 优先级高
func WithTables(tables ...string) DumpOption {
return func(option *dumpOption) {
Expand All @@ -87,7 +70,7 @@ func WithWriter(writer io.Writer) DumpOption {
}
}

func Dump(dns string, opts ...DumpOption) error {
func Dump(dsn string, opts ...DumpOption) error {

Check failure on line 73 in mysqldump.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 14 of func `Dump` is high (> 10) (gocyclo)
// 打印开始
start := time.Now()
log.Printf("[info] [dump] start at %s\n", start.Format("2006-01-02 15:04:05"))
Expand All @@ -105,18 +88,6 @@ func Dump(dns string, opts ...DumpOption) error {
opt(&o)
}

if len(o.dbs) == 0 {
// 默认包含dns中的数据库
dbName, err := GetDBNameFromDNS(dns)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
o.dbs = []string{
dbName,
}
}

if len(o.tables) == 0 {
// 默认包含全部表
o.isAllTable = true
Expand All @@ -138,77 +109,66 @@ func Dump(dns string, opts ...DumpOption) error {
buf.WriteString("\n\n")

// 连接数据库
db, err := sql.Open("mysql", dns)
db, err := sql.Open("mysql", dsn)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
defer db.Close()

// 1. 获取数据库
var dbs []string
if o.isAllDB {
dbs, err = getDBs(db)
dbName, err := GetDBNameFromDSN(dsn)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
_, err = db.Exec(fmt.Sprintf("USE `%s`", dbName))
if err != nil {
log.Printf("[error] %v \n", err)
return err
}

// 2. 获取表
var tables []string
if o.isAllTable {
tmp, err := getAllTables(db)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
tables = tmp
} else {
dbs = o.dbs
tables = o.tables
}

// 2. 获取表
for _, dbStr := range dbs {
_, err = db.Exec(fmt.Sprintf("USE `%s`", dbStr))
// 3. 导出表
for _, table := range tables {
// 删除表
if o.isDropTable {
buf.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS `%s`;\n", table))
}

// 导出表结构
err = writeTableStruct(db, table, buf)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}

var tables []string
if o.isAllTable {
tmp, err := getAllTables(db)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
tables = tmp
} else {
tables = o.tables
}

buf.WriteString(fmt.Sprintf("USE `%s`;\n", dbStr))

// 3. 导出表
for _, table := range tables {
// 删除表
if o.isDropTable {
buf.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS `%s`;\n", table))
}

// 导出表结构
err = writeTableStruct(db, table, buf)
// 导出表数据
if o.isData {
err = writeTableData(db, table, buf)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}

// 导出表数据
if o.isData {
err = writeTableData(db, table, buf)
if err != nil {
log.Printf("[error] %v \n", err)
return err
}
}
}

}

// 导出每个表的结构和数据

buf.WriteString("-- ----------------------------\n")
buf.WriteString("-- Dumped by mysqldump2\n")
buf.WriteString("-- Dumped by mysqldump\n")
buf.WriteString("-- Cost Time: " + time.Since(start).String() + "\n")
buf.WriteString("-- ----------------------------\n")
buf.Flush()
Expand Down
6 changes: 3 additions & 3 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (db *dbWrapper) Exec(query string, args ...interface{}) (sql.Result, error)
}

// Source 加载
func Source(dns string, reader io.Reader, opts ...SourceOption) error {
func Source(dsn string, reader io.Reader, opts ...SourceOption) error {

Check failure on line 67 in source.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 21 of func `Source` is high (> 10) (gocyclo)
// 打印开始
start := time.Now()
log.Printf("[info] [source] start at %s\n", start.Format("2006-01-02 15:04:05"))
Expand All @@ -81,14 +81,14 @@ func Source(dns string, reader io.Reader, opts ...SourceOption) error {
opt(&o)
}

dbName, err := GetDBNameFromDNS(dns)
dbName, err := GetDBNameFromDSN(dsn)
if err != nil {
log.Printf("[error] %v\n", err)
return err
}

// Open database
db, err = sql.Open("mysql", dns)
db, err = sql.Open("mysql", dsn)
if err != nil {
log.Printf("[error] %v\n", err)
return err
Expand Down
9 changes: 6 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"strings"
)

func GetDBNameFromDNS(dns string) (string, error) {
ss1 := strings.Split(dns, "/")
//从dsn中提取出数据库名称,并将其作为结果返回。
//如果无法解析出数据库名称,将返回一个错误。

func GetDBNameFromDSN(dsn string) (string, error) {
ss1 := strings.Split(dsn, "/")
if len(ss1) == 2 {
ss2 := strings.Split(ss1[1], "?")
if len(ss2) == 2 {
return ss2[0], nil
}
}

return "", fmt.Errorf("dns error: %s", dns)
return "", fmt.Errorf("dsn error: %s", dsn)
}

0 comments on commit 36db92f

Please sign in to comment.