diff --git a/Cargo.toml b/Cargo.toml index b09f2b0..3d9db8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,9 @@ time = "~0.1.0" bitflags = "~0.1" libc = "~0.1" +[dev-dependencies] +tempdir = "~0.3.4" + [dependencies.libsqlite3-sys] path = "libsqlite3-sys" version = "0.0.13" diff --git a/src/lib.rs b/src/lib.rs index aa01314..69db74a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,7 +156,7 @@ impl SqliteConnection { /// /// `SqliteConnection::open(path)` is equivalent to `SqliteConnection::open_with_flags(path, /// SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE)`. - pub fn open(path: &Path) -> SqliteResult { + pub fn open>(path: &P) -> SqliteResult { let flags = SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE; SqliteConnection::open_with_flags(path, flags) } @@ -171,9 +171,9 @@ impl SqliteConnection { /// /// Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid /// flag combinations. - pub fn open_with_flags(path: &Path, flags: SqliteOpenFlags) + pub fn open_with_flags>(path: &P, flags: SqliteOpenFlags) -> SqliteResult { - let c_path = try!(path_to_cstring(path)); + let c_path = try!(path_to_cstring(path.as_ref())); InnerSqliteConnection::open_with_flags(&c_path, flags).map(|db| { SqliteConnection{ db: RefCell::new(db) } }) @@ -397,7 +397,7 @@ impl SqliteConnection { /// conn.load_extension(Path::new("my_sqlite_extension"), None) /// } #[cfg(feature = "load_extension")] - pub fn load_extension(&self, dylib_path: &Path, entry_point: Option<&str>) -> SqliteResult<()> { + pub fn load_extension>(&self, dylib_path: &P, entry_point: Option<&str>) -> SqliteResult<()> { self.db.borrow_mut().load_extension(dylib_path, entry_point) } @@ -815,7 +815,9 @@ impl<'stmt> SqliteRow<'stmt> { #[cfg(test)] mod test { extern crate libsqlite3_sys as ffi; + extern crate tempdir; use super::*; + use self::tempdir::TempDir; // this function is never called, but is still type checked; in // particular, calls with specific instantiations will require @@ -829,6 +831,29 @@ mod test { SqliteConnection::open_in_memory().unwrap() } + #[test] + fn test_persistence() { + let temp_dir = TempDir::new("test_open_file").unwrap(); + let path = temp_dir.path().join("test.db3"); + + { + let db = SqliteConnection::open(&path).unwrap(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER); + INSERT INTO foo VALUES(42); + END;"; + db.execute_batch(sql).unwrap(); + } + + let path_string = path.to_str().unwrap(); + let db = SqliteConnection::open(&path_string).unwrap(); + let the_answer = db.query_row_safe("SELECT x FROM foo", + &[], + |r| r.get::(0)); + + assert_eq!(42i64, the_answer.unwrap()); + } + #[test] fn test_open() { assert!(SqliteConnection::open_in_memory().is_ok());