Skip to content

Commit aebe8ac

Browse files
author
Julius de Bruijn
committed
Caching methods in Connection
1 parent eba82e3 commit aebe8ac

File tree

15 files changed

+74
-73
lines changed

15 files changed

+74
-73
lines changed

sqlx-core/src/caching_connection.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

sqlx-core/src/common/statement_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl<T> StatementCache<T> {
4949
}
5050

5151
/// Clear all cached statements from the cache.
52+
#[cfg(any(feature = "postgres", feature = "sqlite"))]
5253
pub fn clear(&mut self) {
5354
self.inner.clear();
5455
}

sqlx-core/src/connection.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::str::FromStr;
33
use futures_core::future::BoxFuture;
44
use futures_core::Future;
55

6-
use crate::database::Database;
6+
use crate::database::{Database, HasStatementCache};
77
use crate::error::{BoxDynError, Error};
88
use crate::transaction::Transaction;
99

@@ -64,6 +64,23 @@ pub trait Connection: Send {
6464
})
6565
}
6666

67+
/// The number of statements currently cached in the connection.
68+
fn cached_statements_size(&self) -> usize
69+
where
70+
Self::Database: HasStatementCache,
71+
{
72+
0
73+
}
74+
75+
/// Removes all statements from the cache, closing them on the server if
76+
/// needed.
77+
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
78+
where
79+
Self::Database: HasStatementCache,
80+
{
81+
Box::pin(async move { Ok(()) })
82+
}
83+
6784
#[doc(hidden)]
6885
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
6986

sqlx-core/src/database.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ pub trait HasArguments<'q> {
7474
/// The concrete type used as a buffer for arguments while encoding.
7575
type ArgumentBuffer: Default;
7676
}
77+
78+
pub trait HasStatementCache {}

sqlx-core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub mod arguments;
2929
#[macro_use]
3030
pub mod pool;
3131

32-
pub mod caching_connection;
3332
pub mod connection;
3433

3534
#[macro_use]

sqlx-core/src/mysql/connection/mod.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use futures_core::future::BoxFuture;
66
use futures_util::FutureExt;
77
use hashbrown::HashMap;
88

9-
use crate::caching_connection::CachingConnection;
109
use crate::common::StatementCache;
1110
use crate::connection::{Connect, Connection};
1211
use crate::error::Error;
@@ -46,22 +45,6 @@ pub struct MySqlConnection {
4645
scratch_row_column_names: Arc<HashMap<UStr, usize>>,
4746
}
4847

49-
impl CachingConnection for MySqlConnection {
50-
fn cached_statements_count(&self) -> usize {
51-
self.cache_statement.len()
52-
}
53-
54-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
55-
Box::pin(async move {
56-
while let Some(statement) = self.cache_statement.remove_lru() {
57-
self.stream.send_packet(StmtClose { statement }).await?;
58-
}
59-
60-
Ok(())
61-
})
62-
}
63-
}
64-
6548
impl Debug for MySqlConnection {
6649
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
6750
f.debug_struct("MySqlConnection").finish()
@@ -94,6 +77,20 @@ impl Connection for MySqlConnection {
9477
self.stream.wait_until_ready().boxed()
9578
}
9679

80+
fn cached_statements_size(&self) -> usize {
81+
self.cache_statement.len()
82+
}
83+
84+
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
85+
Box::pin(async move {
86+
while let Some(statement) = self.cache_statement.remove_lru() {
87+
self.stream.send_packet(StmtClose { statement }).await?;
88+
}
89+
90+
Ok(())
91+
})
92+
}
93+
9794
#[doc(hidden)]
9895
fn should_flush(&self) -> bool {
9996
!self.stream.wbuf.is_empty()

sqlx-core/src/mysql/database.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database::{Database, HasArguments, HasValueRef};
1+
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
22
use crate::mysql::value::{MySqlValue, MySqlValueRef};
33
use crate::mysql::{
44
MySqlArguments, MySqlConnection, MySqlRow, MySqlTransactionManager, MySqlTypeInfo,
@@ -33,3 +33,5 @@ impl HasArguments<'_> for MySql {
3333

3434
type ArgumentBuffer = Vec<u8>;
3535
}
36+
37+
impl HasStatementCache for MySql {}

sqlx-core/src/postgres/connection/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use futures_core::future::BoxFuture;
55
use futures_util::{FutureExt, TryFutureExt};
66
use hashbrown::HashMap;
77

8-
use crate::caching_connection::CachingConnection;
98
use crate::common::StatementCache;
109
use crate::connection::{Connect, Connection};
1110
use crate::error::Error;
@@ -98,19 +97,6 @@ impl Debug for PgConnection {
9897
}
9998
}
10099

101-
impl CachingConnection for PgConnection {
102-
fn cached_statements_count(&self) -> usize {
103-
self.cache_statement.len()
104-
}
105-
106-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
107-
Box::pin(async move {
108-
self.cache_statement.clear();
109-
Ok(())
110-
})
111-
}
112-
}
113-
114100
impl Connection for PgConnection {
115101
type Database = Postgres;
116102

@@ -134,6 +120,17 @@ impl Connection for PgConnection {
134120
self.execute("/* SQLx ping */").map_ok(|_| ()).boxed()
135121
}
136122

123+
fn cached_statements_size(&self) -> usize {
124+
self.cache_statement.len()
125+
}
126+
127+
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
128+
Box::pin(async move {
129+
self.cache_statement.clear();
130+
Ok(())
131+
})
132+
}
133+
137134
#[doc(hidden)]
138135
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
139136
self.wait_until_ready().boxed()

