Skip to content

Commit fdc6a20

Browse files
committed
Merge branch 'master' of github.com:jamf/go-mysqldump
2 parents ac1de70 + c87ced3 commit fdc6a20

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

dump.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Data struct {
2929
footerTmpl *template.Template
3030
mux sync.Mutex
3131
wg sync.WaitGroup
32+
err error
3233
}
3334

3435
type table struct {
@@ -43,7 +44,7 @@ type metaData struct {
4344
CompleteTime string
4445
}
4546

46-
const version = "0.3.3"
47+
const version = "0.3.4"
4748

4849
const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
4950
--
@@ -105,7 +106,6 @@ func (data *Data) Dump() error {
105106
DumpVersion: version,
106107
}
107108

108-
// Get server version
109109
if err := meta.updateServerVersion(data.Connection); err != nil {
110110
return err
111111
}
@@ -118,22 +118,22 @@ func (data *Data) Dump() error {
118118
return err
119119
}
120120

121-
// Get tables
122121
tables, err := data.getTables()
123122
if err != nil {
124123
return err
125124
}
126125

127-
// Get sql for each table
128126
data.wg.Add(len(tables))
129127
for _, name := range tables {
130128
if err := data.dumpTable(name); err != nil {
131129
return err
132130
}
133131
}
134132
data.wg.Wait()
133+
if data.err != nil {
134+
return data.err
135+
}
135136

136-
// Set complete time
137137
meta.CompleteTime = time.Now().String()
138138
return data.footerTmpl.Execute(data.Out, meta)
139139
}
@@ -152,18 +152,20 @@ func (data *Data) dumpTable(name string) error {
152152
return nil
153153
}
154154

155-
func (data *Data) writeTable(table *table) error {
155+
func (data *Data) writeTable(table *table) {
156156
data.mux.Lock()
157-
err := data.tableTmpl.Execute(data.Out, table)
157+
if err := data.tableTmpl.Execute(data.Out, table); err != nil && data.err == nil {
158+
data.err = err
159+
}
158160
data.mux.Unlock()
159161
data.wg.Done()
160-
return err
162+
return
161163
}
162164

163165
// MARK: get methods
164166

167+
// getTemplates initilaizes the templates on data from the constants in this file
165168
func (data *Data) getTemplates() (err error) {
166-
// Write dump to file
167169
data.headerTmpl, err = template.New("mysqldumpHeader").Parse(headerTmpl)
168170
if err != nil {
169171
return
@@ -184,14 +186,12 @@ func (data *Data) getTemplates() (err error) {
184186
func (data *Data) getTables() ([]string, error) {
185187
tables := make([]string, 0)
186188

187-
// Get table list
188189
rows, err := data.Connection.Query("SHOW TABLES")
189190
if err != nil {
190191
return tables, err
191192
}
192193
defer rows.Close()
193194

194-
// Read result
195195
for rows.Next() {
196196
var table sql.NullString
197197
if err := rows.Scan(&table); err != nil {
@@ -238,7 +238,6 @@ func (data *Data) createTable(name string) (*table, error) {
238238
}
239239

240240
func (data *Data) createTableSQL(name string) (string, error) {
241-
// Get table creation SQL
242241
var tableReturn, tableSQL sql.NullString
243242
err := data.Connection.QueryRow("SHOW CREATE TABLE "+name).Scan(&tableReturn, &tableSQL)
244243

@@ -253,14 +252,12 @@ func (data *Data) createTableSQL(name string) (string, error) {
253252
}
254253

255254
func (data *Data) createTableValues(name string) (string, error) {
256-
// Get Data
257255
rows, err := data.Connection.Query("SELECT * FROM " + name)
258256
if err != nil {
259257
return "", err
260258
}
261259
defer rows.Close()
262260

263-
// Get columns
264261
columns, err := rows.Columns()
265262
if err != nil {
266263
return "", err
@@ -269,7 +266,6 @@ func (data *Data) createTableValues(name string) (string, error) {
269266
return "", errors.New("No columns in table " + name + ".")
270267
}
271268

272-
// Read data
273269
dataText := make([]string, 0)
274270
tt, err := rows.ColumnTypes()
275271
if err != nil {
@@ -296,7 +292,6 @@ func (data *Data) createTableValues(name string) (string, error) {
296292
values[i] = reflect.New(types[i]).Interface()
297293
}
298294
for rows.Next() {
299-
// Read data
300295
if err := rows.Scan(values...); err != nil {
301296
return "", err
302297
}

mysqldump.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ func Dump(db *sql.DB, out io.Writer) error {
5353
}
5454

5555
// Close the dumper.
56-
// Will also close the database the dumper is connected to as well as the out stream if it is a *os.File.
56+
// Will also close the database the dumper is connected to as well as the out stream if it has a Close method.
5757
//
5858
// Not required.
5959
func (d *Data) Close() error {
6060
defer func() {
6161
d.Connection = nil
6262
d.Out = nil
6363
}()
64-
if file, ok := d.Out.(*os.File); ok {
65-
file.Close()
64+
if out, ok := d.Out.(io.Closer); ok {
65+
out.Close()
6666
}
6767
return d.Connection.Close()
6868
}

sanitize.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import "strings"
44

55
var lazyMySQLReplacer *strings.Replacer
66

7-
func mysqlReplacer() *strings.Replacer {
7+
// sanitize MySQL based on
8+
// https://dev.mysql.com/doc/refman/8.0/en/string-literals.html table 9.1
9+
// needs to be placed in either a single or a double quoted string
10+
func sanitize(input string) string {
811
if lazyMySQLReplacer == nil {
912
lazyMySQLReplacer = strings.NewReplacer(
1013
"\x00", "\\0",
@@ -13,19 +16,12 @@ func mysqlReplacer() *strings.Replacer {
1316
"\b", "\\b",
1417
"\n", "\\n",
1518
"\r", "\\r",
16-
// "\t", "\\t",
19+
// "\t", "\\t", Tab literals are acceptable in reads
1720
"\x1A", "\\Z", // ASCII 26 == x1A
1821
"\\", "\\\\",
1922
// "%", "\\%",
2023
// "_", "\\_",
2124
)
2225
}
23-
return lazyMySQLReplacer
24-
}
25-
26-
// MySQL sanitizes mysql based on
27-
// https://dev.mysql.com/doc/refman/8.0/en/string-literals.html table 9.1
28-
// needs to be placed in either a single or a double quoted string
29-
func sanitize(input string) string {
30-
return mysqlReplacer().Replace(input)
26+
return lazyMySQLReplacer.Replace(input)
3127
}

0 commit comments

Comments
 (0)