-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.mysql.go
154 lines (132 loc) · 3.77 KB
/
.mysql.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
// Code generated by auto_generate_repo.
// source:
//
// DO NOT EDIT
package
import (
"time"
"context"
"gorm.io/gorm"
errors "github.com/rotisserie/eris"
)
type QueryOption func(db *gorm.DB) *gorm.DB
type struct {
CreatedBy *string `json:"created_by,omitempty" gorm:"size:100;comment:创建者"`
CreatedAt *time.Time `json:"created_at,omitempty" gorm:"comment:创建时间"`
ModifiedBy *string `json:"modified_by,omitempty" gorm:"size:100;comment:修改者"`
ModifiedAt *time.Time `json:"modified_at,omitempty" gorm:"comment:修改时间"`
DeletedAt *gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"comment:删除时间"`
}
func ( ) TableName() string {
return ""
}
func ( *) BuildFuzzyQuery() QueryOption {
g :=
return func(db *gorm.DB) *gorm.DB {
return db
}
}
func ( *) BuildQuery() QueryOption {
g :=
return func(db *gorm.DB) *gorm.DB {
db = db.Where(g)
return db
}
}
type defaultRepository struct {
db *gorm.DB
}
func NewRepository(db *gorm.DB) Repository {
return &defaultRepository{
db: db,
}
}
func (d *defaultRepository) Create(ctx context.Context, data *) (err error) {
if err := d.db.WithContext(ctx).Model(&{}).Create(data).Error; err != nil {
return errors.Wrap(err, "failed to create entity")
}
return nil
}
func (d *defaultRepository) Lists(ctx context.Context, options map[string]interface{}, query ...QueryOption) (data []*,count int64, err error) {
exec := d.db.WithContext(ctx).Model(&{})
for _, v := range query {
exec = v(exec)
}
exec = exec.Count(&count)
for key, val := range options {
switch key {
case "limit":
exec = exec.Limit(val.(int))
case "offset":
exec = exec.Offset(val.(int))
case "sort":
exec = exec.Order(val).Order("id ASC")
case "select":
exec = exec.Select(val)
}
}
err = exec.Find(&data).Error
if err != nil {
return data,count, errors.Wrap(err, "failed to list the entities")
}
return data,count,err
}
func (d *defaultRepository) Get(ctx context.Context, query *) (data *, err error) {
data = &{}
exec := d.db.WithContext(ctx).Model(&{})
exec = exec.Where(query)
err = exec.Last(data).Error
if err != nil {
return nil, errors.Wrap(err, "failed to get the ")
}
return data, err
}
func (d *defaultRepository) Update(ctx context.Context,update, query *) (data *,err error) {
exec := d.db.WithContext(ctx).Model(&{})
exec = exec.Where(query)
out := &{}
result := exec.Updates(update)
if result.Error != nil {
return nil, errors.Wrap(err, "failed to update entity")
}
if result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}
err = result.Row().Scan(
&out.CreatedBy,
&out.CreatedAt,
&out.ModifiedBy,
&out.ModifiedAt,
&out.DeletedAt,
)
if err != nil {
return nil, errors.Wrap(err, "failed to update entity")
}
return out, nil
}
func (d *defaultRepository) Delete(ctx context.Context, query *) (err error){
exec := d.db.WithContext(ctx).Model(&{})
exec = exec.Where(query).Delete(&{})
err = exec.Error
if err != nil {
return errors.Wrap(err, "failed to delete the ")
}
return nil
}
func (d *defaultRepository) Deletes(ctx context.Context, ids []any) (err error){
exec := d.db.WithContext(ctx).Model(&{})
exec = exec.Where("id in ?", ids)
err = exec.Delete(&{}).Error
if err != nil {
return errors.Wrap(err, "failed to delete the s")
}
return nil
}
type Repository interface {
Create(ctx context.Context, data *) (err error)
Lists(ctx context.Context, options map[string]interface{}, query ...QueryOption) (data []*,count int64,err error)
Get(ctx context.Context, query *) (data *, err error)
Update(ctx context.Context, update , query *) (data *,err error)
Delete(ctx context.Context, query *) (err error)
Deletes(ctx context.Context, ids []any) (err error)
}