Merge pull request #145 from cnphpbb/dev

add mothod Decr.
This commit is contained in:
Lunny Xiao 2014-07-16 09:11:53 +08:00
commit 135bb42e5f
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...) return session.Incr(column, arg...)
} }
// Method Decr 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 // Temporarily change the Get, Find, Update's table
func (engine *Engine) Table(tableNameOrBean interface{}) *Session { func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
session := engine.NewSession() session := engine.NewSession()

View File

@ -146,6 +146,12 @@ func (session *Session) Incr(column string, arg ...interface{}) *Session {
return session return session
} }
// Method Decr 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 // 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...)
@ -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)+" + ?") colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+session.Engine.Quote(v.colName)+" + ?")
args = append(args, v.arg) 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 condiColNames []string
var condiArgs []interface{} var condiArgs []interface{}

View File

@ -20,6 +20,11 @@ type incrParam struct {
arg interface{} arg interface{}
} }
type decrParam struct {
colName string
arg interface{}
}
// statement save all the sql info for executing SQL // statement save all the sql info for executing SQL
type Statement struct { type Statement struct {
RefTable *core.Table RefTable *core.Table
@ -54,6 +59,7 @@ type Statement struct {
mustColumnMap map[string]bool mustColumnMap map[string]bool
inColumns map[string]*inParam inColumns map[string]*inParam
incrColumns map[string]incrParam incrColumns map[string]incrParam
decrColumns map[string]decrParam
} }
// init // init
@ -85,6 +91,7 @@ func (statement *Statement) Init() {
statement.checkVersion = true statement.checkVersion = true
statement.inColumns = make(map[string]*inParam) statement.inColumns = make(map[string]*inParam)
statement.incrColumns = make(map[string]incrParam) statement.incrColumns = make(map[string]incrParam)
statement.decrColumns = make(map[string]decrParam)
} }
// add the raw sql statement // add the raw sql statement
@ -676,11 +683,27 @@ func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
return 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 // Generate "Update ... Set column = column + arg" statment
func (statement *Statement) getInc() map[string]incrParam { func (statement *Statement) getInc() map[string]incrParam {
return statement.incrColumns 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 // 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)