-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
65 lines (58 loc) · 1.13 KB
/
options.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
package gormx
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Option func(db *gorm.DB) *gorm.DB
func applyOptions(db *gorm.DB, opts ...Option) *gorm.DB {
scopes := make([]func(*gorm.DB) *gorm.DB, len(opts))
for i := range opts {
scopes[i] = opts[i]
}
if len(scopes) == 0 {
return db
}
return db.Scopes(scopes...)
}
func NoConflict(names ...string) Option {
return func(db *gorm.DB) *gorm.DB {
var columns []clause.Column
if len(names) > 0 {
columns = make([]clause.Column, len(names))
for i := range names {
columns[i] = clause.Column{
Name: names[i],
}
}
}
return db.Clauses(clause.OnConflict{
Columns: columns,
DoNothing: true,
})
}
}
func Pagination(page, size int) Option {
return func(db *gorm.DB) *gorm.DB {
if page <= 0 {
page = 1
}
switch {
case size > 100:
size = 100
case size <= 0:
size = 20
}
offset := (page - 1) * size
return db.Offset(offset).Limit(size)
}
}
func WithId(id int64) Option {
return func(db *gorm.DB) *gorm.DB {
return db.Where("id=?", id)
}
}
func Wildcard() Option {
return func(db *gorm.DB) *gorm.DB {
return db.Select("*")
}
}