fix pk can't set value when pk is point value or rename (#588)

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

* Revert "fix pk can't set value when pk is point value"

This reverts commit e78c076e57.

* fix pk can't set value when pk is point value or rename

* remove trace

* fix insert nil pk in pg
This commit is contained in:
biless 2017-05-18 10:37:20 +08:00 committed by Lunny Xiao
parent 1ff7790a2e
commit a012756d38
2 changed files with 73 additions and 19 deletions

View File

@ -12,7 +12,6 @@ import (
"strconv"
"strings"
"time"
"github.com/go-xorm/core"
)
@ -196,25 +195,43 @@ func isArrayValueZero(v reflect.Value) bool {
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
var v interface{}
switch tp.Kind() {
case reflect.Int16:
v = int16(id)
case reflect.Int32:
v = int32(id)
case reflect.Int:
v = int(id)
case reflect.Int64:
v = id
case reflect.Uint16:
v = uint16(id)
case reflect.Uint32:
v = uint32(id)
case reflect.Uint64:
v = uint64(id)
case reflect.Uint:
v = uint(id)
kind := tp.Kind()
if kind == reflect.Ptr {
kind = tp.Elem().Kind()
}
return reflect.ValueOf(v).Convert(tp)
switch kind {
case reflect.Int16:
temp := int16(id)
v = &temp
case reflect.Int32:
temp := int32(id)
v = &temp
case reflect.Int:
temp := int(id)
v = &temp
case reflect.Int64:
temp := id
v = &temp
case reflect.Uint16:
temp := uint16(id)
v = &temp
case reflect.Uint32:
temp := uint32(id)
v = &temp
case reflect.Uint64:
temp := uint64(id)
v = &temp
case reflect.Uint:
temp := uint(id)
v = &temp
}
if tp.Kind() == reflect.Ptr {
return reflect.ValueOf(v).Convert(tp)
}
return reflect.ValueOf(v).Elem().Convert(tp)
}
func int64ToInt(id int64, tp reflect.Type) interface{} {
@ -537,6 +554,10 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
if len(fieldValue.String()) == 0 {
continue
}
case reflect.Ptr:
if fieldValue.Pointer() == 0 {
continue
}
}
}

View File

@ -26,3 +26,36 @@ 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)
}
func TestInsertOneIfPkIsPointRename (t *testing.T) {
assert.NoError(t, prepareEngine())
type ID *int64
type TestPoint struct {
Id ID `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)
}