Drop the lifetime parameter of Database

This commit is contained in:
Ivan Ukhov 2015-06-01 11:39:22 -04:00
parent 2cdf79bca0
commit 475fa14c0c
3 changed files with 11 additions and 11 deletions

View File

@ -6,17 +6,17 @@ use std::path::Path;
use {Result, Statement}; use {Result, Statement};
/// A database. /// A database.
pub struct Database<'l> { pub struct Database {
raw: *mut raw::sqlite3, raw: *mut raw::sqlite3,
phantom: PhantomData<&'l raw::sqlite3>, phantom: PhantomData<raw::sqlite3>,
} }
/// A callback triggered for each row of an executed SQL query. /// A callback triggered for each row of an executed SQL query.
pub type ExecuteCallback<'l> = FnMut(Vec<(String, String)>) -> bool + 'l; pub type ExecuteCallback<'l> = FnMut(Vec<(String, String)>) -> bool + 'l;
impl<'l> Database<'l> { impl Database {
/// Open a database. /// Open a database.
pub fn open(path: &Path) -> Result<Database<'l>> { pub fn open(path: &Path) -> Result<Database> {
let mut raw = 0 as *mut _; let mut raw = 0 as *mut _;
unsafe { unsafe {
success!(raw::sqlite3_open(path_to_c_str!(path), &mut raw)); success!(raw::sqlite3_open(path_to_c_str!(path), &mut raw));
@ -25,8 +25,8 @@ impl<'l> Database<'l> {
} }
/// Execute an SQL statement. /// Execute an SQL statement.
pub fn execute<'c>(&mut self, sql: &str, pub fn execute<'l>(&mut self, sql: &str,
callback: Option<&mut ExecuteCallback<'c>>) -> Result<()> { callback: Option<&mut ExecuteCallback<'l>>) -> Result<()> {
unsafe { unsafe {
match callback { match callback {
@ -49,12 +49,12 @@ impl<'l> Database<'l> {
/// Create a prepared statement. /// Create a prepared statement.
#[inline] #[inline]
pub fn statement(&mut self, sql: &str) -> Result<Statement<'l>> { pub fn statement<'l>(&'l mut self, sql: &str) -> Result<Statement<'l>> {
::statement::new(self, sql) ::statement::new(self, sql)
} }
} }
impl<'l> Drop for Database<'l> { impl Drop for Database {
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
unsafe { raw::sqlite3_close(self.raw) }; unsafe { raw::sqlite3_close(self.raw) };

View File

@ -65,6 +65,6 @@ pub use statement::{Statement, Binding, Value};
/// Open a database. /// Open a database.
#[inline] #[inline]
pub fn open<'l>(path: &std::path::Path) -> Result<Database<'l>> { pub fn open(path: &std::path::Path) -> Result<Database> {
Database::open(path) Database::open(path)
} }

View File

@ -7,7 +7,7 @@ use {Database, Result, ResultCode};
/// A prepared statement. /// A prepared statement.
pub struct Statement<'l> { pub struct Statement<'l> {
raw: *mut raw::sqlite3_stmt, raw: *mut raw::sqlite3_stmt,
phantom: PhantomData<&'l raw::sqlite3_stmt>, phantom: PhantomData<(&'l raw::sqlite3, raw::sqlite3_stmt)>,
} }
/// A binding of a prepared statement. /// A binding of a prepared statement.
@ -101,7 +101,7 @@ impl Value for String {
} }
#[inline] #[inline]
pub fn new<'l>(database: &mut Database<'l>, sql: &str) -> Result<Statement<'l>> { pub fn new<'l>(database: &'l mut Database, sql: &str) -> Result<Statement<'l>> {
let mut raw = 0 as *mut _; let mut raw = 0 as *mut _;
unsafe { unsafe {
success!(database, raw::sqlite3_prepare(::database::as_raw(database), str_to_c_str!(sql), success!(database, raw::sqlite3_prepare(::database::as_raw(database), str_to_c_str!(sql),