From 2fd9dfca7b15af497e0098bce2b25dbc92266626 Mon Sep 17 00:00:00 2001 From: harry890829 Date: Mon, 18 May 2020 19:16:50 +0800 Subject: [PATCH] update support bit operation --- go.mod | 5 +++-- go.sum | 6 ++++++ orm/db.go | 10 ++++++++++ orm/orm_queryset.go | 8 +++++++- orm/orm_test.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9468c1b6..0bf4e9d3 100644 --- a/go.mod +++ b/go.mod @@ -15,13 +15,14 @@ require ( github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 // indirect github.com/elazarl/go-bindata-assetfs v1.0.0 github.com/go-redis/redis v6.14.2+incompatible - github.com/go-sql-driver/mysql v1.4.1 + github.com/go-sql-driver/mysql v1.5.0 github.com/gogo/protobuf v1.1.1 github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect github.com/gomodule/redigo v2.0.0+incompatible github.com/lib/pq v1.0.0 - github.com/mattn/go-sqlite3 v1.10.0 + github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/pelletier/go-toml v1.2.0 // indirect + github.com/pingcap/tidb v2.0.11+incompatible // indirect github.com/pkg/errors v0.8.0 // indirect github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373 diff --git a/go.sum b/go.sum index 1fe5e032..a685ad56 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiN github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d h1:xy93KVe+KrIIwWDEAfQBdIfsiHJkepbYsDr+VY3g9/o= github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= @@ -43,8 +45,12 @@ github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= +github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pingcap/tidb v2.0.11+incompatible h1:Shz+ry1DzQNsPk1QAejnM+5tgjbwZuzPnIER5aCjQ6c= +github.com/pingcap/tidb v2.0.11+incompatible/go.mod h1:I8C6jrPINP2rrVunTRd7C9fRRhQrtR43S1/CL5ix/yQ= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 h1:xT+JlYxNGqyT+XcU8iUrN18JYed2TvG9yN5ULG2jATM= diff --git a/orm/db.go b/orm/db.go index 2148daaa..50e57925 100644 --- a/orm/db.go +++ b/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 { diff --git a/orm/orm_queryset.go b/orm/orm_queryset.go index 7f2fdb8f..878b836b 100644 --- a/orm/orm_queryset.go +++ b/orm/orm_queryset.go @@ -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")) } diff --git a/orm/orm_test.go b/orm/orm_test.go index bdb430b6..eff6d602 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -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)