-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathclauses.go
60 lines (49 loc) · 1.24 KB
/
clauses.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
package dbresolver
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Operation specifies dbresolver mode
type Operation string
const (
writeName = "gorm:db_resolver:write"
readName = "gorm:db_resolver:read"
)
// ModifyStatement modify operation mode
func (op Operation) ModifyStatement(stmt *gorm.Statement) {
var optName string
if op == Write {
optName = writeName
stmt.Settings.Delete(readName)
} else if op == Read {
optName = readName
stmt.Settings.Delete(writeName)
}
if optName != "" {
stmt.Settings.Store(optName, struct{}{})
if fc := stmt.DB.Callback().Query().Get("gorm:db_resolver"); fc != nil {
fc(stmt.DB)
}
}
}
// Build implements clause.Expression interface
func (op Operation) Build(clause.Builder) {
}
// Use specifies configuration
func Use(str string) clause.Expression {
return using{Use: str}
}
type using struct {
Use string
}
const usingName = "gorm:db_resolver:using"
// ModifyStatement modify operation mode
func (u using) ModifyStatement(stmt *gorm.Statement) {
stmt.Clauses[usingName] = clause.Clause{Expression: u}
if fc := stmt.DB.Callback().Query().Get("gorm:db_resolver"); fc != nil {
fc(stmt.DB)
}
}
// Build implements clause.Expression interface
func (u using) Build(clause.Builder) {
}