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:
10
orm/db.go
10
orm/db.go
@ -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 {
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user