fix pk can't set value when pk is point value

This commit is contained in:
biless 2017-05-16 16:46:33 +08:00
parent 4a2a924369
commit e78c076e57
3 changed files with 59 additions and 17 deletions

View File

@ -194,31 +194,57 @@ func isArrayValueZero(v reflect.Value) bool {
return true return true
} }
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value { func int64ToIntValue(id int64, tv *reflect.Value) reflect.Value {
var v interface{} var v interface{}
switch tp.Kind() {
case reflect.Int16: switch tv.Interface().(type) {
case int16:
v = int16(id) v = int16(id)
case reflect.Int32: case int32:
v = int32(id) v = int32(id)
case reflect.Int: case int:
v = int(id) v = int(id)
case reflect.Int64: case int64:
v = id v = int64(id)
case reflect.Uint16: case uint16:
v = uint16(id) v = uint16(id)
case reflect.Uint32: case uint32:
v = uint32(id) v = uint32(id)
case reflect.Uint64: case uint64:
v = uint64(id) v = uint64(id)
case reflect.Uint: case uint:
v = uint(id) 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{} { func int64ToInt(id int64, tv *reflect.Value) interface{} {
return int64ToIntValue(id, tp).Interface() return int64ToIntValue(id, tv).Interface()
} }
func isPKZero(pk core.PK) bool { func isPKZero(pk core.PK) bool {

View File

@ -424,7 +424,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
return 1, nil return 1, nil
} }
aiValue.Set(int64ToIntValue(id, aiValue.Type())) aiValue.Set(int64ToIntValue(id, aiValue))
return 1, nil return 1, nil
} else if session.Engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 { } 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 return 1, nil
} }
aiValue.Set(int64ToIntValue(id, aiValue.Type())) aiValue.Set(int64ToIntValue(id, aiValue))
return 1, nil return 1, nil
} else { } else {
@ -512,7 +512,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
return res.RowsAffected() return res.RowsAffected()
} }
aiValue.Set(int64ToIntValue(id, aiValue.Type())) aiValue.Set(int64ToIntValue(id, aiValue))
return res.RowsAffected() return res.RowsAffected()
} }

View File

@ -26,3 +26,19 @@ func TestInsertOne(t *testing.T) {
_, err := testEngine.InsertOne(data) _, err := testEngine.InsertOne(data)
assert.NoError(t, err) 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)
}