add Options() to specify query options

This commit is contained in:
江林锦 2015-04-23 12:44:35 +08:00
parent 7fd31fc7e7
commit 26045610e1
3 changed files with 29 additions and 1 deletions

View File

@ -571,6 +571,13 @@ func (engine *Engine) SetExpr(column string, expression string) *Session {
return session.SetExpr(column, expression) return session.SetExpr(column, expression)
} }
// Method Options specify the query option
func (engine *Engine) Options(options ...string) *Session {
session := engine.NewSession()
session.IsAutoClose = true
return session.Options(options...)
}
// Temporarily change the Get, Find, Update's table // Temporarily change the Get, Find, Update's table
func (engine *Engine) Table(tableNameOrBean interface{}) *Session { func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
session := engine.NewSession() session := engine.NewSession()

View File

@ -168,6 +168,12 @@ func (session *Session) SetExpr(column string, expression string) *Session {
return session return session
} }
// Method Options specify the query option
func (session *Session) Options(options ...string) *Session {
session.Statement.Options = options
return session
}
// Method Cols provides some columns to special // Method Cols provides some columns to special
func (session *Session) Cols(columns ...string) *Session { func (session *Session) Cols(columns ...string) *Session {
session.Statement.Cols(columns...) session.Statement.Cols(columns...)
@ -722,7 +728,7 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) { func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
if session.Statement.RefTable == nil || if session.Statement.RefTable == nil ||
indexNoCase(sqlStr, "having") != -1 || indexNoCase(sqlStr, "having") != -1 ||
indexNoCase(sqlStr, "group by") != -1 || indexNoCase(sqlStr, "group by") != -1 ||
session.Tx != nil { session.Tx != nil {
return ErrCacheFailed return ErrCacheFailed
} }

View File

@ -69,6 +69,7 @@ type Statement struct {
incrColumns map[string]incrParam incrColumns map[string]incrParam
decrColumns map[string]decrParam decrColumns map[string]decrParam
exprColumns map[string]exprParam exprColumns map[string]exprParam
Options []string
} }
// init // init
@ -1308,6 +1309,20 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
} }
} }
if len(statement.Options) > 0 {
switch statement.Engine.dialect.DBType() {
case core.MYSQL:
for _, option := range statement.Options {
switch option {
case "select:for-update":
a += " FOR UPDATE"
case "select:lock-in-share-mode":
a += " LOCK IN SHARE MODE"
}
}
}
}
return return
} }