Copy blobs.

This commit is contained in:
Nuno Cruces
2024-08-01 18:31:25 +01:00
parent 6c12a8c1fa
commit a6815531e0
6 changed files with 23 additions and 7 deletions

View File

@@ -81,7 +81,11 @@ func writeblob(ctx sqlite3.Context, arg ...sqlite3.Value) {
return // notest
}
_, err = blob.Write(arg[5].RawBlob())
if p, ok := arg[5].Pointer().(io.Reader); ok {
_, err = blob.ReadFrom(p)
} else {
_, err = blob.Write(arg[5].RawBlob())
}
if err != nil {
ctx.ResultError(err)
return // notest

View File

@@ -5,6 +5,7 @@ import (
"log"
"os"
"reflect"
"strings"
"testing"
"github.com/ncruces/go-sqlite3"
@@ -47,7 +48,7 @@ func Example() {
// Read the BLOB.
_, err = db.Exec(`SELECT openblob('main', 'test', 'col', rowid, false, ?) FROM test`,
sqlite3.Pointer[blobio.OpenCallback](func(blob *sqlite3.Blob, _ ...sqlite3.Value) error {
_, err = io.Copy(os.Stdout, blob)
_, err = blob.WriteTo(os.Stdout)
return err
}))
if err != nil {
@@ -181,7 +182,6 @@ func Test_writeblob(t *testing.T) {
err = db.Exec(`
CREATE TABLE test (col);
INSERT INTO test VALUES (x'cafe');
-- INSERT INTO test2 VALUES (x'babe');
`)
if err != nil {
t.Fatal(err)
@@ -194,7 +194,18 @@ func Test_writeblob(t *testing.T) {
t.Log(err)
}
err = db.Exec(`SELECT writeblob('main', 'test', 'col', 1, 0, x'babe')`)
stmt, _, err := db.Prepare(`SELECT writeblob('main', 'test', 'col', 1, 0, ?)`)
if err != nil {
t.Log(err)
}
defer stmt.Close()
err = stmt.BindPointer(1, strings.NewReader("\xba\xbe"))
if err != nil {
t.Log(err)
}
err = stmt.Exec()
if err != nil {
t.Log(err)
}