8f5ca303 by slene

orm fix when use uint as pk

1 parent dc8f9320
...@@ -304,7 +304,11 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. ...@@ -304,7 +304,11 @@ 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 ind.Field(mi.fields.pk.fieldIndex).SetInt(0) 307 if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
308 ind.Field(mi.fields.pk.fieldIndex).SetUint(0)
309 } else {
310 ind.Field(mi.fields.pk.fieldIndex).SetInt(0)
311 }
308 } 312 }
309 313
310 err := d.deleteRels(q, mi, []interface{}{pkValue}, tz) 314 err := d.deleteRels(q, mi, []interface{}{pkValue}, tz)
......
...@@ -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
......
...@@ -60,7 +60,8 @@ type DataNull struct { ...@@ -60,7 +60,8 @@ type DataNull struct {
60 60
61 // only for mysql 61 // only for mysql
62 type UserBig struct { 62 type UserBig struct {
63 Id uint64 63 Id uint64
64 Name string
64 } 65 }
65 66
66 type User struct { 67 type User struct {
......
...@@ -70,7 +70,11 @@ func (o *orm) Insert(md interface{}) (int64, error) { ...@@ -70,7 +70,11 @@ 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 ind.Field(mi.fields.pk.fieldIndex).SetInt(id) 73 if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
74 ind.Field(mi.fields.pk.fieldIndex).SetUint(uint64(id))
75 } else {
76 ind.Field(mi.fields.pk.fieldIndex).SetInt(id)
77 }
74 } 78 }
75 } 79 }
76 return id, nil 80 return id, nil
...@@ -93,7 +97,11 @@ func (o *orm) Delete(md interface{}) (int64, error) { ...@@ -93,7 +97,11 @@ 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 {
96 ind.Field(mi.fields.pk.fieldIndex).SetInt(0) 100 if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
101 ind.Field(mi.fields.pk.fieldIndex).SetUint(0)
102 } else {
103 ind.Field(mi.fields.pk.fieldIndex).SetInt(0)
104 }
97 } 105 }
98 } 106 }
99 return num, nil 107 return num, nil
......
...@@ -34,7 +34,11 @@ func (o *insertSet) Insert(md interface{}) (int64, error) { ...@@ -34,7 +34,11 @@ 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 ind.Field(o.mi.fields.pk.fieldIndex).SetInt(id) 37 if o.mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
38 ind.Field(o.mi.fields.pk.fieldIndex).SetUint(uint64(id))
39 } else {
40 ind.Field(o.mi.fields.pk.fieldIndex).SetInt(id)
41 }
38 } 42 }
39 } 43 }
40 return id, nil 44 return id, nil
......
...@@ -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,33 +232,34 @@ func TestModelSyntax(t *testing.T) { ...@@ -231,33 +232,34 @@ func TestModelSyntax(t *testing.T) {
231 } 232 }
232 } 233 }
233 234
235 var Data_Values = map[string]interface{}{
236 "Boolean": true,
237 "Char": "char",
238 "Text": "text",
239 "Date": time.Now(),
240 "DateTime": time.Now(),
241 "Byte": byte(1<<8 - 1),
242 "Rune": rune(1<<31 - 1),
243 "Int": int(1<<31 - 1),
244 "Int8": int8(1<<7 - 1),
245 "Int16": int16(1<<15 - 1),
246 "Int32": int32(1<<31 - 1),
247 "Int64": int64(1<<63 - 1),
248 "Uint": uint(1<<32 - 1),
249 "Uint8": uint8(1<<8 - 1),
250 "Uint16": uint16(1<<16 - 1),
251 "Uint32": uint32(1<<32 - 1),
252 "Uint64": uint64(1<<63 - 1), // uint64 values with high bit set are not supported
253 "Float32": float32(100.1234),
254 "Float64": float64(100.1234),
255 "Decimal": float64(100.1234),
256 }
257
234 func TestDataTypes(t *testing.T) { 258 func TestDataTypes(t *testing.T) {
235 values := map[string]interface{}{
236 "Boolean": true,
237 "Char": "char",
238 "Text": "text",
239 "Date": time.Now(),
240 "DateTime": time.Now(),
241 "Byte": byte(1<<8 - 1),
242 "Rune": rune(1<<31 - 1),
243 "Int": int(1<<31 - 1),
244 "Int8": int8(1<<7 - 1),
245 "Int16": int16(1<<15 - 1),
246 "Int32": int32(1<<31 - 1),
247 "Int64": int64(1<<63 - 1),
248 "Uint": uint(1<<32 - 1),
249 "Uint8": uint8(1<<8 - 1),
250 "Uint16": uint16(1<<16 - 1),
251 "Uint32": uint32(1<<32 - 1),
252 "Uint64": uint64(1<<63 - 1), // uint64 values with high bit set are not supported
253 "Float32": float32(100.1234),
254 "Float64": float64(100.1234),
255 "Decimal": float64(100.1234),
256 }
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,9 +919,52 @@ func TestRaw(t *testing.T) { ...@@ -933,9 +919,52 @@ 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 }
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))
937 } 964 }
938 965
966 case IsPostgres:
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)
941 if pre != nil { 970 if pre != nil {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!