diff --git a/select.go b/select.go index a216ba7..4d32201 100644 --- a/select.go +++ b/select.go @@ -105,6 +105,8 @@ type selectWithOffset interface { type toSelectWithLock interface { LockInShareMode() selectWithLock ForUpdate() selectWithLock + ForUpdateNoWait() selectWithLock + ForUpdateSkipLocked() selectWithLock } type selectWithLock interface { @@ -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, diff --git a/select_test.go b/select_test.go index 5619735..d4103f4 100644 --- a/select_test.go +++ b/select_test.go @@ -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) {