/
option.go
151 lines (126 loc) · 3.41 KB
/
option.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
package gormutil
import (
"log"
"os"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
type GORMOption func(o *gorm.Config)
func applyOptions(o *gorm.Config, options ...GORMOption) *gorm.Config {
for _, opt := range options {
opt(o)
}
return o
}
func WithSkipDefaultTransaction(SkipDefaultTransaction bool) GORMOption {
return func(o *gorm.Config) {
o.SkipDefaultTransaction = SkipDefaultTransaction
}
}
func WithNamingStrategy(NamingStrategy schema.Namer) GORMOption {
return func(o *gorm.Config) {
o.NamingStrategy = NamingStrategy
}
}
func WithFullSaveAssociations(FullSaveAssociations bool) GORMOption {
return func(o *gorm.Config) {
o.FullSaveAssociations = FullSaveAssociations
}
}
func WithLogger(Logger logger.Interface) GORMOption {
return func(o *gorm.Config) {
o.Logger = Logger
}
}
func WithNowFunc(NowFunc func() time.Time) GORMOption {
return func(o *gorm.Config) {
o.NowFunc = NowFunc
}
}
func WithDryRun(DryRun bool) GORMOption {
return func(o *gorm.Config) {
o.DryRun = DryRun
}
}
func WithPrepareStmt(PrepareStmt bool) GORMOption {
return func(o *gorm.Config) {
o.PrepareStmt = PrepareStmt
}
}
func WithDisableAutomaticPing(DisableAutomaticPing bool) GORMOption {
return func(o *gorm.Config) {
o.DisableAutomaticPing = DisableAutomaticPing
}
}
func WithDisableForeignKeyConstraintWhenMigrating(DisableForeignKeyConstraintWhenMigrating bool) GORMOption {
return func(o *gorm.Config) {
o.DisableForeignKeyConstraintWhenMigrating = DisableForeignKeyConstraintWhenMigrating
}
}
func WithIgnoreRelationshipsWhenMigrating(IgnoreRelationshipsWhenMigrating bool) GORMOption {
return func(o *gorm.Config) {
o.IgnoreRelationshipsWhenMigrating = IgnoreRelationshipsWhenMigrating
}
}
func WithDisableNestedTransaction(DisableNestedTransaction bool) GORMOption {
return func(o *gorm.Config) {
o.DisableNestedTransaction = DisableNestedTransaction
}
}
func WithAllowGlobalUpdate(AllowGlobalUpdate bool) GORMOption {
return func(o *gorm.Config) {
o.AllowGlobalUpdate = AllowGlobalUpdate
}
}
func WithQueryFields(QueryFields bool) GORMOption {
return func(o *gorm.Config) {
o.QueryFields = QueryFields
}
}
func WithCreateBatchSize(CreateBatchSize int) GORMOption {
return func(o *gorm.Config) {
o.CreateBatchSize = CreateBatchSize
}
}
func WithTranslateError(TranslateError bool) GORMOption {
return func(o *gorm.Config) {
o.TranslateError = TranslateError
}
}
func WithClauseBuilders(ClauseBuilders map[string]clause.ClauseBuilder) GORMOption {
return func(o *gorm.Config) {
o.ClauseBuilders = ClauseBuilders
}
}
func WithConnPool(ConnPool gorm.ConnPool) GORMOption {
return func(o *gorm.Config) {
o.ConnPool = ConnPool
}
}
func WithPlugins(Plugins map[string]gorm.Plugin) GORMOption {
return func(o *gorm.Config) {
o.Plugins = Plugins
}
}
func WithStdLogger(level ...logger.LogLevel) GORMOption {
return WithLogger(StdLogger(level...))
}
func StdLogger(level ...logger.LogLevel) logger.Interface {
lev := logger.Info
if len(level) > 0 {
lev = level[0]
}
return logger.New(log.New(os.Stdout, "[GORM]", log.LstdFlags), logger.Config{
SlowThreshold: time.Millisecond * 200,
Colorful: false,
IgnoreRecordNotFoundError: true,
ParameterizedQueries: false,
LogLevel: lev,
})
}
func LogrusLogger(config logger.Config) logger.Interface {
return &logrusLogger{Config: config}
}