@@ -40,6 +40,7 @@ type table struct {
40
40
Name string
41
41
Err error
42
42
43
+ cols []string
43
44
data * Data
44
45
rows * sql.Rows
45
46
values []interface {}
@@ -219,7 +220,7 @@ func (data *Data) writeTable(table *table) error {
219
220
220
221
// MARK: get methods
221
222
222
- // getTemplates initilaizes the templates on data from the constants in this file
223
+ // getTemplates initializes the templates on data from the constants in this file
223
224
func (data * Data ) getTemplates () (err error ) {
224
225
data .headerTmpl , err = template .New ("mysqldumpHeader" ).Parse (headerTmpl )
225
226
if err != nil {
@@ -301,18 +302,18 @@ func (table *table) CreateSQL() (string, error) {
301
302
return tableSQL .String , nil
302
303
}
303
304
304
- func (table * table ) getColumnsToDump () ([] string , error ) {
305
+ func (table * table ) initColumnData () error {
305
306
colInfo , err := table .data .tx .Query ("SHOW COLUMNS FROM " + table .NameEsc ())
306
307
if err != nil {
307
- return nil , err
308
+ return err
308
309
}
309
310
310
311
var result []string
311
312
312
313
for colInfo .Next () {
313
314
cols , err := colInfo .Columns ()
314
315
if err != nil {
315
- return nil , err
316
+ return err
316
317
}
317
318
318
319
// Allocate and link space to scan to this must be done every iteration
@@ -324,7 +325,7 @@ func (table *table) getColumnsToDump() ([]string, error) {
324
325
325
326
// Read into the pointers to the info marker
326
327
if err := colInfo .Scan (scans ... ); err != nil {
327
- return nil , err
328
+ return err
328
329
}
329
330
330
331
// Find the the fields we care about
@@ -342,28 +343,33 @@ func (table *table) getColumnsToDump() ([]string, error) {
342
343
}
343
344
344
345
if ! extra .Valid || ! strings .Contains (extra .String , "VIRTUAL" ) {
345
- result = append (result , "`" + field .String + "`" )
346
+ result = append (result , field .String )
346
347
}
347
348
}
348
- return result , nil
349
+ table .cols = result
350
+ return nil
351
+ }
352
+
353
+ func (table * table ) columnsList () string {
354
+ return "`" + strings .Join (table .cols , "`, `" ) + "`"
349
355
}
350
356
351
357
func (table * table ) Init () error {
352
358
if len (table .values ) != 0 {
353
359
return errors .New ("can't init twice" )
354
360
}
355
361
356
- columns , err := table .getColumnsToDump ()
357
- if err != nil {
362
+ if err := table .initColumnData (); err != nil {
358
363
return err
359
364
}
360
365
361
- if len (columns ) == 0 {
366
+ if len (table . cols ) == 0 {
362
367
// No data to dump since this is a virtual table
363
368
return nil
364
369
}
365
370
366
- table .rows , err = table .data .tx .Query ("SELECT " + strings .Join (columns , "," ) + " FROM " + table .NameEsc ())
371
+ var err error
372
+ table .rows , err = table .data .tx .Query ("SELECT " + table .columnsList () + " FROM " + table .NameEsc ())
367
373
if err != nil {
368
374
return err
369
375
}
@@ -485,7 +491,7 @@ func (table *table) Stream() <-chan string {
485
491
}
486
492
487
493
if insert .Len () == 0 {
488
- fmt .Fprintf (& insert , "INSERT INTO %s VALUES " , table .NameEsc ())
494
+ fmt .Fprintf (& insert , "INSERT INTO %s (%s) VALUES " , table .NameEsc (), table . columnsList ())
489
495
} else {
490
496
insert .WriteString ("," )
491
497
}
0 commit comments