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 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 // Only not use the paramters as select or update columns
func (session *Session) Omit(columns ...string) *Session { func (session *Session) Omit(columns ...string) *Session {
session.Statement.Omit(columns...) session.Statement.Omit(columns...)

View File

@ -66,6 +66,7 @@ type Statement struct {
UseCache bool UseCache bool
UseAutoTime bool UseAutoTime bool
IsDistinct bool IsDistinct bool
IsForUpdate bool
TableAlias string TableAlias string
allUseBool bool allUseBool bool
checkVersion bool checkVersion bool
@ -102,6 +103,7 @@ func (statement *Statement) Init() {
statement.UseCache = true statement.UseCache = true
statement.UseAutoTime = true statement.UseAutoTime = true
statement.IsDistinct = false statement.IsDistinct = false
statement.IsForUpdate = false
statement.TableAlias = "" statement.TableAlias = ""
statement.selectStr = "" statement.selectStr = ""
statement.allUseBool = false statement.allUseBool = false
@ -802,6 +804,12 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
return statement return statement
} }
// Generate "SELECT ... FOR UPDATE" statment
func (statement *Statement) ForUpdate() *Statement {
statement.IsForUpdate = true
return statement
}
// replace select // replace select
func (s *Statement) Select(str string) *Statement { func (s *Statement) Select(str string) *Statement {
s.selectStr = str 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) 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 return
} }