taitan_orm/database/sqlite/
transaction.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200


use crate::sql_generator::DefaultSqlGenerator;
use crate::sql_generator_container::SqlGeneratorContainer;
use sqlx::Sqlite;
use crate::sql_generic_executor::SqlGenericExecutor;
use crate::{transaction_impl, CountResult};

#[derive(Debug)]
pub struct SqliteTransaction<'a> {
    transaction: sqlx::Transaction<'a, Sqlite>,
    sql_generator: &'a DefaultSqlGenerator,
}

impl<'a> SqliteTransaction<'a> {
    pub fn new(trx: sqlx::Transaction<'a, Sqlite>, sql_generator: &'a DefaultSqlGenerator) -> Self {
        Self {
            transaction: trx,
            sql_generator,
        }
    }

    #[inline]
    pub async fn commit(self) -> crate::Result<()> {
        Ok(self.transaction.commit().await?)
    }

    #[inline]
    pub async fn rollback(self) -> crate::Result<()> {
        Ok(self.transaction.rollback().await?)
    }
}

impl<'t> SqlGenericExecutor for SqliteTransaction<'t> {
    type DB = Sqlite;
    type CountType = CountResult;

    fn get_affected_rows(query_result: &<Self::DB as sqlx::Database>::QueryResult) -> u64 {
        query_result.rows_affected()
    }
}

impl<'t> crate::SqlExecutorMut for SqliteTransaction<'t> {
    transaction_impl!(SqliteConnection);
}
impl<'a> SqlGeneratorContainer for SqliteTransaction<'a> {
    type G = DefaultSqlGenerator;

    fn get_generator(&self) -> &Self::G {
        &self.sql_generator
    }
}

// impl<'a> SqliteWriteCommander for SqliteTransaction<'a> {}
//
// impl<'a> SqliteReadCommander for SqliteTransaction<'a> {}
//
// impl<'a> SqliteTemplateCommander for SqliteTransaction<'a> {}




//
// impl<'s> crate::SqlExecutor for SqliteTransaction<'s> {
//
//     type Connection = SqliteConnection;
//
//     async fn execute<'a>(&'a mut self, stmt: &'a str, args: <Self::DB as sqlx::Database>::Arguments<'a>) -> crate::Result<u64>
//     {
//         Self::generic_execute(&mut *self.transaction, stmt, args).await
//     }
//
//
//     async fn execute_plain<'a>(&'a mut self, stmt: &'a str) -> crate::Result<u64> {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_execute_plain(&mut *(self.transaction), stmt, args).await
//     }
//
//     async fn fetch_exists<'a>(&'a mut self, stmt: &'a str, args: <Self::DB as sqlx::Database>::Arguments<'a>) -> crate::Result<bool> {
//         Self::generic_exists(&mut *self.transaction, stmt, args).await
//     }
//
//
//     async fn fetch_exists_plain<'a, A>(&'a mut self, stmt: &'a str) -> crate::Result<bool> {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_exists_plain(&mut *self.transaction, stmt, args).await
//     }
//
//
//     async fn fetch_option<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         selection: &'a SE::Selection,
//         args: <Self::DB as sqlx::Database>::Arguments<'a>,
//     ) -> crate::Result<Option<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         Self::generic_fetch_option(&mut *self.transaction, stmt, selection, args).await
//     }
//
//     async fn fetch_option_plain<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         selection: &'a SE::Selection,
//     ) -> crate::Result<Option<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin
//     {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_fetch_option_plain(&mut *self.transaction, stmt, selection, args).await
//     }
//
//     async fn fetch_all<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         selection: &'a SE::Selection,
//         args: <Self::DB as sqlx::Database>::Arguments<'a>,
//     ) -> crate::Result<Vec<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         Self::generic_fetch_all(&mut *self.transaction, stmt, selection, args).await
//     }
//
//     async fn fetch_all_plain<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         selection: &'a SE::Selection,
//     ) -> crate::Result<Vec<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_fetch_all_plain(&mut *self.transaction, stmt, selection, args).await
//     }
//
//     async fn fetch_one_full<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         args: <Self::DB as sqlx::Database>::Arguments<'a>,
//     ) -> crate::Result<SE>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         Self::generic_fetch_one_full(&mut *self.transaction, stmt, args).await
//     }
//
//     async fn fetch_one_full_plain<'a, SE>(&'a mut self, stmt: &'a str) -> crate::Result<SE>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_fetch_one_full_plain(&mut *self.transaction, stmt, args).await
//     }
//
//     async fn fetch_option_full<'a, SE>(
//         &'a mut self,
//         stmt: &'a str,
//         args: <Self::DB as sqlx::Database>::Arguments<'a>,
//     ) -> crate::Result<Option<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         Self::generic_fetch_option_full(&mut *self.transaction, stmt, args).await
//     }
//
//     async fn fetch_option_full_plain<'a, SE>(&'a mut self, stmt: &'a str) -> crate::Result<Option<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin,
//     {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_fetch_option_full_plain(&mut *self.transaction, stmt, args).await
//     }
//
//
//     async fn fetch_all_full<'a, SE>(&'a mut self, stmt: &'a str, args: <Self::DB as sqlx::Database>::Arguments<'a>) -> crate::Result<Vec<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin
//     {
//         Self::generic_fetch_all_full(&mut *self.transaction, stmt, args).await
//     }
//
//     async fn fetch_all_full_plain<'a, SE>(&'a mut self, stmt: &'a str) -> crate::Result<Vec<SE>>
//     where
//         SE: taitan_orm_trait::SelectedEntity<Self::DB> + Send + Unpin
//     {
//         let args: std::marker::PhantomData<<Self::DB as sqlx::Database>::Arguments<'a>> =
//             std::marker::PhantomData::default();
//         Self::generic_fetch_all_full_plain(&mut *self.transaction, stmt, args).await
//     }
// }