Skip to content

Commit

Permalink
add ForUpdateNoWait & ForUpdateSkipLocked
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Aug 22, 2023
1 parent 99fe0a6 commit 12fc7d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 12 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ type selectWithOffset interface {
type toSelectWithLock interface {
LockInShareMode() selectWithLock
ForUpdate() selectWithLock
ForUpdateNoWait() selectWithLock
ForUpdateSkipLocked() selectWithLock
}

type selectWithLock interface {
Expand Down Expand Up @@ -392,6 +394,16 @@ func (s selectStatus) ForUpdate() selectWithLock {
return s
}

func (s selectStatus) ForUpdateNoWait() selectWithLock {
s.lock = " FOR UPDATE NOWAIT"
return s
}

func (s selectStatus) ForUpdateSkipLocked() selectWithLock {
s.lock = " FOR UPDATE SKIP LOCKED"
return s
}

func (s selectStatus) asDerivedTable(name string) Table {
return derivedTable{
name: name,
Expand Down
12 changes: 9 additions & 3 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,16 @@ func TestFetchAll(t *testing.T) {
}

func TestLock(t *testing.T) {
db := database{}
db := newMockDatabase()
table1 := NewTable("table1")
db.Select(1).From(table1).LockInShareMode()
db.Select(1).From(table1).ForUpdate()
_, _ = db.Select(1).From(table1).LockInShareMode().FetchAll()
assertLastSql(t, "SELECT 1 FROM `table1` LOCK IN SHARE MODE")
_, _ = db.Select(1).From(table1).ForUpdate().FetchAll()
assertLastSql(t, "SELECT 1 FROM `table1` FOR UPDATE")
_, _ = db.Select(1).From(table1).ForUpdateNoWait().FetchAll()
assertLastSql(t, "SELECT 1 FROM `table1` FOR UPDATE NOWAIT")
_, _ = db.Select(1).From(table1).ForUpdateSkipLocked().FetchAll()
assertLastSql(t, "SELECT 1 FROM `table1` FOR UPDATE SKIP LOCKED")
}

func TestUnion(t *testing.T) {
Expand Down

0 comments on commit 12fc7d2

Please sign in to comment.