Skip to content

Commit 5973de7

Browse files
committed
Added support for ZeroBlob
1 parent 6a8d29a commit 5973de7

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

goliat.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ const (
7474
DONE ErrorCode = C.SQLITE_DONE // 101
7575
)
7676

77+
type ZeroBlob struct {
78+
Size uint64
79+
}
80+
7781
type DatabaseError struct {
7882
Code ErrorCode
7983
Message string
@@ -379,6 +383,10 @@ func (b *BindValue) SetNull() {
379383
b.value = nil
380384
}
381385

386+
func (b *BindValue) SetZeroBlob(size uint64) {
387+
b.value = ZeroBlob{Size: size}
388+
}
389+
382390
type BindHandler interface {
383391
ToSQLiteValue() BindValue
384392
}
@@ -426,6 +434,8 @@ func (stmt *Statement) BindValue(index int, value any) error {
426434
C.sqlite3_bind_text(stmt.h.ptr, C.int(index), cvalue.h.ptr, -1, C.transient)
427435
case nil:
428436
C.sqlite3_bind_null(stmt.h.ptr, C.int(index))
437+
case ZeroBlob:
438+
C.sqlite3_bind_zeroblob64(stmt.h.ptr, C.int(index), C.sqlite3_uint64(v.Size))
429439
default:
430440
if handler, ok := value.(BindHandler); ok {
431441
return stmt.BindValue(index, handler.ToSQLiteValue().value)

goliat_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,23 @@ func TestQueryRowShouldErrorNotFoundIfNoResult(t *testing.T) {
468468
assert.Error(t, err)
469469
assert.ErrorIs(t, err, goliat.ErrNoRows)
470470
}
471+
472+
func TestBindZeroBlob(t *testing.T) {
473+
db, err := goliat.Open(":memory:")
474+
assert.NoError(t, err)
475+
defer db.Close()
476+
477+
err = db.Exec("CREATE TABLE foo (bar BLOB)")
478+
assert.NoError(t, err)
479+
480+
err = db.Exec("INSERT INTO foo (bar) VALUES (?)", goliat.ZeroBlob{Size: 10})
481+
assert.NoError(t, err)
482+
483+
var data []byte
484+
err = db.QueryRow("SELECT bar FROM foo").Scan(&data)
485+
assert.NoError(t, err)
486+
assert.Equal(t, 10, len(data))
487+
for _, b := range data {
488+
assert.Equal(t, byte(0), b)
489+
}
490+
}

0 commit comments

Comments
 (0)