implement Expr()
This commit is contained in:
parent
72f04dadd8
commit
5a47ac4347
|
@ -558,6 +558,13 @@ func (engine *Engine) Decr(column string, arg ...interface{}) *Session {
|
|||
return session.Decr(column, arg...)
|
||||
}
|
||||
|
||||
// Method Expr provides a update string like "column = {expression}"
|
||||
func (engine *Engine) Expr(column string, expression string) *Session {
|
||||
session := engine.NewSession()
|
||||
session.IsAutoClose = true
|
||||
return session.Expr(column, expression)
|
||||
}
|
||||
|
||||
// Temporarily change the Get, Find, Update's table
|
||||
func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
|
||||
session := engine.NewSession()
|
||||
|
|
11
session.go
11
session.go
|
@ -158,6 +158,12 @@ func (session *Session) Decr(column string, arg ...interface{}) *Session {
|
|||
return session
|
||||
}
|
||||
|
||||
// Method Expr provides a query string like "column = {expression}"
|
||||
func (session *Session) Expr(column string, expression string) *Session {
|
||||
session.Statement.Expr(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{}
|
||||
|
|
19
statement.go
19
statement.go
|
@ -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) Expr(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)
|
||||
|
|
Loading…
Reference in New Issue