Skip to content

Commit fd1724a

Browse files
committed
Support statement closure
1 parent f03b4b1 commit fd1724a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

postgres-tokio/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,32 @@ impl Connection {
555555
.boxed()
556556
}
557557

558+
fn raw_close(self, type_: u8, name: &str) -> BoxFuture<Connection, Error> {
559+
let mut close = vec![];
560+
let mut sync = vec![];
561+
frontend::close(type_, name, &mut close)
562+
.map(|()| frontend::sync(&mut sync))
563+
.into_future()
564+
.and_then(move |()| {
565+
let it = Some(close).into_iter().chain(Some(sync)).map(Ok::<_, io::Error>);
566+
self.0.send_all(futures::stream::iter(it))
567+
})
568+
.and_then(|s| s.0.read())
569+
.map_err(Error::Io)
570+
.and_then(|(m, s)| {
571+
match m {
572+
backend::Message::CloseComplete => Either::A(Ok(Connection(s)).into_future()),
573+
backend::Message::ErrorResponse(body) => {
574+
Either::B(Connection(s).ready_err(body))
575+
}
576+
_ => Either::A(Err(bad_message()).into_future()),
577+
}
578+
})
579+
.and_then(|s| s.ready(()))
580+
.map(|((), s)| s)
581+
.boxed()
582+
}
583+
558584
pub fn cancel_data(&self) -> CancelData {
559585
self.0.cancel_data
560586
}
@@ -581,6 +607,10 @@ impl Statement {
581607
.map(|(n, conn)| (n, self, conn))
582608
.boxed()
583609
}
610+
611+
pub fn close(self, conn: Connection) -> BoxFuture<Connection, Error> {
612+
conn.raw_close(b'S', &self.name)
613+
}
584614
}
585615

586616
fn connect_err(fields: &mut ErrorFields) -> ConnectError {

postgres-tokio/src/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ fn prepare_execute() {
113113
c.unwrap().prepare("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, name VARCHAR)")
114114
})
115115
.and_then(|(s, c)| s.execute(&[], c))
116-
.and_then(|(n, _, c)| {
116+
.and_then(|(n, s, c)| {
117117
assert_eq!(0, n);
118+
s.close(c)
119+
})
120+
.and_then(|c| {
118121
c.prepare("INSERT INTO foo (name) VALUES ($1), ($2)")
119122
})
120123
.and_then(|(s, c)| s.execute(&[&"steven", &"bob"], c))

0 commit comments

Comments
 (0)