-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.go
More file actions
116 lines (103 loc) · 2.59 KB
/
schema.go
File metadata and controls
116 lines (103 loc) · 2.59 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
package session
import (
"go/ast"
"reflect"
"strings"
"sync"
)
//struct 标签解析结果
type Filed struct {
Name string // 字段名
i int // 位置
Type string // 字段类型
TableColumn string // 对应数据库表列名
Tag string // 约束条件
}
// Schema 主要包含被映射的字段(Fields)
type Schema struct {
Fields []*Filed // 字段属性组合
FieldNames []string // 字段名称
FieldMap map[string]*Filed // key:字段名 value:字段属性
}
var (
structMutex sync.RWMutex
structCache = make(map[reflect.Type]*Schema)
)
// 对象与表结构转换
func StructForType(t reflect.Type) *Schema {
// step1: 缓存获取
structMutex.RLock()
st, found := structCache[t]
structMutex.RUnlock()
if found {
return st
}
structMutex.Lock()
defer structMutex.Unlock()
st, found = structCache[t]
if found {
return st
}
// step2: 对象关系映射
st = &Schema{FieldMap: make(map[string]*Filed)}
dataTypeOf(t, st)
// step3: 缓存
structCache[t] = st
return st
}
// 对象与表结构转换(实际工作函数)
func dataTypeOf(types reflect.Type, schema *Schema) {
// 遍历所有字段
for i := 0; i < types.NumField(); i++ {
p := types.Field(i)
// 忽略匿名字段和私有字段
if p.Anonymous || !ast.IsExported(p.Name) {
continue
}
field := &Filed{
Name: p.Name,
i: i,
}
var tag = field.Name
field.TableColumn = field.Name
if tg, ok := p.Tag.Lookup("torm"); ok {
tag = tg
}
// 获得额外约束条件
tagArr := strings.Split(tag, ",")
if len(tagArr) > 0 {
if tagArr[0] == "-" {
continue
}
// 数据库中对应列表名称
if len(tagArr[0]) > 0 {
field.TableColumn = tagArr[0]
}
// 数据库中对应列表类型
if len(tagArr) > 1 && len(tagArr[1]) > 0 {
field.Type = tagArr[1]
}
}
// 存储所有字段信息
schema.Fields = append(schema.Fields, field)
schema.FieldMap[p.Name] = field
// 存储所有字段名称
schema.FieldNames = append(schema.FieldNames, p.Name)
}
}
func (s *Schema) RecordValues(dest interface{}) []interface{} {
destValue := reflect.Indirect(reflect.ValueOf(dest))
var fieldValues []interface{}
for _, field := range s.Fields {
fieldValues = append(fieldValues, destValue.FieldByName(field.Name).Interface())
}
return fieldValues
}
func (s *Schema) UpdateParam(dest interface{}) map[string]interface{} {
destValue := reflect.Indirect(reflect.ValueOf(dest))
m := make(map[string]interface{})
for _, field := range s.Fields {
m[field.TableColumn] = destValue.FieldByName(field.Name).Interface()
}
return m
}