-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_repo.go
354 lines (313 loc) · 7.73 KB
/
model_repo.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package model
import (
"database/sql"
. "github.com/yang-zzhong/go-querybuilder"
)
type rowshandler func(*sql.Rows, []string) error
type handlerForQueryModel func(m interface{}, pk interface{}) error
// oncreate and onupdate callback type
type modify func(model interface{}) error
type setpage func(*Repo) error
const (
t_one = 1 // relationship is a has one relationship
t_many = 2 // relationship is a has many relationship
t_bad = 3 // bad or not found relationship
)
// a relationship with repo
type with struct {
name string // relationship name
m interface{} // relationship target
n Nexus // relationship nexus
t int // relationship type t_one|t_many
handler repoHandler
}
// repo
type Repo struct {
model interface{} // repo row model
modifier Modifier // sql modifier
oncreate modify // on create callback
onupdate modify // on update callback
ondelete modify // on delete callback
withs []with // maintain fetch model relationship
*Builder
}
// new custom repo
func NewRepo(m interface{}, p Modifier) *Repo {
repo := new(Repo)
repo.model = m
repo.modifier = p
repo.oncreate = func(_ interface{}) error { return nil }
repo.onupdate = func(_ interface{}) error { return nil }
repo.ondelete = func(_ interface{}) error { return nil }
repo.Builder = NewBuilder(p)
repo.withs = []with{}
repo.From(repo.model.(Model).TableName())
return repo
}
func (repo *Repo) Another() *Repo {
r := new(Repo)
r.model = repo.model
r.modifier = repo.modifier
r.oncreate = repo.oncreate
r.onupdate = repo.onupdate
r.ondelete = repo.ondelete
r.Builder = NewBuilder(r.modifier)
r.withs = []with{}
r.From(r.model.(Model).TableName())
return r
}
// set on create callback
func (repo *Repo) OnCreate(c modify) *Repo {
repo.oncreate = c
return repo
}
// set on update callback
func (repo *Repo) OnUpdate(c modify) *Repo {
repo.onupdate = c
return repo
}
// set on update callback
func (repo *Repo) OnDelete(c modify) *Repo {
repo.ondelete = c
return repo
}
// clean builder
func (repo *Repo) Clean() {
repo.Builder.Init()
}
func (repo *Repo) Count() (int, error) {
db := repo.model.(Model).DB()
rows, err := db.Query(repo.ForCount(), repo.Params()...)
if err != nil {
return 0, err
}
defer rows.Close()
var count int
for rows.Next() {
if err = rows.Scan(&count); err != nil {
return 0, err
}
}
return count, nil
}
func (repo *Repo) MustCount() int {
if count, err := repo.Count(); err != nil {
panic(err)
} else {
return count
}
}
func (repo *Repo) Query(handle rowshandler) error {
db := repo.model.(Model).DB()
rows, err := db.Query(repo.ForQuery(), repo.Params()...)
if err != nil {
return err
}
defer rows.Close()
var cols []string
if cols, err = rows.Columns(); err != nil {
return err
}
for rows.Next() {
if err := handle(rows, cols); err != nil {
return err
}
}
return nil
}
func (repo *Repo) MustFetch() []interface{} {
if ms, err := repo.Fetch(); err != nil {
panic(err)
} else {
return ms
}
}
func (repo *Repo) Fetch() (models []interface{}, err error) {
err = repo.fetch(func(m interface{}, _ interface{}) error {
models = append(models, m)
return nil
})
if err != nil {
return
}
if nexusValues, rerr := repo.nexusValues(models); rerr == nil {
for id, _ := range models {
repo.bindNexus(models[id], nexusValues)
}
} else {
err = rerr
}
return
}
func (repo *Repo) MustFetchKey(col string) map[interface{}]interface{} {
if ms, err := repo.FetchKey(col); err != nil {
panic(err)
} else {
return ms
}
}
func (repo *Repo) FetchKey(col string) (models map[interface{}]interface{}, err error) {
models = make(map[interface{}]interface{})
forNexusValues := []interface{}{}
err = repo.fetch(func(m interface{}, id interface{}) error {
models[id] = m
forNexusValues = append(forNexusValues, m)
return nil
})
if err != nil {
return
}
if nexusValues, rerr := repo.nexusValues(forNexusValues); rerr == nil {
for id, _ := range models {
repo.bindNexus(models[id], nexusValues)
}
} else {
err = rerr
}
return
}
func (repo *Repo) fetch(handle handlerForQueryModel) error {
var cols []interface{}
colget := false
return repo.Query(func(rows *sql.Rows, columns []string) error {
var err error
if !colget {
if cols, err = repo.model.(Mapable).Mapper().cols(columns); err != nil {
return err
}
colget = true
}
if err = rows.Scan(cols...); err != nil {
return &Error{ERR_SCAN, err}
}
var m, id interface{}
m, id, err = repo.model.(Mapable).Mapper().pack(columns, cols, repo.model.(Model).PK())
if err == nil {
return handle(m, id)
}
return err
})
}
func (repo *Repo) MustOne() interface{} {
if m, _, err := repo.One(); err != nil {
panic(err)
} else {
return m
}
}
func (repo *Repo) One() (interface{}, bool, error) {
if rows, err := repo.Fetch(); err != nil {
return nil, false, err
} else {
for _, row := range rows {
return row, true, nil
}
}
return nil, false, nil
}
func (repo *Repo) MustFind(id interface{}) interface{} {
if m, _, err := repo.Find(id); err != nil {
panic(err)
} else {
return m
}
}
func (repo *Repo) Find(id interface{}) (interface{}, bool, error) {
r := repo.Another()
pk := repo.model.(Model).PK()
r.Where(pk, id).Limit(1)
if rows, err := r.Fetch(); err != nil {
return nil, false, err
} else {
for _, row := range rows {
return row, true, nil
}
}
return nil, false, nil
}
func (repo *Repo) UpdateRaw(raw map[string]interface{}) error {
db := repo.model.(Model).DB()
_, err := db.Exec(repo.ForUpdate(raw), repo.Params()...)
return err
}
func (repo *Repo) DeleteRaw(raw map[string]interface{}) error {
db := repo.model.(Model).DB()
_, err := db.Exec(repo.ForRemove(), repo.Params()...)
return err
}
func (repo *Repo) Update(model interface{}) error {
field := repo.model.(Model).PK()
v, err := repo.model.(Mapable).Mapper().colValue(model, field)
if err != nil {
return err
}
if err := repo.onupdate(model); err != nil {
return err
}
r := repo.Another()
sql := r.Where(field, v).ForUpdate(repo.model.(Mapable).Mapper().extract(model))
db := repo.model.(Model).DB()
_, err = db.Exec(sql, r.Params()...)
return err
}
func (repo *Repo) Creates(models []interface{}) error {
r := repo.Another()
var data []map[string]interface{}
for _, m := range models {
if err := r.oncreate(m); err != nil {
return err
}
data = append(data, r.model.(Mapable).Mapper().extract(m))
}
db := repo.model.(Model).DB()
if _, err := db.Exec(r.ForInsert(data), r.Params()...); err != nil {
return err
}
for _, m := range models {
m.(Model).SetFresh(false)
}
return nil
}
func (repo *Repo) Create(model interface{}) error {
var data []map[string]interface{}
if err := repo.oncreate(model); err != nil {
return err
}
data = append(data, repo.model.(Mapable).Mapper().extract(model))
r := repo.Another()
db := repo.model.(Model).DB()
if _, err := db.Exec(r.ForInsert(data), r.Params()...); err != nil {
return err
}
model.(Model).SetFresh(false)
return nil
}
func (repo *Repo) Delete(model interface{}) error {
if err := repo.ondelete(model); err != nil {
return err
}
field := repo.model.(Model).PK()
v, err := repo.model.(Mapable).Mapper().colValue(model, field)
if err != nil {
return err
}
r := repo.Another()
db := repo.model.(Model).DB()
_, err = db.Exec(r.Where(field, v).ForRemove(), r.Params()...)
return err
}
func (repo *Repo) Deletes(models []interface{}) error {
field := repo.model.(Model).PK()
ids := []interface{}{}
for _, model := range models {
v, err := repo.model.(Mapable).Mapper().colValue(model, field)
if err != nil {
return err
}
ids = append(ids, v)
}
r := repo.Another()
db := repo.model.(Model).DB()
_, err := db.Exec(r.WhereIn(field, ids).ForRemove(), r.Params()...)
return err
}