diff --git a/sqlx-core/src/caching_connection.rs b/sqlx-core/src/caching_connection.rs deleted file mode 100644 index 88482514b5..0000000000 --- a/sqlx-core/src/caching_connection.rs +++ /dev/null @@ -1,13 +0,0 @@ -use futures_core::future::BoxFuture; - -use crate::error::Error; - -/// A connection that is capable of caching prepared statements. -pub trait CachingConnection: Send { - /// The number of statements currently cached in the connection. - fn cached_statements_count(&self) -> usize; - - /// Removes all statements from the cache, closing them on the server if - /// needed. - fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>; -} diff --git a/sqlx-core/src/common/statement_cache.rs b/sqlx-core/src/common/statement_cache.rs index 2740bde771..6ad7c5b7f8 100644 --- a/sqlx-core/src/common/statement_cache.rs +++ b/sqlx-core/src/common/statement_cache.rs @@ -49,6 +49,7 @@ impl StatementCache { } /// Clear all cached statements from the cache. + #[cfg(any(feature = "postgres", feature = "sqlite"))] pub fn clear(&mut self) { self.inner.clear(); } diff --git a/sqlx-core/src/connection.rs b/sqlx-core/src/connection.rs index e6eed54fbf..ae1a0a3b4a 100644 --- a/sqlx-core/src/connection.rs +++ b/sqlx-core/src/connection.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use futures_core::future::BoxFuture; use futures_core::Future; -use crate::database::Database; +use crate::database::{Database, HasStatementCache}; use crate::error::{BoxDynError, Error}; use crate::transaction::Transaction; @@ -64,6 +64,23 @@ pub trait Connection: Send { }) } + /// The number of statements currently cached in the connection. + fn cached_statements_size(&self) -> usize + where + Self::Database: HasStatementCache, + { + 0 + } + + /// Removes all statements from the cache, closing them on the server if + /// needed. + fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> + where + Self::Database: HasStatementCache, + { + Box::pin(async move { Ok(()) }) + } + #[doc(hidden)] fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>; diff --git a/sqlx-core/src/database.rs b/sqlx-core/src/database.rs index fc400d4b9d..388c45baac 100644 --- a/sqlx-core/src/database.rs +++ b/sqlx-core/src/database.rs @@ -74,3 +74,5 @@ pub trait HasArguments<'q> { /// The concrete type used as a buffer for arguments while encoding. type ArgumentBuffer: Default; } + +pub trait HasStatementCache {} diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index 3785c2bde2..118b42c8d9 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -29,7 +29,6 @@ pub mod arguments; #[macro_use] pub mod pool; -pub mod caching_connection; pub mod connection; #[macro_use] diff --git a/sqlx-core/src/mysql/connection/mod.rs b/sqlx-core/src/mysql/connection/mod.rs index 028c1a9008..0e8ef38731 100644 --- a/sqlx-core/src/mysql/connection/mod.rs +++ b/sqlx-core/src/mysql/connection/mod.rs @@ -6,7 +6,6 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use hashbrown::HashMap; -use crate::caching_connection::CachingConnection; use crate::common::StatementCache; use crate::connection::{Connect, Connection}; use crate::error::Error; @@ -46,22 +45,6 @@ pub struct MySqlConnection { scratch_row_column_names: Arc>, } -impl CachingConnection for MySqlConnection { - fn cached_statements_count(&self) -> usize { - self.cache_statement.len() - } - - fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { - Box::pin(async move { - while let Some(statement) = self.cache_statement.remove_lru() { - self.stream.send_packet(StmtClose { statement }).await?; - } - - Ok(()) - }) - } -} - impl Debug for MySqlConnection { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("MySqlConnection").finish() @@ -94,6 +77,20 @@ impl Connection for MySqlConnection { self.stream.wait_until_ready().boxed() } + fn cached_statements_size(&self) -> usize { + self.cache_statement.len() + } + + fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { + Box::pin(async move { + while let Some(statement) = self.cache_statement.remove_lru() { + self.stream.send_packet(StmtClose { statement }).await?; + } + + Ok(()) + }) + } + #[doc(hidden)] fn should_flush(&self) -> bool { !self.stream.wbuf.is_empty() diff --git a/sqlx-core/src/mysql/database.rs b/sqlx-core/src/mysql/database.rs index ef97686578..6bb8083202 100644 --- a/sqlx-core/src/mysql/database.rs +++ b/sqlx-core/src/mysql/database.rs @@ -1,4 +1,4 @@ -use crate::database::{Database, HasArguments, HasValueRef}; +use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef}; use crate::mysql::value::{MySqlValue, MySqlValueRef}; use crate::mysql::{ MySqlArguments, MySqlConnection, MySqlRow, MySqlTransactionManager, MySqlTypeInfo, @@ -33,3 +33,5 @@ impl HasArguments<'_> for MySql { type ArgumentBuffer = Vec; } + +impl HasStatementCache for MySql {} diff --git a/sqlx-core/src/postgres/connection/mod.rs b/sqlx-core/src/postgres/connection/mod.rs index ef1e437bc9..1821e279c0 100644 --- a/sqlx-core/src/postgres/connection/mod.rs +++ b/sqlx-core/src/postgres/connection/mod.rs @@ -5,7 +5,6 @@ use futures_core::future::BoxFuture; use futures_util::{FutureExt, TryFutureExt}; use hashbrown::HashMap; -use crate::caching_connection::CachingConnection; use crate::common::StatementCache; use crate::connection::{Connect, Connection}; use crate::error::Error; @@ -98,19 +97,6 @@ impl Debug for PgConnection { } } -impl CachingConnection for PgConnection { - fn cached_statements_count(&self) -> usize { - self.cache_statement.len() - } - - fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { - Box::pin(async move { - self.cache_statement.clear(); - Ok(()) - }) - } -} - impl Connection for PgConnection { type Database = Postgres; @@ -134,6 +120,17 @@ impl Connection for PgConnection { self.execute("/* SQLx ping */").map_ok(|_| ()).boxed() } + fn cached_statements_size(&self) -> usize { + self.cache_statement.len() + } + + fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { + Box::pin(async move { + self.cache_statement.clear(); + Ok(()) + }) + } + #[doc(hidden)] fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> { self.wait_until_ready().boxed() diff --git a/sqlx-core/src/postgres/database.rs b/sqlx-core/src/postgres/database.rs index 8b1614120e..8b3af756e6 100644 --- a/sqlx-core/src/postgres/database.rs +++ b/sqlx-core/src/postgres/database.rs @@ -1,4 +1,4 @@ -use crate::database::{Database, HasArguments, HasValueRef}; +use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef}; use crate::postgres::arguments::PgArgumentBuffer; use crate::postgres::value::{PgValue, PgValueRef}; use crate::postgres::{PgArguments, PgConnection, PgRow, PgTransactionManager, PgTypeInfo}; @@ -32,3 +32,5 @@ impl HasArguments<'_> for Postgres { type ArgumentBuffer = PgArgumentBuffer; } + +impl HasStatementCache for Postgres {} diff --git a/sqlx-core/src/sqlite/connection/mod.rs b/sqlx-core/src/sqlite/connection/mod.rs index 69042d8303..d768f14332 100644 --- a/sqlx-core/src/sqlite/connection/mod.rs +++ b/sqlx-core/src/sqlite/connection/mod.rs @@ -6,7 +6,6 @@ use futures_util::future; use hashbrown::HashMap; use libsqlite3_sys::sqlite3; -use crate::caching_connection::CachingConnection; use crate::common::StatementCache; use crate::connection::{Connect, Connection}; use crate::error::Error; @@ -49,19 +48,6 @@ impl Debug for SqliteConnection { } } -impl CachingConnection for SqliteConnection { - fn cached_statements_count(&self) -> usize { - self.statements.len() - } - - fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { - Box::pin(async move { - self.statements.clear(); - Ok(()) - }) - } -} - impl Connection for SqliteConnection { type Database = Sqlite; @@ -75,6 +61,17 @@ impl Connection for SqliteConnection { Box::pin(future::ok(())) } + fn cached_statements_size(&self) -> usize { + self.statements.len() + } + + fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> { + Box::pin(async move { + self.statements.clear(); + Ok(()) + }) + } + #[doc(hidden)] fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> { // For SQLite, FLUSH does effectively nothing diff --git a/sqlx-core/src/sqlite/database.rs b/sqlx-core/src/sqlite/database.rs index 3d8ea27520..fa00660fbe 100644 --- a/sqlx-core/src/sqlite/database.rs +++ b/sqlx-core/src/sqlite/database.rs @@ -1,4 +1,4 @@ -use crate::database::{Database, HasArguments, HasValueRef}; +use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef}; use crate::sqlite::{ SqliteArgumentValue, SqliteArguments, SqliteConnection, SqliteRow, SqliteTransactionManager, SqliteTypeInfo, SqliteValue, SqliteValueRef, @@ -33,3 +33,5 @@ impl<'q> HasArguments<'q> for Sqlite { type ArgumentBuffer = Vec>; } + +impl HasStatementCache for Sqlite {} diff --git a/src/lib.rs b/src/lib.rs index f05cc6dcf8..a11f3ce347 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(docsrs, feature(doc_cfg))] pub use sqlx_core::arguments::{Arguments, IntoArguments}; -pub use sqlx_core::caching_connection::CachingConnection; pub use sqlx_core::connection::{Connect, Connection}; pub use sqlx_core::database::{self, Database}; pub use sqlx_core::executor::{Execute, Executor}; diff --git a/tests/mysql/mysql.rs b/tests/mysql/mysql.rs index 95a1815b3f..becede928d 100644 --- a/tests/mysql/mysql.rs +++ b/tests/mysql/mysql.rs @@ -1,6 +1,6 @@ use futures::TryStreamExt; use sqlx::mysql::{MySql, MySqlPool, MySqlRow}; -use sqlx::{CachingConnection, Connection, Executor, Row}; +use sqlx::{Connection, Executor, Row}; use sqlx_test::new; #[sqlx_macros::test] @@ -193,9 +193,9 @@ async fn it_caches_statements() -> anyhow::Result<()> { assert_eq!(i, val); } - assert_eq!(1, conn.cached_statements_count()); + assert_eq!(1, conn.cached_statements_size()); conn.clear_cached_statements().await?; - assert_eq!(0, conn.cached_statements_count()); + assert_eq!(0, conn.cached_statements_size()); Ok(()) } diff --git a/tests/postgres/postgres.rs b/tests/postgres/postgres.rs index b7a40cf45c..e582b671bf 100644 --- a/tests/postgres/postgres.rs +++ b/tests/postgres/postgres.rs @@ -1,7 +1,6 @@ use futures::TryStreamExt; use sqlx::postgres::PgRow; use sqlx::postgres::{PgDatabaseError, PgErrorPosition, PgSeverity}; -use sqlx::CachingConnection; use sqlx::{postgres::Postgres, Connection, Executor, PgPool, Row}; use sqlx_test::new; use std::time::Duration; @@ -504,9 +503,9 @@ async fn it_caches_statements() -> anyhow::Result<()> { assert_eq!(i, val); } - assert_eq!(1, conn.cached_statements_count()); + assert_eq!(1, conn.cached_statements_size()); conn.clear_cached_statements().await?; - assert_eq!(0, conn.cached_statements_count()); + assert_eq!(0, conn.cached_statements_size()); Ok(()) } diff --git a/tests/sqlite/sqlite.rs b/tests/sqlite/sqlite.rs index 6935f04ff7..fd099da60a 100644 --- a/tests/sqlite/sqlite.rs +++ b/tests/sqlite/sqlite.rs @@ -1,6 +1,6 @@ use futures::TryStreamExt; use sqlx::{ - query, sqlite::Sqlite, CachingConnection, Connect, Connection, Executor, Row, SqliteConnection, + query, sqlite::Sqlite, Connect, Connection, Executor, Row, SqliteConnection, SqlitePool, }; use sqlx_test::new; @@ -286,9 +286,9 @@ async fn it_caches_statements() -> anyhow::Result<()> { assert_eq!(i, val); } - assert_eq!(1, conn.cached_statements_count()); + assert_eq!(1, conn.cached_statements_size()); conn.clear_cached_statements().await?; - assert_eq!(0, conn.cached_statements_count()); + assert_eq!(0, conn.cached_statements_size()); Ok(()) }