c3p0/
tx.rs

1use sqlx::Database;
2
3use crate::{C3p0Error, DataType, NewRecord, Record, WithData};
4
5/// A trait for a transaction.
6pub trait Tx {
7    type DB: Database;
8
9    /// Creates the table if it does not exist.
10    fn create_table_if_not_exists<DATA: WithData>(
11        &mut self,
12    ) -> impl Future<Output = Result<(), C3p0Error>>;
13
14    /// Drops the table if it exists.
15    fn drop_table_if_exists<DATA: WithData>(
16        &mut self,
17        cascade: bool,
18    ) -> impl Future<Output = Result<(), C3p0Error>>;
19
20    /// Returns the number of rows in the table.
21    fn count_all<DATA: WithData>(&mut self) -> impl Future<Output = Result<u64, C3p0Error>>;
22
23    /// Returns true if the entry with the given id exists.
24    fn exists_by_id<DATA: WithData>(
25        &mut self,
26        id: u64,
27    ) -> impl Future<Output = Result<bool, C3p0Error>>;
28
29    /// Returns all entries in the table.
30    fn fetch_all<DATA: DataType>(
31        &mut self,
32    ) -> impl Future<Output = Result<Vec<Record<DATA>>, C3p0Error>>;
33
34    /// Returns the entry with the given id. Returns None if the entry does not exist.
35    fn fetch_one_optional_by_id<DATA: DataType>(
36        &mut self,
37        id: u64,
38    ) -> impl Future<Output = Result<Option<Record<DATA>>, C3p0Error>>;
39
40    /// Returns the entry with the given id. Returns an error if the entry does not exist.
41    fn fetch_one_by_id<DATA: DataType>(
42        &mut self,
43        id: u64,
44    ) -> impl Future<Output = Result<Record<DATA>, C3p0Error>>;
45
46    /// Deletes the entry with the given id.
47    fn delete<DATA: DataType>(
48        &mut self,
49        record: Record<DATA>,
50    ) -> impl Future<Output = Result<Record<DATA>, C3p0Error>>;
51
52    /// Deletes all entries in the table.
53    fn delete_all<DATA: WithData>(&mut self) -> impl Future<Output = Result<u64, C3p0Error>>;
54
55    /// Deletes the entry with the given id.
56    fn delete_by_id<DATA: WithData>(
57        &mut self,
58        id: u64,
59    ) -> impl Future<Output = Result<u64, C3p0Error>>;
60
61    /// Updates the entry with the given id. Returns an error if the entry does not exist.
62    /// This uses optimistic locking by using the version field to detect update conflicts; it will update the entry and will throw an error if the version does not match.
63    /// The version field is incremented by 1 for each update.
64    fn update<DATA: DataType>(
65        &mut self,
66        record: Record<DATA>,
67    ) -> impl Future<Output = Result<Record<DATA>, C3p0Error>>;
68
69    /// Creates a new entry.
70    fn save<DATA: DataType>(
71        &mut self,
72        record: NewRecord<DATA>,
73    ) -> impl Future<Output = Result<Record<DATA>, C3p0Error>>;
74}