rust-postgres/tokio-postgres/src/statement.rs

86 lines
2.0 KiB
Rust
Raw Normal View History

2019-07-24 02:54:22 +00:00
use crate::client::InnerClient;
2019-07-25 02:18:15 +00:00
use crate::codec::FrontendMessage;
use crate::connection::RequestMessages;
2019-07-24 02:54:22 +00:00
use crate::types::Type;
2019-07-25 02:18:15 +00:00
use postgres_protocol::message::frontend;
2019-07-24 02:54:22 +00:00
use std::sync::{Arc, Weak};
2019-07-25 02:18:15 +00:00
struct StatementInner {
2019-07-24 02:54:22 +00:00
client: Weak<InnerClient>,
name: String,
params: Vec<Type>,
columns: Vec<Column>,
}
2019-07-25 02:18:15 +00:00
impl Drop for StatementInner {
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'S', &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-07-25 02:18:15 +00:00
}
}
}
2019-08-02 01:40:14 +00:00
/// A prepared statement.
///
/// Prepared statements can only be used with the connection that created them.
2019-07-25 02:18:15 +00:00
#[derive(Clone)]
pub struct Statement(Arc<StatementInner>);
2019-07-24 02:54:22 +00:00
impl Statement {
pub(crate) fn new(
inner: &Arc<InnerClient>,
name: String,
params: Vec<Type>,
columns: Vec<Column>,
) -> Statement {
2019-07-25 02:18:15 +00:00
Statement(Arc::new(StatementInner {
2019-07-24 02:54:22 +00:00
client: Arc::downgrade(inner),
name,
params,
columns,
2019-07-25 02:18:15 +00:00
}))
}
pub(crate) fn name(&self) -> &str {
&self.0.name
2019-07-24 02:54:22 +00:00
}
/// Returns the expected types of the statement's parameters.
pub fn params(&self) -> &[Type] {
2019-07-25 02:18:15 +00:00
&self.0.params
2019-07-24 02:54:22 +00:00
}
/// Returns information about the columns returned when the statement is queried.
pub fn columns(&self) -> &[Column] {
2019-07-25 02:18:15 +00:00
&self.0.columns
2019-07-24 02:54:22 +00:00
}
}
2019-08-02 01:40:14 +00:00
/// Information about a column of a query.
2019-07-24 02:54:22 +00:00
#[derive(Debug)]
pub struct Column {
name: String,
type_: Type,
}
impl Column {
pub(crate) fn new(name: String, type_: Type) -> Column {
Column { name, type_ }
}
/// Returns the name of the column.
pub fn name(&self) -> &str {
&self.name
}
/// Returns the type of the column.
pub fn type_(&self) -> &Type {
&self.type_
}
}