From 944ed6bbca72ac85441bb1f660afe6d98e1d91db Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 22 Sep 2023 16:41:07 -0700 Subject: [PATCH 1/3] support unnamed statements --- postgres-protocol/src/message/backend.rs | 23 ++++++++- tokio-postgres/src/bind.rs | 2 +- tokio-postgres/src/connect.rs | 2 +- tokio-postgres/src/connect_raw.rs | 10 ++-- tokio-postgres/src/connection.rs | 3 +- tokio-postgres/src/copy_in.rs | 11 ++-- tokio-postgres/src/copy_out.rs | 10 ++-- tokio-postgres/src/error/mod.rs | 12 +++-- tokio-postgres/src/prepare.rs | 8 +-- tokio-postgres/src/query.rs | 13 +++-- tokio-postgres/src/simple_query.rs | 6 +-- tokio-postgres/src/statement.rs | 66 ++++++++++++++++++------ tokio-postgres/src/to_statement.rs | 5 +- 13 files changed, 124 insertions(+), 47 deletions(-) diff --git a/postgres-protocol/src/message/backend.rs b/postgres-protocol/src/message/backend.rs index 1b5be1098..da267101c 100644 --- a/postgres-protocol/src/message/backend.rs +++ b/postgres-protocol/src/message/backend.rs @@ -72,6 +72,7 @@ impl Header { } /// An enum representing Postgres backend messages. +#[derive(Debug, PartialEq)] #[non_exhaustive] pub enum Message { AuthenticationCleartextPassword, @@ -333,6 +334,7 @@ impl Read for Buffer { } } +#[derive(Debug, PartialEq)] pub struct AuthenticationMd5PasswordBody { salt: [u8; 4], } @@ -344,6 +346,7 @@ impl AuthenticationMd5PasswordBody { } } +#[derive(Debug, PartialEq)] pub struct AuthenticationGssContinueBody(Bytes); impl AuthenticationGssContinueBody { @@ -353,6 +356,7 @@ impl AuthenticationGssContinueBody { } } +#[derive(Debug, PartialEq)] pub struct AuthenticationSaslBody(Bytes); impl AuthenticationSaslBody { @@ -362,6 +366,7 @@ impl AuthenticationSaslBody { } } +#[derive(Debug, PartialEq)] pub struct SaslMechanisms<'a>(&'a [u8]); impl<'a> FallibleIterator for SaslMechanisms<'a> { @@ -387,6 +392,7 @@ impl<'a> FallibleIterator for SaslMechanisms<'a> { } } +#[derive(Debug, PartialEq)] pub struct AuthenticationSaslContinueBody(Bytes); impl AuthenticationSaslContinueBody { @@ -396,6 +402,7 @@ impl AuthenticationSaslContinueBody { } } +#[derive(Debug, PartialEq)] pub struct AuthenticationSaslFinalBody(Bytes); impl AuthenticationSaslFinalBody { @@ -405,6 +412,7 @@ impl AuthenticationSaslFinalBody { } } +#[derive(Debug, PartialEq)] pub struct BackendKeyDataBody { process_id: i32, secret_key: i32, @@ -422,6 +430,7 @@ impl BackendKeyDataBody { } } +#[derive(Debug, PartialEq)] pub struct CommandCompleteBody { tag: Bytes, } @@ -433,6 +442,7 @@ impl CommandCompleteBody { } } +#[derive(Debug, PartialEq)] pub struct CopyDataBody { storage: Bytes, } @@ -449,6 +459,7 @@ impl CopyDataBody { } } +#[derive(Debug, PartialEq)] pub struct CopyInResponseBody { format: u8, len: u16, @@ -470,6 +481,7 @@ impl CopyInResponseBody { } } +#[derive(Debug, PartialEq)] pub struct ColumnFormats<'a> { buf: &'a [u8], remaining: u16, @@ -503,6 +515,7 @@ impl<'a> FallibleIterator for ColumnFormats<'a> { } } +#[derive(Debug, PartialEq)] pub struct CopyOutResponseBody { format: u8, len: u16, @@ -524,7 +537,7 @@ impl CopyOutResponseBody { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct DataRowBody { storage: Bytes, len: u16, @@ -599,6 +612,7 @@ impl<'a> FallibleIterator for DataRowRanges<'a> { } } +#[derive(Debug, PartialEq)] pub struct ErrorResponseBody { storage: Bytes, } @@ -657,6 +671,7 @@ impl<'a> ErrorField<'a> { } } +#[derive(Debug, PartialEq)] pub struct NoticeResponseBody { storage: Bytes, } @@ -668,6 +683,7 @@ impl NoticeResponseBody { } } +#[derive(Debug, PartialEq)] pub struct NotificationResponseBody { process_id: i32, channel: Bytes, @@ -691,6 +707,7 @@ impl NotificationResponseBody { } } +#[derive(Debug, PartialEq)] pub struct ParameterDescriptionBody { storage: Bytes, len: u16, @@ -706,6 +723,7 @@ impl ParameterDescriptionBody { } } +#[derive(Debug, PartialEq)] pub struct Parameters<'a> { buf: &'a [u8], remaining: u16, @@ -739,6 +757,7 @@ impl<'a> FallibleIterator for Parameters<'a> { } } +#[derive(Debug, PartialEq)] pub struct ParameterStatusBody { name: Bytes, value: Bytes, @@ -756,6 +775,7 @@ impl ParameterStatusBody { } } +#[derive(Debug, PartialEq)] pub struct ReadyForQueryBody { status: u8, } @@ -767,6 +787,7 @@ impl ReadyForQueryBody { } } +#[derive(Debug, PartialEq)] pub struct RowDescriptionBody { storage: Bytes, len: u16, diff --git a/tokio-postgres/src/bind.rs b/tokio-postgres/src/bind.rs index 9c5c49218..dac1a3c06 100644 --- a/tokio-postgres/src/bind.rs +++ b/tokio-postgres/src/bind.rs @@ -31,7 +31,7 @@ where match responses.next().await? { Message::BindComplete => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } Ok(Portal::new(client, name, statement)) diff --git a/tokio-postgres/src/connect.rs b/tokio-postgres/src/connect.rs index ca57b9cdd..e697e5bc6 100644 --- a/tokio-postgres/src/connect.rs +++ b/tokio-postgres/src/connect.rs @@ -195,7 +195,7 @@ where } } Some(_) => {} - None => return Err(Error::unexpected_message()), + None => return Err(Error::closed()), } } } diff --git a/tokio-postgres/src/connect_raw.rs b/tokio-postgres/src/connect_raw.rs index 19be9eb01..4539cec3d 100644 --- a/tokio-postgres/src/connect_raw.rs +++ b/tokio-postgres/src/connect_raw.rs @@ -190,14 +190,14 @@ where )) } Some(Message::ErrorResponse(body)) => return Err(Error::db(body)), - Some(_) => return Err(Error::unexpected_message()), + Some(m) => return Err(Error::unexpected_message(m)), None => return Err(Error::closed()), } match stream.try_next().await.map_err(Error::io)? { Some(Message::AuthenticationOk) => Ok(()), Some(Message::ErrorResponse(body)) => Err(Error::db(body)), - Some(_) => Err(Error::unexpected_message()), + Some(m) => Err(Error::unexpected_message(m)), None => Err(Error::closed()), } } @@ -291,7 +291,7 @@ where let body = match stream.try_next().await.map_err(Error::io)? { Some(Message::AuthenticationSaslContinue(body)) => body, Some(Message::ErrorResponse(body)) => return Err(Error::db(body)), - Some(_) => return Err(Error::unexpected_message()), + Some(m) => return Err(Error::unexpected_message(m)), None => return Err(Error::closed()), }; @@ -309,7 +309,7 @@ where let body = match stream.try_next().await.map_err(Error::io)? { Some(Message::AuthenticationSaslFinal(body)) => body, Some(Message::ErrorResponse(body)) => return Err(Error::db(body)), - Some(_) => return Err(Error::unexpected_message()), + Some(m) => return Err(Error::unexpected_message(m)), None => return Err(Error::closed()), }; @@ -348,7 +348,7 @@ where } Some(Message::ReadyForQuery(_)) => return Ok((process_id, secret_key, parameters)), Some(Message::ErrorResponse(body)) => return Err(Error::db(body)), - Some(_) => return Err(Error::unexpected_message()), + Some(m) => return Err(Error::unexpected_message(m)), None => return Err(Error::closed()), } } diff --git a/tokio-postgres/src/connection.rs b/tokio-postgres/src/connection.rs index 414335955..652038cc0 100644 --- a/tokio-postgres/src/connection.rs +++ b/tokio-postgres/src/connection.rs @@ -139,7 +139,8 @@ where Some(response) => response, None => match messages.next().map_err(Error::parse)? { Some(Message::ErrorResponse(error)) => return Err(Error::db(error)), - _ => return Err(Error::unexpected_message()), + Some(m) => return Err(Error::unexpected_message(m)), + None => return Err(Error::closed()), }, }; diff --git a/tokio-postgres/src/copy_in.rs b/tokio-postgres/src/copy_in.rs index 59e31fea6..9bc3bd3af 100644 --- a/tokio-postgres/src/copy_in.rs +++ b/tokio-postgres/src/copy_in.rs @@ -114,7 +114,7 @@ where let rows = extract_row_affected(&body)?; return Poll::Ready(Ok(rows)); } - _ => return Poll::Ready(Err(Error::unexpected_message())), + m => return Poll::Ready(Err(Error::unexpected_message(m))), } } } @@ -205,14 +205,19 @@ where .await .map_err(|_| Error::closed())?; + match responses.next().await? { + Message::ParseComplete => {} + m => return Err(Error::unexpected_message(m)), + } + match responses.next().await? { Message::BindComplete => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } match responses.next().await? { Message::CopyInResponse(_) => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } Ok(CopyInSink { diff --git a/tokio-postgres/src/copy_out.rs b/tokio-postgres/src/copy_out.rs index 1e6949252..4141bee92 100644 --- a/tokio-postgres/src/copy_out.rs +++ b/tokio-postgres/src/copy_out.rs @@ -26,13 +26,17 @@ async fn start(client: &InnerClient, buf: Bytes) -> Result { let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; match responses.next().await? { + Message::ParseComplete => match responses.next().await? { + Message::BindComplete => {} + m => return Err(Error::unexpected_message(m)), + }, Message::BindComplete => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } match responses.next().await? { Message::CopyOutResponse(_) => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } Ok(responses) @@ -56,7 +60,7 @@ impl Stream for CopyOutStream { match ready!(this.responses.poll_next(cx)?) { Message::CopyData(body) => Poll::Ready(Some(Ok(body.into_bytes()))), Message::CopyDone => Poll::Ready(None), - _ => Poll::Ready(Some(Err(Error::unexpected_message()))), + m => Poll::Ready(Some(Err(Error::unexpected_message(m)))), } } } diff --git a/tokio-postgres/src/error/mod.rs b/tokio-postgres/src/error/mod.rs index f1e2644c6..764f77f9c 100644 --- a/tokio-postgres/src/error/mod.rs +++ b/tokio-postgres/src/error/mod.rs @@ -1,7 +1,7 @@ //! Errors. use fallible_iterator::FallibleIterator; -use postgres_protocol::message::backend::{ErrorFields, ErrorResponseBody}; +use postgres_protocol::message::backend::{ErrorFields, ErrorResponseBody, Message}; use std::error::{self, Error as _Error}; use std::fmt; use std::io; @@ -339,7 +339,7 @@ pub enum ErrorPosition { #[derive(Debug, PartialEq)] enum Kind { Io, - UnexpectedMessage, + UnexpectedMessage(Message), Tls, ToSql(usize), FromSql(usize), @@ -379,7 +379,9 @@ impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.0.kind { Kind::Io => fmt.write_str("error communicating with the server")?, - Kind::UnexpectedMessage => fmt.write_str("unexpected message from server")?, + Kind::UnexpectedMessage(msg) => { + write!(fmt, "unexpected message from server: {:?}", msg)? + } Kind::Tls => fmt.write_str("error performing TLS handshake")?, Kind::ToSql(idx) => write!(fmt, "error serializing parameter {}", idx)?, Kind::FromSql(idx) => write!(fmt, "error deserializing column {}", idx)?, @@ -445,8 +447,8 @@ impl Error { Error::new(Kind::Closed, None) } - pub(crate) fn unexpected_message() -> Error { - Error::new(Kind::UnexpectedMessage, None) + pub(crate) fn unexpected_message(message: Message) -> Error { + Error::new(Kind::UnexpectedMessage(message), None) } #[allow(clippy::needless_pass_by_value)] diff --git a/tokio-postgres/src/prepare.rs b/tokio-postgres/src/prepare.rs index e3f09a7c2..5f8b1b065 100644 --- a/tokio-postgres/src/prepare.rs +++ b/tokio-postgres/src/prepare.rs @@ -69,18 +69,18 @@ pub async fn prepare( match responses.next().await? { Message::ParseComplete => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } let parameter_description = match responses.next().await? { Message::ParameterDescription(body) => body, - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), }; let row_description = match responses.next().await? { Message::RowDescription(body) => Some(body), Message::NoData => None, - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), }; let mut parameters = vec![]; @@ -142,7 +142,7 @@ async fn get_type(client: &Arc, oid: Oid) -> Result { let row = match rows.try_next().await? { Some(row) => row, - None => return Err(Error::unexpected_message()), + None => return Err(Error::closed()), }; let name: String = row.try_get(0)?; diff --git a/tokio-postgres/src/query.rs b/tokio-postgres/src/query.rs index e6e1d00a8..f67795552 100644 --- a/tokio-postgres/src/query.rs +++ b/tokio-postgres/src/query.rs @@ -123,7 +123,7 @@ where } Message::EmptyQueryResponse => rows = 0, Message::ReadyForQuery(_) => return Ok(rows), - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } } } @@ -132,8 +132,12 @@ async fn start(client: &InnerClient, buf: Bytes) -> Result { let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; match responses.next().await? { + Message::ParseComplete => match responses.next().await? { + Message::BindComplete => {} + m => return Err(Error::unexpected_message(m)), + }, Message::BindComplete => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } Ok(responses) @@ -146,6 +150,9 @@ where I::IntoIter: ExactSizeIterator, { client.with_buf(|buf| { + if statement.name().is_empty() { + frontend::parse("", statement.query().unwrap(), [], buf).unwrap(); + } encode_bind(statement, params, "", buf)?; frontend::execute("", 0, buf).map_err(Error::encode)?; frontend::sync(buf); @@ -228,7 +235,7 @@ impl Stream for RowStream { } Message::EmptyQueryResponse | Message::PortalSuspended => {} Message::ReadyForQuery(_) => return Poll::Ready(None), - _ => return Poll::Ready(Some(Err(Error::unexpected_message()))), + m => return Poll::Ready(Some(Err(Error::unexpected_message(m)))), } } } diff --git a/tokio-postgres/src/simple_query.rs b/tokio-postgres/src/simple_query.rs index bcc6d928b..9838b0809 100644 --- a/tokio-postgres/src/simple_query.rs +++ b/tokio-postgres/src/simple_query.rs @@ -58,7 +58,7 @@ pub async fn batch_execute(client: &InnerClient, query: &str) -> Result<(), Erro | Message::EmptyQueryResponse | Message::RowDescription(_) | Message::DataRow(_) => {} - _ => return Err(Error::unexpected_message()), + m => return Err(Error::unexpected_message(m)), } } } @@ -107,12 +107,12 @@ impl Stream for SimpleQueryStream { Message::DataRow(body) => { let row = match &this.columns { Some(columns) => SimpleQueryRow::new(columns.clone(), body)?, - None => return Poll::Ready(Some(Err(Error::unexpected_message()))), + None => return Poll::Ready(Some(Err(Error::closed()))), }; return Poll::Ready(Some(Ok(SimpleQueryMessage::Row(row)))); } Message::ReadyForQuery(_) => return Poll::Ready(None), - _ => return Poll::Ready(Some(Err(Error::unexpected_message()))), + m => return Poll::Ready(Some(Err(Error::unexpected_message(m)))), } } } diff --git a/tokio-postgres/src/statement.rs b/tokio-postgres/src/statement.rs index 97561a8e4..3bc93c4f1 100644 --- a/tokio-postgres/src/statement.rs +++ b/tokio-postgres/src/statement.rs @@ -8,22 +8,31 @@ use std::{ sync::{Arc, Weak}, }; -struct StatementInner { - client: Weak, - name: String, - params: Vec, - columns: Vec, +enum StatementInner { + Unnamed { + query: String, + params: Vec, + columns: Vec, + }, + Named { + client: Weak, + name: String, + params: Vec, + columns: Vec, + }, } impl Drop for StatementInner { fn drop(&mut self) { - if let Some(client) = self.client.upgrade() { - let buf = client.with_buf(|buf| { - frontend::close(b'S', &self.name, buf).unwrap(); - frontend::sync(buf); - buf.split().freeze() - }); - let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf))); + if let StatementInner::Named { client, name, .. } = self { + if let Some(client) = client.upgrade() { + let buf = client.with_buf(|buf| { + frontend::close(b'S', name, buf).unwrap(); + frontend::sync(buf); + buf.split().freeze() + }); + let _ = client.send(RequestMessages::Single(FrontendMessage::Raw(buf))); + } } } } @@ -41,7 +50,7 @@ impl Statement { params: Vec, columns: Vec, ) -> Statement { - Statement(Arc::new(StatementInner { + Statement(Arc::new(StatementInner::Named { client: Arc::downgrade(inner), name, params, @@ -49,22 +58,47 @@ impl Statement { })) } + pub(crate) fn unnamed(prepared: Statement, query: String) -> Self { + Statement(Arc::new(StatementInner::Unnamed { + query, + params: prepared.params().to_owned(), + columns: prepared.columns().to_owned(), + })) + } + pub(crate) fn name(&self) -> &str { - &self.0.name + match &*self.0 { + StatementInner::Unnamed { .. } => "", + StatementInner::Named { name, .. } => name, + } + } + + pub(crate) fn query(&self) -> Option<&str> { + match &*self.0 { + StatementInner::Unnamed { query, .. } => Some(query), + StatementInner::Named { .. } => None, + } } /// Returns the expected types of the statement's parameters. pub fn params(&self) -> &[Type] { - &self.0.params + match &*self.0 { + StatementInner::Unnamed { params, .. } => params, + StatementInner::Named { params, .. } => params, + } } /// Returns information about the columns returned when the statement is queried. pub fn columns(&self) -> &[Column] { - &self.0.columns + match &*self.0 { + StatementInner::Unnamed { columns, .. } => columns, + StatementInner::Named { columns, .. } => columns, + } } } /// Information about a column of a query. +#[derive(Clone)] pub struct Column { name: String, type_: Type, diff --git a/tokio-postgres/src/to_statement.rs b/tokio-postgres/src/to_statement.rs index 427f77dd7..66d2e115d 100644 --- a/tokio-postgres/src/to_statement.rs +++ b/tokio-postgres/src/to_statement.rs @@ -15,7 +15,10 @@ mod private { pub async fn into_statement(self, client: &Client) -> Result { match self { ToStatementType::Statement(s) => Ok(s.clone()), - ToStatementType::Query(s) => client.prepare(s).await, + ToStatementType::Query(s) => { + let prepared = client.prepare(s).await?; + Ok(Statement::unnamed(prepared, s.to_owned())) + } } } } From 23ee0d1b3b67421e3cf0f83108a711798898c1a9 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 22 Sep 2023 17:08:33 -0700 Subject: [PATCH 2/3] clean up --- tokio-postgres/src/client.rs | 6 +++++- tokio-postgres/src/copy_in.rs | 11 ++++++----- tokio-postgres/src/prepare.rs | 15 ++++++++++++--- tokio-postgres/src/query.rs | 4 ++-- tokio-postgres/src/statement.rs | 9 ++++----- tokio-postgres/src/to_statement.rs | 5 +---- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/tokio-postgres/src/client.rs b/tokio-postgres/src/client.rs index 427a05049..3cc94dece 100644 --- a/tokio-postgres/src/client.rs +++ b/tokio-postgres/src/client.rs @@ -231,7 +231,11 @@ impl Client { query: &str, parameter_types: &[Type], ) -> Result { - prepare::prepare(&self.inner, query, parameter_types).await + prepare::prepare(&self.inner, query, parameter_types, false).await + } + + pub(crate) async fn prepare_unnamed(&self, query: &str) -> Result { + prepare::prepare(&self.inner, query, &[], true).await } /// Executes a statement, returning a vector of the resulting rows. diff --git a/tokio-postgres/src/copy_in.rs b/tokio-postgres/src/copy_in.rs index 9bc3bd3af..f997e9433 100644 --- a/tokio-postgres/src/copy_in.rs +++ b/tokio-postgres/src/copy_in.rs @@ -206,11 +206,12 @@ where .map_err(|_| Error::closed())?; match responses.next().await? { - Message::ParseComplete => {} - m => return Err(Error::unexpected_message(m)), - } - - match responses.next().await? { + Message::ParseComplete => { + match responses.next().await? { + Message::BindComplete => {} + m => return Err(Error::unexpected_message(m)), + } + } Message::BindComplete => {} m => return Err(Error::unexpected_message(m)), } diff --git a/tokio-postgres/src/prepare.rs b/tokio-postgres/src/prepare.rs index 5f8b1b065..99974c107 100644 --- a/tokio-postgres/src/prepare.rs +++ b/tokio-postgres/src/prepare.rs @@ -62,8 +62,13 @@ pub async fn prepare( client: &Arc, query: &str, types: &[Type], + unnamed: bool, ) -> Result { - let name = format!("s{}", NEXT_ID.fetch_add(1, Ordering::SeqCst)); + let name = if unnamed { + String::new() + } else { + format!("s{}", NEXT_ID.fetch_add(1, Ordering::SeqCst)) + }; let buf = encode(client, &name, query, types)?; let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; @@ -100,7 +105,11 @@ pub async fn prepare( } } - Ok(Statement::new(client, name, parameters, columns)) + if unnamed { + Ok(Statement::unnamed(query.to_owned(), parameters, columns)) + } else { + Ok(Statement::named(client, name, parameters, columns)) + } } fn prepare_rec<'a>( @@ -108,7 +117,7 @@ fn prepare_rec<'a>( query: &'a str, types: &'a [Type], ) -> Pin> + 'a + Send>> { - Box::pin(prepare(client, query, types)) + Box::pin(prepare(client, query, types, false)) } fn encode(client: &InnerClient, name: &str, query: &str, types: &[Type]) -> Result { diff --git a/tokio-postgres/src/query.rs b/tokio-postgres/src/query.rs index f67795552..c590f7def 100644 --- a/tokio-postgres/src/query.rs +++ b/tokio-postgres/src/query.rs @@ -150,8 +150,8 @@ where I::IntoIter: ExactSizeIterator, { client.with_buf(|buf| { - if statement.name().is_empty() { - frontend::parse("", statement.query().unwrap(), [], buf).unwrap(); + if let Some(query) = statement.query() { + frontend::parse("", query, [], buf).unwrap(); } encode_bind(statement, params, "", buf)?; frontend::execute("", 0, buf).map_err(Error::encode)?; diff --git a/tokio-postgres/src/statement.rs b/tokio-postgres/src/statement.rs index 3bc93c4f1..bcee6f053 100644 --- a/tokio-postgres/src/statement.rs +++ b/tokio-postgres/src/statement.rs @@ -44,7 +44,7 @@ impl Drop for StatementInner { pub struct Statement(Arc); impl Statement { - pub(crate) fn new( + pub(crate) fn named( inner: &Arc, name: String, params: Vec, @@ -58,11 +58,11 @@ impl Statement { })) } - pub(crate) fn unnamed(prepared: Statement, query: String) -> Self { + pub(crate) fn unnamed(query: String, params: Vec, columns: Vec) -> Self { Statement(Arc::new(StatementInner::Unnamed { query, - params: prepared.params().to_owned(), - columns: prepared.columns().to_owned(), + params, + columns, })) } @@ -98,7 +98,6 @@ impl Statement { } /// Information about a column of a query. -#[derive(Clone)] pub struct Column { name: String, type_: Type, diff --git a/tokio-postgres/src/to_statement.rs b/tokio-postgres/src/to_statement.rs index 66d2e115d..ef1e65272 100644 --- a/tokio-postgres/src/to_statement.rs +++ b/tokio-postgres/src/to_statement.rs @@ -15,10 +15,7 @@ mod private { pub async fn into_statement(self, client: &Client) -> Result { match self { ToStatementType::Statement(s) => Ok(s.clone()), - ToStatementType::Query(s) => { - let prepared = client.prepare(s).await?; - Ok(Statement::unnamed(prepared, s.to_owned())) - } + ToStatementType::Query(s) => client.prepare_unnamed(s).await, } } } From ad0169dec1b6781e8a15c85e13fd9e1747223965 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 22 Sep 2023 18:43:21 -0700 Subject: [PATCH 3/3] x --- tokio-postgres/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index ec5e3cbec..c11de2e2b 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -59,7 +59,7 @@ postgres-types = { version = "0.2.5", path = "../postgres-types" } tokio = { version = "1.27", features = ["io-util"] } tokio-util = { version = "0.7", features = ["codec"] } rand = "0.8.5" -whoami = "1.4.1" +whoami = "1.4" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] socket2 = { version = "0.5", features = ["all"] }