Skip to content

Commit 87abdbd

Browse files
committed
reflect helper method
1 parent d07121e commit 87abdbd

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

dump.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -381,32 +381,40 @@ func (table *table) Init() error {
381381

382382
table.values = make([]interface{}, len(tt))
383383
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 ||
393393
st.Kind() == reflect.Int8 ||
394394
st.Kind() == reflect.Int16 ||
395395
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{})
406401
}
407-
table.values[i] = reflect.New(t).Interface()
408402
}
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{})
410418
}
411419

412420
func (table *table) Next() bool {
@@ -460,6 +468,12 @@ func (table *table) RowBuffer() *bytes.Buffer {
460468
} else {
461469
b.WriteString(nullType)
462470
}
471+
case *sql.NullFloat64:
472+
if s.Valid {
473+
fmt.Fprintf(&b, "%f", s.Float64)
474+
} else {
475+
b.WriteString(nullType)
476+
}
463477
case *sql.RawBytes:
464478
if len(*s) == 0 {
465479
b.WriteString(nullType)
@@ -491,7 +505,7 @@ func (table *table) Stream() <-chan string {
491505
}
492506

493507
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 ")
495509
} else {
496510
insert.WriteString(",")
497511
}

0 commit comments

Comments
 (0)