sqlite.Blob
type pub BlobA handle to a SQLite BLOB value.
Data is read using Blob.read, while writes are performed using Blob.write.
If a write is performed on a Blob that is opened in read-only mode then a
Error.ReadOnly error is produced.
To read/write starting at a specific offset, use Blob.seek to set the start
offset followed by a call to Blob.read or Blob.write. A call to
Blob.read advances the offset according to the number of bytes read, similar
to reading from a file or socket.
Instance methods
flush
Show source codeHide source code
fn pub mut flush -> Result[Nil, Error] {
Result.Ok(nil)
}fn pub mut flush -> Result[Nil, Error]Flushes any pending writes to the file system.
Flushing writes is a potentially expensive operation, and unnecessarily calling this method may degrade performance.
When flushing data to disk it's important to remember that the actual behaviour may vary based on the type of file system, operating system and storage hardware that's used. In particular, it's possible for one of these components to say "Yup, I totally flushed the data, you're all good!" when in fact they have not fully flushed the data.
Show source codeHide source code
fn pub mut print[B: BytesTrait](bytes: ref B) -> Result[Nil, E] {
try write(bytes)
write('\n')
}fn pub mut print[B: Bytes](bytes: ref B) -> Result[Nil, E]Writes the entirety of bytes to the underlying stream, followed by
a Unix newline.
read
Show source codeHide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
let size = min(size, self.size - @offset)
into.reserve_exact(size)
let len = size as Int32
let off = @offset as Int32
match sqlite3_blob_read(@handle, into.pointer, len, off) as Int {
case SQLITE_OK -> {
into.size = into.size + size
@offset += size
Result.Ok(size)
}
case v -> throw Error.new(v)
}
}fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error]Reads up to size bytes from self into the given ByteArray.
If size is greater than the remaining number of bytes in self, only up
to that remainder is read.
Similar to files or sockets, a call to Blob.read advances the offset for
future reads and writes.
Errors
When reading from an expired blob a Error.Aborted error is produced,
though SQLite may also produce errors for other reasons.
read_all
Show source codeHide source code
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, Error] {
read(into: bytes, size: size)
}fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, Error]Reads from self into the given ByteArray, returning when all input is
consumed.
The return value is the number of bytes read.
Errors
This method returns an Error if the underlying call to Read.read returns
an Error.
read_exact
Show source codeHide source code
fn pub mut read_exact(
into: mut ByteArray,
size: Int,
) -> Result[Nil, ReadExactError[E]] {
let mut pending = size
while pending > 0 {
match read(into, pending) {
case Ok(0) if pending > 0 -> throw ReadExactError.EndOfInput
case Ok(n) -> pending -= n
case Error(e) -> throw ReadExactError.Read(e)
}
}
Result.Ok(nil)
}fn pub mut read_exact(into: mut ByteArray, size: Int) -> Result[Nil, ReadExactError[E]]Reads exactly size bytes into into.
Whereas Read.read might return early if fewer bytes are available in the
input stream, Read.read_exact continues reading until the desired amount
of bytes is read.
Errors
If the end of the input stream is encountered before filling the buffer, an
Error.EndOfInput error is returned.
If an error is returned, no assumption can be made about the state of the
into buffer, i.e. there's no guarantee data read so far is in the buffer
in the event of an error.
seek
Show source codeHide source code
fn pub mut seek(position: SeekFrom) -> Result[Int, IoError] {
let new = match position {
case Start(v) -> v
case Current(v) -> @offset + v
case End(v) -> size - v
}
if new < 0 { throw IoError.InvalidSeek }
@offset = new
Result.Ok(@offset)
}fn pub mut seek(position: SeekFrom) -> Result[Int, Error]Seeks to the given offset, returning the new offset.
Upon success the new offset (in bytes) is returned.
Seeking beyond the end of the stream is allowed, but seeking before the start of the stream is an error.
size
Show source codeHide source code
fn pub size -> Int {
sqlite3_blob_bytes(@handle) as Int
}fn pub size -> IntReturns the size of self in bytes.
write
Show source codeHide source code
fn pub mut write[B: Bytes](bytes: ref B) -> Result[Nil, Error] {
let len = bytes.size as Int32
let off = @offset as Int32
match sqlite3_blob_write(@handle, bytes.pointer, len, off) as Int {
case SQLITE_OK -> Result.Ok(nil)
case v -> throw Error.new(v)
}
}fn pub mut write[B: Bytes](bytes: ref B) -> Result[Nil, Error]Writes the entirety of bytes to the underlying stream.
Types implementing this method must guarantee that upon returning from this
method, either all of the data is written and a Ok(Nil) is returned, or
an Error is returned.
Implemented traits
Drop
impl Drop for BlobRead
impl Read[Error] for BlobSeek
impl Seek[Error] for BlobWrite
impl Write[Error] for Blob