From b0946fabf185eb021a03d32758bfebdeb75a5d3d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 23 Dec 2016 14:09:05 -0500 Subject: [PATCH] Transaction creation and completion --- postgres-tokio/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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)),