/
with_hint.go
58 lines (49 loc) · 1.15 KB
/
with_hint.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
package hints
import (
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type WithHint struct {
Type string
Keys []string
}
func (indexHint WithHint) ModifyStatement(stmt *gorm.Statement) {
dialector := sqlserver.Dialector{}
if stmt.Name() == dialector.Name() {
for _, name := range []string{"FROM"} {
clause := stmt.Clauses[name]
if clause.AfterExpression == nil {
clause.AfterExpression = indexHint
} else {
clause.AfterExpression = Exprs{clause.AfterExpression, indexHint}
}
stmt.Clauses[name] = clause
}
}
}
func (indexHint WithHint) Build(builder clause.Builder) {
if len(indexHint.Keys) > 0 {
_, _ = builder.WriteString(indexHint.Type)
_ = builder.WriteByte('(')
for idx, key := range indexHint.Keys {
if idx > 0 {
_ = builder.WriteByte(',')
}
_, _ = builder.WriteString(key)
}
_ = builder.WriteByte(')')
}
}
func With(names ...string) WithHint {
return WithHint{Type: "with ", Keys: names}
}
type Exprs []clause.Expression
func (exprs Exprs) Build(builder clause.Builder) {
for idx, expr := range exprs {
if idx > 0 {
_ = builder.WriteByte(' ')
}
expr.Build(builder)
}
}