-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrows.go
More file actions
144 lines (113 loc) · 2.81 KB
/
rows.go
File metadata and controls
144 lines (113 loc) · 2.81 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
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
package sqlp
import (
"database/sql"
"strings"
er "github.com/kaboc/sqlp/errors"
ref "github.com/kaboc/sqlp/reflect"
)
type Rows struct {
Rows *sql.Rows
structPtr interface{}
dest []interface{}
columnNames []string
}
func (rs *Rows) Close() error {
return rs.Rows.Close()
}
func (rs *Rows) Columns() ([]string, error) {
return rs.Rows.Columns()
}
func (rs *Rows) ColumnTypes() ([]*columnTypes, error) {
return rs.Rows.ColumnTypes()
}
func (rs *Rows) Err() error {
return rs.Rows.Err()
}
func (rs *Rows) Next() bool {
return rs.Rows.Next()
}
func (rs *Rows) NextResultSet() bool {
return rs.Rows.NextResultSet()
}
func (rs *Rows) Scan(dest ...interface{}) error {
return rs.Rows.Scan(dest...)
}
func prepareScanToStruct(structPtr interface{}, columnNames []string) ([]interface{}, error) {
dest := make([]interface{}, len(columnNames))
structV := ref.PtrValueOf(structPtr)
fields := ref.GetStructFields(structV)
outerLoop:
for i, v := range columnNames {
for _, v2 := range fields {
if v == v2.Tag || (v2.Tag == v2.Name && strings.EqualFold(strings.Replace(v, "_", "", -1), strings.Replace(v2.Name, "_", "", -1))) {
if v2.Name != strings.Title(v2.Name) {
return nil, er.New("one or more destination struct fields are unexported")
}
dest[i] = structV.FieldByName(v2.Name).Addr().Interface()
continue outerLoop
}
}
return nil, er.Errorf("struct field for column '%s' is missing", v)
}
return dest, nil
}
func (rs *Rows) ScanToStruct(structPtr interface{}) error {
if structPtr != rs.structPtr {
if err := isStructPtr(structPtr); err != nil {
return err
}
columnNames, err := rs.Columns()
if err != nil {
return err
}
dest, err := prepareScanToStruct(structPtr, columnNames)
if err != nil {
return err
}
rs.structPtr = structPtr
rs.dest = dest
}
err := rs.Rows.Scan(rs.dest...)
if err != nil {
return err
}
return nil
}
func prepareScanToMap(columnNames []string) []interface{} {
columnLen := len(columnNames)
dest := make([]interface{}, columnLen)
v := make([]RawBytes, columnLen)
for i := range columnNames {
dest[i] = &v[i]
}
return dest
}
func (rs *Rows) ScanToMap() (map[string]string, error) {
if rs.columnNames == nil {
columnNames, err := rs.Columns()
if err != nil {
return nil, err
}
rs.columnNames = columnNames
rs.dest = prepareScanToMap(columnNames)
}
if err := rs.Scan(rs.dest...); err != nil {
return nil, err
}
result := make(map[string]string)
for i, v := range rs.columnNames {
result[v] = string(*((rs.dest)[i].(*RawBytes)))
}
return result, nil
}
func (rs *Rows) ScanToSlice() ([]string, error) {
mp, err := rs.ScanToMap()
if err != nil {
return nil, err
}
result := make([]string, len(rs.columnNames))
for i, v := range rs.columnNames {
result[i] = mp[v]
}
return result, nil
}