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