Merge pull request #202 from hilyjiang/master

implement Expr()
This commit is contained in:
Lunny Xiao 2015-01-28 14:14:43 +08:00
commit 5f7fccdb73
3 changed files with 37 additions and 0 deletions

View File

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

View File

@ -158,6 +158,12 @@ func (session *Session) Decr(column string, arg ...interface{}) *Session {
return session
}
// Method SetExpr provides a query string like "column = {expression}"
func (session *Session) SetExpr(column string, expression string) *Session {
session.Statement.SetExpr(column, expression)
return session
}
// Method Cols provides some columns to special
func (session *Session) Cols(columns ...string) *Session {
session.Statement.Cols(columns...)
@ -3237,6 +3243,11 @@ 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 = expression"
exprColumns := session.Statement.getExpr()
for _, v := range exprColumns {
colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+v.expr)
}
var condiColNames []string
var condiArgs []interface{}

View File

@ -26,6 +26,11 @@ type decrParam struct {
arg interface{}
}
type exprParam struct {
colName string
expr string
}
// statement save all the sql info for executing SQL
type Statement struct {
RefTable *core.Table
@ -63,6 +68,7 @@ type Statement struct {
inColumns map[string]*inParam
incrColumns map[string]incrParam
decrColumns map[string]decrParam
exprColumns map[string]exprParam
}
// init
@ -98,6 +104,7 @@ func (statement *Statement) Init() {
statement.inColumns = make(map[string]*inParam)
statement.incrColumns = make(map[string]incrParam)
statement.decrColumns = make(map[string]decrParam)
statement.exprColumns = make(map[string]exprParam)
}
// add the raw sql statement
@ -716,6 +723,13 @@ func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
return statement
}
// Generate "Update ... Set column = {expression}" statment
func (statement *Statement) SetExpr(column string, expression string) *Statement {
k := strings.ToLower(column)
statement.exprColumns[k] = exprParam{column, expression}
return statement
}
// Generate "Update ... Set column = column + arg" statment
func (statement *Statement) getInc() map[string]incrParam {
return statement.incrColumns
@ -726,6 +740,11 @@ func (statement *Statement) getDec() map[string]decrParam {
return statement.decrColumns
}
// Generate "Update ... Set column = {expression}" statment
func (statement *Statement) getExpr() map[string]exprParam {
return statement.exprColumns
}
// Generate "Where column IN (?) " statment
func (statement *Statement) In(column string, args ...interface{}) *Statement {
k := strings.ToLower(column)