crawshaw / sqlite

Go SQLite3 driver
ISC License
571 stars 68 forks source link

BindBytes stores empty blobs as NULL #117

Closed israel-lugo closed 2 years ago

israel-lugo commented 3 years ago

stmt.BindBytes(col, make([]byte, 0)) should store an empty blob. It's storing a NULL instead. This is breaking code that needs to differentiate between an empty value and a nonexistent value.

This is probably because we're passing a NULL pointer to C.transient_bind_blob:

func (stmt *Stmt) BindBytes(param int, value []byte) {
        if stmt.stmt == nil {
                return
        }
        var v *C.uchar
        if len(value) != 0 {
                v = (*C.uchar)(unsafe.Pointer(&value[0]))
        }
        res := C.transient_bind_blob(stmt.stmt, C.int(param), v, C.int(len(value)))
        runtime.KeepAlive(value)
        stmt.handleBindErr("BindBytes", res)
}

I suggest doing the same thing that was done in BindText i.e. have an empty array which gets passed when len(value) == 0, and is not freed.

AdamSLevy commented 3 years ago

Fully support this. Feel free to submit a PR.

israel-lugo commented 3 years ago

Fully support this. Feel free to submit a PR.

Great, thanks! PR is in :)