SqliteConnection::open takes a std::path.

Add new constructors, `open_in_memory` and `open_in_memory_with_flags`,
for opening in-memory databases.

Closes #13.
This commit is contained in:
John Gallagher 2015-02-06 20:07:23 -05:00
parent 4ca7b4964f
commit 1dc78b66da
3 changed files with 32 additions and 17 deletions

View File

@ -18,7 +18,7 @@
//! } //! }
//! //!
//! fn main() { //! fn main() {
//! let conn = SqliteConnection::open(":memory:").unwrap(); //! let conn = SqliteConnection::open_in_memory().unwrap();
//! //!
//! conn.execute("CREATE TABLE person ( //! conn.execute("CREATE TABLE person (
//! id INTEGER PRIMARY KEY, //! id INTEGER PRIMARY KEY,
@ -55,11 +55,13 @@ extern crate libc;
#[macro_use] extern crate rustc_bitflags; #[macro_use] extern crate rustc_bitflags;
use std::mem; use std::mem;
use std::path;
use std::ptr; use std::ptr;
use std::fmt; use std::fmt;
use std::rc::{Rc}; use std::rc::{Rc};
use std::cell::{RefCell, Cell}; use std::cell::{RefCell, Cell};
use std::ffi::{CString}; use std::ffi::{CString, AsOsStr};
use std::os::unix::{OsStrExt};
use std::ffi as std_ffi; use std::ffi as std_ffi;
use std::str; use std::str;
use libc::{c_int, c_void, c_char}; use libc::{c_int, c_void, c_char};
@ -133,21 +135,36 @@ pub struct SqliteConnection {
impl SqliteConnection { impl SqliteConnection {
/// Open a new connection to a SQLite database. /// Open a new connection to a SQLite database.
/// ///
/// Use the special path `:memory:` to create an in-memory database.
/// `SqliteConnection::open(path)` is equivalent to `SqliteConnection::open_with_flags(path, /// `SqliteConnection::open(path)` is equivalent to `SqliteConnection::open_with_flags(path,
/// SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE)`. /// SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE)`.
pub fn open(path: &str) -> SqliteResult<SqliteConnection> { pub fn open(path: &path::Path) -> SqliteResult<SqliteConnection> {
let flags = SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE; let flags = SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE;
SqliteConnection::open_with_flags(path, flags) SqliteConnection::open_with_flags(path, flags)
} }
/// Open a new connection to an in-memory SQLite database.
pub fn open_in_memory() -> SqliteResult<SqliteConnection> {
let flags = SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE;
SqliteConnection::open_in_memory_with_flags(flags)
}
/// Open a new connection to a SQLite database. /// Open a new connection to a SQLite database.
/// ///
/// Use the special path `:memory:` to create an in-memory database. See [Opening A New
/// Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid /// Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid
/// flag combinations. /// flag combinations.
pub fn open_with_flags(path: &str, flags: SqliteOpenFlags) -> SqliteResult<SqliteConnection> { pub fn open_with_flags(path: &path::Path, flags: SqliteOpenFlags)
InnerSqliteConnection::open_with_flags(path, flags).map(|db| { -> SqliteResult<SqliteConnection> {
InnerSqliteConnection::open_with_flags(path.as_os_str().as_byte_slice(), flags).map(|db| {
SqliteConnection{ db: RefCell::new(db) }
})
}
/// Open a new connection to an in-memory SQLite database.
///
/// Database Connection](http://www.sqlite.org/c3ref/open.html) for a description of valid
/// flag combinations.
pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult<SqliteConnection> {
InnerSqliteConnection::open_with_flags(":memory:".as_bytes(), flags).map(|db| {
SqliteConnection{ db: RefCell::new(db) } SqliteConnection{ db: RefCell::new(db) }
}) })
} }
@ -351,8 +368,9 @@ bitflags! {
} }
impl InnerSqliteConnection { impl InnerSqliteConnection {
fn open_with_flags(path: &str, flags: SqliteOpenFlags) -> SqliteResult<InnerSqliteConnection> { fn open_with_flags(path: &[u8], flags: SqliteOpenFlags)
let c_path = CString::from_slice(path.as_bytes()); -> SqliteResult<InnerSqliteConnection> {
let c_path = CString::from_slice(path);
unsafe { unsafe {
let mut db: *mut ffi::sqlite3 = mem::uninitialized(); let mut db: *mut ffi::sqlite3 = mem::uninitialized();
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null()); let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null());
@ -700,12 +718,12 @@ mod test {
use super::*; use super::*;
fn checked_memory_handle() -> SqliteConnection { fn checked_memory_handle() -> SqliteConnection {
SqliteConnection::open(":memory:").unwrap() SqliteConnection::open_in_memory().unwrap()
} }
#[test] #[test]
fn test_open() { fn test_open() {
assert!(SqliteConnection::open(":memory:").is_ok()); assert!(SqliteConnection::open_in_memory().is_ok());
let db = checked_memory_handle(); let db = checked_memory_handle();
assert!(db.close().is_ok()); assert!(db.close().is_ok());
@ -718,11 +736,8 @@ mod test {
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_READ_WRITE, SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_READ_WRITE,
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_CREATE, SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_CREATE,
].iter() { ].iter() {
assert!(SqliteConnection::open_with_flags(":memory:", *bad_flags).is_err()); assert!(SqliteConnection::open_in_memory_with_flags(*bad_flags).is_err());
} }
assert!(SqliteConnection::open_with_flags(
"file::memory:", SQLITE_OPEN_READ_ONLY|SQLITE_OPEN_URI).is_ok());
} }
#[test] #[test]

View File

@ -163,7 +163,7 @@ mod test {
use SqliteConnection; use SqliteConnection;
fn checked_memory_handle() -> SqliteConnection { fn checked_memory_handle() -> SqliteConnection {
let db = SqliteConnection::open(":memory:").unwrap(); let db = SqliteConnection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo (x INTEGER)").unwrap(); db.execute_batch("CREATE TABLE foo (x INTEGER)").unwrap();
db db
} }

View File

@ -228,7 +228,7 @@ mod test {
use super::time; use super::time;
fn checked_memory_handle() -> SqliteConnection { fn checked_memory_handle() -> SqliteConnection {
let db = SqliteConnection::open(":memory:").unwrap(); let db = SqliteConnection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT)").unwrap(); db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT)").unwrap();
db db
} }