Skip to content

Commit d962d70

Browse files
committed
Move Connection, AnyConnectionBackend and TransactionManager to SqlStr
1 parent e89de06 commit d962d70

File tree

16 files changed

+43
-73
lines changed

16 files changed

+43
-73
lines changed

sqlx-core/src/any/connection/backend.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::sql_str::SqlStr;
44
use either::Either;
55
use futures_core::future::BoxFuture;
66
use futures_core::stream::BoxStream;
7-
use std::borrow::Cow;
87
use std::fmt::Debug;
98

109
pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
@@ -34,7 +33,7 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
3433
///
3534
/// If we are already inside a transaction and `statement.is_some()`, then
3635
/// `Error::InvalidSavePoint` is returned without running any statements.
37-
fn begin(&mut self, statement: Option<Cow<'static, str>>) -> BoxFuture<'_, crate::Result<()>>;
36+
fn begin(&mut self, statement: Option<SqlStr>) -> BoxFuture<'_, crate::Result<()>>;
3837

3938
fn commit(&mut self) -> BoxFuture<'_, crate::Result<()>>;
4039

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use futures_core::future::BoxFuture;
2-
use std::borrow::Cow;
32
use std::future::Future;
43

54
use crate::any::{Any, AnyConnectOptions};
65
use crate::connection::{ConnectOptions, Connection};
76
use crate::error::Error;
87

98
use crate::database::Database;
9+
use crate::sql_str::SqlSafeStr;
1010
pub use backend::AnyConnectionBackend;
1111

1212
use crate::transaction::Transaction;
@@ -96,12 +96,12 @@ impl Connection for AnyConnection {
9696

9797
fn begin_with(
9898
&mut self,
99-
statement: impl Into<Cow<'static, str>>,
99+
statement: impl SqlSafeStr,
100100
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
101101
where
102102
Self: Sized,
103103
{
104-
Transaction::begin(self, Some(statement.into()))
104+
Transaction::begin(self, Some(statement.into_sql_str()))
105105
}
106106

107107
fn cached_statements_size(&self) -> usize {

sqlx-core/src/any/transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::borrow::Cow;
21
use std::future::Future;
32

43
use crate::any::{Any, AnyConnection};
54
use crate::database::Database;
65
use crate::error::Error;
6+
use crate::sql_str::SqlStr;
77
use crate::transaction::TransactionManager;
88

99
pub struct AnyTransactionManager;
@@ -13,7 +13,7 @@ impl TransactionManager for AnyTransactionManager {
1313

1414
fn begin<'conn>(
1515
conn: &'conn mut AnyConnection,
16-
statement: Option<Cow<'static, str>>,
16+
statement: Option<SqlStr>,
1717
) -> impl Future<Output = Result<(), Error>> + Send + 'conn {
1818
conn.backend.begin(statement)
1919
}

sqlx-core/src/connection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::database::{Database, HasStatementCache};
22
use crate::error::Error;
33

4+
use crate::sql_str::SqlSafeStr;
45
use crate::transaction::{Transaction, TransactionManager};
56
use futures_core::future::BoxFuture;
67
use log::LevelFilter;
7-
use std::borrow::Cow;
88
use std::fmt::Debug;
99
use std::future::Future;
1010
use std::str::FromStr;
@@ -59,12 +59,12 @@ pub trait Connection: Send {
5959
/// `statement` does not put the connection into a transaction.
6060
fn begin_with(
6161
&mut self,
62-
statement: impl Into<Cow<'static, str>>,
62+
statement: impl SqlSafeStr,
6363
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
6464
where
6565
Self: Sized,
6666
{
67-
Transaction::begin(self, Some(statement.into()))
67+
Transaction::begin(self, Some(statement.into_sql_str()))
6868
}
6969

7070
/// Returns `true` if the connection is currently in a transaction.

sqlx-core/src/pool/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
//! [`Pool::acquire`] or
5555
//! [`Pool::begin`].
5656
57-
use std::borrow::Cow;
5857
use std::fmt;
5958
use std::future::Future;
6059
use std::pin::{pin, Pin};
@@ -69,6 +68,7 @@ use futures_util::FutureExt;
6968
use crate::connection::Connection;
7069
use crate::database::Database;
7170
use crate::error::Error;
71+
use crate::sql_str::SqlSafeStr;
7272
use crate::transaction::Transaction;
7373

7474
pub use self::connection::PoolConnection;
@@ -390,11 +390,11 @@ impl<DB: Database> Pool<DB> {
390390
/// Retrieves a connection and immediately begins a new transaction using `statement`.
391391
pub async fn begin_with(
392392
&self,
393-
statement: impl Into<Cow<'static, str>>,
393+
statement: impl SqlSafeStr,
394394
) -> Result<Transaction<'static, DB>, Error> {
395395
Transaction::begin(
396396
MaybePoolConnection::PoolConnection(self.acquire().await?),
397-
Some(statement.into()),
397+
Some(statement.into_sql_str()),
398398
)
399399
.await
400400
}
@@ -403,12 +403,12 @@ impl<DB: Database> Pool<DB> {
403403
/// transaction using `statement`.
404404
pub async fn try_begin_with(
405405
&self,
406-
statement: impl Into<Cow<'static, str>>,
406+
statement: impl SqlSafeStr,
407407
) -> Result<Option<Transaction<'static, DB>>, Error> {
408408
match self.try_acquire() {
409409
Some(conn) => Transaction::begin(
410410
MaybePoolConnection::PoolConnection(conn),
411-
Some(statement.into()),
411+
Some(statement.into_sql_str()),
412412
)
413413
.await
414414
.map(Some),

sqlx-core/src/transaction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Cow;
21
use std::fmt::{self, Debug, Formatter};
32
use std::future::{self, Future};
43
use std::ops::{Deref, DerefMut};
@@ -25,7 +24,7 @@ pub trait TransactionManager {
2524
/// `Error::InvalidSavePoint` is returned without running any statements.
2625
fn begin<'conn>(
2726
conn: &'conn mut <Self::Database as Database>::Connection,
28-
statement: Option<Cow<'static, str>>,
27+
statement: Option<SqlStr>,
2928
) -> impl Future<Output = Result<(), Error>> + Send + 'conn;
3029

3130
/// Commit the active transaction or release the most recent savepoint.
@@ -99,7 +98,7 @@ where
9998
#[doc(hidden)]
10099
pub fn begin(
101100
conn: impl Into<MaybePoolConnection<'c, DB>>,
102-
statement: Option<Cow<'static, str>>,
101+
statement: Option<SqlStr>,
103102
) -> BoxFuture<'c, Result<Self, Error>> {
104103
let mut conn = conn.into();
105104

sqlx-mysql/src/any.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use sqlx_core::describe::Describe;
1717
use sqlx_core::executor::Executor;
1818
use sqlx_core::sql_str::SqlStr;
1919
use sqlx_core::transaction::TransactionManager;
20-
use std::borrow::Cow;
2120
use std::{future, pin::pin};
2221

2322
sqlx_core::declare_driver_with_optional_migrate!(DRIVER = MySql);
@@ -39,10 +38,7 @@ impl AnyConnectionBackend for MySqlConnection {
3938
Connection::ping(self).boxed()
4039
}
4140

42-
fn begin(
43-
&mut self,
44-
statement: Option<Cow<'static, str>>,
45-
) -> BoxFuture<'_, sqlx_core::Result<()>> {
41+
fn begin(&mut self, statement: Option<SqlStr>) -> BoxFuture<'_, sqlx_core::Result<()>> {
4642
MySqlTransactionManager::begin(self, statement).boxed()
4743
}
4844

sqlx-mysql/src/connection/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::borrow::Cow;
21
use std::fmt::{self, Debug, Formatter};
32
use std::future::Future;
43

54
pub(crate) use sqlx_core::connection::*;
5+
use sqlx_core::sql_str::SqlSafeStr;
66
pub(crate) use stream::{MySqlStream, Waiting};
77

88
use crate::common::StatementCache;
@@ -117,12 +117,12 @@ impl Connection for MySqlConnection {
117117

118118
fn begin_with(
119119
&mut self,
120-
statement: impl Into<Cow<'static, str>>,
120+
statement: impl SqlSafeStr,
121121
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
122122
where
123123
Self: Sized,
124124
{
125-
Transaction::begin(self, Some(statement.into()))
125+
Transaction::begin(self, Some(statement.into_sql_str()))
126126
}
127127

128128
fn shrink_buffers(&mut self) {

sqlx-mysql/src/transaction.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::borrow::Cow;
2-
3-
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
1+
use sqlx_core::sql_str::SqlStr;
42

53
use crate::connection::Waiting;
64
use crate::error::Error;
@@ -16,16 +14,14 @@ pub struct MySqlTransactionManager;
1614
impl TransactionManager for MySqlTransactionManager {
1715
type Database = MySql;
1816

19-
async fn begin(
20-
conn: &mut MySqlConnection,
21-
statement: Option<Cow<'static, str>>,
22-
) -> Result<(), Error> {
17+
async fn begin(conn: &mut MySqlConnection, statement: Option<SqlStr>) -> Result<(), Error> {
2318
let depth = conn.inner.transaction_depth;
19+
2420
let statement = match statement {
2521
// custom `BEGIN` statements are not allowed if we're already in a transaction
2622
// (we need to issue a `SAVEPOINT` instead)
2723
Some(_) if depth > 0 => return Err(Error::InvalidSavePointStatement),
28-
Some(statement) => AssertSqlSafe(statement).into_sql_str(),
24+
Some(statement) => statement,
2925
None => begin_ansi_transaction_sql(depth),
3026
};
3127
conn.execute(statement).await?;

sqlx-postgres/src/any.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use futures_core::future::BoxFuture;
66
use futures_core::stream::BoxStream;
77
use futures_util::{stream, FutureExt, StreamExt, TryFutureExt, TryStreamExt};
88
use sqlx_core::sql_str::SqlStr;
9-
use std::borrow::Cow;
109
use std::{future, pin::pin};
1110

1211
use sqlx_core::any::{
@@ -41,10 +40,7 @@ impl AnyConnectionBackend for PgConnection {
4140
Connection::ping(self).boxed()
4241
}
4342

44-
fn begin(
45-
&mut self,
46-
statement: Option<Cow<'static, str>>,
47-
) -> BoxFuture<'_, sqlx_core::Result<()>> {
43+
fn begin(&mut self, statement: Option<SqlStr>) -> BoxFuture<'_, sqlx_core::Result<()>> {
4844
PgTransactionManager::begin(self, statement).boxed()
4945
}
5046

0 commit comments

Comments
 (0)