Skip to content

Commit 44fa44a

Browse files
committed
Sync transactions
1 parent 7592560 commit 44fa44a

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

postgres/src/client.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tokio_postgres::{MakeTlsMode, Socket, TlsMode};
66

77
#[cfg(feature = "runtime")]
88
use crate::Builder;
9-
use crate::Statement;
9+
use crate::{Statement, Transaction};
1010

1111
pub struct Client(tokio_postgres::Client);
1212

@@ -51,6 +51,11 @@ impl Client {
5151
pub fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
5252
self.0.batch_execute(query).wait()
5353
}
54+
55+
pub fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
56+
self.batch_execute("BEGIN")?;
57+
Ok(Transaction::new(self))
58+
}
5459
}
5560

5661
impl From<tokio_postgres::Client> for Client {

postgres/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use tokio::runtime::{self, Runtime};
77
mod builder;
88
mod client;
99
mod statement;
10+
mod transaction;
1011

1112
#[cfg(feature = "runtime")]
1213
pub use crate::builder::*;
1314
pub use crate::client::*;
1415
pub use crate::statement::*;
16+
pub use crate::transaction::*;
1517

1618
#[cfg(feature = "runtime")]
1719
lazy_static! {

postgres/src/transaction.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use tokio_postgres::types::{ToSql, Type};
2+
use tokio_postgres::{Error, Row};
3+
4+
use crate::{Client, Statement};
5+
6+
pub struct Transaction<'a> {
7+
client: &'a mut Client,
8+
done: bool,
9+
}
10+
11+
impl<'a> Drop for Transaction<'a> {
12+
fn drop(&mut self) {
13+
if !self.done {
14+
let _ = self.rollback_inner();
15+
}
16+
}
17+
}
18+
19+
impl<'a> Transaction<'a> {
20+
pub(crate) fn new(client: &'a mut Client) -> Transaction<'a> {
21+
Transaction {
22+
client,
23+
done: false,
24+
}
25+
}
26+
27+
pub fn commit(mut self) -> Result<(), Error> {
28+
self.done = true;
29+
self.client.batch_execute("COMMIT")
30+
}
31+
32+
pub fn rollback(mut self) -> Result<(), Error> {
33+
self.done = true;
34+
self.rollback_inner()
35+
}
36+
37+
fn rollback_inner(&mut self) -> Result<(), Error> {
38+
self.client.batch_execute("ROLLBACK")
39+
}
40+
41+
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
42+
self.client.prepare(query)
43+
}
44+
45+
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
46+
self.client.prepare_typed(query, types)
47+
}
48+
49+
pub fn execute(&mut self, statement: &Statement, params: &[&dyn ToSql]) -> Result<u64, Error> {
50+
self.client.execute(statement, params)
51+
}
52+
53+
pub fn query(
54+
&mut self,
55+
statement: &Statement,
56+
params: &[&dyn ToSql],
57+
) -> Result<Vec<Row>, Error> {
58+
self.client.query(statement, params)
59+
}
60+
61+
pub fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
62+
self.client.batch_execute(query)
63+
}
64+
}

0 commit comments

Comments
 (0)