diff --git a/session.go b/session.go index f0edb0ba..9b17958e 100644 --- a/session.go +++ b/session.go @@ -225,6 +225,12 @@ func (session *Session) Distinct(columns ...string) *Session { return session } +// Set Read/Write locking for UPDATE +func (session *Session) ForUpdate() *Session { + session.Statement.IsForUpdate = true + return session +} + // Only not use the paramters as select or update columns func (session *Session) Omit(columns ...string) *Session { session.Statement.Omit(columns...) diff --git a/statement.go b/statement.go index f4d0bcf5..063a6193 100644 --- a/statement.go +++ b/statement.go @@ -66,6 +66,7 @@ type Statement struct { UseCache bool UseAutoTime bool IsDistinct bool + IsForUpdate bool TableAlias string allUseBool bool checkVersion bool @@ -102,6 +103,7 @@ func (statement *Statement) Init() { statement.UseCache = true statement.UseAutoTime = true statement.IsDistinct = false + statement.IsForUpdate = false statement.TableAlias = "" statement.selectStr = "" statement.allUseBool = false @@ -802,6 +804,12 @@ func (statement *Statement) Distinct(columns ...string) *Statement { return statement } +// Generate "SELECT ... FOR UPDATE" statment +func (statement *Statement) ForUpdate() *Statement { + statement.IsForUpdate = true + return statement +} + // replace select func (s *Statement) Select(str string) *Statement { s.selectStr = str @@ -1274,6 +1282,9 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) { a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start) } } + if statement.IsForUpdate { + a += " FOR UPDATE" + } return }