-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhints_test.go
90 lines (65 loc) · 2.91 KB
/
hints_test.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
package hints_test
import (
"testing"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/hints"
)
var DB, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
DryRun: true,
})
type User struct {
ID uint
Name string
}
func AssertSQL(t *testing.T, result *gorm.DB, sql string) {
if result.Statement.SQL.String() != sql {
t.Fatalf("SQL expects: %v, got %v", sql, result.Statement.SQL.String())
}
}
func TestHint(t *testing.T) {
result := DB.Find(&User{})
AssertSQL(t, result, "SELECT * FROM `users`")
result = DB.Clauses(hints.New("hint")).Find(&User{})
AssertSQL(t, result, "SELECT /*+ hint */ * FROM `users`")
result = DB.Clauses(hints.Comment("select", "hint")).Find(&User{})
AssertSQL(t, result, "SELECT /* hint */ * FROM `users`")
result = DB.Clauses(hints.CommentBefore("select", "hint")).Find(&User{})
AssertSQL(t, result, "/* hint */ SELECT * FROM `users`")
result = DB.Clauses(hints.CommentAfter("select", "hint")).Find(&User{})
AssertSQL(t, result, "SELECT * /* hint */ FROM `users`")
result = DB.Clauses(hints.CommentAfter("where", "hint")).Find(&User{}, "id = ?", 1)
AssertSQL(t, result, "SELECT * FROM `users` WHERE id = ? /* hint */")
result = DB.Clauses(hints.New("hint")).Model(&User{}).Where("name = ?", "xxx").Update("name", "jinzhu")
AssertSQL(t, result, "UPDATE /*+ hint */ `users` SET `name`=? WHERE name = ?")
db := DB.Clauses(hints.New("MAX_EXECUTION_TIME(100)"))
result = db.Clauses(hints.New("USE_INDEX(t1, idx1)")).Find(&User{})
AssertSQL(t, result, "SELECT /*+ MAX_EXECUTION_TIME(100) USE_INDEX(t1, idx1) */ * FROM `users`")
}
func TestIndexHint(t *testing.T) {
result := DB.Clauses(hints.UseIndex("user_name")).Find(&User{})
AssertSQL(t, result, "SELECT * FROM `users` USE INDEX (`user_name`)")
result = DB.Clauses(hints.ForceIndex("user_name", "user_id").ForJoin()).Find(&User{})
AssertSQL(t, result, "SELECT * FROM `users` FORCE INDEX FOR JOIN (`user_name`,`user_id`)")
result = DB.Clauses(
hints.ForceIndex("user_name", "user_id").ForJoin(),
hints.IgnoreIndex("user_name").ForGroupBy(),
).Find(&User{})
AssertSQL(t, result, "SELECT * FROM `users` FORCE INDEX FOR JOIN (`user_name`,`user_id`) IGNORE INDEX FOR GROUP BY (`user_name`)")
result = DB.Clauses(hints.UseIndex("user_name")).Model(&User{}).Where("name = ?", "xxx").Update("name", "jinzhu")
AssertSQL(t, result, "UPDATE `users` USE INDEX (`user_name`) SET `name`=? WHERE name = ?")
}
type User2 struct {
ID int64
Name string `gorm:"index"`
CompanyID *int
Company Company
}
type Company struct {
ID int
Name string
}
func TestJoinIndexHint(t *testing.T) {
result := DB.Clauses(hints.ForceIndex("user_name")).Joins("Company").Find(&User2{})
AssertSQL(t, result, "SELECT `user2`.`id`,`user2`.`name`,`user2`.`company_id`,`Company`.`id` AS `Company__id`,`Company`.`name` AS `Company__name` FROM `user2` FORCE INDEX (`user_name`) LEFT JOIN `companies` `Company` ON `user2`.`company_id` = `Company`.`id`")
}