For nullable columns, store nil values as NULL

This commit is contained in:
Jim Salem 2017-02-14 16:46:52 -05:00 committed by Lunny Xiao
parent 20f3d68709
commit ea07439552
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 13 additions and 2 deletions

View File

@ -155,6 +155,17 @@ func isZero(k interface{}) bool {
return false
}
func isZeroValue(v reflect.Value) bool {
if isZero(v.Interface()) {
return true
}
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
}
return false
}
func isStructZero(v reflect.Value) bool {
if !v.IsValid() {
return true

View File

@ -674,7 +674,7 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok {
if col.Nullable && isZero(fieldValue.Interface()) {
if col.Nullable && isZeroValue(fieldValue) {
var nilValue *int
fieldValue = reflect.ValueOf(nilValue)
}

View File

@ -506,7 +506,7 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok {
if col.Nullable && isZero(fieldValue.Interface()) {
if col.Nullable && isZeroValue(fieldValue) {
var nilValue *int
fieldValue = reflect.ValueOf(nilValue)
}