@@ -3,9 +3,11 @@ package mysqldump
3
3
import (
4
4
"database/sql"
5
5
"errors"
6
+ "fmt"
6
7
"io"
7
8
"os"
8
9
"path"
10
+ "reflect"
9
11
"strings"
10
12
"sync"
11
13
"text/template"
@@ -272,30 +274,55 @@ func createTableValues(db *sql.DB, name string) (string, error) {
272
274
273
275
// Read data
274
276
dataText := make ([]string , 0 )
275
- for rows .Next () {
276
- // Init temp data storage
277
-
278
- //ptrs := make([]interface{}, len(columns))
279
- //var ptrs []interface {} = make([]*sql.NullString, len(columns))
277
+ tt , err := rows .ColumnTypes ()
278
+ if err != nil {
279
+ return "" , err
280
+ }
280
281
281
- data := make ([]* sql.NullString , len (columns ))
282
- ptrs := make ([]interface {}, len (columns ))
283
- for i := range data {
284
- ptrs [i ] = & data [i ]
282
+ types := make ([]reflect.Type , len (tt ))
283
+ for i , tp := range tt {
284
+ st := tp .ScanType ()
285
+ if st == nil || st .Kind () == reflect .Slice {
286
+ types [i ] = reflect .TypeOf (sql.NullString {})
287
+ } else if st .Kind () == reflect .Int ||
288
+ st .Kind () == reflect .Int8 ||
289
+ st .Kind () == reflect .Int16 ||
290
+ st .Kind () == reflect .Int32 ||
291
+ st .Kind () == reflect .Int64 {
292
+ types [i ] = reflect .TypeOf (sql.NullInt64 {})
293
+ } else {
294
+ types [i ] = st
285
295
}
286
-
296
+ }
297
+ values := make ([]interface {}, len (tt ))
298
+ for i := range values {
299
+ values [i ] = reflect .New (types [i ]).Interface ()
300
+ }
301
+ for rows .Next () {
287
302
// Read data
288
- if err := rows .Scan (ptrs ... ); err != nil {
303
+ if err := rows .Scan (values ... ); err != nil {
289
304
return "" , err
290
305
}
291
306
292
307
dataStrings := make ([]string , len (columns ))
293
308
294
- for key , value := range data {
295
- if value != nil && value .Valid {
296
- dataStrings [key ] = "'" + value .String + "'"
297
- } else {
309
+ for key , value := range values {
310
+ if value == nil {
298
311
dataStrings [key ] = "null"
312
+ } else if s , ok := value .(* sql.NullString ); ok {
313
+ if s .Valid {
314
+ dataStrings [key ] = "'" + strings .Replace (s .String , "\n " , "\\ n" , - 1 ) + "'"
315
+ } else {
316
+ dataStrings [key ] = "NULL"
317
+ }
318
+ } else if s , ok := value .(* sql.NullInt64 ); ok {
319
+ if s .Valid {
320
+ dataStrings [key ] = fmt .Sprintf ("%d" , s .Int64 )
321
+ } else {
322
+ dataStrings [key ] = "NULL"
323
+ }
324
+ } else {
325
+ panic (value )
299
326
}
300
327
}
301
328
0 commit comments