Files
sqlite3/blob.go

261 lines
5.4 KiB
Go
Raw Normal View History

2023-02-22 14:19:56 +00:00
package sqlite3
2023-03-29 15:01:25 +01:00
import (
"io"
"github.com/ncruces/go-sqlite3/internal/util"
)
2023-02-27 04:08:38 +00:00
2023-02-22 14:19:56 +00:00
// ZeroBlob represents a zero-filled, length n BLOB
// that can be used as an argument to
2023-02-22 17:51:30 +00:00
// [database/sql.DB.Exec] and similar methods.
2023-02-22 14:19:56 +00:00
type ZeroBlob int64
2023-02-27 04:08:38 +00:00
2023-05-11 15:19:57 +01:00
// Blob is an handle to an open BLOB.
2023-03-07 14:19:22 +00:00
//
2023-03-01 10:34:08 +00:00
// It implements [io.ReadWriteSeeker] for incremental BLOB I/O.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob.html
2023-02-27 04:08:38 +00:00
type Blob struct {
c *Conn
bytes int64
offset int64
2023-04-11 15:33:38 +01:00
handle uint32
2024-09-10 07:41:35 +01:00
bufptr uint32
buflen int64
2023-02-27 04:08:38 +00:00
}
var _ io.ReadWriteSeeker = &Blob{}
// OpenBlob opens a BLOB for incremental I/O.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_open.html
2023-02-27 13:45:32 +00:00
func (c *Conn) OpenBlob(db, table, column string, row int64, write bool) (*Blob, error) {
2023-11-29 10:38:03 +00:00
defer c.arena.mark()()
2023-02-27 04:08:38 +00:00
blobPtr := c.arena.new(ptrlen)
dbPtr := c.arena.string(db)
tablePtr := c.arena.string(table)
columnPtr := c.arena.string(column)
var flags uint64
if write {
flags = 1
}
2024-10-04 13:31:53 +01:00
c.checkInterrupt(c.handle)
2023-11-30 17:52:35 +00:00
r := c.call("sqlite3_blob_open", uint64(c.handle),
2023-02-27 04:08:38 +00:00
uint64(dbPtr), uint64(tablePtr), uint64(columnPtr),
2023-02-27 13:45:32 +00:00
uint64(row), flags, uint64(blobPtr))
2023-02-27 04:08:38 +00:00
2023-05-25 13:17:44 +01:00
if err := c.error(r); err != nil {
2023-02-27 04:08:38 +00:00
return nil, err
}
blob := Blob{c: c}
2023-03-29 15:01:25 +01:00
blob.handle = util.ReadUint32(c.mod, blobPtr)
2023-11-30 17:52:35 +00:00
blob.bytes = int64(c.call("sqlite3_blob_bytes", uint64(blob.handle)))
2023-02-27 04:08:38 +00:00
return &blob, nil
}
// Close closes a BLOB handle.
//
2023-03-01 10:34:08 +00:00
// It is safe to close a nil, zero or closed Blob.
2023-02-27 04:08:38 +00:00
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_close.html
2023-02-27 04:08:38 +00:00
func (b *Blob) Close() error {
if b == nil || b.handle == 0 {
return nil
}
2023-11-30 17:52:35 +00:00
r := b.c.call("sqlite3_blob_close", uint64(b.handle))
2024-09-10 07:41:35 +01:00
b.c.free(b.bufptr)
2023-02-27 04:08:38 +00:00
b.handle = 0
2023-05-25 13:17:44 +01:00
return b.c.error(r)
2023-02-27 04:08:38 +00:00
}
// Size returns the size of the BLOB in bytes.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_bytes.html
2023-02-27 04:08:38 +00:00
func (b *Blob) Size() int64 {
return b.bytes
}
// Read implements the [io.Reader] interface.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_read.html
2023-02-27 04:08:38 +00:00
func (b *Blob) Read(p []byte) (n int, err error) {
if b.offset >= b.bytes {
return 0, io.EOF
}
2023-04-21 13:31:45 +01:00
want := int64(len(p))
2024-09-10 07:41:35 +01:00
avail := b.bytes - b.offset
2023-02-27 04:08:38 +00:00
if want > avail {
want = avail
}
2024-09-10 07:41:35 +01:00
if want > b.buflen {
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
2023-02-27 04:08:38 +00:00
2023-11-30 17:52:35 +00:00
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
2024-09-10 07:41:35 +01:00
uint64(b.bufptr), uint64(want), uint64(b.offset))
2023-05-25 13:17:44 +01:00
err = b.c.error(r)
2023-02-27 04:08:38 +00:00
if err != nil {
return 0, err
}
b.offset += want
if b.offset >= b.bytes {
err = io.EOF
}
2023-04-21 13:31:45 +01:00
2024-09-10 07:41:35 +01:00
copy(p, util.View(b.c.mod, b.bufptr, uint64(want)))
2023-02-27 04:08:38 +00:00
return int(want), err
}
2023-04-21 13:31:45 +01:00
// WriteTo implements the [io.WriterTo] interface.
2023-02-27 04:08:38 +00:00
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_read.html
2023-04-21 13:31:45 +01:00
func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
if b.offset >= b.bytes {
return 0, nil
}
2023-11-02 15:50:43 +00:00
want := int64(1024 * 1024)
2023-04-21 13:31:45 +01:00
avail := b.bytes - b.offset
if want > avail {
want = avail
2023-02-27 04:08:38 +00:00
}
2024-09-10 07:41:35 +01:00
if want > b.buflen {
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
2023-02-27 04:08:38 +00:00
2023-04-21 13:31:45 +01:00
for want > 0 {
2023-11-30 17:52:35 +00:00
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
2024-09-10 07:41:35 +01:00
uint64(b.bufptr), uint64(want), uint64(b.offset))
2023-05-25 13:17:44 +01:00
err = b.c.error(r)
2023-04-21 13:31:45 +01:00
if err != nil {
return n, err
}
2024-09-10 07:41:35 +01:00
mem := util.View(b.c.mod, b.bufptr, uint64(want))
2023-04-21 13:31:45 +01:00
m, err := w.Write(mem[:want])
b.offset += int64(m)
n += int64(m)
if err != nil {
return n, err
}
if int64(m) != want {
2024-08-01 18:31:25 +01:00
// notest // Write misbehaving
2023-04-21 13:31:45 +01:00
return n, io.ErrShortWrite
}
avail = b.bytes - b.offset
if want > avail {
want = avail
}
}
return n, nil
}
// Write implements the [io.Writer] interface.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_write.html
2023-04-21 13:31:45 +01:00
func (b *Blob) Write(p []byte) (n int, err error) {
2024-09-10 07:41:35 +01:00
want := int64(len(p))
if want > b.buflen {
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
util.WriteBytes(b.c.mod, b.bufptr, p)
2023-04-21 13:31:45 +01:00
2023-11-30 17:52:35 +00:00
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
2024-09-10 07:41:35 +01:00
uint64(b.bufptr), uint64(want), uint64(b.offset))
2023-05-25 13:17:44 +01:00
err = b.c.error(r)
2023-02-27 04:08:38 +00:00
if err != nil {
return 0, err
}
b.offset += int64(len(p))
return len(p), nil
}
2023-04-21 13:31:45 +01:00
// ReadFrom implements the [io.ReaderFrom] interface.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_write.html
2023-04-21 13:31:45 +01:00
func (b *Blob) ReadFrom(r io.Reader) (n int64, err error) {
2023-11-02 15:50:43 +00:00
want := int64(1024 * 1024)
2023-04-21 13:31:45 +01:00
avail := b.bytes - b.offset
2023-11-02 15:50:43 +00:00
if l, ok := r.(*io.LimitedReader); ok && want > l.N {
want = l.N
}
2023-04-21 13:31:45 +01:00
if want > avail {
want = avail
}
if want < 1 {
want = 1
}
2024-09-10 07:41:35 +01:00
if want > b.buflen {
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
2023-04-21 13:31:45 +01:00
for {
2024-09-10 07:41:35 +01:00
mem := util.View(b.c.mod, b.bufptr, uint64(want))
2023-04-21 13:31:45 +01:00
m, err := r.Read(mem[:want])
if m > 0 {
2023-11-30 17:52:35 +00:00
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
2024-09-10 07:41:35 +01:00
uint64(b.bufptr), uint64(m), uint64(b.offset))
2023-05-25 13:17:44 +01:00
err := b.c.error(r)
2023-04-21 13:31:45 +01:00
if err != nil {
return n, err
}
b.offset += int64(m)
n += int64(m)
}
if err == io.EOF {
return n, nil
}
if err != nil {
return n, err
}
avail = b.bytes - b.offset
if want > avail {
want = avail
}
if want < 1 {
want = 1
}
}
}
2023-02-27 04:08:38 +00:00
// Seek implements the [io.Seeker] interface.
func (b *Blob) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
2023-03-29 15:01:25 +01:00
return 0, util.WhenceErr
2023-02-27 04:08:38 +00:00
case io.SeekStart:
break
case io.SeekCurrent:
offset += b.offset
case io.SeekEnd:
offset += b.bytes
}
if offset < 0 {
2023-03-29 15:01:25 +01:00
return 0, util.OffsetErr
2023-02-27 04:08:38 +00:00
}
b.offset = offset
return offset, nil
}
2023-02-27 13:45:32 +00:00
// Reopen moves a BLOB handle to a new row of the same database table.
//
2023-11-09 16:35:45 +00:00
// https://sqlite.org/c3ref/blob_reopen.html
2023-02-27 13:45:32 +00:00
func (b *Blob) Reopen(row int64) error {
2023-11-30 17:52:35 +00:00
err := b.c.error(b.c.call("sqlite3_blob_reopen", uint64(b.handle), uint64(row)))
b.bytes = int64(b.c.call("sqlite3_blob_bytes", uint64(b.handle)))
2023-02-27 13:45:32 +00:00
b.offset = 0
2023-04-28 13:50:32 +01:00
return err
2023-02-27 13:45:32 +00:00
}