diff --git a/engine.go b/engine.go index 10dce15a..4bcac9e1 100644 --- a/engine.go +++ b/engine.go @@ -357,6 +357,13 @@ func (engine *Engine) In(column string, args ...interface{}) *Session { return session.In(column, args...) } +// Method Inc 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 func (engine *Engine) Table(tableNameOrBean interface{}) *Session { session := engine.NewSession() diff --git a/session.go b/session.go index 8649584a..8a3a5149 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..5b45d1d6 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 @@ -78,6 +79,7 @@ func (statement *Statement) Init() { statement.mustColumnMap = make(map[string]bool) statement.checkVersion = true statement.inColumns = make(map[string]*inParam) + statement.incColumns = make(map[string]interface{}, 0) } // add the raw sql statement @@ -470,6 +472,18 @@ 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)