tokio_postgres/
portal.rs

1use crate::client::InnerClient;
2use crate::codec::FrontendMessage;
3use crate::connection::RequestMessages;
4use crate::Statement;
5use postgres_protocol::message::frontend;
6use std::sync::{Arc, Weak};
7
8struct Inner {
9    client: Weak<InnerClient>,
10    name: String,
11    statement: Statement,
12}
13
14impl Drop for Inner {
15    fn drop(&mut self) {
16        if let Some(client) = self.client.upgrade() {
17            let buf = client.with_buf(|buf| {
18                frontend::close(b'P', &self.name, buf).unwrap();
19                frontend::sync(buf);
20                buf.split().freeze()
21            });
22            let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
23        }
24    }
25}
26
27/// A portal.
28///
29/// Portals can only be used with the connection that created them, and only exist for the duration of the transaction
30/// in which they were created.
31#[derive(Clone)]
32pub struct Portal(Arc<Inner>);
33
34impl Portal {
35    pub(crate) fn new(client: &Arc<InnerClient>, name: String, statement: Statement) -> Portal {
36        Portal(Arc::new(Inner {
37            client: Arc::downgrade(client),
38            name,
39            statement,
40        }))
41    }
42
43    pub(crate) fn name(&self) -> &str {
44        &self.0.name
45    }
46
47    pub(crate) fn statement(&self) -> &Statement {
48        &self.0.statement
49    }
50}