Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RawSql lifetime issues #3613

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 26 additions & 39 deletions sqlx-core/src/raw_sql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use either::Either;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;

use crate::database::Database;
Expand Down Expand Up @@ -139,26 +140,25 @@ impl<'q, DB: Database> Execute<'q, DB> for RawSql<'q> {
impl<'q> RawSql<'q> {
/// Execute the SQL string and return the total number of rows affected.
#[inline]
pub async fn execute<'e, E>(
self,
executor: E,
) -> crate::Result<<E::Database as Database>::QueryResult>
pub async fn execute<'e, E, DB>(self, executor: E) -> crate::Result<DB::QueryResult>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.execute(self).await
}

/// Execute the SQL string. Returns a stream which gives the number of rows affected for each statement in the string.
#[inline]
pub fn execute_many<'e, E>(
pub fn execute_many<'e, E, DB>(
self,
executor: E,
) -> BoxStream<'e, crate::Result<<E::Database as Database>::QueryResult>>
) -> BoxStream<'e, crate::Result<DB::QueryResult>>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.execute_many(self)
}
Expand All @@ -167,13 +167,11 @@ impl<'q> RawSql<'q> {
///
/// If the string contains multiple statements, their results will be concatenated together.
#[inline]
pub fn fetch<'e, E>(
self,
executor: E,
) -> BoxStream<'e, Result<<E::Database as Database>::Row, Error>>
pub fn fetch<'e, E, DB>(self, executor: E) -> BoxStream<'e, Result<DB::Row, Error>>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.fetch(self)
}
Expand All @@ -183,19 +181,14 @@ impl<'q> RawSql<'q> {
/// For each query in the stream, any generated rows are returned first,
/// then the `QueryResult` with the number of rows affected.
#[inline]
pub fn fetch_many<'e, E>(
pub fn fetch_many<'e, E, DB>(
self,
executor: E,
) -> BoxStream<
'e,
Result<
Either<<E::Database as Database>::QueryResult, <E::Database as Database>::Row>,
Error,
>,
>
) -> BoxStream<'e, Result<Either<DB::QueryResult, DB::Row>, Error>>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.fetch_many(self)
}
Expand All @@ -208,15 +201,13 @@ impl<'q> RawSql<'q> {
/// To avoid exhausting available memory, ensure the result set has a known upper bound,
/// e.g. using `LIMIT`.
#[inline]
pub async fn fetch_all<'e, E>(
self,
executor: E,
) -> crate::Result<Vec<<E::Database as Database>::Row>>
pub fn fetch_all<'e, E, DB>(self, executor: E) -> BoxFuture<'e, crate::Result<Vec<DB::Row>>>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.fetch_all(self).await
executor.fetch_all(self)
}

/// Execute the SQL string, returning the first row or [`Error::RowNotFound`] otherwise.
Expand All @@ -232,15 +223,13 @@ impl<'q> RawSql<'q> {
///
/// Otherwise, you might want to add `LIMIT 1` to your query.
#[inline]
pub async fn fetch_one<'e, E>(
self,
executor: E,
) -> crate::Result<<E::Database as Database>::Row>
pub fn fetch_one<'e, E, DB>(self, executor: E) -> BoxFuture<'e, crate::Result<DB::Row>>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.fetch_one(self).await
executor.fetch_one(self)
}

/// Execute the SQL string, returning the first row or [`None`] otherwise.
Expand All @@ -256,13 +245,11 @@ impl<'q> RawSql<'q> {
///
/// Otherwise, you might want to add `LIMIT 1` to your query.
#[inline]
pub async fn fetch_optional<'e, E>(
self,
executor: E,
) -> crate::Result<<E::Database as Database>::Row>
pub async fn fetch_optional<'e, E, DB>(self, executor: E) -> crate::Result<DB::Row>
where
'q: 'e,
E: Executor<'e>,
DB: Database,
E: Executor<'e, Database = DB>,
{
executor.fetch_one(self).await
}
Expand Down
19 changes: 19 additions & 0 deletions tests/sqlite/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sqlx::{
SqliteConnection, SqlitePool, Statement, TypeInfo,
};
use sqlx_test::new;
use std::future::Future;
use std::sync::Arc;

#[sqlx_macros::test]
Expand Down Expand Up @@ -960,3 +961,21 @@ async fn test_multiple_set_rollback_hook_calls_drop_old_handler() -> anyhow::Res
assert_eq!(1, Arc::strong_count(&ref_counted_object));
Ok(())
}

#[sqlx_macros::test]
async fn issue_3150() {
// Same bounds as `tokio::spawn()`
async fn fake_spawn<F>(future: F) -> F::Output
where
F: Future + Send + 'static,
{
future.await
}

fake_spawn(async {
let mut db = SqliteConnection::connect(":memory:").await.unwrap();
sqlx::raw_sql("").execute(&mut db).await.unwrap();
db.close().await.unwrap();
})
.await;
}