a4df6e40 by slene

fix #265

1 parent 00abdcd0
...@@ -393,7 +393,7 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con ...@@ -393,7 +393,7 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
393 393
394 join := tables.getJoinSql() 394 join := tables.getJoinSql()
395 395
396 var query, T, cols string 396 var query, T string
397 397
398 Q := d.ins.TableQuote() 398 Q := d.ins.TableQuote()
399 399
...@@ -401,30 +401,34 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con ...@@ -401,30 +401,34 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
401 T = "T0." 401 T = "T0."
402 } 402 }
403 403
404 cols := make([]string, 0, len(columns))
405
404 for i, v := range columns { 406 for i, v := range columns {
405 col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q) 407 col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q)
406 if c, ok := values[i].(colValue); ok { 408 if c, ok := values[i].(colValue); ok {
407 switch c.opt { 409 switch c.opt {
408 case Col_Add: 410 case Col_Add:
409 cols += col + " = " + col + " + ? " 411 cols = append(cols, col+" = "+col+" + ?")
410 case Col_Minus: 412 case Col_Minus:
411 cols += col + " = " + col + " - ? " 413 cols = append(cols, col+" = "+col+" - ?")
412 case Col_Multiply: 414 case Col_Multiply:
413 cols += col + " = " + col + " * ? " 415 cols = append(cols, col+" = "+col+" * ?")
414 case Col_Except: 416 case Col_Except:
415 cols += col + " = " + col + " / ? " 417 cols = append(cols, col+" = "+col+" / ?")
416 } 418 }
417 values[i] = c.value 419 values[i] = c.value
418 } else { 420 } else {
419 cols += col + " = ? " 421 cols = append(cols, col+" = ?")
420 } 422 }
421 } 423 }
422 424
425 sets := strings.Join(cols, ", ") + " "
426
423 if d.ins.SupportUpdateJoin() { 427 if d.ins.SupportUpdateJoin() {
424 query = fmt.Sprintf("UPDATE %s%s%s T0 %sSET %s%s", Q, mi.table, Q, join, cols, where) 428 query = fmt.Sprintf("UPDATE %s%s%s T0 %sSET %s%s", Q, mi.table, Q, join, sets, where)
425 } else { 429 } else {
426 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) 430 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)
427 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) 431 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)
428 } 432 }
429 433
430 d.ins.ReplaceMarks(&query) 434 d.ins.ReplaceMarks(&query)
......
...@@ -1513,7 +1513,8 @@ func TestRawPrepare(t *testing.T) { ...@@ -1513,7 +1513,8 @@ func TestRawPrepare(t *testing.T) {
1513 func TestUpdate(t *testing.T) { 1513 func TestUpdate(t *testing.T) {
1514 qs := dORM.QueryTable("user") 1514 qs := dORM.QueryTable("user")
1515 num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{ 1515 num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{
1516 "is_staff": true, 1516 "is_staff": true,
1517 "is_active": true,
1517 }) 1518 })
1518 throwFail(t, err) 1519 throwFail(t, err)
1519 throwFail(t, AssertIs(num, 1)) 1520 throwFail(t, AssertIs(num, 1))
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!