diff --git a/helpers.go b/helpers.go index 324c5bea..13587f9e 100644 --- a/helpers.go +++ b/helpers.go @@ -194,31 +194,57 @@ func isArrayValueZero(v reflect.Value) bool { return true } -func int64ToIntValue(id int64, tp reflect.Type) reflect.Value { +func int64ToIntValue(id int64, tv *reflect.Value) reflect.Value { var v interface{} - switch tp.Kind() { - case reflect.Int16: + + switch tv.Interface().(type) { + case int16: v = int16(id) - case reflect.Int32: + case int32: v = int32(id) - case reflect.Int: + case int: v = int(id) - case reflect.Int64: - v = id - case reflect.Uint16: + case int64: + v = int64(id) + case uint16: v = uint16(id) - case reflect.Uint32: + case uint32: v = uint32(id) - case reflect.Uint64: + case uint64: v = uint64(id) - case reflect.Uint: + case uint: v = uint(id) + case *int16: + temp := int16(id) + v = &temp + case *int32: + temp := int32(id) + v = &temp + case *int: + temp := int(id) + v = &temp + case *int64: + temp := int64(id) + v = &temp + case *uint16: + temp := uint16(id) + v = &temp + case *uint32: + temp := uint32(id) + v = &temp + case *uint: + temp := uint(id) + v = &temp + case *uint64: + temp := uint64(id) + v = &temp } - return reflect.ValueOf(v).Convert(tp) + + return reflect.ValueOf(v).Convert(tv.Type()) } -func int64ToInt(id int64, tp reflect.Type) interface{} { - return int64ToIntValue(id, tp).Interface() +func int64ToInt(id int64, tv *reflect.Value) interface{} { + return int64ToIntValue(id, tv).Interface() } func isPKZero(pk core.PK) bool { diff --git a/session_insert.go b/session_insert.go index 2c8ad782..4b2b157b 100644 --- a/session_insert.go +++ b/session_insert.go @@ -424,7 +424,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - aiValue.Set(int64ToIntValue(id, aiValue.Type())) + aiValue.Set(int64ToIntValue(id, aiValue)) return 1, nil } else if session.Engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 { @@ -469,7 +469,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - aiValue.Set(int64ToIntValue(id, aiValue.Type())) + aiValue.Set(int64ToIntValue(id, aiValue)) return 1, nil } else { @@ -512,7 +512,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return res.RowsAffected() } - aiValue.Set(int64ToIntValue(id, aiValue.Type())) + aiValue.Set(int64ToIntValue(id, aiValue)) return res.RowsAffected() } diff --git a/session_insert_test.go b/session_insert_test.go index b232d3f7..12126190 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -26,3 +26,19 @@ func TestInsertOne(t *testing.T) { _, err := testEngine.InsertOne(data) assert.NoError(t, err) } + +func TestInsertOneIfPkIsPoint(t *testing.T) { + assert.NoError(t, prepareEngine()) + + type TestPoint struct { + Id *int64 `xorm:"autoincr pk notnull 'id'"` + Msg *string `xorm:"varchar(255)"` + Created *time.Time `xorm:"created"` + } + + assert.NoError(t, testEngine.Sync2(new(TestPoint))) + msg := "hi" + data := TestPoint{Msg: &msg} + _, err := testEngine.InsertOne(&data) + assert.NoError(t, err) +} \ No newline at end of file