Method In provides a update string

This commit is contained in:
unphp 2014-04-15 09:54:49 +08:00
parent 38616e71fd
commit 97f4b0e45c
3 changed files with 29 additions and 0 deletions

View File

@ -342,6 +342,11 @@ func (engine *Engine) In(column string, args ...interface{}) *Session {
session := engine.NewSession() session := engine.NewSession()
session.IsAutoClose = true session.IsAutoClose = true
return session.In(column, args...) return session.In(column, args...)
// Method In provides a update string like "column = column + ?"
func (engine *Engine) Inc(column string, arg interface{}) *Session {
session := engine.NewSession()
session.IsAutoClose = true
return session.Inc(column, arg)
} }
// Temporarily change the Get, Find, Update's table // Temporarily change the Get, Find, Update's table

View File

@ -128,6 +128,12 @@ func (session *Session) In(column string, args ...interface{}) *Session {
return session return session
} }
// Method In provides a query string like "count = count + 1"
func (session *Session) Inc(column string, arg interface{}) *Session {
session.Statement.Inc(column, arg)
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...)
@ -2952,6 +2958,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
args = append(args, time.Now()) args = append(args, time.Now())
} }
//for update action to like "column = column + ?"
incColumns := session.Statement.getInc()
for k, v := range incColumns {
colNames = append(colNames, k+" = "+k+" + ?")
args = append(args, v)
}
var condiColNames []string var condiColNames []string
var condiArgs []interface{} var condiArgs []interface{}

View File

@ -48,6 +48,7 @@ type Statement struct {
checkVersion bool checkVersion bool
mustColumnMap map[string]bool mustColumnMap map[string]bool
inColumns map[string]*inParam inColumns map[string]*inParam
incColumns map[string]interface{}
} }
// init // init
@ -470,6 +471,17 @@ func (statement *Statement) Id(id interface{}) *Statement {
return statement return statement
} }
// Generate "Update ... Set column = column + arg" statment
func (statement *Statement) Inc(column string, arg interface{}) *Statement {
k := strings.ToLower(column)
statement.incColumns[k] = arg
return statement
}
// Generate "Update ... Set column = column + arg" statment
func (statement *Statement) getInc() map[string]interface{} {
return statement.incColumns
}
// Generate "Where column IN (?) " statment // Generate "Where column IN (?) " statment
func (statement *Statement) In(column string, args ...interface{}) *Statement { func (statement *Statement) In(column string, args ...interface{}) *Statement {
k := strings.ToLower(column) k := strings.ToLower(column)