forked from go-gorm/hints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhints.go
82 lines (70 loc) · 2.09 KB
/
hints.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
package hints
import (
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Hints struct {
Prefix string
Suffix string
Content string
clauses []string
before bool
after bool
}
func (hints Hints) ModifyStatement(stmt *gorm.Statement) {
for _, name := range hints.clauses {
name = strings.ToUpper(name)
clause := stmt.Clauses[name]
switch {
case hints.before:
if clause.BeforeExpression == nil {
clause.BeforeExpression = hints
} else if old, ok := clause.BeforeExpression.(Hints); ok {
old.Merge(hints)
clause.BeforeExpression = old
} else {
clause.BeforeExpression = Exprs{clause.BeforeExpression, hints}
}
case hints.after:
if clause.AfterExpression == nil {
clause.AfterExpression = hints
} else if old, ok := clause.AfterExpression.(Hints); ok {
old.Merge(hints)
clause.AfterExpression = old
} else {
clause.AfterExpression = Exprs{clause.AfterExpression, hints}
}
default:
if clause.AfterNameExpression == nil {
clause.AfterNameExpression = hints
} else if old, ok := clause.AfterNameExpression.(Hints); ok {
old.Merge(hints)
clause.AfterNameExpression = old
} else {
clause.AfterNameExpression = Exprs{clause.AfterNameExpression, hints}
}
}
stmt.Clauses[name] = clause
}
}
func (hints Hints) Build(builder clause.Builder) {
builder.WriteString(hints.Prefix)
builder.WriteString(hints.Content)
builder.WriteString(hints.Suffix)
}
func (hints *Hints) Merge(h Hints) {
hints.Content += " " + h.Content
}
func New(content string) Hints {
return Hints{Prefix: "/*+ ", Content: content, Suffix: " */", clauses: []string{"SELECT", "UPDATE"}}
}
func Comment(clause string, comment string) Hints {
return Hints{clauses: []string{clause}, Prefix: "/* ", Content: comment, Suffix: " */"}
}
func CommentBefore(clause string, comment string) Hints {
return Hints{clauses: []string{clause}, before: true, Prefix: "/* ", Content: comment, Suffix: " */"}
}
func CommentAfter(clause string, comment string) Hints {
return Hints{clauses: []string{clause}, after: true, Prefix: "/* ", Content: comment, Suffix: " */"}
}