diff --git a/postgres-tokio/src/lib.rs b/postgres-tokio/src/lib.rs index 80091b2b..29c1e835 100644 --- a/postgres-tokio/src/lib.rs +++ b/postgres-tokio/src/lib.rs @@ -638,6 +638,12 @@ impl Connection { .boxed() } + pub fn transaction(self) -> BoxFuture { + self.simple_query("BEGIN") + .map(|(_, c)| Transaction(c)) + .boxed() + } + pub fn close(self) -> BoxFuture<(), Error> { let mut terminate = vec![]; frontend::terminate(&mut terminate); @@ -720,6 +726,24 @@ impl Row { } } +pub struct Transaction(Connection); + +impl Transaction { + pub fn commit(self) -> BoxFuture { + self.finish("COMMIT") + } + + pub fn rollback(self) -> BoxFuture { + self.finish("ROLLBACK") + } + + fn finish(self, query: &str) -> BoxFuture { + self.0.simple_query(query) + .map(|(_, c)| c) + .boxed() + } +} + fn connect_err(fields: &mut ErrorFields) -> ConnectError { match DbError::new(fields) { Ok(err) => ConnectError::Db(Box::new(err)),