commit
5f7fccdb73
|
@ -558,6 +558,13 @@ func (engine *Engine) Decr(column string, arg ...interface{}) *Session {
|
||||||
return session.Decr(column, arg...)
|
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
|
// 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()
|
||||||
|
|
11
session.go
11
session.go
|
@ -158,6 +158,12 @@ func (session *Session) Decr(column string, arg ...interface{}) *Session {
|
||||||
return 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
|
// 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...)
|
||||||
|
@ -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)+" - ?")
|
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 = expression"
|
||||||
|
exprColumns := session.Statement.getExpr()
|
||||||
|
for _, v := range exprColumns {
|
||||||
|
colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+v.expr)
|
||||||
|
}
|
||||||
|
|
||||||
var condiColNames []string
|
var condiColNames []string
|
||||||
var condiArgs []interface{}
|
var condiArgs []interface{}
|
||||||
|
|
19
statement.go
19
statement.go
|
@ -26,6 +26,11 @@ type decrParam struct {
|
||||||
arg interface{}
|
arg interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type exprParam struct {
|
||||||
|
colName string
|
||||||
|
expr string
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -63,6 +68,7 @@ type Statement struct {
|
||||||
inColumns map[string]*inParam
|
inColumns map[string]*inParam
|
||||||
incrColumns map[string]incrParam
|
incrColumns map[string]incrParam
|
||||||
decrColumns map[string]decrParam
|
decrColumns map[string]decrParam
|
||||||
|
exprColumns map[string]exprParam
|
||||||
}
|
}
|
||||||
|
|
||||||
// init
|
// init
|
||||||
|
@ -98,6 +104,7 @@ func (statement *Statement) Init() {
|
||||||
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)
|
statement.decrColumns = make(map[string]decrParam)
|
||||||
|
statement.exprColumns = make(map[string]exprParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the raw sql statement
|
// add the raw sql statement
|
||||||
|
@ -716,6 +723,13 @@ func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
|
||||||
return 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
|
// 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
|
||||||
|
@ -726,6 +740,11 @@ func (statement *Statement) getDec() map[string]decrParam {
|
||||||
return statement.decrColumns
|
return statement.decrColumns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate "Update ... Set column = {expression}" statment
|
||||||
|
func (statement *Statement) getExpr() map[string]exprParam {
|
||||||
|
return statement.exprColumns
|
||||||
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
|
|
Loading…
Reference in New Issue