orm fix when use uint as pk
Showing
6 changed files
with
94 additions
and
44 deletions
| ... | @@ -304,8 +304,12 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. | ... | @@ -304,8 +304,12 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. |
| 304 | 304 | ||
| 305 | if num > 0 { | 305 | if num > 0 { |
| 306 | if mi.fields.pk.auto { | 306 | if mi.fields.pk.auto { |
| 307 | if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 { | ||
| 308 | ind.Field(mi.fields.pk.fieldIndex).SetUint(0) | ||
| 309 | } else { | ||
| 307 | ind.Field(mi.fields.pk.fieldIndex).SetInt(0) | 310 | ind.Field(mi.fields.pk.fieldIndex).SetInt(0) |
| 308 | } | 311 | } |
| 312 | } | ||
| 309 | 313 | ||
| 310 | err := d.deleteRels(q, mi, []interface{}{pkValue}, tz) | 314 | err := d.deleteRels(q, mi, []interface{}{pkValue}, tz) |
| 311 | if err != nil { | 315 | if err != nil { | ... | ... |
| ... | @@ -10,7 +10,11 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac | ... | @@ -10,7 +10,11 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac |
| 10 | fi := mi.fields.pk | 10 | fi := mi.fields.pk |
| 11 | 11 | ||
| 12 | v := ind.Field(fi.fieldIndex) | 12 | v := ind.Field(fi.fieldIndex) |
| 13 | if fi.fieldType&IsIntegerField > 0 { | 13 | if fi.fieldType&IsPostiveIntegerField > 0 { |
| 14 | vu := v.Uint() | ||
| 15 | exist = vu > 0 | ||
| 16 | value = vu | ||
| 17 | } else if fi.fieldType&IsIntegerField > 0 { | ||
| 14 | vu := v.Int() | 18 | vu := v.Int() |
| 15 | exist = vu > 0 | 19 | exist = vu > 0 |
| 16 | value = vu | 20 | value = vu | ... | ... |
| ... | @@ -70,9 +70,13 @@ func (o *orm) Insert(md interface{}) (int64, error) { | ... | @@ -70,9 +70,13 @@ func (o *orm) Insert(md interface{}) (int64, error) { |
| 70 | } | 70 | } |
| 71 | if id > 0 { | 71 | if id > 0 { |
| 72 | if mi.fields.pk.auto { | 72 | if mi.fields.pk.auto { |
| 73 | if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 { | ||
| 74 | ind.Field(mi.fields.pk.fieldIndex).SetUint(uint64(id)) | ||
| 75 | } else { | ||
| 73 | ind.Field(mi.fields.pk.fieldIndex).SetInt(id) | 76 | ind.Field(mi.fields.pk.fieldIndex).SetInt(id) |
| 74 | } | 77 | } |
| 75 | } | 78 | } |
| 79 | } | ||
| 76 | return id, nil | 80 | return id, nil |
| 77 | } | 81 | } |
| 78 | 82 | ||
| ... | @@ -93,9 +97,13 @@ func (o *orm) Delete(md interface{}) (int64, error) { | ... | @@ -93,9 +97,13 @@ func (o *orm) Delete(md interface{}) (int64, error) { |
| 93 | } | 97 | } |
| 94 | if num > 0 { | 98 | if num > 0 { |
| 95 | if mi.fields.pk.auto { | 99 | if mi.fields.pk.auto { |
| 100 | if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 { | ||
| 101 | ind.Field(mi.fields.pk.fieldIndex).SetUint(0) | ||
| 102 | } else { | ||
| 96 | ind.Field(mi.fields.pk.fieldIndex).SetInt(0) | 103 | ind.Field(mi.fields.pk.fieldIndex).SetInt(0) |
| 97 | } | 104 | } |
| 98 | } | 105 | } |
| 106 | } | ||
| 99 | return num, nil | 107 | return num, nil |
| 100 | } | 108 | } |
| 101 | 109 | ... | ... |
| ... | @@ -34,9 +34,13 @@ func (o *insertSet) Insert(md interface{}) (int64, error) { | ... | @@ -34,9 +34,13 @@ func (o *insertSet) Insert(md interface{}) (int64, error) { |
| 34 | } | 34 | } |
| 35 | if id > 0 { | 35 | if id > 0 { |
| 36 | if o.mi.fields.pk.auto { | 36 | if o.mi.fields.pk.auto { |
| 37 | if o.mi.fields.pk.fieldType&IsPostiveIntegerField > 0 { | ||
| 38 | ind.Field(o.mi.fields.pk.fieldIndex).SetUint(uint64(id)) | ||
| 39 | } else { | ||
| 37 | ind.Field(o.mi.fields.pk.fieldIndex).SetInt(id) | 40 | ind.Field(o.mi.fields.pk.fieldIndex).SetInt(id) |
| 38 | } | 41 | } |
| 39 | } | 42 | } |
| 43 | } | ||
| 40 | return id, nil | 44 | return id, nil |
| 41 | } | 45 | } |
| 42 | 46 | ... | ... |
| ... | @@ -211,6 +211,7 @@ func TestRegisterModels(t *testing.T) { | ... | @@ -211,6 +211,7 @@ func TestRegisterModels(t *testing.T) { |
| 211 | RegisterModel(new(Post)) | 211 | RegisterModel(new(Post)) |
| 212 | RegisterModel(new(Tag)) | 212 | RegisterModel(new(Tag)) |
| 213 | RegisterModel(new(Comment)) | 213 | RegisterModel(new(Comment)) |
| 214 | RegisterModel(new(UserBig)) | ||
| 214 | 215 | ||
| 215 | BootStrap() | 216 | BootStrap() |
| 216 | 217 | ||
| ... | @@ -231,8 +232,7 @@ func TestModelSyntax(t *testing.T) { | ... | @@ -231,8 +232,7 @@ func TestModelSyntax(t *testing.T) { |
| 231 | } | 232 | } |
| 232 | } | 233 | } |
| 233 | 234 | ||
| 234 | func TestDataTypes(t *testing.T) { | 235 | var Data_Values = map[string]interface{}{ |
| 235 | values := map[string]interface{}{ | ||
| 236 | "Boolean": true, | 236 | "Boolean": true, |
| 237 | "Char": "char", | 237 | "Char": "char", |
| 238 | "Text": "text", | 238 | "Text": "text", |
| ... | @@ -253,11 +253,13 @@ func TestDataTypes(t *testing.T) { | ... | @@ -253,11 +253,13 @@ func TestDataTypes(t *testing.T) { |
| 253 | "Float32": float32(100.1234), | 253 | "Float32": float32(100.1234), |
| 254 | "Float64": float64(100.1234), | 254 | "Float64": float64(100.1234), |
| 255 | "Decimal": float64(100.1234), | 255 | "Decimal": float64(100.1234), |
| 256 | } | 256 | } |
| 257 | |||
| 258 | func TestDataTypes(t *testing.T) { | ||
| 257 | d := Data{} | 259 | d := Data{} |
| 258 | ind := reflect.Indirect(reflect.ValueOf(&d)) | 260 | ind := reflect.Indirect(reflect.ValueOf(&d)) |
| 259 | 261 | ||
| 260 | for name, value := range values { | 262 | for name, value := range Data_Values { |
| 261 | e := ind.FieldByName(name) | 263 | e := ind.FieldByName(name) |
| 262 | e.Set(reflect.ValueOf(value)) | 264 | e.Set(reflect.ValueOf(value)) |
| 263 | } | 265 | } |
| ... | @@ -272,7 +274,7 @@ func TestDataTypes(t *testing.T) { | ... | @@ -272,7 +274,7 @@ func TestDataTypes(t *testing.T) { |
| 272 | 274 | ||
| 273 | ind = reflect.Indirect(reflect.ValueOf(&d)) | 275 | ind = reflect.Indirect(reflect.ValueOf(&d)) |
| 274 | 276 | ||
| 275 | for name, value := range values { | 277 | for name, value := range Data_Values { |
| 276 | e := ind.FieldByName(name) | 278 | e := ind.FieldByName(name) |
| 277 | vu := e.Interface() | 279 | vu := e.Interface() |
| 278 | switch name { | 280 | switch name { |
| ... | @@ -376,6 +378,17 @@ func TestCRUD(t *testing.T) { | ... | @@ -376,6 +378,17 @@ func TestCRUD(t *testing.T) { |
| 376 | u = &User{Id: 100} | 378 | u = &User{Id: 100} |
| 377 | err = dORM.Read(u) | 379 | err = dORM.Read(u) |
| 378 | throwFail(t, AssertIs(err, T_Equal, ErrNoRows)) | 380 | throwFail(t, AssertIs(err, T_Equal, ErrNoRows)) |
| 381 | |||
| 382 | ub := UserBig{} | ||
| 383 | ub.Name = "name" | ||
| 384 | id, err = dORM.Insert(&ub) | ||
| 385 | throwFail(t, err) | ||
| 386 | throwFail(t, AssertIs(id, T_Equal, 1)) | ||
| 387 | |||
| 388 | ub = UserBig{Id: 1} | ||
| 389 | err = dORM.Read(&ub) | ||
| 390 | throwFail(t, err) | ||
| 391 | throwFail(t, AssertIs(ub.Name, T_Equal, "name")) | ||
| 379 | } | 392 | } |
| 380 | 393 | ||
| 381 | func TestInsertTestData(t *testing.T) { | 394 | func TestInsertTestData(t *testing.T) { |
| ... | @@ -823,7 +836,15 @@ func TestPrepareInsert(t *testing.T) { | ... | @@ -823,7 +836,15 @@ func TestPrepareInsert(t *testing.T) { |
| 823 | throwFail(t, AssertIs(err, T_Equal, ErrStmtClosed)) | 836 | throwFail(t, AssertIs(err, T_Equal, ErrStmtClosed)) |
| 824 | } | 837 | } |
| 825 | 838 | ||
| 826 | func TestRaw(t *testing.T) { | 839 | func TestRawQueryRow(t *testing.T) { |
| 840 | |||
| 841 | } | ||
| 842 | |||
| 843 | func TestRawQueryRows(t *testing.T) { | ||
| 844 | |||
| 845 | } | ||
| 846 | |||
| 847 | func TestRawValues(t *testing.T) { | ||
| 827 | switch { | 848 | switch { |
| 828 | case IsMysql || IsSqlite: | 849 | case IsMysql || IsSqlite: |
| 829 | 850 | ||
| ... | @@ -860,42 +881,7 @@ func TestRaw(t *testing.T) { | ... | @@ -860,42 +881,7 @@ func TestRaw(t *testing.T) { |
| 860 | if num == 3 { | 881 | if num == 3 { |
| 861 | throwFail(t, AssertIs(list[0], T_Equal, "2")) | 882 | throwFail(t, AssertIs(list[0], T_Equal, "2")) |
| 862 | throwFail(t, AssertIs(list[1], T_Equal, "3")) | 883 | throwFail(t, AssertIs(list[1], T_Equal, "3")) |
| 863 | throwFail(t, AssertIs(list[2], T_Equal, "")) | 884 | throwFail(t, AssertIs(list[2], T_Equal, nil)) |
| 864 | } | ||
| 865 | |||
| 866 | pre, err := dORM.Raw("INSERT INTO tag (name) VALUES (?)").Prepare() | ||
| 867 | throwFail(t, err) | ||
| 868 | if pre != nil { | ||
| 869 | r, err := pre.Exec("name1") | ||
| 870 | throwFail(t, err) | ||
| 871 | |||
| 872 | tid, err := r.LastInsertId() | ||
| 873 | throwFail(t, err) | ||
| 874 | throwFail(t, AssertIs(tid, T_Large, 0)) | ||
| 875 | |||
| 876 | r, err = pre.Exec("name2") | ||
| 877 | throwFail(t, err) | ||
| 878 | |||
| 879 | id, err := r.LastInsertId() | ||
| 880 | throwFail(t, err) | ||
| 881 | throwFail(t, AssertIs(id, T_Equal, tid+1)) | ||
| 882 | |||
| 883 | r, err = pre.Exec("name3") | ||
| 884 | throwFail(t, err) | ||
| 885 | |||
| 886 | id, err = r.LastInsertId() | ||
| 887 | throwFail(t, err) | ||
| 888 | throwFail(t, AssertIs(id, T_Equal, tid+2)) | ||
| 889 | |||
| 890 | err = pre.Close() | ||
| 891 | throwFail(t, err) | ||
| 892 | |||
| 893 | res, err := dORM.Raw("DELETE FROM tag WHERE name IN (?, ?, ?)", []string{"name1", "name2", "name3"}).Exec() | ||
| 894 | throwFail(t, err) | ||
| 895 | |||
| 896 | num, err := res.RowsAffected() | ||
| 897 | throwFail(t, err) | ||
| 898 | throwFail(t, AssertIs(num, T_Equal, 3)) | ||
| 899 | } | 885 | } |
| 900 | 886 | ||
| 901 | case IsPostgres: | 887 | case IsPostgres: |
| ... | @@ -933,8 +919,51 @@ func TestRaw(t *testing.T) { | ... | @@ -933,8 +919,51 @@ func TestRaw(t *testing.T) { |
| 933 | if num == 3 { | 919 | if num == 3 { |
| 934 | throwFail(t, AssertIs(list[0], T_Equal, "2")) | 920 | throwFail(t, AssertIs(list[0], T_Equal, "2")) |
| 935 | throwFail(t, AssertIs(list[1], T_Equal, "3")) | 921 | throwFail(t, AssertIs(list[1], T_Equal, "3")) |
| 936 | throwFail(t, AssertIs(list[2], T_Equal, "")) | 922 | throwFail(t, AssertIs(list[2], T_Equal, nil)) |
| 923 | } | ||
| 937 | } | 924 | } |
| 925 | } | ||
| 926 | |||
| 927 | func TestRawPrepare(t *testing.T) { | ||
| 928 | switch { | ||
| 929 | case IsMysql || IsSqlite: | ||
| 930 | |||
| 931 | pre, err := dORM.Raw("INSERT INTO tag (name) VALUES (?)").Prepare() | ||
| 932 | throwFail(t, err) | ||
| 933 | if pre != nil { | ||
| 934 | r, err := pre.Exec("name1") | ||
| 935 | throwFail(t, err) | ||
| 936 | |||
| 937 | tid, err := r.LastInsertId() | ||
| 938 | throwFail(t, err) | ||
| 939 | throwFail(t, AssertIs(tid, T_Large, 0)) | ||
| 940 | |||
| 941 | r, err = pre.Exec("name2") | ||
| 942 | throwFail(t, err) | ||
| 943 | |||
| 944 | id, err := r.LastInsertId() | ||
| 945 | throwFail(t, err) | ||
| 946 | throwFail(t, AssertIs(id, T_Equal, tid+1)) | ||
| 947 | |||
| 948 | r, err = pre.Exec("name3") | ||
| 949 | throwFail(t, err) | ||
| 950 | |||
| 951 | id, err = r.LastInsertId() | ||
| 952 | throwFail(t, err) | ||
| 953 | throwFail(t, AssertIs(id, T_Equal, tid+2)) | ||
| 954 | |||
| 955 | err = pre.Close() | ||
| 956 | throwFail(t, err) | ||
| 957 | |||
| 958 | res, err := dORM.Raw("DELETE FROM tag WHERE name IN (?, ?, ?)", []string{"name1", "name2", "name3"}).Exec() | ||
| 959 | throwFail(t, err) | ||
| 960 | |||
| 961 | num, err := res.RowsAffected() | ||
| 962 | throwFail(t, err) | ||
| 963 | throwFail(t, AssertIs(num, T_Equal, 3)) | ||
| 964 | } | ||
| 965 | |||
| 966 | case IsPostgres: | ||
| 938 | 967 | ||
| 939 | pre, err := dORM.Raw(`INSERT INTO "tag" ("name") VALUES (?) RETURNING "id"`).Prepare() | 968 | pre, err := dORM.Raw(`INSERT INTO "tag" ("name") VALUES (?) RETURNING "id"`).Prepare() |
| 940 | throwFail(t, err) | 969 | throwFail(t, err) | ... | ... |
-
Please register or sign in to post a comment