-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.go
224 lines (192 loc) · 4.58 KB
/
orm.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
package db
import (
"errors"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/ztrue/tracerr"
"gorm.io/gorm"
)
// Condition limit condition search, however, sqli is not protected.
//
// Unprotected fields: Order, Distinct, Where(when not use ? as argument form)
//
// Warning: Caller should check user input on their OWN!
type Condition struct {
Order []any
Distinct []any
Limit any
Offset any
Query string
Args []any
}
func (c *Condition) merge(in *Condition) {
c.Order = append(c.Order, in.Order...)
c.Distinct = append(c.Distinct, in.Distinct...)
if c.Limit == nil {
c.Limit = in.Limit
}
if c.Offset == nil {
c.Offset = in.Offset
}
}
func (c *Condition) MergeAnd(in *Condition) {
if in == nil {
return
}
c.merge(in)
c.parseQuery(false, in.Query, in.Args...)
}
func (c *Condition) MergeOr(in *Condition) {
if in == nil {
return
}
c.merge(in)
c.parseQuery(true, in.Query, in.Args...)
}
func (c *Condition) parseQuery(or bool, query string, args ...any) {
if query == "" {
return
}
if c.Query == "" {
c.Query = query
c.Args = args
} else {
if or {
c.Query = fmt.Sprintf("(%v) OR (%v)", c.Query, query)
} else {
c.Query = fmt.Sprintf("(%v) AND (%v)", c.Query, query)
}
c.Args = append(c.Args, args...)
}
}
func (c *Condition) AndLike(query string, arg string) {
if arg != "" {
c.And(query)
count := strings.Count(query, "?")
for i := 0; i < count; i++ {
c.Args = append(c.Args, "%"+arg+"%")
}
}
}
func (c *Condition) OrLike(query string, arg string) {
if arg != "" {
c.Or(query)
count := strings.Count(query, "?")
for i := 0; i < count; i++ {
c.Args = append(c.Args, "%"+arg+"%")
}
}
}
func (c *Condition) And(query string, args ...any) {
c.parseQuery(false, query, args...)
}
func (c *Condition) Or(query string, args ...any) {
c.parseQuery(true, query, args...)
}
// ParseCondition parse Condition to gorm.DB object.
func ParseCondition(cond *Condition, tx *gorm.DB) *gorm.DB {
if tx == nil {
tx = DB
}
if cond == nil {
return tx
}
for _, v := range cond.Order {
tx = tx.Order(v)
}
if len(cond.Distinct) != 0 {
tx = tx.Distinct(cond.Distinct...)
}
if cond.Limit != nil {
tx = tx.Limit(cond.Limit.(int))
}
if cond.Offset != nil {
tx = tx.Offset(cond.Offset.(int))
}
tx = tx.Where(cond.Query, cond.Args...)
return tx
}
// TODO: change when gorm support generic
type ORM[T DBStruct] struct {
tx *gorm.DB
}
func NewORM[T DBStruct](tx *gorm.DB) *ORM[T] {
if tx == nil {
tx = DB
}
return &ORM[T]{
tx: tx,
}
}
func (o *ORM[T]) Where(query any, args ...any) *ORM[T] {
return NewORM[T](o.tx.Where(query, args...))
}
func (o *ORM[T]) Joins(query string, args ...any) *ORM[T] {
return NewORM[T](o.tx.Joins(query, args...))
}
func (o *ORM[T]) ID(id uuid.UUID) *ORM[T] {
return NewORM[T](o.tx.Where("id = ?", id))
}
func (o *ORM[T]) Save(value *T) error {
return tracerr.Wrap(o.tx.Save(value).Error)
}
func (o *ORM[T]) Updates(column []string, value *T) error {
if column == nil {
return tracerr.Wrap(o.tx.Model(new(T)).Updates(value).Error)
} else {
return tracerr.Wrap(o.tx.Model(new(T)).Select(column).Updates(value).Error)
}
}
func (o *ORM[T]) Update(column string, value any) error {
return tracerr.Wrap(o.tx.Model(new(T)).Update(column, value).Error)
}
func (o *ORM[T]) Delete(conds ...any) (int64, error) {
ret := o.tx.Delete(new(T), conds...)
return ret.RowsAffected, tracerr.Wrap(ret.Error)
}
func (o *ORM[T]) DeleteID(id uuid.UUID) (bool, error) {
ret := o.tx.Delete(new(T), id)
return ret.RowsAffected == 1, tracerr.Wrap(ret.Error)
}
func (o *ORM[T]) DeleteAll() (int64, error) {
ret := o.tx.Where("1 = 1").Delete(new(T))
return ret.RowsAffected, tracerr.Wrap(ret.Error)
}
func (o *ORM[T]) Find(conds ...any) (ret []*T, err error) {
err = tracerr.Wrap(o.tx.Find(&ret, conds...).Error)
return
}
func (o *ORM[T]) Creates(value []*T) error {
return tracerr.Wrap(o.tx.Create(&value).Error)
}
func (o *ORM[T]) Create(value *T) error {
return tracerr.Wrap(o.tx.Create(value).Error)
}
func (o *ORM[T]) Cond(cond *Condition) *ORM[T] {
return NewORM[T](ParseCondition(cond, o.tx))
}
func (o *ORM[T]) Count(cond *Condition) (int64, error) {
var count int64
if err := tracerr.Wrap(ParseCondition(cond, o.tx.Model(new(T))).
Count(&count).Error); err != nil {
return 0, err
}
return count, nil
}
func (o *ORM[T]) Take(conds ...any) (ret *T, err error) {
ret = new(T)
err = tracerr.Wrap(o.tx.Take(ret, conds...).Error)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ret = nil
err = nil
} else {
ret = nil
}
}
return
}
func (o *ORM[T]) TX() *gorm.DB {
return o.tx
}