Rename BackupName -> DatabaseName

This commit is contained in:
John Gallagher 2015-12-09 16:27:18 -05:00
parent 5ebbf5a1a2
commit 8c2e0a0da7
2 changed files with 38 additions and 34 deletions

View File

@ -26,7 +26,6 @@
//! } //! }
//! ``` //! ```
use std::ffi::CString;
use std::marker::PhantomData; use std::marker::PhantomData;
use libc::c_int; use libc::c_int;
@ -35,7 +34,7 @@ use std::time::Duration;
use ffi; use ffi;
use {SqliteConnection, SqliteError, SqliteResult, str_to_cstring}; use {DatabaseName, SqliteConnection, SqliteError, SqliteResult};
/// Possible successful results of calling `Backup::step`. /// Possible successful results of calling `Backup::step`.
pub enum StepResult { pub enum StepResult {
@ -54,27 +53,6 @@ pub enum StepResult {
Locked, Locked,
} }
/// Name for the database to back up. Can be specified for both the source and
/// destination.
pub enum BackupName<'a> {
/// The main database. This is typically what you want.
Main,
/// Back up the temporary database (e.g., any "CREATE TEMPORARY TABLE" tables).
Temp,
/// Backup a database that has been attached via "ATTACH DATABASE ...".
Attached(&'a str),
}
impl<'a> BackupName<'a> {
fn to_cstring(self) -> SqliteResult<CString> {
match self {
BackupName::Main => str_to_cstring("main"),
BackupName::Temp => str_to_cstring("temp"),
BackupName::Attached(s) => str_to_cstring(s),
}
}
}
/// Struct specifying the progress of a backup. The percentage completion can /// Struct specifying the progress of a backup. The percentage completion can
/// be calculated as `(pagecount - remaining) / pagecount`. The progress of a /// be calculated as `(pagecount - remaining) / pagecount`. The progress of a
/// backup is as of the last call to `step` - if the source database is /// backup is as of the last call to `step` - if the source database is
@ -107,7 +85,7 @@ impl<'a, 'b> Backup<'a, 'b> {
/// `NULL`. /// `NULL`.
pub fn new(from: &'a SqliteConnection, pub fn new(from: &'a SqliteConnection,
to: &'b mut SqliteConnection) -> SqliteResult<Backup<'a, 'b>> { to: &'b mut SqliteConnection) -> SqliteResult<Backup<'a, 'b>> {
Backup::new_with_names(from, BackupName::Main, to, BackupName::Main) Backup::new_with_names(from, DatabaseName::Main, to, DatabaseName::Main)
} }
/// Attempt to create a new handle that will allow backups from the /// Attempt to create a new handle that will allow backups from the
@ -119,8 +97,8 @@ impl<'a, 'b> Backup<'a, 'b> {
/// ///
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns /// Will return `Err` if the underlying `sqlite3_backup_init` call returns
/// `NULL`. /// `NULL`.
pub fn new_with_names(from: &'a SqliteConnection, from_name: BackupName, pub fn new_with_names(from: &'a SqliteConnection, from_name: DatabaseName,
to: &'b mut SqliteConnection, to_name: BackupName) to: &'b mut SqliteConnection, to_name: DatabaseName)
-> SqliteResult<Backup<'a, 'b>> -> SqliteResult<Backup<'a, 'b>>
{ {
let to_name = try!(to_name.to_cstring()); let to_name = try!(to_name.to_cstring());
@ -224,9 +202,9 @@ impl<'a, 'b> Drop for Backup<'a, 'b> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use SqliteConnection; use {SqliteConnection, DatabaseName};
use std::time::Duration; use std::time::Duration;
use super::{Backup, BackupName}; use super::Backup;
#[test] #[test]
fn test_backup() { fn test_backup() {
@ -270,7 +248,7 @@ mod test {
let mut dst = SqliteConnection::open_in_memory().unwrap(); let mut dst = SqliteConnection::open_in_memory().unwrap();
{ {
let backup = Backup::new_with_names(&src, BackupName::Temp, &mut dst, BackupName::Main) let backup = Backup::new_with_names(&src, DatabaseName::Temp, &mut dst, DatabaseName::Main)
.unwrap(); .unwrap();
backup.step(-1).unwrap(); backup.step(-1).unwrap();
} }
@ -281,7 +259,7 @@ mod test {
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap(); src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
{ {
let backup = Backup::new_with_names(&src, BackupName::Temp, &mut dst, BackupName::Main) let backup = Backup::new_with_names(&src, DatabaseName::Temp, &mut dst, DatabaseName::Main)
.unwrap(); .unwrap();
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap(); backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
} }
@ -303,8 +281,8 @@ mod test {
let mut dst = SqliteConnection::open_in_memory().unwrap(); let mut dst = SqliteConnection::open_in_memory().unwrap();
{ {
let backup = Backup::new_with_names(&src, BackupName::Attached("my_attached"), let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"),
&mut dst, BackupName::Main).unwrap(); &mut dst, DatabaseName::Main).unwrap();
backup.step(-1).unwrap(); backup.step(-1).unwrap();
} }
@ -314,8 +292,8 @@ mod test {
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap(); src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
{ {
let backup = Backup::new_with_names(&src, BackupName::Attached("my_attached"), let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"),
&mut dst, BackupName::Main).unwrap(); &mut dst, DatabaseName::Main).unwrap();
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap(); backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
} }

View File

@ -143,6 +143,32 @@ fn path_to_cstring(p: &Path) -> SqliteResult<CString> {
str_to_cstring(s) str_to_cstring(s)
} }
/// Name for a database within a SQLite connection.
pub enum DatabaseName<'a> {
/// The main database.
Main,
/// The temporary database (e.g., any "CREATE TEMPORARY TABLE" tables).
Temp,
/// A database that has been attached via "ATTACH DATABASE ...".
Attached(&'a str),
}
// Currently DatabaseName is only used by the backup mod, so hide this (private)
// impl to avoid dead code warnings.
#[cfg(feature = "backup")]
impl<'a> DatabaseName<'a> {
fn to_cstring(self) -> SqliteResult<CString> {
use self::DatabaseName::{Main, Temp, Attached};
match self {
Main => str_to_cstring("main"),
Temp => str_to_cstring("temp"),
Attached(s) => str_to_cstring(s),
}
}
}
/// A connection to a SQLite database. /// A connection to a SQLite database.
pub struct SqliteConnection { pub struct SqliteConnection {
db: RefCell<InnerSqliteConnection>, db: RefCell<InnerSqliteConnection>,