@@ -381,32 +381,40 @@ func (table *table) Init() error {
381
381
382
382
table .values = make ([]interface {}, len (tt ))
383
383
for i , tp := range tt {
384
- var t reflect.Type
385
- st := tp . ScanType ()
386
- dt := tp . DatabaseTypeName ()
387
-
388
- if dt == "BLOB" || dt == "BINARY" {
389
- t = reflect . TypeOf ( sql.RawBytes {})
390
- } else if dt == "VARCHAR" || dt == "TEXT" || dt == "DECIMAL" {
391
- t = reflect . TypeOf (sql. NullString {})
392
- } else if ( st != nil && ( st .Kind () == reflect .Int ||
384
+ table . values [ i ] = reflect .New ( reflectColumnType ( tp )). Interface ()
385
+ }
386
+ return nil
387
+ }
388
+
389
+ func reflectColumnType ( tp * sql.ColumnType ) reflect. Type {
390
+ // reflect for scanable
391
+ if st := tp . ScanType (); st != nil {
392
+ if st .Kind () == reflect .Int ||
393
393
st .Kind () == reflect .Int8 ||
394
394
st .Kind () == reflect .Int16 ||
395
395
st .Kind () == reflect .Int32 ||
396
- st .Kind () == reflect .Int64 )) ||
397
- dt == "BIGINT" || dt == "TINYINT" || dt == "INT" {
398
- t = reflect .TypeOf (sql.NullInt64 {})
399
- } else if (st != nil && (st .Kind () == reflect .Float32 ||
400
- st .Kind () == reflect .Float64 )) ||
401
- tp .DatabaseTypeName () == "DOUBLE" {
402
- t = reflect .TypeOf (sql.NullFloat64 {})
403
- } else {
404
- // unknown datatype
405
- t = reflect .TypeOf (sql.NullString {})
396
+ st .Kind () == reflect .Int64 {
397
+ return reflect .TypeOf (sql.NullInt64 {})
398
+ } else if st .Kind () == reflect .Float32 ||
399
+ st .Kind () == reflect .Float64 {
400
+ return reflect .TypeOf (sql.NullFloat64 {})
406
401
}
407
- table .values [i ] = reflect .New (t ).Interface ()
408
402
}
409
- return nil
403
+
404
+ // determine by name
405
+ switch tp .DatabaseTypeName () {
406
+ case "BLOB" , "BINARY" :
407
+ return reflect .TypeOf (sql.RawBytes {})
408
+ case "VARCHAR" , "TEXT" , "DECIMAL" :
409
+ return reflect .TypeOf (sql.NullString {})
410
+ case "BIGINT" , "TINYINT" , "INT" :
411
+ return reflect .TypeOf (sql.NullInt64 {})
412
+ case "DOUBLE" :
413
+ return reflect .TypeOf (sql.NullFloat64 {})
414
+ }
415
+
416
+ // unknown datatype
417
+ return reflect .TypeOf (sql.NullString {})
410
418
}
411
419
412
420
func (table * table ) Next () bool {
@@ -460,6 +468,12 @@ func (table *table) RowBuffer() *bytes.Buffer {
460
468
} else {
461
469
b .WriteString (nullType )
462
470
}
471
+ case * sql.NullFloat64 :
472
+ if s .Valid {
473
+ fmt .Fprintf (& b , "%f" , s .Float64 )
474
+ } else {
475
+ b .WriteString (nullType )
476
+ }
463
477
case * sql.RawBytes :
464
478
if len (* s ) == 0 {
465
479
b .WriteString (nullType )
@@ -491,7 +505,7 @@ func (table *table) Stream() <-chan string {
491
505
}
492
506
493
507
if insert .Len () == 0 {
494
- fmt .Fprintf (& insert , "INSERT INTO %s (%s) VALUES " , table .NameEsc (), table .columnsList ())
508
+ fmt .Fprint (& insert , "INSERT INTO " , table .NameEsc (), " (" , table .columnsList (), ") VALUES " )
495
509
} else {
496
510
insert .WriteString ("," )
497
511
}
0 commit comments