@@ -16,11 +16,11 @@ import (
16
16
/*
17
17
Data struct to configure dump behavior
18
18
19
- Out: Stream to wite to
20
- Connection: Database connection to dump
21
- IgnoreTables: Mark sensitive tables to ignore
22
- MaxAllowedPacket: Sets the largest packet size to use in backups
23
- LockTables: Lock all tables for the duration of the dump
19
+ Out: Stream to wite to
20
+ Connection: Database connection to dump
21
+ IgnoreTables: Mark sensitive tables to ignore
22
+ MaxAllowedPacket: Sets the largest packet size to use in backups
23
+ LockTables: Lock all tables for the duration of the dump
24
24
*/
25
25
type Data struct {
26
26
Out io.Writer
@@ -68,7 +68,7 @@ const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
68
68
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
69
69
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
70
70
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
71
- SET NAMES utf8mb4 ;
71
+ /*!50503 SET NAMES UTF8 */ ;
72
72
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
73
73
/*!40103 SET TIME_ZONE='+00:00' */;
74
74
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
@@ -99,7 +99,7 @@ const tableTmpl = `
99
99
100
100
DROP TABLE IF EXISTS {{ .NameEsc }};
101
101
/*!40101 SET @saved_cs_client = @@character_set_client */;
102
- SET character_set_client = utf8mb4 ;
102
+ /*!50503 SET character_set_client = utf8mb4 */ ;
103
103
{{ .CreateSQL }};
104
104
/*!40101 SET character_set_client = @saved_cs_client */;
105
105
@@ -296,7 +296,7 @@ func (table *table) CreateSQL() (string, error) {
296
296
}
297
297
298
298
if tableReturn .String != table .Name {
299
- return "" , errors .New ("Returned table is not the same as requested table" )
299
+ return "" , errors .New ("returned table is not the same as requested table" )
300
300
}
301
301
302
302
return tableSQL .String , nil
@@ -383,38 +383,11 @@ func (table *table) Init() error {
383
383
384
384
table .values = make ([]interface {}, len (tt ))
385
385
for i , tp := range tt {
386
- table .values [i ] = reflect .New (reflectColumnType ( tp )).Interface ()
386
+ table .values [i ] = reflect .New (tp . ScanType ( )).Interface ()
387
387
}
388
388
return nil
389
389
}
390
390
391
- func reflectColumnType (tp * sql.ColumnType ) reflect.Type {
392
- // reflect for scanable
393
- switch tp .ScanType ().Kind () {
394
- case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
395
- return reflect .TypeOf (sql.NullInt64 {})
396
- case reflect .Float32 , reflect .Float64 :
397
- return reflect .TypeOf (sql.NullFloat64 {})
398
- case reflect .String :
399
- return reflect .TypeOf (sql.NullString {})
400
- }
401
-
402
- // determine by name
403
- switch tp .DatabaseTypeName () {
404
- case "BLOB" , "BINARY" :
405
- return reflect .TypeOf (sql.RawBytes {})
406
- case "VARCHAR" , "TEXT" , "DECIMAL" , "JSON" :
407
- return reflect .TypeOf (sql.NullString {})
408
- case "BIGINT" , "TINYINT" , "INT" :
409
- return reflect .TypeOf (sql.NullInt64 {})
410
- case "DOUBLE" :
411
- return reflect .TypeOf (sql.NullFloat64 {})
412
- }
413
-
414
- // unknown datatype
415
- return tp .ScanType ()
416
- }
417
-
418
391
func (table * table ) Next () bool {
419
392
if table .rows == nil {
420
393
if err := table .Init (); err != nil {
@@ -443,6 +416,30 @@ func (table *table) RowValues() string {
443
416
return table .RowBuffer ().String ()
444
417
}
445
418
419
+ func writeString (b * bytes.Buffer , s string ) {
420
+ fmt .Fprintf (b , "'%s'" , sanitize (s ))
421
+ }
422
+
423
+ func writeBool (b * bytes.Buffer , s bool ) {
424
+ if s {
425
+ fmt .Fprintf (b , "1" )
426
+ } else {
427
+ fmt .Fprintf (b , "0" )
428
+ }
429
+ }
430
+
431
+ func writeBinary (b * bytes.Buffer , s []byte ) {
432
+ if len (s ) == 0 {
433
+ b .WriteString (nullType )
434
+ } else {
435
+ fmt .Fprintf (b , "_binary '%s'" , sanitize (string (s )))
436
+ }
437
+ }
438
+
439
+ func writeTime (b * bytes.Buffer , s time.Time ) {
440
+ fmt .Fprintf (b , "'%s'" , sanitize (s .UTC ().Format (time .DateTime )))
441
+ }
442
+
446
443
func (table * table ) RowBuffer () * bytes.Buffer {
447
444
var b bytes.Buffer
448
445
b .WriteString ("(" )
@@ -454,9 +451,51 @@ func (table *table) RowBuffer() *bytes.Buffer {
454
451
switch s := value .(type ) {
455
452
case nil :
456
453
b .WriteString (nullType )
454
+ case * string :
455
+ writeString (& b , * s )
457
456
case * sql.NullString :
458
457
if s .Valid {
459
- fmt .Fprintf (& b , "'%s'" , sanitize (s .String ))
458
+ writeString (& b , s .String )
459
+ } else {
460
+ b .WriteString (nullType )
461
+ }
462
+ case * bool :
463
+ writeBool (& b , * s )
464
+ case * sql.NullBool :
465
+ if s .Valid {
466
+ writeBool (& b , s .Bool )
467
+ } else {
468
+ b .WriteString (nullType )
469
+ }
470
+ case * uint :
471
+ fmt .Fprintf (& b , "%d" , * s )
472
+ case * uint8 :
473
+ fmt .Fprintf (& b , "%d" , * s )
474
+ case * uint16 :
475
+ fmt .Fprintf (& b , "%d" , * s )
476
+ case * uint32 :
477
+ fmt .Fprintf (& b , "%d" , * s )
478
+ case * uint64 :
479
+ fmt .Fprintf (& b , "%d" , * s )
480
+ case * int :
481
+ fmt .Fprintf (& b , "%d" , * s )
482
+ case * int8 :
483
+ fmt .Fprintf (& b , "%d" , * s )
484
+ case * int16 :
485
+ fmt .Fprintf (& b , "%d" , * s )
486
+ case * int32 :
487
+ fmt .Fprintf (& b , "%d" , * s )
488
+ case * int64 :
489
+ fmt .Fprintf (& b , "%d" , * s )
490
+ case * sql.NullInt16 :
491
+ if s .Valid {
492
+ fmt .Fprintf (& b , "%d" , s .Int16 )
493
+ } else {
494
+ b .WriteString (nullType )
495
+ }
496
+ case * sql.NullInt32 :
497
+ if s .Valid {
498
+ fmt .Fprintf (& b , "%d" , s .Int32 )
460
499
} else {
461
500
b .WriteString (nullType )
462
501
}
@@ -466,17 +505,27 @@ func (table *table) RowBuffer() *bytes.Buffer {
466
505
} else {
467
506
b .WriteString (nullType )
468
507
}
508
+ case * float32 :
509
+ fmt .Fprintf (& b , "%f" , * s )
510
+ case * float64 :
511
+ fmt .Fprintf (& b , "%f" , * s )
469
512
case * sql.NullFloat64 :
470
513
if s .Valid {
471
514
fmt .Fprintf (& b , "%f" , s .Float64 )
472
515
} else {
473
516
b .WriteString (nullType )
474
517
}
518
+ case * []byte :
519
+ writeBinary (& b , * s )
475
520
case * sql.RawBytes :
476
- if len (* s ) == 0 {
477
- b .WriteString (nullType )
521
+ writeBinary (& b , * s )
522
+ case * time.Time :
523
+ writeTime (& b , * s )
524
+ case * sql.NullTime :
525
+ if s .Valid {
526
+ writeTime (& b , s .Time )
478
527
} else {
479
- fmt . Fprintf ( & b , "_binary '%s'" , sanitize ( string ( * s )) )
528
+ b . WriteString ( nullType )
480
529
}
481
530
default :
482
531
fmt .Fprintf (& b , "'%s'" , value )
0 commit comments