Support statement closure

This commit is contained in:
Steven Fackler 2016-12-21 20:54:21 -05:00
parent f03b4b181a
commit fd1724a6e2
2 changed files with 34 additions and 1 deletions

View File

@ -555,6 +555,32 @@ impl Connection {
.boxed()
}
fn raw_close(self, type_: u8, name: &str) -> BoxFuture<Connection, Error> {
let mut close = vec![];
let mut sync = vec![];
frontend::close(type_, name, &mut close)
.map(|()| frontend::sync(&mut sync))
.into_future()
.and_then(move |()| {
let it = Some(close).into_iter().chain(Some(sync)).map(Ok::<_, io::Error>);
self.0.send_all(futures::stream::iter(it))
})
.and_then(|s| s.0.read())
.map_err(Error::Io)
.and_then(|(m, s)| {
match m {
backend::Message::CloseComplete => Either::A(Ok(Connection(s)).into_future()),
backend::Message::ErrorResponse(body) => {
Either::B(Connection(s).ready_err(body))
}
_ => Either::A(Err(bad_message()).into_future()),
}
})
.and_then(|s| s.ready(()))
.map(|((), s)| s)
.boxed()
}
pub fn cancel_data(&self) -> CancelData {
self.0.cancel_data
}
@ -581,6 +607,10 @@ impl Statement {
.map(|(n, conn)| (n, self, conn))
.boxed()
}
pub fn close(self, conn: Connection) -> BoxFuture<Connection, Error> {
conn.raw_close(b'S', &self.name)
}
}
fn connect_err(fields: &mut ErrorFields) -> ConnectError {

View File

@ -113,8 +113,11 @@ fn prepare_execute() {
c.unwrap().prepare("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, name VARCHAR)")
})
.and_then(|(s, c)| s.execute(&[], c))
.and_then(|(n, _, c)| {
.and_then(|(n, s, c)| {
assert_eq!(0, n);
s.close(c)
})
.and_then(|c| {
c.prepare("INSERT INTO foo (name) VALUES ($1), ($2)")
})
.and_then(|(s, c)| s.execute(&[&"steven", &"bob"], c))