-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbTools.go
More file actions
71 lines (67 loc) · 1.82 KB
/
dbTools.go
File metadata and controls
71 lines (67 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package middleware
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
)
var jsonType = reflect.TypeOf(json.RawMessage{})
// ScanRows rows扫描
func ScanRows(rows *sql.Rows) ([]map[string]string, error) {
result := []map[string]string{}
columns, _ := rows.Columns()
for rows.Next() {
data := make([]interface{}, len(columns))
columnPointers := make([]interface{}, len(columns))
for i, _ := range data {
columnPointers[i] = &data[i]
}
err := rows.Scan(columnPointers...)
if err != nil {
return nil, err
}
row := map[string]string{}
for i, _ := range columns {
if data[i] == nil {
row[columns[i]] = ""
continue
}
row[columns[i]] = convertInterface(data[i])
}
result = append(result, row)
}
rows.Close()
return result, nil
}
func convertInterface(param interface{}) string {
dataVal := reflect.ValueOf(param)
switch dataVal.Kind() {
case reflect.Ptr:
// indirect pointers
if dataVal.IsNil() {
return ""
} else {
return convertInterface(dataVal.Elem().Interface())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%v", dataVal.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", dataVal.Uint())
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%.4f", dataVal.Float())
case reflect.Bool:
return fmt.Sprintf("%v", dataVal.Bool())
case reflect.Slice:
switch t := dataVal.Type(); {
case t == jsonType:
return fmt.Sprintf("%v", t)
case t.Elem().Kind() == reflect.Uint8:
return fmt.Sprintf("%v", string(dataVal.Bytes()))
default:
return fmt.Sprintf("unsupported type %T, a slice of %s", param, t.Elem().Kind())
}
case reflect.String:
return dataVal.String()
}
return fmt.Sprintf("unsupported type %T, a %s", param, dataVal.Kind())
}