Skip to content

Commit a0b6739

Browse files
committed
fix doc test
1 parent a0f122b commit a0b6739

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

sqlx-core/src/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ where
558558
/// let query = format!("SELECT * FROM articles WHERE content LIKE '%{user_input}%'");
559559
/// // where `conn` is `PgConnection` or `MySqlConnection`
560560
/// // or some other type that implements `Executor`.
561-
/// let results = sqlx::query(&query).fetch_all(&mut conn).await?;
561+
/// let results = sqlx::query(sqlx::AssertSqlSafe(query)).fetch_all(&mut conn).await?;
562562
/// # Ok(())
563563
/// # }
564564
/// ```

sqlx-core/src/sql_str.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::borrow::Borrow;
1+
use std::borrow::{Borrow, Cow};
22
use std::hash::{Hash, Hasher};
33
use std::sync::Arc;
44

@@ -102,6 +102,15 @@ impl SqlSafeStr for AssertSqlSafe<Arc<String>> {
102102
}
103103
}
104104

105+
impl SqlSafeStr for AssertSqlSafe<Cow<'static, str>> {
106+
fn into_sql_str(self) -> SqlStr {
107+
match self.0 {
108+
Cow::Borrowed(str) => str.into_sql_str(),
109+
Cow::Owned(str) => AssertSqlSafe(str).into_sql_str(),
110+
}
111+
}
112+
}
113+
105114
/// A SQL string that is ready to execute on a database connection.
106115
///
107116
/// This is essentially `Cow<'static, str>` but which can be constructed from additional types

sqlx-core/src/transaction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::fmt::{self, Debug, Formatter};
23
use std::ops::{Deref, DerefMut};
34

sqlx-mysql/src/transaction.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22

33
use futures_core::future::BoxFuture;
4+
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
45

56
use crate::connection::Waiting;
67
use crate::error::Error;
@@ -27,10 +28,10 @@ impl TransactionManager for MySqlTransactionManager {
2728
// custom `BEGIN` statements are not allowed if we're already in a transaction
2829
// (we need to issue a `SAVEPOINT` instead)
2930
Some(_) if depth > 0 => return Err(Error::InvalidSavePointStatement),
30-
Some(statement) => statement,
31+
Some(statement) => AssertSqlSafe(statement).into_sql_str(),
3132
None => begin_ansi_transaction_sql(depth),
3233
};
33-
conn.execute(&*statement).await?;
34+
conn.execute(statement).await?;
3435
if !conn.in_transaction() {
3536
return Err(Error::BeginFailed);
3637
}

sqlx-postgres/src/any.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use crate::{
55
use futures_core::future::BoxFuture;
66
use futures_core::stream::BoxStream;
77
use futures_util::{stream, StreamExt, TryFutureExt, TryStreamExt};
8-
use std::borrow::Cow;
9-
use std::{future, pin::pin};
108
use sqlx_core::sql_str::SqlStr;
9+
use std::borrow::Cow;
1110
use std::{future, pin::pin};
1211

1312
use sqlx_core::any::{

sqlx-postgres/src/transaction.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use futures_core::future::BoxFuture;
22
use sqlx_core::database::Database;
3+
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
34
use std::borrow::Cow;
45

56
use crate::error::Error;
@@ -25,13 +26,13 @@ impl TransactionManager for PgTransactionManager {
2526
// custom `BEGIN` statements are not allowed if we're already in
2627
// a transaction (we need to issue a `SAVEPOINT` instead)
2728
Some(_) if depth > 0 => return Err(Error::InvalidSavePointStatement),
28-
Some(statement) => statement,
29+
Some(statement) => AssertSqlSafe(statement).into_sql_str(),
2930
None => begin_ansi_transaction_sql(depth),
3031
};
3132

3233
let rollback = Rollback::new(conn);
3334

34-
rollback.conn.queue_simple_query(&statement)?;
35+
rollback.conn.queue_simple_query(statement.as_str())?;
3536
rollback.conn.wait_until_ready().await?;
3637
if !rollback.conn.in_transaction() {
3738
return Err(Error::BeginFailed);

sqlx-sqlite/src/connection/worker.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use std::borrow::Cow;
12
use std::future::Future;
23
use std::sync::atomic::{AtomicUsize, Ordering};
34
use std::sync::Arc;
45
use std::thread;
56

67
use futures_channel::oneshot;
78
use futures_intrusive::sync::{Mutex, MutexGuard};
8-
use sqlx_core::sql_str::SqlStr;
9+
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr, SqlStr};
910
use tracing::span::Span;
1011

1112
use sqlx_core::describe::Describe;
@@ -219,12 +220,12 @@ impl ConnectionWorker {
219220
}
220221
continue;
221222
},
222-
Some(statement) => statement,
223+
Some(statement) => AssertSqlSafe(statement).into_sql_str(),
223224
None => begin_ansi_transaction_sql(depth),
224225
};
225226
let res =
226227
conn.handle
227-
.exec(begin_ansi_transaction_sql(depth).as_str())
228+
.exec(statement.as_str())
228229
.map(|_| {
229230
shared.transaction_depth.fetch_add(1, Ordering::Release);
230231
});

0 commit comments

Comments
 (0)