sqlite_rwc/drivers.rs
1use std::fmt::Debug;
2use std::path::Path;
3
4#[cfg(feature = "rusqlite")]
5pub mod rusqlite;
6
7#[cfg(feature = "diesel")]
8pub mod diesel;
9
10/// Implementation interface for any library which provides binding for sqlite.
11pub trait Driver: 'static + Send + Sync + Debug {
12 #[cfg(feature = "watcher")]
13 type Connection: sqlite_watcher::connection::SqlExecutorMut<Error = Self::Error>;
14 #[cfg(not(feature = "watcher"))]
15 type Connection;
16 type Transaction<'t>;
17 type Error: Debug;
18 type ConnectionError: Debug;
19
20 /// Represents the reference to a connection type. Some drivers my specify this as
21 /// immutable or mutable reference.
22 type ConnectionRef<'c>;
23
24 /// Create new read-only connection with the given `path`.
25 ///
26 /// This method should not creat the db file if it does not exist.
27 ///
28 /// # Errors
29 ///
30 /// Should return error if the connection could not be created.
31 fn new_read_connection(path: &Path) -> Result<Self::Connection, Self::ConnectionError>;
32
33 /// Create a new writable connection with the given `path`.
34 ///
35 /// This method should create the database file if it does not exist.
36 ///
37 /// # Errors
38 ///
39 /// Should return error if the connection could not be created.
40 fn new_write_connection(path: &Path) -> Result<Self::Connection, Self::ConnectionError>;
41
42 /// Create a read transaction over the given `closure`.
43 ///
44 /// A read transaction is created by executing the following SQL statements:
45 ///
46 /// ```sql
47 /// BEGIN
48 /// -- closure()
49 /// END
50 /// ```
51 ///
52 /// # Errors
53 ///
54 /// Return error if the transaction could not be started or the closure failed.
55 ///
56 fn read<F, R, E>(connection: &mut Self::Connection, closure: F) -> Result<R, E>
57 where
58 F: FnOnce(Self::ConnectionRef<'_>) -> Result<R, E>,
59 E: From<Self::Error>;
60
61 /// Create new transaction an execute the given `closure`.
62 ///
63 /// The transaction should be created with `BEGIN IMMEDIATE` to ensure the all file locks
64 /// are acquired at the start of the transaction.
65 ///
66 /// # Errors
67 ///
68 /// Return error if the transaction could not be started, commited or the closure failed
69 /// to execute.
70 fn write<F, R, E>(connection: &mut Self::Connection, closure: F) -> Result<R, E>
71 where
72 F: FnOnce(&mut Self::Transaction<'_>) -> Result<R, E>,
73 E: From<Self::Error>;
74}