@@ -35,7 +35,7 @@ type Data struct {
3535type table struct {
3636 Name string
3737 SQL string
38- Values string
38+ Values [] string
3939}
4040
4141type metaData struct {
@@ -44,7 +44,7 @@ type metaData struct {
4444 CompleteTime string
4545}
4646
47- const version = "0.3.4 "
47+ const version = "0.3.5 "
4848
4949const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
5050--
@@ -81,7 +81,10 @@ DROP TABLE IF EXISTS {{ .Name }};
8181LOCK 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 */;
8790UNLOCK TABLES;
@@ -100,6 +103,8 @@ const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
100103-- Dump completed on {{ .CompleteTime }}
101104`
102105
106+ const nullType = "NULL"
107+
103108// Dump data using struct
104109func (data * Data ) Dump () error {
105110 meta := metaData {
@@ -260,25 +265,25 @@ func (data *Data) createTableSQL(name string) (string, error) {
260265 return tableSQL .String , nil
261266}
262267
263- func (data * Data ) createTableValues (name string ) (string , error ) {
268+ func (data * Data ) createTableValues (name string ) ([] string , error ) {
264269 rows , err := data .Connection .Query ("SELECT * FROM `" + name + "`" )
265270 if err != nil {
266- return "" , err
271+ return nil , err
267272 }
268273 defer rows .Close ()
269274
270275 columns , err := rows .Columns ()
271276 if err != nil {
272- return "" , err
277+ return nil , err
273278 }
274279 if len (columns ) == 0 {
275- return "" , errors .New ("No columns in table " + name + "." )
280+ return nil , errors .New ("No columns in table " + name + "." )
276281 }
277282
278283 dataText := make ([]string , 0 )
279284 tt , err := rows .ColumnTypes ()
280285 if err != nil {
281- return "" , err
286+ return nil , err
282287 }
283288
284289 types := make ([]reflect.Type , len (tt ))
@@ -302,39 +307,42 @@ func (data *Data) createTableValues(name string) (string, error) {
302307 }
303308 for rows .Next () {
304309 if err := rows .Scan (values ... ); err != nil {
305- return "" , err
310+ return dataText , err
306311 }
307312
308313 dataStrings := make ([]string , len (columns ))
309314
310315 for key , value := range values {
311316 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
331318 } 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+ }
333341 }
334342 }
335343
336344 dataText = append (dataText , "(" + strings .Join (dataStrings , "," )+ ")" )
337345 }
338346
339- return strings . Join ( dataText , "," ) , rows .Err ()
347+ return dataText , rows .Err ()
340348}
0 commit comments