-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
240 lines (211 loc) · 4.3 KB
/
Copy pathquery.go
File metadata and controls
240 lines (211 loc) · 4.3 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
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
package sqlorm
import (
"sync"
"github.com/tinh-tinh/tinhtinh/v2/common"
"gorm.io/gorm"
)
type FindOneOptions struct {
Select []string
Order []string
WithDeleted bool
Related []string
Separate bool
}
type FindOptions struct {
Distinct []interface{}
Select []string
Order []string
WithDeleted bool
Limit int
Offset int
Related []string
Separate bool
}
func (repo *Repository[M]) FindAll(where Query, options ...FindOptions) ([]*M, error) {
var model []*M
tx := repo.DB
var opt FindOptions
if len(options) > 0 {
opt = common.MergeStruct(options...)
}
if len(opt.Related) > 0 {
for _, key := range opt.Related {
if opt.Separate {
tx = tx.Preload(key)
} else {
tx = tx.Joins(key)
}
}
}
if len(opt.Distinct) > 0 {
tx = tx.Distinct(opt.Distinct...)
}
if opt.Select != nil {
tx = tx.Select(opt.Select)
}
if opt.Order != nil {
for _, order := range opt.Order {
tx = tx.Order(order)
}
}
if opt.Limit > 0 {
tx = tx.Limit(opt.Limit)
}
if opt.Offset > 0 {
tx = tx.Offset(opt.Offset)
}
if opt.WithDeleted {
tx = tx.Unscoped()
}
if IsQueryBuilder(where) {
queryFnc, ok := where.(func(qb *QueryBuilder))
if ok {
qb := &QueryBuilder{qb: tx}
queryFnc(qb)
tx = qb.qb
}
} else {
tx = tx.Where(where)
}
result := tx.Find(&model)
if result.Error != nil {
return nil, result.Error
}
return model, nil
}
func (repo *Repository[M]) FindOne(where Query, options ...FindOneOptions) (*M, error) {
var model M
tx := repo.DB
var opt FindOneOptions
if len(options) > 0 {
opt = common.MergeStruct(options...)
}
if len(opt.Related) > 0 {
for _, key := range opt.Related {
if opt.Separate {
tx = tx.Preload(key)
} else {
tx = tx.Joins(key)
}
}
}
if opt.Select != nil {
tx = tx.Select(opt.Select)
}
if opt.Order != nil {
for _, order := range opt.Order {
tx = tx.Order(order)
}
}
if opt.WithDeleted {
tx = tx.Unscoped()
}
if IsQueryBuilder(where) {
queryFnc, ok := where.(func(qb *QueryBuilder))
if ok {
qb := &QueryBuilder{qb: tx}
queryFnc(qb)
tx = qb.qb
}
} else {
tx = tx.Where(where)
}
result := tx.First(&model)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, result.Error
}
return &model, nil
}
func (repo *Repository[M]) FindByID(id any, options ...FindOneOptions) (*M, error) {
return repo.FindOne(map[string]interface{}{"id": id}, options...)
}
func (repo *Repository[M]) Count(where interface{}, options ...FindOneOptions) (int64, error) {
var count int64
var model M
var opt FindOneOptions
if len(options) > 0 {
opt = common.MergeStruct(options...)
}
tx := repo.DB.Model(&model)
if opt.WithDeleted {
tx = tx.Unscoped()
}
if IsQueryBuilder(where) {
queryFnc, ok := where.(func(qb *QueryBuilder))
if ok {
qb := &QueryBuilder{qb: tx}
queryFnc(qb)
tx = qb.qb
}
} else {
tx = tx.Where(where)
}
result := tx.Count(&count)
if result.Error != nil {
return 0, result.Error
}
return count, nil
}
func (repo *Repository[M]) Exist(where Query, options ...FindOneOptions) (bool, error) {
var model M
tx := repo.DB
var opt FindOneOptions
if len(options) > 0 {
opt = common.MergeStruct(options...)
}
if opt.Select != nil {
tx = tx.Select(opt.Select)
}
if opt.Order != nil {
for _, order := range opt.Order {
tx = tx.Order(order)
}
}
if opt.WithDeleted {
tx = tx.Unscoped()
}
if IsQueryBuilder(where) {
queryFnc, ok := where.(func(qb *QueryBuilder))
if ok {
qb := &QueryBuilder{qb: tx}
queryFnc(qb)
tx = qb.qb
}
} else {
tx = tx.Where(where)
}
result := tx.First(&model)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return false, nil
}
return false, result.Error
}
return true, nil
}
func (repo *Repository[M]) FindAllAndCount(where Query, options ...FindOptions) ([]*M, int64, error) {
var wg sync.WaitGroup
var findAllRes []*M
var countRes int64
var findErr, countErr error
wg.Add(2)
go func() {
defer wg.Done()
findAllRes, findErr = repo.FindAll(where, options...)
}()
go func() {
defer wg.Done()
countRes, countErr = repo.Count(where)
}()
wg.Wait()
if findErr != nil {
return nil, 0, findErr
}
if countErr != nil {
return nil, 0, countErr
}
return findAllRes, countRes, nil
}