-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
113 lines (96 loc) · 2.29 KB
/
scanner.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dbo
import (
"database/sql"
"reflect"
"strings"
)
func scanRowsToSimpleSlice(rows *sql.Rows,p reflect.Value) {
sliceType := reflect.TypeOf(p.Elem().Interface()).Elem()
elemSlice := reflect.MakeSlice(reflect.SliceOf(sliceType),0,0)
for rows.Next(){
obj := reflect.New(sliceType).Interface()
rows.Scan(obj)
elemSlice = reflect.Append(elemSlice, reflect.ValueOf(obj).Elem())
}
p.Elem().Set(elemSlice)
rows.Close()
}
func scanModel(rows *sql.Rows, target reflect.Value) error {
st := target.Elem().Type()
stType := target.Elem().Type()
cols,err := rows.Columns()
if err != nil{
return err
}
var columnPtr = make([]int,len(cols),len(cols))
for i,col := range cols{
found := false
for j := 0; j < st.NumField(); j++ {
field := st.Field(j)
if strings.ToLower(field.Name) == strings.ToLower(ToCamel(col)) || field.Tag.Get("db") == col{
found = true
columnPtr[i] = j
break
}
}
if !found{
columnPtr[i] = -1
}
}
var nullPtr interface{}
for rows.Next(){
var ptr = make([]interface{},len(columnPtr),len(columnPtr))
ref := reflect.New(stType)
for i,item := range columnPtr{
if item == -1{
ptr[i] = &nullPtr
}else {
ptr[i] = ref.Elem().Field(item).Addr().Interface()
}
}
rows.Scan(ptr...)
target.Elem().Set(ref.Elem())
break
}
return nil
}
func scanSliceModel(rows *sql.Rows, target reflect.Value) error {
st := target.Elem().Type().Elem()
stType := target.Elem().Type().Elem()
cols,err := rows.Columns()
if err != nil{
return err
}
var columnPtr = make([]int,len(cols),len(cols))
for i,col := range cols{
found := false
for j := 0; j < st.NumField(); j++ {
field := st.Field(j)
if strings.ToLower(field.Name) == strings.ToLower(ToCamel(col)) || field.Tag.Get("db") == col{
found = true
columnPtr[i] = j
break
}
}
if !found{
columnPtr[i] = -1
}
}
var nullPtr interface{}
var slice = reflect.MakeSlice(reflect.SliceOf(stType),0,0)
for rows.Next(){
var ptr = make([]interface{},len(columnPtr),len(columnPtr))
ref := reflect.New(stType)
for i,item := range columnPtr{
if item == -1{
ptr[i] = &nullPtr
}else {
ptr[i] = ref.Elem().Field(item).Addr().Interface()
}
}
rows.Scan(ptr...)
slice = reflect.Append(slice,ref.Elem())
}
target.Elem().Set(slice)
return nil
}