@@ -35,7 +35,7 @@ type Data struct {
35
35
type table struct {
36
36
Name string
37
37
SQL string
38
- Values string
38
+ Values [] string
39
39
}
40
40
41
41
type metaData struct {
@@ -44,7 +44,7 @@ type metaData struct {
44
44
CompleteTime string
45
45
}
46
46
47
- const version = "0.3.4 "
47
+ const version = "0.3.5 "
48
48
49
49
const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
50
50
--
@@ -81,7 +81,10 @@ DROP TABLE IF EXISTS {{ .Name }};
81
81
LOCK TABLES {{ .Name }} WRITE;
82
82
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
83
83
{{- 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 -}};
85
88
{{- end }}
86
89
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
87
90
UNLOCK TABLES;
@@ -100,6 +103,8 @@ const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
100
103
-- Dump completed on {{ .CompleteTime }}
101
104
`
102
105
106
+ const nullType = "NULL"
107
+
103
108
// Dump data using struct
104
109
func (data * Data ) Dump () error {
105
110
meta := metaData {
@@ -260,25 +265,25 @@ func (data *Data) createTableSQL(name string) (string, error) {
260
265
return tableSQL .String , nil
261
266
}
262
267
263
- func (data * Data ) createTableValues (name string ) (string , error ) {
268
+ func (data * Data ) createTableValues (name string ) ([] string , error ) {
264
269
rows , err := data .Connection .Query ("SELECT * FROM `" + name + "`" )
265
270
if err != nil {
266
- return "" , err
271
+ return nil , err
267
272
}
268
273
defer rows .Close ()
269
274
270
275
columns , err := rows .Columns ()
271
276
if err != nil {
272
- return "" , err
277
+ return nil , err
273
278
}
274
279
if len (columns ) == 0 {
275
- return "" , errors .New ("No columns in table " + name + "." )
280
+ return nil , errors .New ("No columns in table " + name + "." )
276
281
}
277
282
278
283
dataText := make ([]string , 0 )
279
284
tt , err := rows .ColumnTypes ()
280
285
if err != nil {
281
- return "" , err
286
+ return nil , err
282
287
}
283
288
284
289
types := make ([]reflect.Type , len (tt ))
@@ -302,39 +307,42 @@ func (data *Data) createTableValues(name string) (string, error) {
302
307
}
303
308
for rows .Next () {
304
309
if err := rows .Scan (values ... ); err != nil {
305
- return "" , err
310
+ return dataText , err
306
311
}
307
312
308
313
dataStrings := make ([]string , len (columns ))
309
314
310
315
for key , value := range values {
311
316
if value == nil {
312
- dataStrings [key ] = "NULL"
313
- } else if s , ok := value .(* sql.NullString ); ok {
314
- if s .Valid {
315
- dataStrings [key ] = "'" + sanitize (s .String ) + "'"
316
- } else {
317
- dataStrings [key ] = "NULL"
318
- }
319
- } else if s , ok := value .(* sql.NullInt64 ); ok {
320
- if s .Valid {
321
- dataStrings [key ] = fmt .Sprintf ("%d" , s .Int64 )
322
- } else {
323
- dataStrings [key ] = "NULL"
324
- }
325
- } else if s , ok := value .(* sql.RawBytes ); ok {
326
- if len (* s ) == 0 {
327
- dataStrings [key ] = "NULL"
328
- } else {
329
- dataStrings [key ] = "_binary '" + sanitize (string (* s )) + "'"
330
- }
317
+ dataStrings [key ] = nullType
331
318
} else {
332
- dataStrings [key ] = fmt .Sprint ("'" , value , "'" )
319
+ switch s := value .(type ) {
320
+ case * sql.NullString :
321
+ if s .Valid {
322
+ dataStrings [key ] = "'" + sanitize (s .String ) + "'"
323
+ } else {
324
+ dataStrings [key ] = nullType
325
+ }
326
+ case * sql.NullInt64 :
327
+ if s .Valid {
328
+ dataStrings [key ] = fmt .Sprintf ("%d" , s .Int64 )
329
+ } else {
330
+ dataStrings [key ] = nullType
331
+ }
332
+ case * sql.RawBytes :
333
+ if len (* s ) == 0 {
334
+ dataStrings [key ] = nullType
335
+ } else {
336
+ dataStrings [key ] = "_binary '" + sanitize (string (* s )) + "'"
337
+ }
338
+ default :
339
+ dataStrings [key ] = fmt .Sprint ("'" , value , "'" )
340
+ }
333
341
}
334
342
}
335
343
336
344
dataText = append (dataText , "(" + strings .Join (dataStrings , "," )+ ")" )
337
345
}
338
346
339
- return strings . Join ( dataText , "," ) , rows .Err ()
347
+ return dataText , rows .Err ()
340
348
}
0 commit comments