-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema.go
247 lines (216 loc) · 4.25 KB
/
schema.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
237
238
239
240
241
242
243
244
245
246
247
package y
import (
"log"
"reflect"
"strings"
"sync"
)
const _version = "_version"
type field struct {
reflect.StructField
autoincr bool
}
type xinfo struct {
pk []string
idx map[string]int
}
func (x *xinfo) addpk(col string) {
x.pk = append(x.pk, col)
if len(x.pk) == 1 {
x.addx(col)
}
}
func (x *xinfo) addx(col string) {
x.idx[col] = 1
}
func newxinfo() *xinfo {
return &xinfo{
idx: make(map[string]int),
}
}
type fseq []string
func (f fseq) alias(s string) []string {
a := make([]string, len(f))
for i, v := range f {
a[i] = s + "." + v
}
return a
}
type fkopt struct {
target string
from string
}
func (f fkopt) flip() fkopt {
return fkopt{f.from, f.target}
}
type schema struct {
t reflect.Type
table string
fields map[string]field
fseq fseq
xinfo *xinfo
fks map[string]fkopt
}
func (s *schema) parseField(r reflect.StructField, xopts []string) field {
f := field{StructField: r}
for _, opt := range xopts {
switch opt {
// parse lonely options
case "pk":
s.xinfo.addpk(xopts[0])
case "autoincr":
f.autoincr = true
// parse extended options
default:
ext := strings.Split(opt, ":")
switch ext[0] {
case "fk":
var (
fk string
fkopts []string
)
// has explicit fk?
if len(ext) > 1 {
fk = ext[1]
fkopts = strings.Split(fk, ".")
} else {
fk = xopts[0]
fkopts = strings.Split(fk, "_")
}
if len(fkopts) != 2 {
log.Panicf("y/schema: Couldn't parse foreign key from \"%s\".", fk)
}
s.fks[fkopts[0]] = fkopt{
target: fkopts[1],
from: xopts[0],
}
s.xinfo.addx(xopts[0])
}
}
}
return f
}
func (s *schema) parseName(t reflect.Type) {
s.table = underscore(t.Name())
}
func (s *schema) parseFields(t reflect.Type) {
for i, l := 0, t.NumField(); i < l; i++ {
f := t.Field(i)
col := f.Tag.Get("y")
if col == "-" {
continue
}
if f.Anonymous {
if f.Type.Kind() != reflect.Struct {
panic("y/schema: Y supports embedded struct only.")
}
s.parseFields(f.Type)
continue
}
xopts := strings.Split(col, ",")
if xopts[0] == "" {
xopts[0] = underscore(f.Name)
}
s.fseq = append(s.fseq, xopts[0])
s.fields[xopts[0]] = s.parseField(f, xopts)
}
}
func (s *schema) ptrs() []interface{} {
return make([]interface{}, len(s.fseq))
}
func (s *schema) set(ptrs []interface{}, v value) {
for i, col := range s.fseq {
x := v.field(s.fields[col].Name).Addr()
ptrs[i] = x.Interface()
}
}
func (s *schema) create() value {
return valueOf(reflect.New(s.t))
}
func (s *schema) parse() {
s.parseName(s.t)
s.parseFields(s.t)
}
func (s *schema) fk(in schema) fkopt {
// forward
fk, ok := s.fks[in.table]
if ok {
return fk
}
// reverse
fk, ok = in.fks[s.table]
if !ok {
log.Panicf(
"y/schema: The foreign key between \"%s\" and \"%s\" not found",
s.table, in.table)
}
return fk.flip()
}
func (s *schema) field(name string) field {
f, found := s.fields[name]
if !found {
log.Panicf(
"y/schema: The field \"%s\" not found in table \"%s\".",
name, s.table)
}
return f
}
func (s *schema) fval(v value, name string) reflect.Value {
f := s.field(name)
return v.field(f.Name)
}
func (s *schema) pk(v value) Values {
pks := Values{}
for _, pk := range s.xinfo.pk {
pks[pk] = s.fval(v, pk).Interface()
}
if len(pks) == 0 {
log.Panicf(
"y/schema: No primary key found in the \"%s\".",
s.table)
}
return pks
}
func (s *schema) mapped(v value) Values {
values := make(Values, len(s.fseq))
for _, name := range s.fseq {
values[name] = s.fval(v, name).Interface()
}
return values
}
func (s *schema) sliceOf() reflect.Type {
return reflect.SliceOf(reflect.PtrTo(s.t))
}
func makeSchema(t reflect.Type) schema {
s := schema{
t: t,
fields: make(map[string]field),
xinfo: newxinfo(),
fks: make(map[string]fkopt),
}
s.parse()
return s
}
type cache struct {
types map[reflect.Type]schema
sync.RWMutex
}
var loaded = cache{
types: make(map[reflect.Type]schema),
}
func schemaOf(t reflect.Type) schema {
if t.Kind() != reflect.Struct {
log.Panicln("y/schema: Y supports Struct type only.")
}
loaded.RLock()
s, found := loaded.types[t]
loaded.RUnlock()
if found {
return s
}
loaded.Lock()
defer loaded.Unlock()
s = makeSchema(t)
loaded.types[t] = s
return s
}