Was putting together a quick dumper for MySQL to JSON in Go. However I find that everything that I retrieve from the database is a []byte
array. Thus instead of native JSON integers or booleans, I'm getting everything encoded as strings.
Subset of the code:
import (
"encoding/json"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func dumpTable(w io.Writer, table) {
// ...
rows, err := Query(db, fmt.Sprintf("SELECT * FROM %s", table))
checkError(err)
columns, err := rows.Columns()
checkError(err)
scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
for rows.Next() {
err = rows.Scan(scanArgs...)
checkError(err)
record := make(map[string]interface{})
for i, col := range values {
if col != nil {
fmt.Printf("
%s: type= %s
", columns[i], reflect.TypeOf(col))
switch t := col.(type) {
default:
fmt.Printf("Unexpected type %T
", t)
case bool:
fmt.Printf("bool
")
record[columns[i]] = col.(bool)
case int:
fmt.Printf("int
")
record[columns[i]] = col.(int)
case int64:
fmt.Printf("int64
")
record[columns[i]] = col.(int64)
case float64:
fmt.Printf("float64
")
record[columns[i]] = col.(float64)
case string:
fmt.Printf("string
")
record[columns[i]] = col.(string)
case []byte: // -- all cases go HERE!
fmt.Printf("[]byte
")
record[columns[i]] = string(col.([]byte))
case time.Time:
// record[columns[i]] = col.(string)
}
}
}
s, _ := json.Marshal(record)
w.Write(s)
io.WriteString(w, "
")
}
}