2019-08-02 03:43:13 +00:00
|
|
|
use crate::client::InnerClient;
|
|
|
|
use crate::codec::FrontendMessage;
|
|
|
|
use crate::connection::RequestMessages;
|
|
|
|
use crate::Statement;
|
|
|
|
use postgres_protocol::message::frontend;
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
client: Weak<InnerClient>,
|
|
|
|
name: String,
|
|
|
|
statement: Statement,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Inner {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(client) = self.client.upgrade() {
|
2019-10-13 01:07:09 +00:00
|
|
|
let buf = client.with_buf(|buf| {
|
|
|
|
frontend::close(b'P', &self.name, buf).unwrap();
|
|
|
|
frontend::sync(buf);
|
2019-11-27 00:32:36 +00:00
|
|
|
buf.split().freeze()
|
2019-10-13 01:07:09 +00:00
|
|
|
});
|
|
|
|
let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
|
2019-08-02 03:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A portal.
|
|
|
|
///
|
|
|
|
/// Portals can only be used with the connection that created them, and only exist for the duration of the transaction
|
|
|
|
/// in which they were created.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Portal(Arc<Inner>);
|
|
|
|
|
|
|
|
impl Portal {
|
|
|
|
pub(crate) fn new(client: &Arc<InnerClient>, name: String, statement: Statement) -> Portal {
|
|
|
|
Portal(Arc::new(Inner {
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
name,
|
|
|
|
statement,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn name(&self) -> &str {
|
|
|
|
&self.0.name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn statement(&self) -> &Statement {
|
|
|
|
&self.0.statement
|
|
|
|
}
|
|
|
|
}
|