Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

For nullable columns, store nil values as NULL #531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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
Expand Down Expand Up @@ -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
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)
}
Expand Down