/
query.go
187 lines (157 loc) · 3.96 KB
/
query.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
package query
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)
type (
Tracer interface {
IsEnableTrace() bool
// OpenTrace 开启trace
OpenTrace() Tracer
// CloseTrace 关闭trace
CloseTrace() Tracer
}
ICtx interface {
GetCtx() context.Context
}
// IAction 操作接口
IAction[T any] interface {
IOperation[T]
IOperationX[T]
IBind[T]
IAssociation
}
action[T any] struct {
db *gorm.DB
ctx context.Context
table schema.Tabler
IAssociation
IOperation[T]
IOperationX[T]
Tracer
}
ActionOption[T any] func(a *action[T])
Ctx struct {
ctx context.Context
}
)
func (l *Ctx) GetCtx() context.Context {
return l.ctx
}
// NewCtx 实例化上下文
func NewCtx(ctx context.Context) ICtx {
return &Ctx{ctx: ctx}
}
// NewAction 创建GORM操作接口实例
func NewAction[T any](opts ...ActionOption[T]) IAction[T] {
ac := action[T]{
ctx: context.Background(),
Tracer: NewITracer(),
}
for _, opt := range opts {
opt(&ac)
}
if ac.table != nil {
ac.db = ac.db.Table(ac.table.TableName())
}
if ac.IAssociation == nil {
ac.IAssociation = NewDefaultAssociation(ac.db)
}
WithICtx[T](NewCtx(ac.ctx))(&ac)
return &ac
}
// WithICtx 设置ICtx
func WithICtx[T any](ctx ICtx) ActionOption[T] {
return func(a *action[T]) {
a.IOperation = NewOperationImpl[T](
WithOperationQuery[T](
NewOperationQuery[T](
WithOperationQueryICtx[T](ctx),
WithOperationQueryTracer[T](a.Tracer),
WithOperationQueryIBind[T](a),
),
),
WithOperationMutation[T](
NewOperationMutation[T](
WithOperationMutationICtx[T](ctx),
WithOperationMutationTracer[T](a.Tracer),
WithOperationMutationIBind[T](a),
),
),
)
a.IOperationX = NewOperationXImpl[T](
WithOperationQueryX[T](
NewOperationQueryX[T](
WithOperationQueryXICtx[T](ctx),
WithOperationQueryXTracer[T](a.Tracer),
WithOperationQueryXIBind[T](a),
),
),
WithOperationMutationX[T](
NewOperationMutationX[T](
WithOperationMutationXICtx[T](ctx),
WithOperationMutationXTracer[T](a.Tracer),
WithOperationMutationXIBind[T](a),
),
),
)
}
}
// DB 获取DB, 包含了Table或Model, 用于链式操作
func (a *action[T]) DB() *gorm.DB {
if a.table != nil {
return a.db.Table(a.table.TableName())
}
var m T
return a.db.Model(&m)
}
// Clauses 设置Clauses
func (a *action[T]) Clauses(condList ...clause.Expression) IAction[T] {
a.db = a.db.Clauses(condList...)
return a
}
// Order 跳转到排序动作
func (a *action[T]) Order(column string) IOrder[T] {
return NewOrder[T](column).WithIAction(a)
}
// WithDB 设置DB, 一般用于事务, 这里使用事务的DB, 也可以设置新的DB用于链式操作
func (a *action[T]) WithDB(db *gorm.DB) IAction[T] {
a.db = db
return a
}
// WithContext 设置上下文Ctx
func (a *action[T]) WithContext(ctx context.Context) IAction[T] {
a.ctx = ctx
return a
}
// WithTable 设置Table, 这里传递的是实现了schema.Tabler接口的结构体
func (a *action[T]) WithTable(tabler schema.Tabler) IAction[T] {
a.table = tabler
return a
}
// WithModel 设置Model, Model规范参考: https://gorm.io/zh_CN/docs/models.html
func (a *action[T]) WithModel(model any) IAction[T] {
a.db = a.db.Model(model)
return a
}
// Preload 预加载, 参考: https://gorm.io/zh_CN/docs/preload.html
func (a *action[T]) Preload(preloadKey string, wheres ...ScopeMethod) IAction[T] {
a.db = a.db.Preload(preloadKey, func(db *gorm.DB) *gorm.DB {
return db.Scopes(wheres...)
})
return a
}
// Joins 设置关联, 参考: https://gorm.io/zh_CN/docs/preload.html#Joins-%E9%A2%84%E5%8A%A0%E8%BD%BD
func (a *action[T]) Joins(joinsKey string, wheres ...ScopeMethod) IAction[T] {
a.db = a.db.Joins(joinsKey, func(db *gorm.DB) *gorm.DB {
return db.Scopes(wheres...)
})
return a
}
// Scopes 设置作用域, 参考: https://gorm.io/zh_CN/docs/scopes.html
func (a *action[T]) Scopes(wheres ...ScopeMethod) IAction[T] {
a.db = a.db.Scopes(wheres...)
return a
}