add mothod Decr

This commit is contained in:
kevinGunn 2014-07-15 23:25:24 +08:00
parent 5f8a20eb1c
commit 3c457a8cf0
3 changed files with 43 additions and 0 deletions

View File

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

View File

@ -146,6 +146,12 @@ func (session *Session) Incr(column string, arg ...interface{}) *Session {
return session
}
// Method In provides a query string like "count = count - 1"
func (session *Session) Decr(column string, arg ...interface{}) *Session {
session.Statement.Decr(column, arg...)
return session
}
// Method Cols provides some columns to special
func (session *Session) Cols(columns ...string) *Session {
session.Statement.Cols(columns...)
@ -3043,6 +3049,13 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+session.Engine.Quote(v.colName)+" + ?")
args = append(args, v.arg)
}
//for update action to like "column = column - ?"
decColumns := session.Statement.getDec()
for _, v := range decColumns {
colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+session.Engine.Quote(v.colName)+" - ?")
args = append(args, v.arg)
}
var condiColNames []string
var condiArgs []interface{}

View File

@ -20,6 +20,11 @@ type incrParam struct {
arg interface{}
}
type decrParam struct {
colName string
arg interface{}
}
// statement save all the sql info for executing SQL
type Statement struct {
RefTable *core.Table
@ -54,6 +59,7 @@ type Statement struct {
mustColumnMap map[string]bool
inColumns map[string]*inParam
incrColumns map[string]incrParam
decrColumns map[string]decrParam
}
// init
@ -85,6 +91,7 @@ func (statement *Statement) Init() {
statement.checkVersion = true
statement.inColumns = make(map[string]*inParam)
statement.incrColumns = make(map[string]incrParam)
statement.decrColumns = make(map[string]decrParam)
}
// add the raw sql statement
@ -676,11 +683,27 @@ func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
return statement
}
// Generate "Update ... Set column = column - arg" statment
func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
k := strings.ToLower(column)
if len(arg) > 0 {
statement.decrColumns[k] = decrParam{column, arg[0]}
} else {
statement.decrColumns[k] = decrParam{column, 1}
}
return statement
}
// Generate "Update ... Set column = column + arg" statment
func (statement *Statement) getInc() map[string]incrParam {
return statement.incrColumns
}
// Generate "Update ... Set column = column - arg" statment
func (statement *Statement) getDec() map[string]decrParam {
return statement.decrColumns
}
// Generate "Where column IN (?) " statment
func (statement *Statement) In(column string, args ...interface{}) *Statement {
k := strings.ToLower(column)