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

@@ -143,6 +143,7 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
return n, err
}
if int64(m) != want {
// notest // Write misbehaving
return n, io.ErrShortWrite
}

View File

@@ -176,7 +176,7 @@ func (ctx Context) ResultJSON(value any) {
data, err := json.Marshal(value)
if err != nil {
ctx.ResultError(err)
return
return // notest
}
ctx.ResultRawText(data)
}

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)
}

View File

@@ -29,7 +29,7 @@ func writefile(ctx sqlite3.Context, arg ...sqlite3.Value) {
n, err := createFileAndDir(file, mode, arg[1])
if err != nil {
if len(arg) > 2 {
ctx.ResultError(fmt.Errorf("writefile: %w", err))
ctx.ResultError(fmt.Errorf("writefile: %w", err)) // notest
}
return
}

2
txn.go
View File

@@ -230,7 +230,7 @@ func (c *Conn) txnExecInterrupted(sql string) error {
return err
}
// TxnState starts a deferred transaction.
// TxnState determines the transaction state of a database.
//
// https://sqlite.org/c3ref/txn_state.html
func (c *Conn) TxnState(schema string) TxnState {