Reputation: 21
In my golang program I'm trying to insert into a Oracle table having one of the columns of type DATE. I'm using godror library and inserting data using godror.Batch, if I have two records one having a date value and another with null value for DATE. I'm getting error reflect.Type is nil, not *reflect.rtype
To solve it I tried doing following:
// values is type []interface{} and contains the data to be inserted
for idx := range values {
fmt.Println(idx, colTypes[idx].DatabaseTypeName(), values[idx])
switch values[idx].(type) {
case nil:
if colTypes[idx].DatabaseTypeName() == "LONG RAW" {
values[idx] = []byte{} //For null BLOB data: gives panic in godror.batch.Add()
} else if colTypes[idx].DatabaseTypeName() == "DATE" {
values[idx] = sql.NullTime{}
} else {
values[idx] = ""
}
}
}
err = batchAdd.Add(ctx, values...)
if err != nil {
trx.Rollback()
return err
}
This solved issue for Varchar type columns but for Date I'm getting error: panic: reflect.Set: value of type time.Time is not assignable to type sql.NullTime
I want to insert null into the Date column, but since previous record contains time.Time type and other contains null, I'm getting above error.
Is there any workaround to this by using godror.Batch only, or I'm doing something wrong.
Go version: go version go1.20.6 linux/amd64 Oracle: oracle 19c
I tried to insert one record with null data and one record containing some data for DATE column using godror.Batch. I expected it to insert both the records, but it panics
Upvotes: 1
Views: 166
Reputation: 21
This is solved when I pass empty time.Time type assigning to a variable as follows:
if colTypes[idx].DatabaseTypeName() == "DATE" {
var val time.Time
values[idx] = val
}
This inserts (null) in the column with DATE data type
Upvotes: 0