diff --git a/engine.go b/engine.go index 33595aa9..e0a10ed1 100644 --- a/engine.go +++ b/engine.go @@ -342,6 +342,11 @@ func (engine *Engine) In(column string, args ...interface{}) *Session { session := engine.NewSession() session.IsAutoClose = true 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 diff --git a/session.go b/session.go index c94e89aa..741f9342 100644 --- a/session.go +++ b/session.go @@ -128,6 +128,12 @@ func (session *Session) In(column string, args ...interface{}) *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 func (session *Session) Cols(columns ...string) *Session { session.Statement.Cols(columns...) @@ -2952,6 +2958,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 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 condiArgs []interface{} diff --git a/statement.go b/statement.go index fa82ff83..0b0c2f83 100644 --- a/statement.go +++ b/statement.go @@ -48,6 +48,7 @@ type Statement struct { checkVersion bool mustColumnMap map[string]bool inColumns map[string]*inParam + incColumns map[string]interface{} } // init @@ -470,6 +471,17 @@ func (statement *Statement) Id(id interface{}) *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 func (statement *Statement) In(column string, args ...interface{}) *Statement { k := strings.ToLower(column)