1 parent
d2a9033695
commit
734d389a7c
5 files changed
+91
-13
No files matched your search
@@ -287,19 +287,15 @@ func WithOR(orConditionList ...SetOption) SetOption {
|
||||
// 返回:
|
||||
// - 返回值 1:当前查询或协议操作的结果。
|
||||
func WithOrder(orderRuleList ...string) SetOption {
|
||||
order := append([]string(nil), orderRuleList...)
|
||||
if len(order) == 2 && order[1] == "" {
|
||||
order[1] = "DESC"
|
||||
}
|
||||
return func(o *Option) {
|
||||
if len(orderRuleList) != 2 {
|
||||
if len(order) != 2 || order[0] == "" {
|
||||
return
|
||||
}
|
||||
if len(orderRuleList[0]) == 0 {
|
||||
|
||||
return
|
||||
}
|
||||
if len(orderRuleList[1]) == 0 {
|
||||
|
||||
orderRuleList[1] = "DESC"
|
||||
}
|
||||
o.Order = orderRuleList
|
||||
o.Order = append([]string(nil), order...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,8 +347,8 @@ func WithBetween(field string, left any, right any) SetOption {
|
||||
// 返回:
|
||||
// - 返回值 1:当前查询或协议操作的结果。
|
||||
func WithLockingStrength(lockingStrength string) SetOption {
|
||||
lockingStrength = strings.ToUpper(lockingStrength)
|
||||
return func(o *Option) {
|
||||
lockingStrength = strings.ToUpper(lockingStrength)
|
||||
if lockingStrength != clause.LockingStrengthUpdate && lockingStrength != clause.LockingStrengthShare && lockingStrength != clause.LockingOptionsSkipLocked && lockingStrength != clause.LockingOptionsNoWait {
|
||||
|
||||
return
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOptionsCanBeReusedWithoutChangingOrderInput(t *testing.T) {
|
||||
input := []string{"id", ""}
|
||||
order, lock := WithOrder(input...), WithLockingStrength("update")
|
||||
var (
|
||||
wait sync.WaitGroup
|
||||
)
|
||||
for range 20 {
|
||||
wait.Go(func() {
|
||||
option := &Option{}
|
||||
order(option)
|
||||
lock(option)
|
||||
if len(option.Order) != 2 || option.Order[1] != "DESC" || option.LockingStrength != "UPDATE" {
|
||||
t.Error("option behavior changed")
|
||||
}
|
||||
option.Order[0] = "only-this-call"
|
||||
})
|
||||
}
|
||||
wait.Wait()
|
||||
if input[0] != "id" || input[1] != "" {
|
||||
t.Fatalf("input mutated: %v", input)
|
||||
}
|
||||
option := &Option{}
|
||||
order(option)
|
||||
if option.Order[0] != "id" {
|
||||
t.Fatal("option results share mutable slices")
|
||||
}
|
||||
}
|
||||