pub struct Transaction { /* private fields */ }Expand description
数据库事务
Implementations§
Source§impl Transaction
impl Transaction
pub fn new(conn: Arc<dyn DatabaseConnection>) -> Self
Sourcepub async fn commit(&mut self) -> Result<(), DbError>
pub async fn commit(&mut self) -> Result<(), DbError>
Examples found in repository?
examples/postgresql_example.rs (line 214)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191 println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192 let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194 // Commit
195 let mut tx = db.begin_transaction().await?;
196 tx.execute(
197 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198 &[
199 SimpleUuid::new_v4().to_string().into(),
200 "Dave".into(),
201 40.into(),
202 ],
203 )
204 .await?;
205 tx.execute(
206 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207 &[
208 SimpleUuid::new_v4().to_string().into(),
209 "Frank".into(),
210 35.into(),
211 ],
212 )
213 .await?;
214 tx.commit().await?;
215 println!(" ✅ Transaction committed (2 users)");
216
217 // Rollback
218 let mut tx = db.begin_transaction().await?;
219 tx.execute(
220 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221 &[
222 SimpleUuid::new_v4().to_string().into(),
223 "Eve".into(),
224 45.into(),
225 ],
226 )
227 .await?;
228 tx.rollback().await?;
229 println!(" ✅ Transaction rolled back (Eve not saved)");
230
231 let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232 println!(" ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234 db.close().await?;
235 Ok(())
236}Sourcepub async fn rollback(&mut self) -> Result<(), DbError>
pub async fn rollback(&mut self) -> Result<(), DbError>
Examples found in repository?
examples/postgresql_example.rs (line 228)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191 println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192 let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194 // Commit
195 let mut tx = db.begin_transaction().await?;
196 tx.execute(
197 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198 &[
199 SimpleUuid::new_v4().to_string().into(),
200 "Dave".into(),
201 40.into(),
202 ],
203 )
204 .await?;
205 tx.execute(
206 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207 &[
208 SimpleUuid::new_v4().to_string().into(),
209 "Frank".into(),
210 35.into(),
211 ],
212 )
213 .await?;
214 tx.commit().await?;
215 println!(" ✅ Transaction committed (2 users)");
216
217 // Rollback
218 let mut tx = db.begin_transaction().await?;
219 tx.execute(
220 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221 &[
222 SimpleUuid::new_v4().to_string().into(),
223 "Eve".into(),
224 45.into(),
225 ],
226 )
227 .await?;
228 tx.rollback().await?;
229 println!(" ✅ Transaction rolled back (Eve not saved)");
230
231 let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232 println!(" ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234 db.close().await?;
235 Ok(())
236}Sourcepub async fn execute(
&self,
sql: &str,
params: &[SqlValue],
) -> Result<u64, DbError>
pub async fn execute( &self, sql: &str, params: &[SqlValue], ) -> Result<u64, DbError>
Examples found in repository?
examples/postgresql_example.rs (lines 196-203)
190async fn demonstrate_transactions() -> std::result::Result<(), Box<dyn std::error::Error>> {
191 println!("PostgreSQL transactions (BEGIN / COMMIT / ROLLBACK):");
192 let db = Database::postgresql(PG_HOST, PG_PORT, PG_DB, PG_USER, PG_PASS).await?;
193
194 // Commit
195 let mut tx = db.begin_transaction().await?;
196 tx.execute(
197 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
198 &[
199 SimpleUuid::new_v4().to_string().into(),
200 "Dave".into(),
201 40.into(),
202 ],
203 )
204 .await?;
205 tx.execute(
206 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
207 &[
208 SimpleUuid::new_v4().to_string().into(),
209 "Frank".into(),
210 35.into(),
211 ],
212 )
213 .await?;
214 tx.commit().await?;
215 println!(" ✅ Transaction committed (2 users)");
216
217 // Rollback
218 let mut tx = db.begin_transaction().await?;
219 tx.execute(
220 "INSERT INTO users (id, name, age) VALUES ($1, $2, $3)",
221 &[
222 SimpleUuid::new_v4().to_string().into(),
223 "Eve".into(),
224 45.into(),
225 ],
226 )
227 .await?;
228 tx.rollback().await?;
229 println!(" ✅ Transaction rolled back (Eve not saved)");
230
231 let result = db.query("SELECT COUNT(*) AS count FROM users", &[]).await?;
232 println!(" ✅ Final user count: {:?}", result.rows[0].get("count"));
233
234 db.close().await?;
235 Ok(())
236}pub async fn execute_query( &self, sql: &str, params: &[SqlValue], ) -> Result<QueryResult, DbError>
Trait Implementations§
Source§impl Drop for Transaction
impl Drop for Transaction
Auto Trait Implementations§
impl !RefUnwindSafe for Transaction
impl !UnwindSafe for Transaction
impl Freeze for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl UnsafeUnpin for Transaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more