-
Notifications
You must be signed in to change notification settings - Fork 208
/
index_hints.go
64 lines (52 loc) · 1.24 KB
/
index_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
package dbr
//mysql hint https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
const (
useIndex = "USE INDEX"
ignoreIndex = "IGNORE INDEX"
forceIndex = "FORCE INDEX"
forJoin = "FOR JOIN"
forOrderBY = "FOR ORDER BY"
forGroupBy = "FOR GROUP BY"
)
type indexHint struct {
hint []string
indexList []string
}
func UseIndex(index ...string) indexHint {
return indexHint{hint: []string{useIndex}, indexList: index}
}
func IgnoreIndex(index ...string) indexHint {
return indexHint{hint: []string{ignoreIndex}, indexList: index}
}
func ForceIndex(index ...string) indexHint {
return indexHint{hint: []string{forceIndex}, indexList: index}
}
func (i indexHint) ForJoin() indexHint {
i.hint = append(i.hint, forJoin)
return i
}
func (i indexHint) ForOrderBy() indexHint {
i.hint = append(i.hint, forOrderBY)
return i
}
func (i indexHint) ForGroupBy() indexHint {
i.hint = append(i.hint, forGroupBy)
return i
}
func (i indexHint) Build(d Dialect, buf Buffer) error {
for idx, v := range i.hint {
if idx > 0 {
buf.WriteString(" ")
}
buf.WriteString(v)
}
buf.WriteString("(")
for idx, v := range i.indexList {
if idx > 0 {
buf.WriteString(",")
}
buf.WriteString(d.QuoteIdent(v))
}
buf.WriteString(")")
return nil
}