For nullable columns, store nil values as NULL

This commit is contained in:
Jim Salem 2017-02-14 16:46:52 -05:00
parent 19f6dfc2e8
commit 14356cabc8
1 changed files with 12 additions and 1 deletions

View File

@ -147,6 +147,17 @@ func isZero(k interface{}) bool {
return false 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 { func isStructZero(v reflect.Value) bool {
if !v.IsValid() { if !v.IsValid() {
return true return true
@ -539,7 +550,7 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
// !evalphobia! set fieldValue as nil when column is nullable and zero-value // !evalphobia! set fieldValue as nil when column is nullable and zero-value
if _, ok := getFlagForColumn(session.Statement.nullableMap, col); ok { if _, ok := getFlagForColumn(session.Statement.nullableMap, col); ok {
if col.Nullable && isZero(fieldValue.Interface()) { if col.Nullable && isZeroValue(fieldValue) {
var nilValue *int var nilValue *int
fieldValue = reflect.ValueOf(nilValue) fieldValue = reflect.ValueOf(nilValue)
} }