-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
reflect.go
231 lines (190 loc) · 4.83 KB
/
reflect.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
package service
import (
"context"
"reflect"
"strings"
"github.com/Masterminds/squirrel"
"github.com/fatih/camelcase"
gopluralize "github.com/gertd/go-pluralize"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
)
var pluralize = gopluralize.NewClient()
func tableNameOf(record interface{}) string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
tableName := strings.Join(camelcase.Split(typeName), "_")
tableName = strings.ToLower(tableName)
return pluralize.Plural(tableName)
}
func placeholdersOf(record interface{}) []string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
return nil
}
var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" {
continue
}
dbFields = append(dbFields, ":"+tag)
}
}
return dbFields
}
func fieldsNamesOf(record interface{}) []string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
return nil
}
var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" {
continue
}
dbFields = append(dbFields, tag)
}
}
return dbFields
}
func ParseStructTag(s string) (string, map[string]string) {
opts := make(map[string]string)
ss := strings.Split(s, ",")
if len(ss) > 1 {
for _, opt := range ss[1:] {
aa := strings.SplitN(opt, "=", 2)
if len(aa) == 2 {
opts[aa[0]] = aa[1]
} else {
opts[aa[0]] = ""
}
}
}
return ss[0], opts
}
type ReflectCache struct {
tableNames map[string]string
fields map[string][]string
placeholders map[string][]string
insertSqls map[string]string
}
func NewReflectCache() *ReflectCache {
return &ReflectCache{
tableNames: make(map[string]string),
fields: make(map[string][]string),
placeholders: make(map[string][]string),
insertSqls: make(map[string]string),
}
}
func (c *ReflectCache) InsertSqlOf(t interface{}) string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
sql, ok := c.insertSqls[typeName]
if ok {
return sql
}
tableName := dbCache.TableNameOf(t)
fields := dbCache.FieldsOf(t)
placeholders := dbCache.PlaceholderOf(t)
fieldClause := strings.Join(fields, ", ")
placeholderClause := strings.Join(placeholders, ", ")
sql = `INSERT INTO ` + tableName + ` (` + fieldClause + `) VALUES (` + placeholderClause + `)`
c.insertSqls[typeName] = sql
return sql
}
func (c *ReflectCache) TableNameOf(t interface{}) string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
tableName, ok := c.tableNames[typeName]
if ok {
return tableName
}
tableName = tableNameOf(t)
c.tableNames[typeName] = tableName
return tableName
}
func (c *ReflectCache) PlaceholderOf(t interface{}) []string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
placeholders, ok := c.placeholders[typeName]
if ok {
return placeholders
}
placeholders = placeholdersOf(t)
c.placeholders[typeName] = placeholders
return placeholders
}
func (c *ReflectCache) FieldsOf(t interface{}) []string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
fields, ok := c.fields[typeName]
if ok {
return fields
}
fields = fieldsNamesOf(t)
c.fields[typeName] = fields
return fields
}
// scanRowsOfType use the given type to scan rows
// this is usually slower than the native one since it uses reflect.
func scanRowsOfType(rows *sqlx.Rows, tpe interface{}) (interface{}, error) {
refType := reflect.TypeOf(tpe)
if refType.Kind() == reflect.Ptr {
refType = refType.Elem()
}
sliceRef := reflect.MakeSlice(reflect.SliceOf(refType), 0, 100)
// sliceRef := reflect.New(reflect.SliceOf(refType))
for rows.Next() {
var recordRef = reflect.New(refType)
var record = recordRef.Interface()
if err := rows.StructScan(record); err != nil {
return sliceRef.Interface(), err
}
sliceRef = reflect.Append(sliceRef, recordRef.Elem())
}
return sliceRef.Interface(), rows.Err()
}
func insertType(db *sqlx.DB, record interface{}) error {
sql := dbCache.InsertSqlOf(record)
_, err := db.NamedExec(sql, record)
return err
}
func selectAndScanType(ctx context.Context, db *sqlx.DB, sel squirrel.SelectBuilder, tpe interface{}) (interface{}, error) {
sql, args, err := sel.ToSql()
if err != nil {
return nil, err
}
logrus.Debugf("selectAndScanType: %T <- %s", tpe, sql)
logrus.Debugf("queryArgs: %v", args)
rows, err := db.QueryxContext(ctx, sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanRowsOfType(rows, tpe)
}