From b36f8ed87eeddf8c1b4dbf1b3edcf6c6f905fa45 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 26 Nov 2015 15:52:25 +0800 Subject: [PATCH] bug fixed for #249 --- VERSION | 2 +- helpers.go | 21 +++++++++++++++++++++ session.go | 50 +++++--------------------------------------------- xorm.go | 2 +- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/VERSION b/VERSION index 77c58d3b..8b43e1ae 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.4.4.1124 +xorm v0.4.4.1126 diff --git a/helpers.go b/helpers.go index c83391ff..1ac6ad62 100644 --- a/helpers.go +++ b/helpers.go @@ -55,6 +55,27 @@ func isZero(k interface{}) bool { return false } +func int64ToInt(id int64, k reflect.Kind) interface{} { + var v interface{} = id + switch k { + case reflect.Int16: + v = int16(id) + case reflect.Int32: + v = int32(id) + case reflect.Int: + v = int(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) + } + return v +} + func isPKZero(pk core.PK) bool { for _, k := range pk { if isZero(k) { diff --git a/session.go b/session.go index a0e17eb9..0dfab59a 100644 --- a/session.go +++ b/session.go @@ -2171,7 +2171,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error return 0, err } fieldValue := *ptrFieldValue - if col.IsAutoIncrement && fieldValue.Int() == 0 { + if col.IsAutoIncrement && isZero(fieldValue.Interface()) { continue } if col.MapType == core.ONLYFROMDB { @@ -2219,7 +2219,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error } fieldValue := *ptrFieldValue - if col.IsAutoIncrement && fieldValue.Int() == 0 { + if col.IsAutoIncrement && isZero(fieldValue.Interface()) { continue } if col.MapType == core.ONLYFROMDB { @@ -3219,19 +3219,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - var v interface{} = id - switch aiValue.Type().Kind() { - case reflect.Int32: - v = int32(id) - case reflect.Int: - v = int(id) - case reflect.Uint32: - v = uint32(id) - case reflect.Uint64: - v = uint64(id) - case reflect.Uint: - v = uint(id) - } + v := int64ToInt(id, aiValue.Type().Kind()) aiValue.Set(reflect.ValueOf(v)) return 1, nil @@ -3278,19 +3266,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 1, nil } - var v interface{} = id - switch aiValue.Type().Kind() { - case reflect.Int32: - v = int32(id) - case reflect.Int: - v = int(id) - case reflect.Uint32: - v = uint32(id) - case reflect.Uint64: - v = uint64(id) - case reflect.Uint: - v = uint(id) - } + v := int64ToInt(id, aiValue.Type().Kind()) aiValue.Set(reflect.ValueOf(v)) return 1, nil @@ -3334,23 +3310,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return res.RowsAffected() } - var v interface{} = id - switch aiValue.Type().Kind() { - case reflect.Int16: - v = int16(id) - case reflect.Int32: - v = int32(id) - case reflect.Int: - v = int(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) - } + v := int64ToInt(id, aiValue.Type().Kind()) aiValue.Set(reflect.ValueOf(v)) return res.RowsAffected() diff --git a/xorm.go b/xorm.go index a97c62f7..88d23b81 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.4.1124" + Version string = "0.4.4.1126" ) func regDrvsNDialects() bool {