From d9979055b95038ae61756e69ed8d97ea4d5e24a2 Mon Sep 17 00:00:00 2001 From: Artem Vasiliev Date: Tue, 13 May 2025 15:29:20 +0300 Subject: [PATCH 1/4] Added prepared_static for &GenericClient --- postgres_query/src/client.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/postgres_query/src/client.rs b/postgres_query/src/client.rs index 28ffd5f..7ac892b 100644 --- a/postgres_query/src/client.rs +++ b/postgres_query/src/client.rs @@ -145,6 +145,10 @@ macro_rules! client_deref_impl { T::prepare(self, sql).await } + async fn prepare_static(&self, sql: &'static str) -> Result { + T::prepare_static(self, sql).await + } + async fn execute_raw<'a>( &'a self, statement: &Statement, From 283c7adfc294c05a612bd3feb3899743f45b8dad Mon Sep 17 00:00:00 2001 From: Artem Vasiliev Date: Thu, 15 May 2025 19:22:31 +0300 Subject: [PATCH 2/4] Implement prepare_static for all overloads --- postgres_query/src/client.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/postgres_query/src/client.rs b/postgres_query/src/client.rs index 7ac892b..e37821f 100644 --- a/postgres_query/src/client.rs +++ b/postgres_query/src/client.rs @@ -68,6 +68,11 @@ impl GenericClient for Client { Client::prepare(self, sql).await } + #[deny(unconditional_recursion)] + async fn prepare_static(&self, sql: &'static str) -> Result { + Client::prepare_static(self, sql).await + } + #[deny(unconditional_recursion)] async fn execute_raw<'a>( &'a self, @@ -95,6 +100,11 @@ impl GenericClient for DpClient { DpClientWrapper::prepare(self, sql).await } + #[deny(unconditional_recursion)] + async fn prepare_static(&self, sql: &'static str) -> Result { + DpClientWrapper::prepare_static(self, sql).await + } + #[deny(unconditional_recursion)] async fn execute_raw<'a>( &'a self, @@ -120,6 +130,10 @@ impl GenericClient for Transaction<'_> { Transaction::prepare(self, sql).await } + async fn prepare_static(&self, sql: &'static str) -> Result { + Transaction::prepare_static(self, sql).await + } + async fn execute_raw<'a>( &'a self, statement: &Statement, From 2c41cf9be634c9fccbd5e35e57d6f83d754e5ebb Mon Sep 17 00:00:00 2001 From: Artem Vasiliev Date: Thu, 15 May 2025 19:32:39 +0300 Subject: [PATCH 3/4] revert change --- postgres_query/src/client.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/postgres_query/src/client.rs b/postgres_query/src/client.rs index e37821f..7ac892b 100644 --- a/postgres_query/src/client.rs +++ b/postgres_query/src/client.rs @@ -68,11 +68,6 @@ impl GenericClient for Client { Client::prepare(self, sql).await } - #[deny(unconditional_recursion)] - async fn prepare_static(&self, sql: &'static str) -> Result { - Client::prepare_static(self, sql).await - } - #[deny(unconditional_recursion)] async fn execute_raw<'a>( &'a self, @@ -100,11 +95,6 @@ impl GenericClient for DpClient { DpClientWrapper::prepare(self, sql).await } - #[deny(unconditional_recursion)] - async fn prepare_static(&self, sql: &'static str) -> Result { - DpClientWrapper::prepare_static(self, sql).await - } - #[deny(unconditional_recursion)] async fn execute_raw<'a>( &'a self, @@ -130,10 +120,6 @@ impl GenericClient for Transaction<'_> { Transaction::prepare(self, sql).await } - async fn prepare_static(&self, sql: &'static str) -> Result { - Transaction::prepare_static(self, sql).await - } - async fn execute_raw<'a>( &'a self, statement: &Statement, From d54a8fadec0f0697167aff5ea26038f56353a65c Mon Sep 17 00:00:00 2001 From: Artem Vasiliev Date: Thu, 15 May 2025 19:55:35 +0300 Subject: [PATCH 4/4] Reuse cache --- postgres_query/src/client/cache.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/postgres_query/src/client/cache.rs b/postgres_query/src/client/cache.rs index 8aae80c..448b750 100644 --- a/postgres_query/src/client/cache.rs +++ b/postgres_query/src/client/cache.rs @@ -65,6 +65,14 @@ where } } + /// Wrap new client with an existing cache. + pub fn map_clone(&self, client: U) -> Caching { + Caching { + client, + cache: self.cache.clone(), + } + } + /// Return the inner client. pub fn into_inner(self) -> C { self.client @@ -213,10 +221,11 @@ macro_rules! impl_cached_transaction { impl Caching<$client> { /// Start a new transaction that shares the same cache as the current client. pub async fn transaction(&mut self) -> Result, Error> { - <$client>::transaction(self) + let cache = self.cache.clone(); + let tx = <$client>::transaction(self) .await - .map(Caching::new) - .map_err(Error::BeginTransaction) + .map_err(Error::BeginTransaction)?; + Ok(Caching { client: tx, cache }) } } };