Skip to content

Add Blob I/O #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
94 changes: 94 additions & 0 deletions blob_io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2022 Yasuhiro Matsumoto <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package sqlite3

/*
#ifndef USE_LIBSQLITE3
#include "sqlite3-binding.h"
#else
#include <sqlite3.h>
#endif
#include <stdlib.h>
*/
import "C"

import (
"io"
"runtime"
"unsafe"
)

// SQLiteBlob implements the SQLite Blob I/O interface.
type SQLiteBlob struct {
conn *SQLiteConn
blob *C.sqlite3_blob
size int
offs int
}

// Blob opens a blob.
//
// The flag parameter is ignored.
func (conn *SQLiteConn) Blob(database, table, column string, rowid int64, flags int) (*SQLiteBlob, error) {
databaseptr := C.CString(database)
defer C.free(unsafe.Pointer(databaseptr))

tableptr := C.CString(table)
defer C.free(unsafe.Pointer(tableptr))

columnptr := C.CString(column)
defer C.free(unsafe.Pointer(columnptr))

var blob *C.sqlite3_blob
ret := C.sqlite3_blob_open(conn.db, databaseptr, tableptr, columnptr, C.longlong(rowid), C.int(flags), &blob)

if ret == C.SQLITE_OK {
size := int(C.sqlite3_blob_bytes(blob))
bb := &SQLiteBlob{conn, blob, size, 0}

runtime.SetFinalizer(bb, (*SQLiteBlob).Close)

return bb, nil
}

return nil, conn.lastError()
}

// Read implements the io.Reader interface.
func (s *SQLiteBlob) Read(b []byte) (n int, err error) {
if s.offs >= s.size {
return 0, io.EOF
}

n = s.size - s.offs
if len(b) < n {
n = len(b)
}

p := &b[0]
ret := C.sqlite3_blob_read(s.blob, unsafe.Pointer(p), C.int(n), C.int(s.offs))
if ret != C.SQLITE_OK {
return 0, s.conn.lastError()
}

s.offs += n

return n, nil
}

// Close implements the io.Closer interface.
func (s *SQLiteBlob) Close() error {
ret := C.sqlite3_blob_close(s.blob)

s.blob = nil
runtime.SetFinalizer(s, nil)

if ret != C.SQLITE_OK {
return s.conn.lastError()
}

return nil
}
94 changes: 94 additions & 0 deletions blob_io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2022 Yasuhiro Matsumoto <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

//go:build cgo
// +build cgo

package sqlite3

import (
"bytes"
"database/sql"
"io"
"testing"
)

func TestBlobIO(t *testing.T) {
driverName := "sqlite3_TestBlobIO2"
var driverConn *SQLiteConn
sql.Register(driverName, &SQLiteDriver{
ConnectHook: func(conn *SQLiteConn) error {
driverConn = conn
return nil
}})

db, err := sql.Open(driverName, ":memory:")
if err != nil {
t.Fatal("Fail to open:", err)
}
defer db.Close()

// Test data
expected := []byte("I ❤️ SQLite in \x00\x01\x02…")
rowid := int64(6581)

query := `
CREATE TABLE data (
value BLOB
);

INSERT INTO data (_rowid_, value)
VALUES (:rowid, :value);
`

_, err = db.Exec(query, sql.Named("rowid", rowid), sql.Named("value", expected))
if err != nil {
t.Fatal("Failed to execute", err)
}

// Open blob
blob, err := driverConn.Blob("main", "data", "value", rowid, 0)
if err != nil {
t.Error("failed", err)
}
defer blob.Close()

// Read blob incrementally
middle := len(expected) / 2
first := expected[:middle]
second := expected[middle:]

// Read part Ⅰ
b1 := make([]byte, len(first))
n1, err := blob.Read(b1)

if err != nil || n1 != len(b1) {
t.Errorf("Failed to read %d bytes", n1)
}

if bytes.Compare(first, b1) != 0 {
t.Error("Expected\n", first, "got\n", b1)
}

// Read part Ⅱ
b2 := make([]byte, len(second))
n2, err := blob.Read(b2)

if err != nil || n2 != len(b2) {
t.Errorf("Failed to read %d bytes", n2)
}

if bytes.Compare(second, b2) != 0 {
t.Error("Expected\n", second, "got\n", b2)
}

// EOF
b3 := make([]byte, 10)
n3, err := blob.Read(b3)

if err != io.EOF || n3 != 0 {
t.Error("Expected EOF", err)
}
}