2015-12-15 05:21:38 +08:00
|
|
|
//! Incremental BLOB I/O.
|
|
|
|
//!
|
2018-08-17 00:29:46 +08:00
|
|
|
//! Note that SQLite does not provide API-level access to change the size of a
|
|
|
|
//! BLOB; that must be performed through SQL statements.
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
2018-08-17 00:29:46 +08:00
|
|
|
//! `Blob` conforms to `std::io::Read`, `std::io::Write`, and `std::io::Seek`,
|
|
|
|
//! so it plays nicely with other types that build on these (such as
|
|
|
|
//! `std::io::BufReader` and `std::io::BufWriter`). However, you must be
|
|
|
|
//! careful with the size of the blob. For example, when using a `BufWriter`,
|
|
|
|
//! the `BufWriter` will accept more data than the `Blob`
|
|
|
|
//! will allow, so make sure to call `flush` and check for errors. (See the
|
|
|
|
//! unit tests in this module for an example.)
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2016-02-02 01:13:07 +08:00
|
|
|
//! use rusqlite::blob::ZeroBlob;
|
2018-09-16 17:10:19 +08:00
|
|
|
//! use rusqlite::{Connection, DatabaseName, NO_PARAMS};
|
2018-08-17 00:29:46 +08:00
|
|
|
//! use std::io::{Read, Seek, SeekFrom, Write};
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let db = Connection::open_in_memory().unwrap();
|
2018-08-17 00:29:46 +08:00
|
|
|
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
|
|
|
//! .unwrap();
|
2018-09-16 15:49:23 +08:00
|
|
|
//! db.execute(
|
|
|
|
//! "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
|
2018-09-16 17:10:19 +08:00
|
|
|
//! NO_PARAMS,
|
2018-10-28 15:51:02 +08:00
|
|
|
//! )
|
|
|
|
//! .unwrap();
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
|
|
|
//! let rowid = db.last_insert_rowid();
|
2018-08-17 00:29:46 +08:00
|
|
|
//! let mut blob = db
|
|
|
|
//! .blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
|
|
|
//! .unwrap();
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
|
|
|
//! // Make sure to test that the number of bytes written matches what you expect;
|
2018-10-28 15:51:02 +08:00
|
|
|
//! // if you try to write too much, the data will be truncated to the size of the
|
|
|
|
//! // BLOB.
|
2015-12-15 05:21:38 +08:00
|
|
|
//! let bytes_written = blob.write(b"01234567").unwrap();
|
|
|
|
//! assert_eq!(bytes_written, 8);
|
|
|
|
//!
|
|
|
|
//! // Same guidance - make sure you check the number of bytes read!
|
|
|
|
//! blob.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
//! let mut buf = [0u8; 20];
|
|
|
|
//! let bytes_read = blob.read(&mut buf[..]).unwrap();
|
|
|
|
//! assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10
|
|
|
|
//!
|
2018-09-16 15:49:23 +08:00
|
|
|
//! db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)])
|
2018-08-17 00:29:46 +08:00
|
|
|
//! .unwrap();
|
2015-12-15 05:21:38 +08:00
|
|
|
//!
|
|
|
|
//! // given a new row ID, we can reopen the blob on that row
|
|
|
|
//! let rowid = db.last_insert_rowid();
|
|
|
|
//! blob.reopen(rowid).unwrap();
|
|
|
|
//!
|
|
|
|
//! assert_eq!(blob.size(), 64);
|
|
|
|
//! }
|
|
|
|
//! ```
|
2015-12-15 05:06:53 +08:00
|
|
|
use std::cmp::min;
|
2018-08-11 18:48:21 +08:00
|
|
|
use std::io;
|
2015-08-02 00:51:02 +08:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use super::ffi;
|
2016-05-26 10:57:43 +08:00
|
|
|
use super::types::{ToSql, ToSqlOutput};
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::{Connection, DatabaseName, Result};
|
2015-08-02 00:51:02 +08:00
|
|
|
|
2015-12-15 05:21:38 +08:00
|
|
|
/// Handle to an open BLOB.
|
2015-12-13 17:53:29 +08:00
|
|
|
pub struct Blob<'conn> {
|
|
|
|
conn: &'conn Connection,
|
2015-08-02 00:51:02 +08:00
|
|
|
blob: *mut ffi::sqlite3_blob,
|
|
|
|
pos: i32,
|
|
|
|
}
|
|
|
|
|
2015-12-13 17:53:29 +08:00
|
|
|
impl Connection {
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Open a handle to the BLOB located in `row_id`, `column`, `table` in
|
|
|
|
/// database `db`.
|
2015-12-05 18:28:33 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Will return `Err` if `db`/`table`/`column` cannot be converted to a
|
|
|
|
/// C-compatible string or if the underlying SQLite BLOB open call
|
|
|
|
/// fails.
|
2018-08-11 18:48:21 +08:00
|
|
|
pub fn blob_open<'a>(
|
|
|
|
&'a self,
|
2018-12-08 04:57:04 +08:00
|
|
|
db: DatabaseName<'_>,
|
2018-08-11 18:48:21 +08:00
|
|
|
table: &str,
|
|
|
|
column: &str,
|
|
|
|
row_id: i64,
|
|
|
|
read_only: bool,
|
|
|
|
) -> Result<Blob<'a>> {
|
2015-08-02 00:51:02 +08:00
|
|
|
let mut c = self.db.borrow_mut();
|
|
|
|
let mut blob = ptr::null_mut();
|
2018-10-31 03:11:35 +08:00
|
|
|
let db = db.to_cstring()?;
|
|
|
|
let table = super::str_to_cstring(table)?;
|
|
|
|
let column = super::str_to_cstring(column)?;
|
2015-12-13 18:05:11 +08:00
|
|
|
let rc = unsafe {
|
2018-08-11 18:48:21 +08:00
|
|
|
ffi::sqlite3_blob_open(
|
|
|
|
c.db(),
|
|
|
|
db.as_ptr(),
|
|
|
|
table.as_ptr(),
|
|
|
|
column.as_ptr(),
|
|
|
|
row_id,
|
|
|
|
if read_only { 0 } else { 1 },
|
|
|
|
&mut blob,
|
|
|
|
)
|
2015-12-13 18:05:11 +08:00
|
|
|
};
|
2018-08-11 18:48:21 +08:00
|
|
|
c.decode_result(rc).map(|_| Blob {
|
|
|
|
conn: self,
|
|
|
|
blob,
|
|
|
|
pos: 0,
|
|
|
|
})
|
2015-08-02 00:51:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Blob<'_> {
|
2015-12-15 05:21:38 +08:00
|
|
|
/// Move a BLOB handle to a new row.
|
2015-12-05 18:28:33 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if the underlying SQLite BLOB reopen call fails.
|
2015-12-13 17:53:29 +08:00
|
|
|
pub fn reopen(&mut self, row: i64) -> Result<()> {
|
2015-12-13 18:05:11 +08:00
|
|
|
let rc = unsafe { ffi::sqlite3_blob_reopen(self.blob, row) };
|
2015-08-02 00:51:02 +08:00
|
|
|
if rc != ffi::SQLITE_OK {
|
|
|
|
return self.conn.decode_result(rc);
|
|
|
|
}
|
|
|
|
self.pos = 0;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-12-15 05:21:38 +08:00
|
|
|
/// Return the size in bytes of the BLOB.
|
2015-08-02 00:51:02 +08:00
|
|
|
pub fn size(&self) -> i32 {
|
2015-12-13 18:05:11 +08:00
|
|
|
unsafe { ffi::sqlite3_blob_bytes(self.blob) }
|
2015-08-02 00:51:02 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 05:21:38 +08:00
|
|
|
/// Close a BLOB handle.
|
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Calling `close` explicitly is not required (the BLOB will be closed
|
|
|
|
/// when the `Blob` is dropped), but it is available so you can get any
|
|
|
|
/// errors that occur.
|
2015-12-05 18:28:33 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if the underlying SQLite close call fails.
|
2015-12-13 17:53:29 +08:00
|
|
|
pub fn close(mut self) -> Result<()> {
|
2015-08-02 00:51:02 +08:00
|
|
|
self.close_()
|
|
|
|
}
|
|
|
|
|
2015-12-13 17:53:29 +08:00
|
|
|
fn close_(&mut self) -> Result<()> {
|
2015-08-02 00:51:02 +08:00
|
|
|
let rc = unsafe { ffi::sqlite3_blob_close(self.blob) };
|
|
|
|
self.blob = ptr::null_mut();
|
|
|
|
self.conn.decode_result(rc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl io::Read for Blob<'_> {
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Read data from a BLOB incrementally. Will return Ok(0) if the end of
|
|
|
|
/// the blob has been reached.
|
2015-12-15 02:45:44 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
2015-12-15 05:06:53 +08:00
|
|
|
/// Will return `Err` if the underlying SQLite read call fails.
|
2015-12-15 02:45:44 +08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-12-15 05:06:53 +08:00
|
|
|
let max_allowed_len = (self.size() - self.pos) as usize;
|
|
|
|
let n = min(buf.len(), max_allowed_len) as i32;
|
2015-12-15 02:45:44 +08:00
|
|
|
if n <= 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2018-08-11 18:48:21 +08:00
|
|
|
let rc = unsafe { ffi::sqlite3_blob_read(self.blob, buf.as_ptr() as *mut _, n, self.pos) };
|
2015-12-15 03:13:14 +08:00
|
|
|
self.conn
|
|
|
|
.decode_result(rc)
|
|
|
|
.map(|_| {
|
2018-08-11 18:48:21 +08:00
|
|
|
self.pos += n;
|
|
|
|
n as usize
|
2018-10-28 15:51:02 +08:00
|
|
|
})
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
|
2015-12-15 02:45:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl io::Write for Blob<'_> {
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Write data into a BLOB incrementally. Will return `Ok(0)` if the end of
|
|
|
|
/// the blob has been reached; consider using `Write::write_all(buf)`
|
|
|
|
/// if you want to get an error if the entirety of the buffer cannot be
|
|
|
|
/// written.
|
2015-12-15 02:50:18 +08:00
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// This function may only modify the contents of the BLOB; it is not
|
|
|
|
/// possible to increase the size of a BLOB using this API.
|
2015-12-15 02:50:18 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
2015-12-15 05:06:53 +08:00
|
|
|
/// Will return `Err` if the underlying SQLite write call fails.
|
2015-12-15 02:50:18 +08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2015-12-15 05:06:53 +08:00
|
|
|
let max_allowed_len = (self.size() - self.pos) as usize;
|
|
|
|
let n = min(buf.len(), max_allowed_len) as i32;
|
2015-12-15 02:50:18 +08:00
|
|
|
if n <= 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2018-08-11 18:48:21 +08:00
|
|
|
let rc = unsafe { ffi::sqlite3_blob_write(self.blob, buf.as_ptr() as *mut _, n, self.pos) };
|
2015-12-15 03:13:14 +08:00
|
|
|
self.conn
|
|
|
|
.decode_result(rc)
|
|
|
|
.map(|_| {
|
2018-08-11 18:48:21 +08:00
|
|
|
self.pos += n;
|
|
|
|
n as usize
|
2018-10-28 15:51:02 +08:00
|
|
|
})
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
|
2015-12-15 02:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl io::Seek for Blob<'_> {
|
2015-12-15 03:12:11 +08:00
|
|
|
/// Seek to an offset, in bytes, in BLOB.
|
|
|
|
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
|
|
|
let pos = match pos {
|
|
|
|
io::SeekFrom::Start(offset) => offset as i64,
|
2018-05-05 01:55:55 +08:00
|
|
|
io::SeekFrom::Current(offset) => i64::from(self.pos) + offset,
|
|
|
|
io::SeekFrom::End(offset) => i64::from(self.size()) + offset,
|
2015-12-15 03:12:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if pos < 0 {
|
2018-08-11 18:48:21 +08:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"invalid seek to negative position",
|
|
|
|
))
|
2018-05-05 01:55:55 +08:00
|
|
|
} else if pos > i64::from(self.size()) {
|
2018-08-11 18:48:21 +08:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"invalid seek to position past end of blob",
|
|
|
|
))
|
2015-12-15 03:12:11 +08:00
|
|
|
} else {
|
|
|
|
self.pos = pos as i32;
|
|
|
|
Ok(pos as u64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 00:51:02 +08:00
|
|
|
#[allow(unused_must_use)]
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Drop for Blob<'_> {
|
2015-08-02 00:51:02 +08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.close_();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 01:13:07 +08:00
|
|
|
/// BLOB of length N that is filled with zeroes.
|
2016-05-20 09:09:40 +08:00
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Zeroblobs are intended to serve as placeholders for BLOBs whose content is
|
|
|
|
/// later written using incremental BLOB I/O routines.
|
2016-05-20 09:09:40 +08:00
|
|
|
///
|
2016-02-02 01:13:07 +08:00
|
|
|
/// A negative value for the zeroblob results in a zero-length BLOB.
|
2018-08-11 18:48:21 +08:00
|
|
|
#[derive(Copy, Clone)]
|
2016-02-02 01:13:07 +08:00
|
|
|
pub struct ZeroBlob(pub i32);
|
|
|
|
|
|
|
|
impl ToSql for ZeroBlob {
|
2018-12-08 04:57:04 +08:00
|
|
|
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
2016-02-02 01:13:07 +08:00
|
|
|
let ZeroBlob(length) = *self;
|
2016-05-26 10:57:43 +08:00
|
|
|
Ok(ToSqlOutput::ZeroBlob(length))
|
2016-02-02 01:13:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 00:51:02 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::{Connection, DatabaseName, Result};
|
2018-10-31 03:13:41 +08:00
|
|
|
use std::io::{BufRead, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
|
2015-08-02 00:51:02 +08:00
|
|
|
|
2015-12-15 04:03:29 +08:00
|
|
|
fn db_with_test_blob() -> Result<(Connection, i64)> {
|
2018-10-31 03:11:35 +08:00
|
|
|
let db = Connection::open_in_memory()?;
|
2015-08-02 00:51:02 +08:00
|
|
|
let sql = "BEGIN;
|
2015-12-15 04:03:29 +08:00
|
|
|
CREATE TABLE test (content BLOB);
|
|
|
|
INSERT INTO test VALUES (ZEROBLOB(10));
|
|
|
|
END;";
|
2018-10-31 03:11:35 +08:00
|
|
|
db.execute_batch(sql)?;
|
2015-08-02 00:51:02 +08:00
|
|
|
let rowid = db.last_insert_rowid();
|
2015-12-15 04:03:29 +08:00
|
|
|
Ok((db, rowid))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_blob() {
|
|
|
|
let (db, rowid) = db_with_test_blob().unwrap();
|
2015-08-02 00:51:02 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let mut blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-12-15 04:03:29 +08:00
|
|
|
assert_eq!(4, blob.write(b"Clob").unwrap());
|
2015-12-15 05:06:53 +08:00
|
|
|
assert_eq!(6, blob.write(b"567890xxxxxx").unwrap()); // cannot write past 10
|
2017-02-09 04:11:15 +08:00
|
|
|
assert_eq!(0, blob.write(b"5678").unwrap()); // still cannot write past 10
|
2015-08-02 00:51:02 +08:00
|
|
|
|
2015-12-15 04:03:29 +08:00
|
|
|
blob.reopen(rowid).unwrap();
|
|
|
|
blob.close().unwrap();
|
2015-08-02 00:51:02 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, true)
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-08-02 00:51:02 +08:00
|
|
|
let mut bytes = [0u8; 5];
|
|
|
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
2015-12-15 04:03:29 +08:00
|
|
|
assert_eq!(&bytes, b"Clob5");
|
2015-08-02 00:51:02 +08:00
|
|
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
2015-12-15 05:06:53 +08:00
|
|
|
assert_eq!(&bytes, b"67890");
|
2015-08-02 00:51:02 +08:00
|
|
|
assert_eq!(0, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
|
2015-12-15 04:03:29 +08:00
|
|
|
blob.seek(SeekFrom::Start(2)).unwrap();
|
|
|
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
assert_eq!(&bytes, b"ob567");
|
|
|
|
|
2015-12-15 05:06:53 +08:00
|
|
|
// only first 4 bytes of `bytes` should be read into
|
|
|
|
blob.seek(SeekFrom::Current(-1)).unwrap();
|
|
|
|
assert_eq!(4, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
assert_eq!(&bytes, b"78907");
|
2015-12-15 04:03:29 +08:00
|
|
|
|
|
|
|
blob.seek(SeekFrom::End(-6)).unwrap();
|
|
|
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
2015-12-15 05:06:53 +08:00
|
|
|
assert_eq!(&bytes, b"56789");
|
2015-12-15 04:03:29 +08:00
|
|
|
|
|
|
|
blob.reopen(rowid).unwrap();
|
|
|
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
assert_eq!(&bytes, b"Clob5");
|
2015-12-15 05:11:07 +08:00
|
|
|
|
|
|
|
// should not be able to seek negative or past end
|
|
|
|
assert!(blob.seek(SeekFrom::Current(-20)).is_err());
|
|
|
|
assert!(blob.seek(SeekFrom::End(0)).is_ok());
|
|
|
|
assert!(blob.seek(SeekFrom::Current(1)).is_err());
|
2015-12-16 02:39:47 +08:00
|
|
|
|
|
|
|
// write_all should detect when we return Ok(0) because there is no space left,
|
|
|
|
// and return a write error
|
|
|
|
blob.reopen(rowid).unwrap();
|
|
|
|
assert!(blob.write_all(b"0123456789x").is_err());
|
2015-12-15 04:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_blob_in_bufreader() {
|
|
|
|
let (db, rowid) = db_with_test_blob().unwrap();
|
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let mut blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-12-15 04:03:29 +08:00
|
|
|
assert_eq!(8, blob.write(b"one\ntwo\n").unwrap());
|
|
|
|
|
|
|
|
blob.reopen(rowid).unwrap();
|
|
|
|
let mut reader = BufReader::new(blob);
|
|
|
|
|
|
|
|
let mut line = String::new();
|
|
|
|
assert_eq!(4, reader.read_line(&mut line).unwrap());
|
|
|
|
assert_eq!("one\n", line);
|
|
|
|
|
|
|
|
line.truncate(0);
|
|
|
|
assert_eq!(4, reader.read_line(&mut line).unwrap());
|
|
|
|
assert_eq!("two\n", line);
|
|
|
|
|
|
|
|
line.truncate(0);
|
|
|
|
assert_eq!(2, reader.read_line(&mut line).unwrap());
|
|
|
|
assert_eq!("\0\0", line);
|
2015-08-02 00:51:02 +08:00
|
|
|
}
|
2015-12-15 05:06:53 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_blob_in_bufwriter() {
|
|
|
|
let (db, rowid) = db_with_test_blob().unwrap();
|
|
|
|
|
|
|
|
{
|
2018-08-11 18:48:21 +08:00
|
|
|
let blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-12-15 05:06:53 +08:00
|
|
|
let mut writer = BufWriter::new(blob);
|
|
|
|
|
|
|
|
// trying to write too much and then flush should fail
|
|
|
|
assert_eq!(8, writer.write(b"01234567").unwrap());
|
|
|
|
assert_eq!(8, writer.write(b"01234567").unwrap());
|
|
|
|
assert!(writer.flush().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// ... but it should've written the first 10 bytes
|
2018-08-11 18:48:21 +08:00
|
|
|
let mut blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2016-05-17 01:52:17 +08:00
|
|
|
.unwrap();
|
2015-12-15 05:06:53 +08:00
|
|
|
let mut bytes = [0u8; 10];
|
|
|
|
assert_eq!(10, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
assert_eq!(b"0123456701", &bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-11 18:48:21 +08:00
|
|
|
let blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-12-15 05:06:53 +08:00
|
|
|
let mut writer = BufWriter::new(blob);
|
|
|
|
|
|
|
|
// trying to write_all too much should fail
|
|
|
|
writer.write_all(b"aaaaaaaaaabbbbb").unwrap();
|
|
|
|
assert!(writer.flush().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// ... but it should've written the first 10 bytes
|
2018-08-11 18:48:21 +08:00
|
|
|
let mut blob = db
|
|
|
|
.blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
2016-05-17 01:52:17 +08:00
|
|
|
.unwrap();
|
2015-12-15 05:06:53 +08:00
|
|
|
let mut bytes = [0u8; 10];
|
|
|
|
assert_eq!(10, blob.read(&mut bytes[..]).unwrap());
|
|
|
|
assert_eq!(b"aaaaaaaaaa", &bytes);
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 18:05:11 +08:00
|
|
|
}
|