forked from JamesStewy/go-mysqldump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.go
139 lines (116 loc) · 2.71 KB
/
dump.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
package sqldump
import (
"database/sql"
"errors"
"os"
"strings"
)
type table struct {
Name string
Sequences string
SQL string
Values string
}
type dump struct {
file *os.File
DumpVersion string
ServerVersion string
Tables []*table
Table *table
CompleteTime string
}
// Dump a MySQL/MariaDB or PostgreSQL database or selection of tables from same based on the options supplied through the dumper.
func (d *Dumper) Dump(filters ...string) error {
// Check dump directory
if e, _ := exists(d.path); e {
return errors.New("Dump '" + d.path + "' already exists.")
}
// Create dump file
f, err := os.Create(d.path)
if err != nil {
return err
}
defer f.Close()
data := dump{
file: f,
DumpVersion: version,
Tables: make([]*table, 0),
}
list := filters
// Get server version, thereby identifying type.
if data.ServerVersion, err = d.getServerVersion(); err != nil {
return err
}
if strings.Contains(data.ServerVersion, "PostgreSQL") {
d.pg = true
if len(list) == 0 {
list, err = d.getPostgresTables()
if err != nil {
return err
}
}
return d.DumpPostgres(data, list...)
}
if len(list) == 0 {
list, err = d.getMySQLTables()
if err != nil {
return err
}
}
return d.DumpMySQL(data, filters...)
}
func (d *Dumper) getServerVersion() (string, error) {
var serverversion sql.NullString
if err := d.db.QueryRow("SELECT version()").Scan(&serverversion); err != nil {
return "", err
}
return serverversion.String, nil
}
func (d *Dumper) createTableValues(name string, offset, max int64) (string, error) {
// Get Data
if max == 0 {
max = 1000
}
var rows *sql.Rows
var err error
if d.pg {
rows, err = d.db.Query("SELECT * FROM "+name+" LIMIT $1 OFFSET $2;", max, offset)
} else {
rows, err = d.db.Query("SELECT * FROM "+name+" LIMIT ? OFFSET ?;", max, offset)
}
if err != nil {
return "", err
}
defer rows.Close()
// Get columns
columns, err := rows.Columns()
if err != nil {
return "", err
}
if len(columns) == 0 {
return "", errors.New("No columns in table " + name + ".")
}
// Read data
datatext := make([]string, 0)
for rows.Next() {
data := make([]*sql.NullString, len(columns))
ptrs := make([]interface{}, len(columns))
for i, _ := range data {
ptrs[i] = &data[i]
}
// Read data
if err := rows.Scan(ptrs...); err != nil {
return "", err
}
dataStrings := make([]string, len(columns))
for key, value := range data {
if value != nil && value.Valid {
dataStrings[key] = "'" + value.String + "'"
} else {
dataStrings[key] = "null"
}
}
datatext = append(datatext, "("+strings.Join(dataStrings, ",")+")")
}
return strings.Join(datatext, ","), rows.Err()
}