Skip to content

Commit b0946fa

Browse files
committed
Transaction creation and completion
1 parent fb46202 commit b0946fa

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

postgres-tokio/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,12 @@ impl Connection {
638638
.boxed()
639639
}
640640

641+
pub fn transaction(self) -> BoxFuture<Transaction, Error> {
642+
self.simple_query("BEGIN")
643+
.map(|(_, c)| Transaction(c))
644+
.boxed()
645+
}
646+
641647
pub fn close(self) -> BoxFuture<(), Error> {
642648
let mut terminate = vec![];
643649
frontend::terminate(&mut terminate);
@@ -720,6 +726,24 @@ impl Row {
720726
}
721727
}
722728

729+
pub struct Transaction(Connection);
730+
731+
impl Transaction {
732+
pub fn commit(self) -> BoxFuture<Connection, Error> {
733+
self.finish("COMMIT")
734+
}
735+
736+
pub fn rollback(self) -> BoxFuture<Connection, Error> {
737+
self.finish("ROLLBACK")
738+
}
739+
740+
fn finish(self, query: &str) -> BoxFuture<Connection, Error> {
741+
self.0.simple_query(query)
742+
.map(|(_, c)| c)
743+
.boxed()
744+
}
745+
}
746+
723747
fn connect_err(fields: &mut ErrorFields) -> ConnectError {
724748
match DbError::new(fields) {
725749
Ok(err) => ConnectError::Db(Box::new(err)),

0 commit comments

Comments
 (0)