Transaction creation and completion

This commit is contained in:
Steven Fackler 2016-12-23 14:09:05 -05:00
parent fb46202bd3
commit b0946fabf1

View File

@ -638,6 +638,12 @@ impl Connection {
.boxed()
}
pub fn transaction(self) -> BoxFuture<Transaction, Error> {
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<Connection, Error> {
self.finish("COMMIT")
}
pub fn rollback(self) -> BoxFuture<Connection, Error> {
self.finish("ROLLBACK")
}
fn finish(self, query: &str) -> BoxFuture<Connection, Error> {
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)),