1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 09:50:39 +00:00

update support bit operation

This commit is contained in:
harry890829
2020-05-18 19:16:50 +08:00
parent 3e30f37172
commit 71cb1379b4
5 changed files with 52 additions and 3 deletions

View File

@ -770,6 +770,16 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
cols = append(cols, col+" = "+col+" * ?")
case ColExcept:
cols = append(cols, col+" = "+col+" / ?")
case ColBitAnd:
cols = append(cols, col+" = "+col+" & ?")
case ColBitRShift:
cols = append(cols, col+" = "+col+" >> ?")
case ColBitLShift:
cols = append(cols, col+" = "+col+" << ?")
case ColBitXOR:
cols = append(cols, col+" = "+col+" ^ ?")
case ColBitOr:
cols = append(cols, col+" = "+col+" | ?")
}
values[i] = c.value
} else {

View File

@ -32,6 +32,11 @@ const (
ColMinus
ColMultiply
ColExcept
ColBitAnd
ColBitRShift
ColBitLShift
ColBitXOR
ColBitOr
)
// ColValue do the field raw changes. e.g Nums = Nums + 10. usage:
@ -40,7 +45,8 @@ const (
// }
func ColValue(opt operator, value interface{}) interface{} {
switch opt {
case ColAdd, ColMinus, ColMultiply, ColExcept:
case ColAdd, ColMinus, ColMultiply, ColExcept, ColBitAnd, ColBitRShift,
ColBitLShift, ColBitXOR, ColBitOr:
default:
panic(fmt.Errorf("orm.ColValue wrong operator"))
}

View File

@ -1973,6 +1973,36 @@ func TestUpdate(t *testing.T) {
throwFail(t, err)
throwFail(t, AssertIs(num, 1))
num, err = qs.Filter("user_name", "slene").Update(Params{
"Nums": ColValue(ColBitAnd, 1),
})
throwFail(t, err)
throwFail(t, AssertIs(num, 1))
num, err = qs.Filter("user_name", "slene").Update(Params{
"Nums": ColValue(ColBitRShift, 1),
})
throwFail(t, err)
throwFail(t, AssertIs(num, 0))
num, err = qs.Filter("user_name", "slene").Update(Params{
"Nums": ColValue(ColBitLShift, 1),
})
throwFail(t, err)
throwFail(t, AssertIs(num, 0))
num, err = qs.Filter("user_name", "slene").Update(Params{
"Nums": ColValue(ColBitXOR, 1),
})
throwFail(t, err)
throwFail(t, AssertIs(num, 1))
num, err = qs.Filter("user_name", "slene").Update(Params{
"Nums": ColValue(ColBitOr, 1),
})
throwFail(t, err)
throwFail(t, AssertIs(num, 0))
user := User{UserName: "slene"}
err = dORM.Read(&user, "UserName")
throwFail(t, err)