sqlx-core/src/postgres/database.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database::{Database, HasArguments, HasValueRef};
1+
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
22
use crate::postgres::arguments::PgArgumentBuffer;
33
use crate::postgres::value::{PgValue, PgValueRef};
44
use crate::postgres::{PgArguments, PgConnection, PgRow, PgTransactionManager, PgTypeInfo};
@@ -32,3 +32,5 @@ impl HasArguments<'_> for Postgres {
3232

3333
type ArgumentBuffer = PgArgumentBuffer;
3434
}
35+
36+
impl HasStatementCache for Postgres {}

sqlx-core/src/sqlite/connection/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use futures_util::future;
66
use hashbrown::HashMap;
77
use libsqlite3_sys::sqlite3;
88

9-
use crate::caching_connection::CachingConnection;
109
use crate::common::StatementCache;
1110
use crate::connection::{Connect, Connection};
1211
use crate::error::Error;
@@ -49,19 +48,6 @@ impl Debug for SqliteConnection {
4948
}
5049
}
5150

52-
impl CachingConnection for SqliteConnection {
53-
fn cached_statements_count(&self) -> usize {
54-
self.statements.len()
55-
}
56-
57-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
58-
Box::pin(async move {
59-
self.statements.clear();
60-
Ok(())
61-
})
62-
}
63-
}
64-
6551
impl Connection for SqliteConnection {
6652
type Database = Sqlite;
6753

@@ -75,6 +61,17 @@ impl Connection for SqliteConnection {
7561
Box::pin(future::ok(()))
7662
}
7763

64+
fn cached_statements_size(&self) -> usize {
65+
self.statements.len()
66+
}
67+
68+
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
69+
Box::pin(async move {
70+
self.statements.clear();
71+
Ok(())
72+
})
73+
}
74+
7875
#[doc(hidden)]
7976
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
8077
// For SQLite, FLUSH does effectively nothing

sqlx-core/src/sqlite/database.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database::{Database, HasArguments, HasValueRef};
1+
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
22
use crate::sqlite::{
33
SqliteArgumentValue, SqliteArguments, SqliteConnection, SqliteRow, SqliteTransactionManager,
44
SqliteTypeInfo, SqliteValue, SqliteValueRef,
@@ -33,3 +33,5 @@ impl<'q> HasArguments<'q> for Sqlite {
3333

3434
type ArgumentBuffer = Vec<SqliteArgumentValue<'q>>;
3535
}
36+
37+
impl HasStatementCache for Sqlite {}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg_attr(docsrs, feature(doc_cfg))]
22

33
pub use sqlx_core::arguments::{Arguments, IntoArguments};
4-
pub use sqlx_core::caching_connection::CachingConnection;
54
pub use sqlx_core::connection::{Connect, Connection};
65
pub use sqlx_core::database::{self, Database};
76
pub use sqlx_core::executor::{Execute, Executor};

tests/mysql/mysql.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use futures::TryStreamExt;
22
use sqlx::mysql::{MySql, MySqlPool, MySqlRow};
3-
use sqlx::{CachingConnection, Connection, Executor, Row};
3+
use sqlx::{Connection, Executor, Row};
44
use sqlx_test::new;
55

66
#[sqlx_macros::test]
@@ -193,9 +193,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
193193
assert_eq!(i, val);
194194
}
195195

196-
assert_eq!(1, conn.cached_statements_count());
196+
assert_eq!(1, conn.cached_statements_size());
197197
conn.clear_cached_statements().await?;
198-
assert_eq!(0, conn.cached_statements_count());
198+
assert_eq!(0, conn.cached_statements_size());
199199

200200
Ok(())
201201
}

tests/postgres/postgres.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use futures::TryStreamExt;
22
use sqlx::postgres::PgRow;
33
use sqlx::postgres::{PgDatabaseError, PgErrorPosition, PgSeverity};
4-
use sqlx::CachingConnection;
54
use sqlx::{postgres::Postgres, Connection, Executor, PgPool, Row};
65
use sqlx_test::new;
76
use std::time::Duration;
@@ -504,9 +503,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
504503
assert_eq!(i, val);
505504
}
506505

507-
assert_eq!(1, conn.cached_statements_count());
506+
assert_eq!(1, conn.cached_statements_size());
508507
conn.clear_cached_statements().await?;
509-
assert_eq!(0, conn.cached_statements_count());
508+
assert_eq!(0, conn.cached_statements_size());
510509

511510
Ok(())
512511
}

tests/sqlite/sqlite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use futures::TryStreamExt;
22
use sqlx::{
3-
query, sqlite::Sqlite, CachingConnection, Connect, Connection, Executor, Row, SqliteConnection,
3+
query, sqlite::Sqlite, Connect, Connection, Executor, Row, SqliteConnection,
44
SqlitePool,
55
};
66
use sqlx_test::new;
@@ -286,9 +286,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
286286
assert_eq!(i, val);
287287
}
288288

289-
assert_eq!(1, conn.cached_statements_count());
289+
assert_eq!(1, conn.cached_statements_size());
290290
conn.clear_cached_statements().await?;
291-
assert_eq!(0, conn.cached_statements_count());
291+
assert_eq!(0, conn.cached_statements_size());
292292

293293
Ok(())
294294
}

0 commit comments

Comments
 (0)