-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneric.go
214 lines (188 loc) · 4.35 KB
/
generic.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
package lorm
import (
"database/sql"
"log"
"sync"
"github.com/bradleypeabody/gouuidv6"
"github.com/pkg/errors"
"github.com/tada-team/lorm/op"
)
func UUID() string { return gouuidv6.New().String() }
func DoPks[PK iPK, R interface{ Pk() PK }](l []R) []PK {
pks := make([]PK, len(l))
for i, r := range l {
pks[i] = r.Pk()
}
return pks
}
func DoCount(f Filter, table op.Table) int {
var res int
if !f.IsEmpty() {
query := op.Select(op.Count(table.Pk())).From(table).Where(f.GetConds())
err := TxScan(f.Tx(), nil, query, f.GetArgs(), &res)
if err != nil {
log.Panicln(errors.Wrapf(err, "%s.Count() fail on %s and %v", table, query, f.GetArgs()))
}
}
return res
}
func DoDeleteFiltered(f Filter, table op.Table) error {
if !f.IsEmpty() {
query := op.Delete(table).Where(f.GetConds())
if _, err := TxExec(f.Tx(), nil, query, f.GetArgs()); err != nil {
return err
}
}
return nil
}
func DoExists(f Filter, table op.Table) bool {
var res bool
if !f.IsEmpty() {
query := op.RawQuery("SELECT", op.Exists(op.Select(op.One).From(table).Where(f.GetConds())))
err := TxScan(f.Tx(), nil, query, f.GetArgs(), &res)
if err != nil {
log.Panicln(errors.Wrapf(err, "%s.Exists() fail on %s and %v", table, query, f.GetArgs()))
}
}
return res
}
func DoSave(r Record, t op.Table) error {
if r.HasPk() {
return DoUpdate(r, t)
}
return DoInsert(r, t)
}
func DoUpdate(r Record, t op.Table) error {
if err := r.PreSave(); err != nil {
return err
}
values := r.GetAllFields()
kv := make(op.Set, len(values)-1)
args := op.NewArgs()
for i, f := range t.GetAllFields() {
if f.BareName() != t.Pk().BareName() {
kv[f] = args.Next(values[i])
}
}
query := op.Update(t, kv).Where(r.PkCond(&args))
if _, err := TxExec(r.Tx(), r, query, args); err != nil {
return err
}
if err := r.PostSave(); err != nil {
return err
}
return nil
}
func DoInsert(r Record, t op.Table) error {
if err := r.PreSave(); err != nil {
return err
}
if !r.HasPk() {
r.NewPk() // uuid or other custom type generation
}
values := r.GetAllFields()
kv := make(op.Set, len(values))
args := op.NewArgs()
pkName := t.Pk().BareName()
pkIdx := 0
for i, f := range t.GetAllFields() {
if f.BareName() == pkName {
pkIdx = i
if isEmpty(values[i]) {
continue
}
}
kv[f] = args.Next(values[i])
}
query := op.Insert(t, kv).Returning(pkName)
if err := TxScan(r.Tx(), r, query, args, values[pkIdx]); err != nil {
return err
}
if !r.HasPk() {
return errors.New("programming error: no pk after insert")
}
if err := r.PostSave(); err != nil {
return err
}
return nil
}
func DoInTx(r Record, fn func() error) error {
if tx := r.Tx(); tx != nil {
return tx.OnSuccess(fn)
}
return fn()
}
func DoListTx(l List) *Tx {
byTx := make(map[*Tx]struct{}, 1)
for _, r := range l.Records() {
byTx[r.Tx()] = struct{}{}
}
if len(byTx) > 1 {
log.Panicln("invalid transaction number:", len(byTx))
}
for tx := range byTx {
return tx
}
return nil
}
func DoSetListTx(tx *Tx, l List) {
for _, r := range l.Records() {
r.SetTx(tx)
}
}
func DoDelete(r Record, t op.Table) error {
args := op.NewArgs()
query := op.Delete(t).Where(r.PkCond(&args))
_, err := TxExec(r.Tx(), nil, query, args)
return err
}
func DoGet(f Filter, r Record, t op.Table) (bool, error) {
if f.IsEmpty() {
return false, nil
}
query := CachedSelect(t).Where(f.GetConds()).Lock(f.GetLock()).OrderBy(f.GetOrderBy())
err := TxScan(f.Tx(), r, query, f.GetArgs(), r.GetAllFields()...)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
MaybeAddToTx(f.Tx(), r)
return true, nil
}
var ReloadError = errors.New("lorm: reload error")
func DoReload(r Record, t op.Table) error {
args := op.NewArgs()
query := CachedSelect(t).Where(r.PkCond(&args))
err := TxScan(r.Tx(), r, query, args, r.GetAllFields()...)
if err == sql.ErrNoRows {
return ReloadError
}
return err
}
type HasPk interface {
HasPk() bool
}
func MustHavePk(r HasPk) {
if !r.HasPk() {
log.Panicln("must have primary key")
}
}
var (
selectCache = make(map[string]*op.SelectQuery)
selectCacheMux sync.RWMutex
)
func CachedSelect(t op.Table) *op.SelectQuery {
selectCacheMux.RLock()
sel := selectCache[t.String()]
selectCacheMux.RUnlock()
if sel != nil {
return sel
}
v := op.Select().From(t)
selectCacheMux.Lock()
selectCache[t.String()] = &v
selectCacheMux.Unlock()
return &v
}