2020-02-07 18:45:35 +03:00
|
|
|
package clause_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-02-07 18:45:35 +03:00
|
|
|
)
|
|
|
|
|
2020-06-06 17:52:08 +03:00
|
|
|
func TestLocking(t *testing.T) {
|
2020-02-07 18:45:35 +03:00
|
|
|
results := []struct {
|
|
|
|
Clauses []clause.Interface
|
|
|
|
Result string
|
|
|
|
Vars []interface{}
|
|
|
|
}{
|
|
|
|
{
|
2023-12-15 11:32:56 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: clause.LockingStrengthUpdate}},
|
2020-02-07 18:45:35 +03:00
|
|
|
"SELECT * FROM `users` FOR UPDATE", nil,
|
|
|
|
},
|
|
|
|
{
|
2023-12-15 11:32:56 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: clause.LockingStrengthShare, Table: clause.Table{Name: clause.CurrentTable}}},
|
2020-06-06 16:35:28 +03:00
|
|
|
"SELECT * FROM `users` FOR SHARE OF `users`", nil,
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
2023-12-15 11:32:56 +03:00
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: clause.LockingStrengthUpdate, Options: clause.LockingOptionsNoWait}},
|
2020-06-06 16:35:28 +03:00
|
|
|
"SELECT * FROM `users` FOR UPDATE NOWAIT", nil,
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
2023-12-15 11:32:56 +03:00
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Locking{Strength: clause.LockingStrengthUpdate, Options: clause.LockingOptionsSkipLocked}},
|
|
|
|
"SELECT * FROM `users` FOR UPDATE SKIP LOCKED", nil,
|
|
|
|
},
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx, result := range results {
|
|
|
|
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
|
|
|
|
checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|