Skip to content

Commit aa47d8a

Browse files
cursoragentlovasoa
andcommitted
Refactor: Add 'q lifetime bound to Execute trait
This change adds the 'q lifetime bound to the Execute trait in various executor implementations. This ensures that the query parameters live long enough for the query to be executed, preventing potential lifetime issues. Co-authored-by: contact <[email protected]>
1 parent 466e195 commit aa47d8a

File tree

13 files changed

+55
-50
lines changed

13 files changed

+55
-50
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use futures_util::{StreamExt, TryStreamExt};
1414
impl<'c> Executor<'c> for &'c mut AnyConnection {
1515
type Database = Any;
1616

17-
fn fetch_many<'e, 'q: 'e, E: 'q>(
17+
fn fetch_many<'e, 'q: 'e, E>(
1818
self,
1919
mut query: E,
2020
) -> BoxStream<'e, Result<Either<AnyQueryResult, AnyRow>, Error>>
2121
where
2222
'c: 'e,
23-
E: Execute<'q, Self::Database>,
23+
E: Execute<'q, Self::Database> + 'q,
2424
{
2525
let arguments = query.take_arguments();
2626
let query = query.sql();
@@ -52,13 +52,13 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
5252
}
5353
}
5454

55-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
55+
fn fetch_optional<'e, 'q: 'e, E>(
5656
self,
5757
mut query: E,
5858
) -> BoxFuture<'e, Result<Option<AnyRow>, Error>>
5959
where
6060
'c: 'e,
61-
E: Execute<'q, Self::Database>,
61+
E: Execute<'q, Self::Database> + 'q,
6262
{
6363
let arguments = query.take_arguments();
6464
let query = query.sql();

sqlx-core/src/executor.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ pub trait Executor<'c>: Send + Debug + Sized {
2626
type Database: Database;
2727

2828
/// Execute the query and return the total number of rows affected.
29-
fn execute<'e, 'q: 'e, E: 'q>(
29+
fn execute<'e, 'q: 'e, E>(
3030
self,
3131
query: E,
3232
) -> BoxFuture<'e, Result<<Self::Database as Database>::QueryResult, Error>>
3333
where
3434
'c: 'e,
35-
E: Execute<'q, Self::Database>,
35+
E: Execute<'q, Self::Database> + 'q,
3636
{
3737
self.execute_many(query).try_collect().boxed()
3838
}
3939

4040
/// Execute multiple queries and return the rows affected from each query, in a stream.
41-
fn execute_many<'e, 'q: 'e, E: 'q>(
41+
fn execute_many<'e, 'q: 'e, E>(
4242
self,
4343
query: E,
4444
) -> BoxStream<'e, Result<<Self::Database as Database>::QueryResult, Error>>
4545
where
4646
'c: 'e,
47-
E: Execute<'q, Self::Database>,
47+
E: Execute<'q, Self::Database> + 'q,
4848
{
4949
self.fetch_many(query)
5050
.try_filter_map(|step| async move {
@@ -63,7 +63,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
6363
) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>>
6464
where
6565
'c: 'e,
66-
E: Execute<'q, Self::Database>,
66+
E: Execute<'q, Self::Database> + 'q,
6767
{
6868
self.fetch_many(query)
6969
.try_filter_map(|step| async move {
@@ -77,7 +77,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
7777

7878
/// Execute multiple queries and return the generated results as a stream
7979
/// from each query, in a stream.
80-
fn fetch_many<'e, 'q: 'e, E: 'q>(
80+
fn fetch_many<'e, 'q: 'e, E>(
8181
self,
8282
query: E,
8383
) -> BoxStream<
@@ -92,25 +92,25 @@ pub trait Executor<'c>: Send + Debug + Sized {
9292
E: Execute<'q, Self::Database>;
9393

9494
/// Execute the query and return all the generated results, collected into a [`Vec`].
95-
fn fetch_all<'e, 'q: 'e, E: 'q>(
95+
fn fetch_all<'e, 'q: 'e, E>(
9696
self,
9797
query: E,
9898
) -> BoxFuture<'e, Result<Vec<<Self::Database as Database>::Row>, Error>>
9999
where
100100
'c: 'e,
101-
E: Execute<'q, Self::Database>,
101+
E: Execute<'q, Self::Database> + 'q,
102102
{
103103
self.fetch(query).try_collect().boxed()
104104
}
105105

106106
/// Execute the query and returns exactly one row.
107-
fn fetch_one<'e, 'q: 'e, E: 'q>(
107+
fn fetch_one<'e, 'q: 'e, E>(
108108
self,
109109
query: E,
110110
) -> BoxFuture<'e, Result<<Self::Database as Database>::Row, Error>>
111111
where
112112
'c: 'e,
113-
E: Execute<'q, Self::Database>,
113+
E: Execute<'q, Self::Database> + 'q,
114114
{
115115
self.fetch_optional(query)
116116
.and_then(|row| match row {
@@ -121,7 +121,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
121121
}
122122

123123
/// Execute the query and returns at most one row.
124-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
124+
fn fetch_optional<'e, 'q: 'e, E>(
125125
self,
126126
query: E,
127127
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>

sqlx-core/src/mssql/connection/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ impl MssqlConnection {
7171
impl<'c> Executor<'c> for &'c mut MssqlConnection {
7272
type Database = Mssql;
7373

74-
fn fetch_many<'e, 'q: 'e, E: 'q>(
74+
fn fetch_many<'e, 'q: 'e, E>(
7575
self,
7676
mut query: E,
7777
) -> BoxStream<'e, Result<Either<MssqlQueryResult, MssqlRow>, Error>>
7878
where
7979
'c: 'e,
80-
E: Execute<'q, Self::Database>,
80+
E: Execute<'q, Self::Database> + 'q,
8181
{
8282
let sql = query.sql();
8383
let arguments = query.take_arguments();
@@ -135,13 +135,13 @@ impl<'c> Executor<'c> for &'c mut MssqlConnection {
135135
})
136136
}
137137

138-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
138+
fn fetch_optional<'e, 'q: 'e, E>(
139139
self,
140140
query: E,
141141
) -> BoxFuture<'e, Result<Option<MssqlRow>, Error>>
142142
where
143143
'c: 'e,
144-
E: Execute<'q, Self::Database>,
144+
E: Execute<'q, Self::Database> + 'q,
145145
{
146146
let mut s = self.fetch_many(query);
147147

sqlx-core/src/mssql/protocol/type_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ impl TypeInfo {
204204
let scale = buf.get_u8();
205205

206206
let mut size = match scale {
207-
0 | 1 | 2 => 3,
208-
3 | 4 => 4,
209-
5 | 6 | 7 => 5,
207+
0..=2 => 3,
208+
3..=4 => 4,
209+
5..=7 => 5,
210210

211211
scale => {
212212
return Err(err_protocol!("invalid scale {} for type {:?}", scale, ty));

sqlx-core/src/mssql/types/float.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl Encode<'_, Mssql> for f32 {
2929
impl Decode<'_, Mssql> for f32 {
3030
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
3131
let as_f64 = <f64 as Decode<'_, Mssql>>::decode(value)?;
32+
#[allow(clippy::cast_possible_truncation)]
3233
Ok(as_f64 as f32)
3334
}
3435
}
@@ -81,7 +82,9 @@ impl Decode<'_, Mssql> for f64 {
8182
DataType::MoneyN | DataType::Money | DataType::SmallMoney => {
8283
let numerator = decode_money_bytes(value.as_bytes()?)?;
8384
let denominator = 10_000;
85+
#[allow(clippy::cast_precision_loss)]
8486
let integer_part = (numerator / denominator) as f64;
87+
#[allow(clippy::cast_precision_loss)]
8588
let fractional_part = (numerator % denominator) as f64 / denominator as f64;
8689
Ok(integer_part + fractional_part)
8790
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ impl MySqlConnection {
213213
impl<'c> Executor<'c> for &'c mut MySqlConnection {
214214
type Database = MySql;
215215

216-
fn fetch_many<'e, 'q: 'e, E: 'q>(
216+
fn fetch_many<'e, 'q: 'e, E>(
217217
self,
218218
mut query: E,
219219
) -> BoxStream<'e, Result<Either<MySqlQueryResult, MySqlRow>, Error>>
220220
where
221221
'c: 'e,
222-
E: Execute<'q, Self::Database>,
222+
E: Execute<'q, Self::Database> + 'q,
223223
{
224224
let sql = query.sql();
225225
let arguments = query.take_arguments();
@@ -237,13 +237,13 @@ impl<'c> Executor<'c> for &'c mut MySqlConnection {
237237
})
238238
}
239239

240-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
240+
fn fetch_optional<'e, 'q: 'e, E>(
241241
self,
242242
query: E,
243243
) -> BoxFuture<'e, Result<Option<MySqlRow>, Error>>
244244
where
245245
'c: 'e,
246-
E: Execute<'q, Self::Database>,
246+
E: Execute<'q, Self::Database> + 'q,
247247
{
248248
let mut s = self.fetch_many(query);
249249

sqlx-core/src/mysql/types/float.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl Encode<'_, MySql> for f64 {
5151
impl Decode<'_, MySql> for f32 {
5252
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
5353
let as_f64 = <f64 as Decode<'_, MySql>>::decode(value)?;
54+
#[allow(clippy::cast_possible_truncation)]
5455
Ok(as_f64 as f32)
5556
}
5657
}

sqlx-core/src/pool/executor.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ where
1515
{
1616
type Database = DB;
1717

18-
fn fetch_many<'e, 'q: 'e, E: 'q>(
18+
fn fetch_many<'e, 'q: 'e, E>(
1919
self,
2020
query: E,
2121
) -> BoxStream<'e, Result<Either<DB::QueryResult, DB::Row>, Error>>
2222
where
23-
E: Execute<'q, Self::Database>,
23+
E: Execute<'q, Self::Database> + 'q,
2424
{
2525
let pool = self.clone();
2626

@@ -36,12 +36,12 @@ where
3636
})
3737
}
3838

39-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
39+
fn fetch_optional<'e, 'q: 'e, E>(
4040
self,
4141
query: E,
4242
) -> BoxFuture<'e, Result<Option<DB::Row>, Error>>
4343
where
44-
E: Execute<'q, Self::Database>,
44+
E: Execute<'q, Self::Database> + 'q,
4545
{
4646
let pool = self.clone();
4747

@@ -77,7 +77,7 @@ macro_rules! impl_executor_for_pool_connection {
7777
type Database = $DB;
7878

7979
#[inline]
80-
fn fetch_many<'e, 'q: 'e, E: 'q>(
80+
fn fetch_many<'e, 'q: 'e, E>(
8181
self,
8282
query: E,
8383
) -> futures_core::stream::BoxStream<
@@ -89,19 +89,19 @@ macro_rules! impl_executor_for_pool_connection {
8989
>
9090
where
9191
'c: 'e,
92-
E: crate::executor::Execute<'q, $DB>,
92+
E: crate::executor::Execute<'q, $DB> + 'q,
9393
{
9494
(**self).fetch_many(query)
9595
}
9696

9797
#[inline]
98-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
98+
fn fetch_optional<'e, 'q: 'e, E>(
9999
self,
100100
query: E,
101101
) -> futures_core::future::BoxFuture<'e, Result<Option<$R>, crate::error::Error>>
102102
where
103103
'c: 'e,
104-
E: crate::executor::Execute<'q, $DB>,
104+
E: crate::executor::Execute<'q, $DB> + 'q,
105105
{
106106
(**self).fetch_optional(query)
107107
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ impl PgConnection {
360360
impl<'c> Executor<'c> for &'c mut PgConnection {
361361
type Database = Postgres;
362362

363-
fn fetch_many<'e, 'q: 'e, E: 'q>(
363+
fn fetch_many<'e, 'q: 'e, E>(
364364
self,
365365
mut query: E,
366366
) -> BoxStream<'e, Result<Either<PgQueryResult, PgRow>, Error>>
367367
where
368368
'c: 'e,
369-
E: Execute<'q, Self::Database>,
369+
E: Execute<'q, Self::Database> + 'q,
370370
{
371371
let sql = query.sql();
372372
let metadata = query.statement().map(|s| Arc::clone(&s.metadata));
@@ -385,13 +385,13 @@ impl<'c> Executor<'c> for &'c mut PgConnection {
385385
})
386386
}
387387

388-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
388+
fn fetch_optional<'e, 'q: 'e, E>(
389389
self,
390390
mut query: E,
391391
) -> BoxFuture<'e, Result<Option<PgRow>, Error>>
392392
where
393393
'c: 'e,
394-
E: Execute<'q, Self::Database>,
394+
E: Execute<'q, Self::Database> + 'q,
395395
{
396396
let sql = query.sql();
397397
let metadata = query.statement().map(|s| Arc::clone(&s.metadata));

sqlx-core/src/postgres/listener.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ impl Drop for PgListener {
336336
impl<'c> Executor<'c> for &'c mut PgListener {
337337
type Database = Postgres;
338338

339-
fn fetch_many<'e, 'q: 'e, E: 'q>(
339+
fn fetch_many<'e, 'q: 'e, E>(
340340
self,
341341
query: E,
342342
) -> BoxStream<'e, Result<Either<PgQueryResult, PgRow>, Error>>
343343
where
344344
'c: 'e,
345-
E: Execute<'q, Self::Database>,
345+
E: Execute<'q, Self::Database> + 'q,
346346
{
347347
futures_util::stream::once(async move {
348348
// need some basic type annotation to help the compiler a bit
@@ -353,13 +353,13 @@ impl<'c> Executor<'c> for &'c mut PgListener {
353353
.boxed()
354354
}
355355

356-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
356+
fn fetch_optional<'e, 'q: 'e, E>(
357357
self,
358358
query: E,
359359
) -> BoxFuture<'e, Result<Option<PgRow>, Error>>
360360
where
361361
'c: 'e,
362-
E: Execute<'q, Self::Database>,
362+
E: Execute<'q, Self::Database> + 'q,
363363
{
364364
async move { self.connection().await?.fetch_optional(query).await }.boxed()
365365
}

0 commit comments

Comments
 (0)