From 8c2e0a0da7674870dda1c0d2d3777bb6bfc4de90 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Wed, 9 Dec 2015 16:27:18 -0500 Subject: [PATCH] Rename BackupName -> DatabaseName --- src/backup.rs | 46 ++++++++++++---------------------------------- src/lib.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/backup.rs b/src/backup.rs index 9fceeac..03e425c 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -26,7 +26,6 @@ //! } //! ``` -use std::ffi::CString; use std::marker::PhantomData; use libc::c_int; @@ -35,7 +34,7 @@ use std::time::Duration; use ffi; -use {SqliteConnection, SqliteError, SqliteResult, str_to_cstring}; +use {DatabaseName, SqliteConnection, SqliteError, SqliteResult}; /// Possible successful results of calling `Backup::step`. pub enum StepResult { @@ -54,27 +53,6 @@ pub enum StepResult { 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 { - 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 /// be calculated as `(pagecount - remaining) / pagecount`. The progress of a /// 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`. pub fn new(from: &'a SqliteConnection, to: &'b mut SqliteConnection) -> SqliteResult> { - 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 @@ -119,8 +97,8 @@ impl<'a, 'b> Backup<'a, 'b> { /// /// Will return `Err` if the underlying `sqlite3_backup_init` call returns /// `NULL`. - pub fn new_with_names(from: &'a SqliteConnection, from_name: BackupName, - to: &'b mut SqliteConnection, to_name: BackupName) + pub fn new_with_names(from: &'a SqliteConnection, from_name: DatabaseName, + to: &'b mut SqliteConnection, to_name: DatabaseName) -> SqliteResult> { let to_name = try!(to_name.to_cstring()); @@ -224,9 +202,9 @@ impl<'a, 'b> Drop for Backup<'a, 'b> { #[cfg(test)] mod test { - use SqliteConnection; + use {SqliteConnection, DatabaseName}; use std::time::Duration; - use super::{Backup, BackupName}; + use super::Backup; #[test] fn test_backup() { @@ -270,7 +248,7 @@ mod test { 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(); backup.step(-1).unwrap(); } @@ -281,7 +259,7 @@ mod test { 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(); 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 backup = Backup::new_with_names(&src, BackupName::Attached("my_attached"), - &mut dst, BackupName::Main).unwrap(); + let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"), + &mut dst, DatabaseName::Main).unwrap(); backup.step(-1).unwrap(); } @@ -314,8 +292,8 @@ mod test { src.execute_batch("INSERT INTO foo VALUES(43)").unwrap(); { - let backup = Backup::new_with_names(&src, BackupName::Attached("my_attached"), - &mut dst, BackupName::Main).unwrap(); + let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"), + &mut dst, DatabaseName::Main).unwrap(); backup.run_to_completion(5, Duration::from_millis(250), None).unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index cbd50e1..b43c6be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,6 +143,32 @@ fn path_to_cstring(p: &Path) -> SqliteResult { 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 { + 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. pub struct SqliteConnection { db: RefCell,