Skip to content

Commit

Permalink
Copy blobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Aug 2, 2024
1 parent 6c12a8c commit a681553
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion ext/blobio/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions ext/blobio/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"reflect"
"strings"
"testing"

"github.com/ncruces/go-sqlite3"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion ext/fileio/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a681553

Please sign in to comment.