-
Notifications
You must be signed in to change notification settings - Fork 2
/
repo.go
183 lines (165 loc) · 5.04 KB
/
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
package repo
import (
"context"
"errors"
"fmt"
"strings"
"github.com/go-kratos/kratos/v2/log"
"github.com/jinzhu/copier"
"github.com/yrcs/yuneto/pkg/util"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Repo base repository.
type Repo[E, T any] interface {
// Exists determines whether po exists (whether to ignore case).
Exists(ctx context.Context, fieldName string, fieldValue any, ignoreCase ...bool) (bool, error)
// Unique determines whether po is unique (whether to ignore case).
Unique(ctx context.Context, id uint64, fieldName string, fieldValue any, ignoreCase ...bool) (bool, error)
// Find find po by id (with lockMode).
Find(ctx context.Context, id uint64, lockMode ...LockMode) (E, error)
// FindByField find po by fieldName and fieldValue (with ignoreCase and/or lockMode).
FindByField(ctx context.Context, fieldName string, fieldValue any, args ...any) (E, error)
// Create create po.
Create(ctx context.Context, do E) (E, error)
// Update update po.
Update(ctx context.Context, m map[string]any) (E, error)
// Delete delete po.
Delete(ctx context.Context, id uint64) error
// DeleteByIDs delete pos.
DeleteByIDs(ctx context.Context, ids []uint64) error
}
type BaseRepo[E, T any] struct {
DO E
PO T
DB *gorm.DB
Log *log.Helper
}
var _ Repo[*any, *any] = (*BaseRepo[*any, *any])(nil)
func (r *BaseRepo[E, T]) Exists(ctx context.Context, fieldName string, fieldValue any, ignoreCase ...bool) (bool, error) {
db := r.DB.WithContext(ctx)
var ignored bool
for _, v := range ignoreCase {
ignored = v
break
}
var result *gorm.DB
if ignored {
result = db.Where("LOWER("+fieldName+") = ?", strings.ToLower(fmt.Sprintf("%v", fieldValue))).First(&r.PO)
} else {
result = db.Where(map[string]any{fieldName: fieldValue}).First(&r.PO)
}
if err := result.Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return true, err
}
return result.RowsAffected > 0, nil
}
func (r *BaseRepo[E, T]) Unique(ctx context.Context, id uint64, fieldName string, fieldValue any, ignoreCase ...bool) (bool, error) {
db := r.DB.WithContext(ctx)
var ignored bool
for _, v := range ignoreCase {
ignored = v
break
}
var result *gorm.DB
if ignored {
result = db.Where("id <> ?", id).Where("LOWER("+fieldName+") = ?", strings.ToLower(fmt.Sprintf("%v", fieldValue))).First(&r.PO)
} else {
result = db.Where("id <> ?", id).Where(map[string]any{fieldName: fieldValue}).First(&r.PO)
}
if err := result.Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return true, nil
}
return false, err
}
return result.RowsAffected <= 0, nil
}
func (r *BaseRepo[E, T]) Find(ctx context.Context, id uint64, lockMode ...LockMode) (E, error) {
db := r.DB.WithContext(ctx)
var mode *LockMode
for _, v := range lockMode {
mode = &v
break
}
var result *gorm.DB
if mode != nil {
result = db.Clauses(clause.Locking{Strength: mode.String()}).First(&r.PO, id)
} else {
result = db.First(&r.PO, id)
}
if err := result.Error; err != nil {
var zero E
if errors.Is(err, gorm.ErrRecordNotFound) {
return zero, nil
}
return zero, err
}
r.DO = util.InitStruct(r.DO)
copier.CopyWithOption(r.DO, r.PO, copier.Option{IgnoreEmpty: true, DeepCopy: true})
return r.DO, nil
}
func (r *BaseRepo[E, T]) FindByField(ctx context.Context, fieldName string, fieldValue any, args ...any) (E, error) {
db := r.DB.WithContext(ctx)
var ignoreCase bool
var lockMode *LockMode
for i, v := range args {
if i > 1 {
break
}
switch v := v.(type) {
case bool:
ignoreCase = v
case LockMode:
lockMode = &v
}
}
if ignoreCase {
db = db.Where("LOWER("+fieldName+") = ?", strings.ToLower(fmt.Sprintf("%v", fieldValue)))
} else {
db = db.Where(map[string]any{fieldName: fieldValue})
}
if lockMode != nil {
db = db.Clauses(clause.Locking{Strength: lockMode.String()})
}
result := db.First(&r.PO)
if err := result.Error; err != nil {
var zero E
if errors.Is(err, gorm.ErrRecordNotFound) {
return zero, nil
}
return zero, err
}
r.DO = util.InitStruct(r.DO)
copier.CopyWithOption(r.DO, r.PO, copier.Option{IgnoreEmpty: true, DeepCopy: true})
return r.DO, nil
}
func (r *BaseRepo[E, T]) Create(ctx context.Context, do E) (E, error) {
return do, r.DB.WithContext(ctx).Create(&do).Error
}
func (r *BaseRepo[E, T]) Update(ctx context.Context, m map[string]any) (E, error) {
db := r.DB.WithContext(ctx)
result := db.Where("id = ?", m["Id"]).First(&r.PO)
if err := result.Error; err != nil {
var zero E
if errors.Is(err, gorm.ErrRecordNotFound) {
return zero, nil
}
return zero, err
}
result = r.DB.WithContext(ctx).Model(r.PO).Omit("Id").Updates(m)
r.DO = util.InitStruct(r.DO)
copier.CopyWithOption(r.DO, r.PO, copier.Option{IgnoreEmpty: true, DeepCopy: true})
return r.DO, result.Error
}
func (r *BaseRepo[E, T]) Delete(ctx context.Context, id uint64) error {
r.PO = util.InitStruct(r.PO)
return r.DB.WithContext(ctx).Delete(r.PO, id).Error
}
func (r *BaseRepo[E, T]) DeleteByIDs(ctx context.Context, ids []uint64) error {
r.PO = util.InitStruct(r.PO)
return r.DB.WithContext(ctx).Delete(r.PO, ids).Error
}