Skip to content

Commit 9823f56

Browse files
committed
All in the templates
1 parent 0937960 commit 9823f56

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

dump.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Data struct {
3535
type table struct {
3636
Name string
3737
SQL string
38-
Values string
38+
Values []string
3939
}
4040

4141
type metaData struct {
@@ -81,7 +81,10 @@ DROP TABLE IF EXISTS {{ .Name }};
8181
LOCK TABLES {{ .Name }} WRITE;
8282
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
8383
{{- if .Values }}
84-
INSERT INTO {{ .Name }} VALUES {{ .Values }};
84+
INSERT INTO {{ .Name }} VALUES
85+
{{- range $index, $element := .Values -}}
86+
{{- if $index }},{{ else }} {{ end -}}{{ $element }}
87+
{{- end -}};
8588
{{- end }}
8689
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
8790
UNLOCK TABLES;
@@ -262,25 +265,25 @@ func (data *Data) createTableSQL(name string) (string, error) {
262265
return tableSQL.String, nil
263266
}
264267

265-
func (data *Data) createTableValues(name string) (string, error) {
268+
func (data *Data) createTableValues(name string) ([]string, error) {
266269
rows, err := data.Connection.Query("SELECT * FROM `" + name + "`")
267270
if err != nil {
268-
return "", err
271+
return nil, err
269272
}
270273
defer rows.Close()
271274

272275
columns, err := rows.Columns()
273276
if err != nil {
274-
return "", err
277+
return nil, err
275278
}
276279
if len(columns) == 0 {
277-
return "", errors.New("No columns in table " + name + ".")
280+
return nil, errors.New("No columns in table " + name + ".")
278281
}
279282

280283
dataText := make([]string, 0)
281284
tt, err := rows.ColumnTypes()
282285
if err != nil {
283-
return "", err
286+
return nil, err
284287
}
285288

286289
types := make([]reflect.Type, len(tt))
@@ -304,7 +307,7 @@ func (data *Data) createTableValues(name string) (string, error) {
304307
}
305308
for rows.Next() {
306309
if err := rows.Scan(values...); err != nil {
307-
return "", err
310+
return dataText, err
308311
}
309312

310313
dataStrings := make([]string, len(columns))
@@ -341,5 +344,5 @@ func (data *Data) createTableValues(name string) (string, error) {
341344
dataText = append(dataText, "("+strings.Join(dataStrings, ",")+")")
342345
}
343346

344-
return strings.Join(dataText, ","), rows.Err()
347+
return dataText, rows.Err()
345348
}

dump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestCreateTableOk(t *testing.T) {
286286
expectedResult := &table{
287287
Name: "`Test_Table`",
288288
SQL: "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`s` char(60) DEFAULT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1",
289-
Values: "('1',NULL,'Test Name 1'),('2','[email protected]','Test Name 2')",
289+
Values: []string{"('1',NULL,'Test Name 1')", "('2','[email protected]','Test Name 2')"},
290290
}
291291

292292
if !reflect.DeepEqual(result, expectedResult) {

mysqldump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ UNLOCK TABLES;
9696
`
9797

9898
if !reflect.DeepEqual(result, expected) {
99-
t.Fatalf("expected %#v, got %#v", expected, result)
99+
t.Fatalf("expected \n%#v, got \n%#v", expected, result)
100100
}
101101
}

0 commit comments

Comments
 (0)