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, name: String, statement: Statement, } impl Drop for Inner { fn drop(&mut self) { if let Some(client) = self.client.upgrade() { let mut buf = vec![]; frontend::close(b'P', &self.name, &mut buf).expect("portal name not valid"); frontend::sync(&mut buf); let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf))); } } } /// 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); impl Portal { pub(crate) fn new(client: &Arc, 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 } }