diff --git a/engine.go b/engine.go index b17b006d..caf1dbe4 100644 --- a/engine.go +++ b/engine.go @@ -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() diff --git a/session.go b/session.go index f4394abc..a84f6429 100644 --- a/session.go +++ b/session.go @@ -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{} diff --git a/statement.go b/statement.go index d872e267..9286250d 100644 --- a/statement.go +++ b/statement.go @@ -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)