Add support for OF in row lock clauses

This adds support for statements such as `SELECT ... FOR UPDATE OF table NOWAIT` where `OF table`
could not be specified previously. Fixes #285.
This commit is contained in:
Matthew Dowdell 2023-11-30 07:52:54 +00:00
parent 6a13530ec1
commit f16f0b5e5d
4 changed files with 48 additions and 1 deletions

View file

@ -4,12 +4,14 @@ package jet
type RowLock interface {
Serializer
OF(...Table) RowLock
NOWAIT() RowLock
SKIP_LOCKED() RowLock
}
type selectLockImpl struct {
lockStrength string
of []Table
noWait, skipLocked bool
}
@ -20,10 +22,15 @@ func NewRowLock(name string) func() RowLock {
}
}
func newSelectLock(lockStrength string) RowLock {
func newSelectLock(lockStrength string) *selectLockImpl {
return &selectLockImpl{lockStrength: lockStrength}
}
func (s *selectLockImpl) OF(tables ...Table) RowLock {
s.of = tables
return s
}
func (s *selectLockImpl) NOWAIT() RowLock {
s.noWait = true
return s
@ -37,6 +44,23 @@ func (s *selectLockImpl) SKIP_LOCKED() RowLock {
func (s *selectLockImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
out.WriteString(s.lockStrength)
if len(s.of) > 0 {
out.WriteString("OF")
for i, of := range s.of {
if i > 0 {
out.WriteString(", ")
}
table := of.Alias()
if table == "" {
table = of.TableName()
}
out.WriteIdentifier(table)
}
}
if s.noWait {
out.WriteString("NOWAIT")
}