sqlite_rwc/
sync_adapter.rs

1use crate::drivers::Driver;
2use crate::pool::{ConnectionAdapter, ConnectionPool, PooledConnection};
3use std::ops::Deref;
4
5pub type SyncConnectionPool<T> = ConnectionPool<T, SyncConnectionAdapter<T>>;
6
7/// Defines behaviors appropriate for usage of a connection with sync/non-async usage.
8pub struct SyncConnectionAdapter<T: Driver>(T::Connection);
9
10impl<T: Driver> SyncConnectionAdapter<T> {
11    fn read<F, R, E>(&mut self, closure: F) -> Result<R, E>
12    where
13        F: FnOnce(T::ConnectionRef<'_>) -> Result<R, E>,
14        E: From<T::Error>,
15    {
16        T::read(&mut self.0, closure)
17    }
18}
19
20impl<T: Driver> ConnectionAdapter<T> for SyncConnectionAdapter<T> {
21    fn from_driver_connection(connection: T::Connection) -> Self {
22        Self(connection)
23    }
24}
25
26impl<T: Driver> PooledConnection<T, SyncConnectionAdapter<T>> {
27    /// Perform a read operation with a stable scope.
28    ///
29    /// This method uses an explicit transaction to ensure the reader does not take into
30    /// account ongoing modifications of writers during the execution of the statements in
31    /// the scope of the closure.
32    ///
33    /// Use [`Self::read`] if you want to have the default sqlite behavior, where each statement
34    /// gets their own transaction.
35    ///
36    /// # Errors
37    ///
38    /// Returns error if the closure failed or if the read transaction could not be initiated.
39    pub fn scoped_read<F, R, E>(&mut self, closure: F) -> Result<R, E>
40    where
41        F: FnOnce(T::ConnectionRef<'_>) -> Result<R, E>,
42        E: From<T::Error>,
43    {
44        let conn = self.connection_mut();
45        conn.read(closure)
46    }
47
48    /// Perform a read operation without a stable scope.
49    ///
50    /// Unlike [`Self::scoped_read`], this method uses the default Sqlite transaction behavior
51    /// where each statement will create its own transaction.
52    ///
53    /// # Errors
54    ///
55    /// Returns error if the closure failed.
56    pub fn read<F, R, E>(&mut self, closure: F) -> Result<R, E>
57    where
58        F: FnOnce(&T::Connection) -> Result<R, E>,
59        E: From<T::Error>,
60    {
61        let conn = self.connection_mut();
62        closure(&conn.0)
63    }
64
65    /// Create a transaction and run the given `closure` in its scope.
66    ///
67    /// If the closure returns an error the transaction is aborted, otherwise it is commited.
68    ///
69    /// # Errors
70    ///
71    /// Returns error if the closure failed or the transaction failed to commit.
72    pub fn transaction<F, R, E>(&mut self, closure: F) -> Result<R, E>
73    where
74        F: FnOnce(&mut T::Transaction<'_>) -> Result<R, E>,
75        E: From<T::Error>,
76    {
77        self.pool.transaction(closure)
78    }
79}
80
81impl<T: Driver> Deref for PooledConnection<T, SyncConnectionAdapter<T>> {
82    type Target = T::Connection;
83
84    fn deref(&self) -> &Self::Target {
85        &self.connection().0
86    }
87}