-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao.go
200 lines (179 loc) · 4.64 KB
/
dao.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
package database
import (
"context"
"github.com/pkg/errors"
"github.com/teablog/tea/internal/db"
"gorm.io/gorm"
)
type DAO struct {
}
func (dao *DAO) SelectByPage(ctx context.Context, list interface{}, searchOpt ...SessionOption) (int64, error) {
var err error
needCount := true
for _, opt := range searchOpt {
if opt.Type == sessionOptionNoCount {
needCount = false
}
}
count := int64(0)
if needCount {
session := db.DB.Session(&gorm.Session{})
for _, opt := range searchOpt {
session = processSessionOptionForCount(opt, session)
}
res := session.Count(&count)
if res.Error != nil {
return 0, errors.Wrapf(res.Error, "{{查询条数失败!}}")
}
}
session, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
for _, opt := range searchOpt {
session = processSessionOption(opt, session)
}
res := session.Find(list)
if res.Error != nil {
return 0, errors.Wrapf(res.Error, "{{查询失败}}")
}
return count, nil
}
// Select 单个查询尽量用get,因为select返回的不是nil 是id=0的对象
func (dao *DAO) Select(ctx context.Context, list interface{}, searchOpt ...SessionOption) error {
session, err := Session.getFromContext(ctx)
if err != nil {
return err
}
for _, opt := range searchOpt {
session = processSessionOption(opt, session)
}
res := session.Find(list)
if res.Error != nil {
return errors.Wrapf(res.Error, "{{查询失败}}")
}
return nil
}
func (dao *DAO) Get(ctx context.Context, row interface{}, searchOpt ...SessionOption) (bool, error) {
sess, err := Session.getFromContext(ctx)
if err != nil {
return false, err
}
for _, opt := range searchOpt {
sess = processSessionOption(opt, sess)
}
res := sess.Take(row)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, nil
}
return false, errors.Wrapf(err, "{{查询失败}}")
}
return true, nil
}
func (dao *DAO) Count(ctx context.Context, searchOpt ...SessionOption) (int64, error) {
sess, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
for _, opt := range searchOpt {
sess = processSessionOption(opt, sess)
}
count := int64(0)
res := sess.Count(&count)
if res.Error != nil {
return 0, errors.Wrapf(res.Error, "{{查询失败}}")
}
return count, nil
}
func (dao *DAO) Updates(ctx context.Context, data interface{}, searchOpt ...SessionOption) (int64, error) {
session, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
session = session.Model(data)
for _, opt := range searchOpt {
session = opt.Process(session)
}
res := session.Updates(data)
if res.Error != nil {
return 0, res.Error
}
return res.RowsAffected, res.Error
}
func (dao *DAO) Insert(ctx context.Context, data interface{}, searchOpt ...SessionOption) (int64, error) {
session, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
for _, opt := range searchOpt {
session = opt.Process(session)
}
res := session.Create(data)
if res.Error != nil {
return 0, res.Error
}
return res.RowsAffected, res.Error
}
func (dao *DAO) Delete(ctx context.Context, data interface{}, searchOpt ...SessionOption) (int64, error) {
session, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
for _, opt := range searchOpt {
session = opt.Process(session)
}
res := session.Delete(data)
return res.RowsAffected, res.Error
}
func (dao *DAO) Pluck(ctx context.Context, field string, data []interface{}, searchOpt ...SessionOption) error {
session, err := Session.getFromContext(ctx)
if err != nil {
return err
}
for _, opt := range searchOpt {
session = opt.Process(session)
}
res := session.Pluck(field, data)
return res.Error
}
func (dao *DAO) Save(ctx context.Context, data interface{}, searchOpt ...SessionOption) (int64, error) {
session, err := Session.getFromContext(ctx)
if err != nil {
return 0, err
}
for _, opt := range searchOpt {
session = opt.Process(session)
}
res := session.Create(data)
if res.Error != nil {
return 0, res.Error
}
return res.RowsAffected, res.Error
}
func (dao *DAO) CreateInBatches(ctx context.Context, data interface{}, options ...SessionOption) error {
session, err := Session.getFromContext(ctx)
if err != nil {
return err
}
for _, opt := range options {
session = opt.Process(session)
}
res := session.CreateInBatches(data, 100)
return res.Error
}
func (dao *DAO) Transaction(ctx context.Context, actions ...func(ctx context.Context) error) error {
sess, err := Session.getFromContext(ctx)
if err != nil {
return err
}
sess.Begin()
for _, act := range actions {
if actError := act(ctx); actError != nil {
sess.Rollback()
return actError
}
}
sess.Commit()
return nil
}