Make blob_open take a DatabaseName instead of a str.

This commit is contained in:
John Gallagher 2015-12-14 13:38:36 -05:00
parent 73611d45d5
commit 5ac5f3e9b5
2 changed files with 9 additions and 9 deletions

View File

@ -3,7 +3,7 @@ use std::mem;
use std::ptr; use std::ptr;
use super::ffi; use super::ffi;
use {Error, Result, Connection}; use {Error, Result, Connection, DatabaseName};
/// Handle to an open BLOB /// Handle to an open BLOB
pub struct Blob<'conn> { pub struct Blob<'conn> {
@ -20,14 +20,14 @@ pub enum SeekFrom {
} }
impl Connection { impl Connection {
/// Open a handle to the BLOB located in `row`, `column`, `table` in database `db` ('main', 'temp', ...) /// Open a handle to the BLOB located in `row`, `column`, `table` in database `db`.
/// ///
/// # Failure /// # Failure
/// ///
/// Will return `Err` if `db`/`table`/`column` cannot be converted to a C-compatible string or if the /// Will return `Err` if `db`/`table`/`column` cannot be converted to a C-compatible string or if the
/// underlying SQLite BLOB open call fails. /// underlying SQLite BLOB open call fails.
pub fn blob_open<'a>(&'a self, pub fn blob_open<'a>(&'a self,
db: &str, db: DatabaseName,
table: &str, table: &str,
column: &str, column: &str,
row: i64, row: i64,
@ -35,7 +35,7 @@ impl Connection {
-> Result<Blob<'a>> { -> Result<Blob<'a>> {
let mut c = self.db.borrow_mut(); let mut c = self.db.borrow_mut();
let mut blob = ptr::null_mut(); let mut blob = ptr::null_mut();
let db = try!(super::str_to_cstring(db)); let db = try!(db.to_cstring());
let table = try!(super::str_to_cstring(table)); let table = try!(super::str_to_cstring(table));
let column = try!(super::str_to_cstring(column)); let column = try!(super::str_to_cstring(column));
let rc = unsafe { let rc = unsafe {
@ -179,7 +179,7 @@ impl<'conn> Drop for Blob<'conn> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use Connection; use {Connection, DatabaseName};
#[test] #[test]
#[cfg_attr(rustfmt, rustfmt_skip)] #[cfg_attr(rustfmt, rustfmt_skip)]
@ -192,7 +192,7 @@ mod test {
db.execute_batch(sql).unwrap(); db.execute_batch(sql).unwrap();
let rowid = db.last_insert_rowid(); let rowid = db.last_insert_rowid();
let mut blob = db.blob_open("main", "test", "content", rowid, false).unwrap(); let mut blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, false).unwrap();
blob.write(b"Clob").unwrap(); blob.write(b"Clob").unwrap();
let err = blob.write(b"5678901"); let err = blob.write(b"5678901");
// writeln!(io::stderr(), "{:?}", err); // writeln!(io::stderr(), "{:?}", err);
@ -201,7 +201,7 @@ mod test {
assert!(blob.reopen(rowid).is_ok()); assert!(blob.reopen(rowid).is_ok());
assert!(blob.close().is_ok()); assert!(blob.close().is_ok());
blob = db.blob_open("main", "test", "content", rowid, true).unwrap(); blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, true).unwrap();
let mut bytes = [0u8; 5]; let mut bytes = [0u8; 5];
assert_eq!(5, blob.read(&mut bytes[..]).unwrap()); assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(5, blob.read(&mut bytes[..]).unwrap()); assert_eq!(5, blob.read(&mut bytes[..]).unwrap());

View File

@ -171,9 +171,9 @@ pub enum DatabaseName<'a> {
Attached(&'a str), Attached(&'a str),
} }
// Currently DatabaseName is only used by the backup mod, so hide this (private) // Currently DatabaseName is only used by the backup and blob mods, so hide this (private)
// impl to avoid dead code warnings. // impl to avoid dead code warnings.
#[cfg(feature = "backup")] #[cfg(any(feature = "backup", feature = "blob"))]
impl<'a> DatabaseName<'a> { impl<'a> DatabaseName<'a> {
fn to_cstring(self) -> Result<CString> { fn to_cstring(self) -> Result<CString> {
use self::DatabaseName::{Main, Temp, Attached}; use self::DatabaseName::{Main, Temp, Attached};