-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapping.go
149 lines (140 loc) · 3.14 KB
/
mapping.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package dbx
import (
"database/sql"
"errors"
"fmt"
"reflect"
"github.com/microbun/dbx/reflectx"
)
type mode int
const (
single mode = 1
slice mode = 2
)
func mapping(rows *sql.Rows, dest interface{}, m mode) error {
defer func() {
err := rows.Close()
if err != nil {
panic(err)
}
}()
value := reflect.ValueOf(dest)
//TODO: add map support
if value.Kind() == reflect.Map {
return errors.New("dest unsupported map")
}
if value.Kind() != reflect.Ptr {
return errors.New("dest must be a ptr")
}
direct := reflect.Indirect(value)
columns, err := rows.Columns()
if err != nil {
return err
}
switch m {
case slice:
{
return toSlice(direct, columns, rows)
}
case single:
{
return toSingle(direct, columns, rows)
}
default:
{
return errors.New("unknown scan m")
}
}
}
func toSingle(dest reflect.Value, columns []string, rows *sql.Rows) error {
if reflectx.IsBasicValue(dest) {
if len(columns) == 1 {
if exists := rows.Next(); exists {
return rows.Scan(dest.Addr().Interface())
} else {
return sql.ErrNoRows
}
} else {
return fmt.Errorf("multi columns not scan to a basic type")
}
} else if reflectx.IsStructValue(dest) {
properties := reflectx.NewProperties(len(columns))
if exists := rows.Next(); exists {
err := traversal(dest, properties, columns)
if err != nil {
return err
}
return rows.Scan(properties.Values()...)
} else {
return sql.ErrNoRows
}
} else {
return fmt.Errorf("argument not a struct or basic")
}
}
func toSlice(dest reflect.Value, columns []string, rows *sql.Rows) error {
kind := dest.Kind()
if kind != reflect.Array && kind != reflect.Slice {
return fmt.Errorf("argument not a array or slice")
}
valueType := dest.Type().Elem()
isPtr := valueType.Kind() == reflect.Ptr
if isPtr {
valueType = valueType.Elem()
}
if reflectx.IsBasicType(valueType) {
if len(columns) == 1 {
for rows.Next() {
pv := reflect.New(valueType)
dv := reflect.Indirect(pv)
err := rows.Scan(pv.Interface())
if err != nil {
return err
}
if isPtr {
dest.Set(reflect.Append(dest, pv))
} else {
dest.Set(reflect.Append(dest, dv))
}
}
} else {
return fmt.Errorf("dest slice not a struct or basic")
}
} else if reflectx.IsStructType(valueType) {
for rows.Next() {
pv := reflect.New(valueType)
dv := reflect.Indirect(pv)
properties := reflectx.NewProperties(len(columns))
err := traversal(dv, properties, columns)
if err != nil {
return err
}
err = rows.Scan(properties.Values()...)
if err != nil {
return err
}
if isPtr {
dest.Set(reflect.Append(dest, pv))
} else {
dest.Set(reflect.Append(dest, dv))
}
}
} else {
return fmt.Errorf("unknown slice type")
}
return nil
}
func traversal(v reflect.Value, props reflectx.Properties, columns []string) error {
direct := reflect.Indirect(v)
sv := map[string]reflectx.Property{}
reflectx.ReflectProperty(v, sv)
for i, name := range columns {
prop, ok := sv[name]
if ok {
props[i] = prop
} else {
return fmt.Errorf("missing field `%s` in %s.%s", name, direct.Type().PkgPath(), direct.Type().Name())
}
}
return nil
}