From 0267fe0482f43cffc6bdaf9311d0810a9e2ae27b Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 9 Apr 2021 16:51:20 -0700 Subject: [PATCH] refactor(postgres): review connection executor and tweak raw_query --- sqlx-postgres/src/connection/executor.rs | 8 +-- .../src/connection/executor/fetch_all.rs | 7 +-- .../src/connection/executor/fetch_optional.rs | 5 +- .../src/connection/executor/raw_prepare.rs | 5 +- .../src/connection/executor/raw_query.rs | 59 +++++++++++-------- sqlx-postgres/src/error/client.rs | 6 +- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/sqlx-postgres/src/connection/executor.rs b/sqlx-postgres/src/connection/executor.rs index 229f58f86..290e7d484 100644 --- a/sqlx-postgres/src/connection/executor.rs +++ b/sqlx-postgres/src/connection/executor.rs @@ -1,5 +1,5 @@ #[cfg(feature = "async")] -use futures_util::future::BoxFuture; +use futures_util::future::{BoxFuture, FutureExt}; use sqlx_core::{Execute, Executor, Result, Runtime}; use crate::protocol::backend::ReadyForQuery; @@ -37,7 +37,7 @@ impl Executor for PgConnection { 'q: 'x, 'v: 'x, { - Box::pin(self.execute_async(query)) + self.execute_async(query).boxed() } #[cfg(feature = "async")] @@ -50,7 +50,7 @@ impl Executor for PgConnection { 'q: 'x, 'v: 'x, { - Box::pin(self.fetch_all_async(query)) + self.fetch_all_async(query).boxed() } #[cfg(feature = "async")] @@ -66,7 +66,7 @@ impl Executor for PgConnection { 'q: 'x, 'v: 'x, { - Box::pin(self.fetch_optional_async(query)) + self.fetch_optional_async(query).boxed() } } diff --git a/sqlx-postgres/src/connection/executor/fetch_all.rs b/sqlx-postgres/src/connection/executor/fetch_all.rs index b2b494028..332c0a9d9 100644 --- a/sqlx-postgres/src/connection/executor/fetch_all.rs +++ b/sqlx-postgres/src/connection/executor/fetch_all.rs @@ -16,16 +16,15 @@ impl PgConnection { BackendMessageType::BindComplete => {} BackendMessageType::DataRow => { - rows.push(PgRow::new(message.deserialize()?, &columns)); + rows.push(PgRow::new(message.deserialize()?, columns)); } BackendMessageType::RowDescription => { *columns = Some(message.deserialize::()?.columns.into()); } - BackendMessageType::CommandComplete => { - // one statement has finished - } + // one statement has finished + BackendMessageType::CommandComplete => {} BackendMessageType::ReadyForQuery => { self.handle_ready_for_query(message.deserialize()?); diff --git a/sqlx-postgres/src/connection/executor/fetch_optional.rs b/sqlx-postgres/src/connection/executor/fetch_optional.rs index 9d2a33bed..ac5d4022c 100644 --- a/sqlx-postgres/src/connection/executor/fetch_optional.rs +++ b/sqlx-postgres/src/connection/executor/fetch_optional.rs @@ -28,9 +28,8 @@ impl PgConnection { *columns = Some(message.deserialize::()?.columns.into()); } - BackendMessageType::CommandComplete => { - // one statement has finished - } + // one statement has finished + BackendMessageType::CommandComplete => {} BackendMessageType::ReadyForQuery => { self.handle_ready_for_query(message.deserialize()?); diff --git a/sqlx-postgres/src/connection/executor/raw_prepare.rs b/sqlx-postgres/src/connection/executor/raw_prepare.rs index 5edee4831..903dde43f 100644 --- a/sqlx-postgres/src/connection/executor/raw_prepare.rs +++ b/sqlx-postgres/src/connection/executor/raw_prepare.rs @@ -41,9 +41,8 @@ impl PgConnection { statement: &mut RawStatement, ) -> Result { match message.ty { - BackendMessageType::ParseComplete => { - // next message should be - } + // next message should be + BackendMessageType::ParseComplete => {} BackendMessageType::ReadyForQuery => { self.handle_ready_for_query(message.deserialize()?); diff --git a/sqlx-postgres/src/connection/executor/raw_query.rs b/sqlx-postgres/src/connection/executor/raw_query.rs index a0e680bb2..2ea05dee7 100644 --- a/sqlx-postgres/src/connection/executor/raw_query.rs +++ b/sqlx-postgres/src/connection/executor/raw_query.rs @@ -1,38 +1,47 @@ use sqlx_core::{Execute, Result, Runtime}; use crate::protocol::frontend::{self, Bind, PortalRef, Query, StatementRef, Sync}; -use crate::{PgConnection, Postgres}; +use crate::raw_statement::RawStatement; +use crate::{PgArguments, PgConnection, Postgres}; + +impl PgConnection { + fn write_raw_query_statement( + &mut self, + statement: &RawStatement, + arguments: &PgArguments<'_>, + ) -> Result<()> { + // bind values to the prepared statement + self.stream.write_message(&Bind { + portal: PortalRef::Unnamed, + statement: StatementRef::Named(statement.id), + arguments, + parameters: &statement.parameters, + })?; + + // describe the bound prepared statement (portal) + self.stream.write_message(&frontend::Describe { + target: frontend::Target::Portal(PortalRef::Unnamed), + })?; + + // execute the bound prepared statement (portal) + self.stream + .write_message(&frontend::Execute { portal: PortalRef::Unnamed, max_rows: 0 })?; + + // is what closes the extended query invocation and + // issues a + self.stream.write_message(&Sync)?; + + Ok(()) + } +} macro_rules! impl_raw_query { ($(@$blocking:ident)? $self:ident, $query:ident) => {{ if let Some(arguments) = $query.arguments() { - // prepare the statement for execution let statement = raw_prepare!($(@$blocking)? $self, $query.sql(), arguments); - // bind values to the prepared statement - $self.stream.write_message(&Bind { - portal: PortalRef::Unnamed, - statement: StatementRef::Named(statement.id), - arguments, - parameters: &statement.parameters, - })?; - - // describe the bound prepared statement (portal) - $self.stream.write_message(&frontend::Describe { - target: frontend::Target::Portal(PortalRef::Unnamed), - })?; - - // execute the bound prepared statement (portal) - $self.stream.write_message(&frontend::Execute { - portal: PortalRef::Unnamed, - max_rows: 0, - })?; - - // is what closes the extended query invocation and - // issues a - $self.stream.write_message(&Sync)?; + $self.write_raw_query_statement(&statement, arguments)?; } else { - // directly execute the query as an unprepared, simple query $self.stream.write_message(&Query { sql: $query.sql() })?; }; diff --git a/sqlx-postgres/src/error/client.rs b/sqlx-postgres/src/error/client.rs index 845057c8a..86ec838e0 100644 --- a/sqlx-postgres/src/error/client.rs +++ b/sqlx-postgres/src/error/client.rs @@ -20,7 +20,7 @@ pub enum PgClientError { impl Display for PgClientError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { - Self::NotUtf8(source) => write!(f, "{}", source), + Self::NotUtf8(source) => write!(f, "unexpected invalid utf-8: {}", source), Self::UnknownAuthenticationMethod(method) => { write!(f, "unknown authentication method: {}", method) @@ -50,7 +50,7 @@ impl StdError for PgClientError {} impl ClientError for PgClientError {} impl From for Error { - fn from(err: PgClientError) -> Error { - Error::client(err) + fn from(err: PgClientError) -> Self { + Self::client(err) } }