From 8e990ff3c66bc95951fa368fa364caba89f79125 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Sat, 8 Jun 2019 19:50:27 +0200 Subject: [PATCH] Introduce connection flags --- src/connection.rs | 53 +++++++++++++++++++++++++++++++---------------- src/lib.rs | 12 +++++++---- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index b956477..579e2dd 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -12,35 +12,26 @@ pub struct Connection { phantom: PhantomData, } +/// A set of connection flags. +#[derive(Clone, Copy, Debug)] +pub struct ConnectionFlags(c_int); + unsafe impl Send for Connection {} impl Connection { - /// Open a connection to a new or existing database. + /// Open a read-write connection to a new or existing database. pub fn open>(path: T) -> Result { - let mut raw = 0 as *mut _; - unsafe { - ok!(ffi::sqlite3_open_v2( - path_to_cstr!(path.as_ref()).as_ptr(), - &mut raw, - ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE, - 0 as *const _, - )); - } - Ok(Connection { - raw: raw, - busy_callback: None, - phantom: PhantomData, - }) + Connection::open_with_flags(path, ConnectionFlags::new().set_create().set_read_write()) } - /// Open a read-only connection to an existing database. - pub fn open_readonly>(path: T) -> Result { + /// Open a database connection with a specific set of connection flags. + pub fn open_with_flags>(path: T, flags: ConnectionFlags) -> Result { let mut raw = 0 as *mut _; unsafe { ok!(ffi::sqlite3_open_v2( path_to_cstr!(path.as_ref()).as_ptr(), &mut raw, - ffi::SQLITE_OPEN_READONLY, + flags.0, 0 as *const _, )); } @@ -166,6 +157,32 @@ impl Drop for Connection { } } +impl ConnectionFlags { + /// Create a set of connection flags. + #[inline] + pub fn new() -> Self { + ConnectionFlags(0) + } + + /// Create the database if it does not already exist. + pub fn set_create(mut self) -> Self { + self.0 |= ffi::SQLITE_OPEN_CREATE; + self + } + + /// Open the database for reading only. + pub fn set_read_only(mut self) -> Self { + self.0 |= ffi::SQLITE_OPEN_READWRITE; + self + } + + /// Open the database for reading and writing. + pub fn set_read_write(mut self) -> Self { + self.0 |= ffi::SQLITE_OPEN_READWRITE; + self + } +} + extern "C" fn busy_callback(callback: *mut c_void, attempts: c_int) -> c_int where F: FnMut(usize) -> bool, diff --git a/src/lib.rs b/src/lib.rs index 84dcb5d..70b9dd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -294,19 +294,23 @@ mod cursor; mod statement; pub use connection::Connection; +pub use connection::ConnectionFlags; pub use cursor::Cursor; pub use statement::{Bindable, Readable, State, Statement}; -/// Open a connection to a new or existing database. +/// Open a read-write connection to a new or existing database. #[inline] pub fn open>(path: T) -> Result { Connection::open(path) } -/// Open a read-only connection to an existing database. +/// Open a connection to a database with a specific set of connection flags. #[inline] -pub fn open_readonly>(path: T) -> Result { - Connection::open_readonly(path) +pub fn open_with_flags>( + path: T, + flags: ConnectionFlags, +) -> Result { + Connection::open_with_flags(path, flags) } /// Return the version number of SQLite.