Added feature for SELECT ... FOR UPDATE

This commit is contained in:
evalphobia 2015-08-28 16:54:19 +09:00
parent adadb47c60
commit e89f74daa0
2 changed files with 17 additions and 0 deletions

View File

@ -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...)

View File

@ -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
}