yb_tokio_postgres/
statement.rs

1use crate::client::InnerClient;
2use crate::codec::FrontendMessage;
3use crate::connection::RequestMessages;
4use crate::types::Type;
5use postgres_protocol::message::frontend;
6use std::{
7    fmt,
8    sync::{Arc, Weak},
9};
10
11struct StatementInner {
12    client: Weak<InnerClient>,
13    name: String,
14    params: Vec<Type>,
15    columns: Vec<Column>,
16}
17
18impl Drop for StatementInner {
19    fn drop(&mut self) {
20        if let Some(client) = self.client.upgrade() {
21            let buf = client.with_buf(|buf| {
22                frontend::close(b'S', &self.name, buf).unwrap();
23                frontend::sync(buf);
24                buf.split().freeze()
25            });
26            let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
27        }
28    }
29}
30
31/// A prepared statement.
32///
33/// Prepared statements can only be used with the connection that created them.
34#[derive(Clone)]
35pub struct Statement(Arc<StatementInner>);
36
37impl Statement {
38    pub(crate) fn new(
39        inner: &Arc<InnerClient>,
40        name: String,
41        params: Vec<Type>,
42        columns: Vec<Column>,
43    ) -> Statement {
44        Statement(Arc::new(StatementInner {
45            client: Arc::downgrade(inner),
46            name,
47            params,
48            columns,
49        }))
50    }
51
52    pub(crate) fn name(&self) -> &str {
53        &self.0.name
54    }
55
56    /// Returns the expected types of the statement's parameters.
57    pub fn params(&self) -> &[Type] {
58        &self.0.params
59    }
60
61    /// Returns information about the columns returned when the statement is queried.
62    pub fn columns(&self) -> &[Column] {
63        &self.0.columns
64    }
65}
66
67/// Information about a column of a query.
68pub struct Column {
69    name: String,
70    type_: Type,
71}
72
73impl Column {
74    pub(crate) fn new(name: String, type_: Type) -> Column {
75        Column { name, type_ }
76    }
77
78    /// Returns the name of the column.
79    pub fn name(&self) -> &str {
80        &self.name
81    }
82
83    /// Returns the type of the column.
84    pub fn type_(&self) -> &Type {
85        &self.type_
86    }
87}
88
89impl fmt::Debug for Column {
90    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
91        fmt.debug_struct("Column")
92            .field("name", &self.name)
93            .field("type", &self.type_)
94            .finish()
95    }
96}