Skip to content

Commit d07121e

Browse files
committed
col info
1 parent 9fcf937 commit d07121e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

dump.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type table struct {
4040
Name string
4141
Err error
4242

43+
cols []string
4344
data *Data
4445
rows *sql.Rows
4546
values []interface{}
@@ -219,7 +220,7 @@ func (data *Data) writeTable(table *table) error {
219220

220221
// MARK: get methods
221222

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
223224
func (data *Data) getTemplates() (err error) {
224225
data.headerTmpl, err = template.New("mysqldumpHeader").Parse(headerTmpl)
225226
if err != nil {
@@ -301,18 +302,18 @@ func (table *table) CreateSQL() (string, error) {
301302
return tableSQL.String, nil
302303
}
303304

304-
func (table *table) getColumnsToDump() ([]string, error) {
305+
func (table *table) initColumnData() error {
305306
colInfo, err := table.data.tx.Query("SHOW COLUMNS FROM " + table.NameEsc())
306307
if err != nil {
307-
return nil, err
308+
return err
308309
}
309310

310311
var result []string
311312

312313
for colInfo.Next() {
313314
cols, err := colInfo.Columns()
314315
if err != nil {
315-
return nil, err
316+
return err
316317
}
317318

318319
// Allocate and link space to scan to this must be done every iteration
@@ -324,7 +325,7 @@ func (table *table) getColumnsToDump() ([]string, error) {
324325

325326
// Read into the pointers to the info marker
326327
if err := colInfo.Scan(scans...); err != nil {
327-
return nil, err
328+
return err
328329
}
329330

330331
// Find the the fields we care about
@@ -342,28 +343,33 @@ func (table *table) getColumnsToDump() ([]string, error) {
342343
}
343344

344345
if !extra.Valid || !strings.Contains(extra.String, "VIRTUAL") {
345-
result = append(result, "`"+field.String+"`")
346+
result = append(result, field.String)
346347
}
347348
}
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, "`, `") + "`"
349355
}
350356

351357
func (table *table) Init() error {
352358
if len(table.values) != 0 {
353359
return errors.New("can't init twice")
354360
}
355361

356-
columns, err := table.getColumnsToDump()
357-
if err != nil {
362+
if err := table.initColumnData(); err != nil {
358363
return err
359364
}
360365

361-
if len(columns) == 0 {
366+
if len(table.cols) == 0 {
362367
// No data to dump since this is a virtual table
363368
return nil
364369
}
365370

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())
367373
if err != nil {
368374
return err
369375
}
@@ -485,7 +491,7 @@ func (table *table) Stream() <-chan string {
485491
}
486492

487493
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())
489495
} else {
490496
insert.WriteString(",")
491497
}

0 commit comments

Comments
 (0)