Skip to content

Commit 011beef

Browse files
committed
Breaking things
1 parent 47914c6 commit 011beef

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

dump.go

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package mysqldump
33
import (
44
"database/sql"
55
"errors"
6+
"fmt"
67
"io"
78
"os"
89
"path"
10+
"reflect"
911
"strings"
1012
"sync"
1113
"text/template"
@@ -272,30 +274,55 @@ func createTableValues(db *sql.DB, name string) (string, error) {
272274

273275
// Read data
274276
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+
}
280281

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
285295
}
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() {
287302
// Read data
288-
if err := rows.Scan(ptrs...); err != nil {
303+
if err := rows.Scan(values...); err != nil {
289304
return "", err
290305
}
291306

292307
dataStrings := make([]string, len(columns))
293308

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 {
298311
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)
299326
}
300327
}
301328

0 commit comments

Comments
 (0)