-
Notifications
You must be signed in to change notification settings - Fork 3
/
result.go
236 lines (224 loc) · 4.74 KB
/
result.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package sql
import (
"database/sql"
"fmt"
"reflect"
"github.com/abulo/ratel/v3/core/logger"
)
// Row 获取记录
type Row struct {
rows *Rows
err error
}
// ToInterface ...
func (r *Row) ToAny() (result map[string]any, err error) {
if r.rows.err != nil {
err = r.rows.err
return
}
items, err := r.rows.ToAny()
if err != nil {
r.err = err
return nil, err
}
if len(items) > 0 {
result = items[0]
}
return result, err
}
// ToMap get Map
func (r *Row) ToMap() (result map[string]string, err error) {
if r.rows.err != nil {
err = r.rows.err
return
}
items, err := r.rows.ToMap()
if err != nil {
r.err = err
return nil, err
}
if len(items) > 0 {
result = items[0]
}
return result, err
}
// ToStruct get Struct
func (r *Row) ToStruct(st any) error {
if r.rows.err != nil {
return r.rows.err
}
defer func() {
if err := r.rows.rows.Close(); err != nil {
logger.Logger.Error("Error closing rs: ", err)
}
}()
//获取变量的类型
stType := reflect.TypeOf(st)
stVal := reflect.ValueOf(st)
if stType.Kind() != reflect.Ptr {
return fmt.Errorf("the variable type is %v, not a pointer", stType.Kind())
}
stTypeInd := stType.Elem()
v := reflect.New(stTypeInd)
tagList, err := extractTagInfo(v)
if err != nil {
return err
}
fields, err := r.rows.rows.Columns()
if err != nil {
return err
}
refs := make([]any, len(fields))
for i, field := range fields {
if f, ok := tagList[field]; ok {
refs[i] = f.Addr().Interface()
} else {
refs[i] = new(any)
}
}
if !r.rows.rows.Next() {
return sql.ErrNoRows
}
if err := r.rows.rows.Scan(refs...); err != nil {
return err
}
stVal.Elem().Set(v.Elem())
return nil
}
// Rows
type Rows struct {
rows *sql.Rows
err error
}
// ToAny 用法
func (r *Rows) ToAny() (data []map[string]any, err error) {
if r.rows == nil {
return nil, r.err
}
defer func() {
if err := r.rows.Close(); err != nil {
logger.Logger.Error("Error closing rs: ", err)
}
}()
fields, err := r.rows.Columns()
if err != nil {
r.err = err
return nil, err
}
data = make([]map[string]any, 0)
num := len(fields)
refs := make([]any, num)
for i := 0; i < num; i++ {
var ref any
refs[i] = &ref
}
for r.rows.Next() {
result := make(map[string]any)
if err := r.rows.Scan(refs...); err != nil {
return nil, err
}
for i, field := range fields {
if val, err := toString(refs[i]); err == nil {
result[field] = any(val)
} else {
return nil, err
}
}
data = append(data, result)
}
return data, nil
}
// ToMap 用法
func (r *Rows) ToMap() (data []map[string]string, err error) {
if r.rows == nil {
return nil, r.err
}
defer func() {
if err := r.rows.Close(); err != nil {
logger.Logger.Error("Error closing rs: ", err)
}
}()
fields, err := r.rows.Columns()
if err != nil {
r.err = err
return nil, err
}
data = make([]map[string]string, 0)
num := len(fields)
refs := make([]any, num)
for i := 0; i < num; i++ {
var ref any
refs[i] = &ref
}
for r.rows.Next() {
result := make(map[string]string)
if err := r.rows.Scan(refs...); err != nil {
return nil, err
}
for i, field := range fields {
if val, err := toString(refs[i]); err == nil {
result[field] = val
} else {
return nil, err
}
}
data = append(data, result)
}
return data, nil
}
// ToStruct 用法
func (r *Rows) ToStruct(st any) error {
if r.rows == nil {
return r.err
}
defer func() {
if err := r.rows.Close(); err != nil {
logger.Logger.Error("Error closing rs: ", err)
}
}()
//st->&[]user
//获取变量的类型,类型为指针
stType := reflect.TypeOf(st)
//获取变量的值
stVal := reflect.ValueOf(st)
stValInd := reflect.Indirect(stVal)
//1.参数必须是指针
if stType.Kind() != reflect.Ptr {
return fmt.Errorf("the variable type is %v, not a pointer", stType.Kind())
}
//指针指向的类型:slice
stTypeInd := stType.Elem()
//2.传入的类型必须是slice,slice的成员类型必须是struct
if stTypeInd.Kind() != reflect.Slice || stTypeInd.Elem().Kind() != reflect.Struct {
return fmt.Errorf("the variable type is %v, not a slice struct", stType.Elem().Kind())
}
//初始化struct
v := reflect.New(stTypeInd.Elem())
//提取结构体中的tag
tagList, err := extractTagInfo(v)
if err != nil {
return err
}
fields, err := r.rows.Columns()
if err != nil {
r.err = err
return err
}
refs := make([]any, len(fields))
for i, field := range fields {
//如果对应的字段在结构体中有映射,则使用结构体成员变量的地址
if f, ok := tagList[field]; ok {
refs[i] = f.Addr().Interface()
} else {
refs[i] = new(any)
}
}
for r.rows.Next() {
if err := r.rows.Scan(refs...); err != nil {
return err
}
stValInd = reflect.Append(stValInd, v.Elem())
}
stVal.Elem().Set(stValInd)
return nil
}