1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 03:30:53 +00:00
This commit is contained in:
slene 2013-11-01 18:32:03 +08:00
parent 00abdcd0a1
commit a4df6e403c
2 changed files with 14 additions and 9 deletions

View File

@ -393,7 +393,7 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
join := tables.getJoinSql() join := tables.getJoinSql()
var query, T, cols string var query, T string
Q := d.ins.TableQuote() Q := d.ins.TableQuote()
@ -401,30 +401,34 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
T = "T0." T = "T0."
} }
cols := make([]string, 0, len(columns))
for i, v := range columns { for i, v := range columns {
col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q) col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q)
if c, ok := values[i].(colValue); ok { if c, ok := values[i].(colValue); ok {
switch c.opt { switch c.opt {
case Col_Add: case Col_Add:
cols += col + " = " + col + " + ? " cols = append(cols, col+" = "+col+" + ?")
case Col_Minus: case Col_Minus:
cols += col + " = " + col + " - ? " cols = append(cols, col+" = "+col+" - ?")
case Col_Multiply: case Col_Multiply:
cols += col + " = " + col + " * ? " cols = append(cols, col+" = "+col+" * ?")
case Col_Except: case Col_Except:
cols += col + " = " + col + " / ? " cols = append(cols, col+" = "+col+" / ?")
} }
values[i] = c.value values[i] = c.value
} else { } else {
cols += col + " = ? " cols = append(cols, col+" = ?")
} }
} }
sets := strings.Join(cols, ", ") + " "
if d.ins.SupportUpdateJoin() { if d.ins.SupportUpdateJoin() {
query = fmt.Sprintf("UPDATE %s%s%s T0 %sSET %s%s", Q, mi.table, Q, join, cols, where) query = fmt.Sprintf("UPDATE %s%s%s T0 %sSET %s%s", Q, mi.table, Q, join, sets, where)
} else { } else {
supQuery := fmt.Sprintf("SELECT T0.%s%s%s FROM %s%s%s T0 %s%s", Q, mi.fields.pk.column, Q, Q, mi.table, Q, join, where) supQuery := fmt.Sprintf("SELECT T0.%s%s%s FROM %s%s%s T0 %s%s", Q, mi.fields.pk.column, Q, Q, mi.table, Q, join, where)
query = fmt.Sprintf("UPDATE %s%s%s SET %sWHERE %s%s%s IN ( %s )", Q, mi.table, Q, cols, Q, mi.fields.pk.column, Q, supQuery) query = fmt.Sprintf("UPDATE %s%s%s SET %sWHERE %s%s%s IN ( %s )", Q, mi.table, Q, sets, Q, mi.fields.pk.column, Q, supQuery)
} }
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)

View File

@ -1513,7 +1513,8 @@ func TestRawPrepare(t *testing.T) {
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
qs := dORM.QueryTable("user") qs := dORM.QueryTable("user")
num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{ num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{
"is_staff": true, "is_staff": true,
"is_active": true,
}) })
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(num, 1)) throwFail(t, AssertIs(num, 1))