Skip to content

Commit b00f4aa

Browse files
committed
Update the definition of Executor and AnyConnectionBackend + update Postgres driver
1 parent fdd4c4f commit b00f4aa

32 files changed

+282
-188
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::any::{Any, AnyArguments, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo};
22
use crate::describe::Describe;
3+
use crate::sql_str::SqlStr;
34
use either::Either;
45
use futures_core::future::BoxFuture;
56
use futures_core::stream::BoxStream;
@@ -69,23 +70,23 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
6970

7071
fn fetch_many<'q>(
7172
&'q mut self,
72-
query: &'q str,
73+
query: SqlStr,
7374
persistent: bool,
7475
arguments: Option<AnyArguments<'q>>,
7576
) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
7677

7778
fn fetch_optional<'q>(
7879
&'q mut self,
79-
query: &'q str,
80+
query: SqlStr,
8081
persistent: bool,
8182
arguments: Option<AnyArguments<'q>>,
8283
) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
8384

8485
fn prepare_with<'c, 'q: 'c>(
8586
&'c mut self,
86-
sql: &'q str,
87+
sql: SqlStr,
8788
parameters: &[AnyTypeInfo],
8889
) -> BoxFuture<'c, crate::Result<AnyStatement>>;
8990

90-
fn describe<'q>(&'q mut self, sql: &'q str) -> BoxFuture<'q, crate::Result<Describe<Any>>>;
91+
fn describe<'q>(&'q mut self, sql: SqlStr) -> BoxFuture<'q, crate::Result<Describe<Any>>>;
9192
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::any::{Any, AnyConnection, AnyQueryResult, AnyRow, AnyStatement, AnyTy
22
use crate::describe::Describe;
33
use crate::error::Error;
44
use crate::executor::{Execute, Executor};
5+
use crate::sql_str::SqlSafeStr;
56
use either::Either;
67
use futures_core::future::BoxFuture;
78
use futures_core::stream::BoxStream;
@@ -23,8 +24,8 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
2324
Ok(arguments) => arguments,
2425
Err(error) => return stream::once(future::ready(Err(error))).boxed(),
2526
};
26-
self.backend
27-
.fetch_many(query.sql(), query.persistent(), arguments)
27+
let persistent = query.persistent();
28+
self.backend.fetch_many(query.sql(), persistent, arguments)
2829
}
2930

3031
fn fetch_optional<'e, 'q: 'e, E>(
@@ -39,28 +40,29 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
3940
Ok(arguments) => arguments,
4041
Err(error) => return future::ready(Err(error)).boxed(),
4142
};
43+
let persistent = query.persistent();
4244
self.backend
43-
.fetch_optional(query.sql(), query.persistent(), arguments)
45+
.fetch_optional(query.sql(), persistent, arguments)
4446
}
4547

46-
fn prepare_with<'e, 'q: 'e>(
48+
fn prepare_with<'e>(
4749
self,
48-
sql: &'q str,
50+
sql: impl SqlSafeStr,
4951
parameters: &[AnyTypeInfo],
5052
) -> BoxFuture<'e, Result<AnyStatement, Error>>
5153
where
5254
'c: 'e,
5355
{
54-
self.backend.prepare_with(sql, parameters)
56+
self.backend.prepare_with(sql.into_sql_str(), parameters)
5557
}
5658

57-
fn describe<'e, 'q: 'e>(
59+
fn describe<'e>(
5860
self,
59-
sql: &'q str,
61+
sql: impl SqlSafeStr,
6062
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>>
6163
where
6264
'c: 'e,
6365
{
64-
self.backend.describe(sql)
66+
self.backend.describe(sql.into_sql_str())
6567
}
6668
}

sqlx-core/src/any/statement.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::column::ColumnIndex;
33
use crate::database::Database;
44
use crate::error::Error;
55
use crate::ext::ustr::UStr;
6-
use crate::sql_str::{AssertSqlSafe, SqlSafeStr, SqlStr};
6+
use crate::sql_str::SqlStr;
77
use crate::statement::Statement;
88
use crate::HashMap;
99
use either::Either;
@@ -32,8 +32,8 @@ impl Statement for AnyStatement {
3232
}
3333
}
3434

35-
fn sql(&self) -> &str {
36-
&self.sql.as_str()
35+
fn sql(&self) -> SqlStr {
36+
self.sql.clone()
3737
}
3838

3939
fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
@@ -64,7 +64,7 @@ impl<'i> ColumnIndex<AnyStatement> for &'i str {
6464
impl<'q> AnyStatement {
6565
#[doc(hidden)]
6666
pub fn try_from_statement<S>(
67-
query: &'q str,
67+
query: SqlStr,
6868
statement: &S,
6969
column_names: Arc<HashMap<UStr, usize>>,
7070
) -> crate::Result<Self>
@@ -91,7 +91,7 @@ impl<'q> AnyStatement {
9191
.collect::<Result<Vec<_>, _>>()?;
9292

9393
Ok(Self {
94-
sql: AssertSqlSafe(query).into_sql_str(),
94+
sql: query,
9595
columns,
9696
column_names,
9797
parameters,

sqlx-core/src/executor.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::database::Database;
22
use crate::describe::Describe;
33
use crate::error::{BoxDynError, Error};
4+
use crate::sql_str::{SqlSafeStr, SqlStr};
45

56
use either::Either;
67
use futures_core::future::BoxFuture;
@@ -146,9 +147,9 @@ pub trait Executor<'c>: Send + Debug + Sized {
146147
/// This explicit API is provided to allow access to the statement metadata available after
147148
/// it prepared but before the first row is returned.
148149
#[inline]
149-
fn prepare<'e, 'q: 'e>(
150+
fn prepare<'e>(
150151
self,
151-
query: &'q str,
152+
query: impl SqlSafeStr,
152153
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement, Error>>
153154
where
154155
'c: 'e,
@@ -161,9 +162,9 @@ pub trait Executor<'c>: Send + Debug + Sized {
161162
///
162163
/// Only some database drivers (PostgreSQL, MSSQL) can take advantage of
163164
/// this extra information to influence parameter type inference.
164-
fn prepare_with<'e, 'q: 'e>(
165+
fn prepare_with<'e>(
165166
self,
166-
sql: &'q str,
167+
sql: impl SqlSafeStr,
167168
parameters: &'e [<Self::Database as Database>::TypeInfo],
168169
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement, Error>>
169170
where
@@ -175,9 +176,9 @@ pub trait Executor<'c>: Send + Debug + Sized {
175176
/// This is used by compile-time verification in the query macros to
176177
/// power their type inference.
177178
#[doc(hidden)]
178-
fn describe<'e, 'q: 'e>(
179+
fn describe<'e>(
179180
self,
180-
sql: &'q str,
181+
sql: impl SqlSafeStr,
181182
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>>
182183
where
183184
'c: 'e;
@@ -192,7 +193,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
192193
///
193194
pub trait Execute<'q, DB: Database>: Send + Sized {
194195
/// Gets the SQL that will be executed.
195-
fn sql(&self) -> &'q str;
196+
fn sql(self) -> SqlStr;
196197

197198
/// Gets the previously cached statement, if available.
198199
fn statement(&self) -> Option<&DB::Statement>;
@@ -210,12 +211,13 @@ pub trait Execute<'q, DB: Database>: Send + Sized {
210211
fn persistent(&self) -> bool;
211212
}
212213

213-
// NOTE: `Execute` is explicitly not implemented for String and &String to make it slightly more
214-
// involved to write `conn.execute(format!("SELECT {val}"))`
215-
impl<'q, DB: Database> Execute<'q, DB> for &'q str {
214+
impl<'q, DB: Database, T> Execute<'q, DB> for (T, Option<<DB as Database>::Arguments<'q>>)
215+
where
216+
T: SqlSafeStr + Send,
217+
{
216218
#[inline]
217-
fn sql(&self) -> &'q str {
218-
self
219+
fn sql(self) -> SqlStr {
220+
self.0.into_sql_str()
219221
}
220222

221223
#[inline]
@@ -225,7 +227,7 @@ impl<'q, DB: Database> Execute<'q, DB> for &'q str {
225227

226228
#[inline]
227229
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
228-
Ok(None)
230+
Ok(self.1.take())
229231
}
230232

231233
#[inline]
@@ -234,10 +236,13 @@ impl<'q, DB: Database> Execute<'q, DB> for &'q str {
234236
}
235237
}
236238

237-
impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Arguments<'q>>) {
239+
impl<'q, DB: Database, T> Execute<'q, DB> for T
240+
where
241+
T: SqlSafeStr + Send,
242+
{
238243
#[inline]
239-
fn sql(&self) -> &'q str {
240-
self.0
244+
fn sql(self) -> SqlStr {
245+
self.into_sql_str()
241246
}
242247

243248
#[inline]
@@ -247,7 +252,7 @@ impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Ar
247252

248253
#[inline]
249254
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
250-
Ok(self.1.take())
255+
Ok(None)
251256
}
252257

253258
#[inline]

sqlx-core/src/logger.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::connection::LogSettings;
1+
use crate::{connection::LogSettings, sql_str::SqlStr};
22
use std::time::Instant;
33

44
// Yes these look silly. `tracing` doesn't currently support dynamic levels
@@ -60,16 +60,16 @@ pub(crate) fn private_level_filter_to_trace_level(
6060
private_level_filter_to_levels(filter).map(|(level, _)| level)
6161
}
6262

63-
pub struct QueryLogger<'q> {
64-
sql: &'q str,
63+
pub struct QueryLogger {
64+
sql: SqlStr,
6565
rows_returned: u64,
6666
rows_affected: u64,
6767
start: Instant,
6868
settings: LogSettings,
6969
}
7070

71-
impl<'q> QueryLogger<'q> {
72-
pub fn new(sql: &'q str, settings: LogSettings) -> Self {
71+
impl QueryLogger {
72+
pub fn new(sql: SqlStr, settings: LogSettings) -> Self {
7373
Self {
7474
sql,
7575
rows_returned: 0,
@@ -104,18 +104,18 @@ impl<'q> QueryLogger<'q> {
104104
let log_is_enabled = log::log_enabled!(target: "sqlx::query", log_level)
105105
|| private_tracing_dynamic_enabled!(target: "sqlx::query", tracing_level);
106106
if log_is_enabled {
107-
let mut summary = parse_query_summary(self.sql);
107+
let mut summary = parse_query_summary(self.sql.as_str());
108108

109-
let sql = if summary != self.sql {
109+
let sql = if summary != self.sql.as_str() {
110110
summary.push_str(" …");
111111
format!(
112112
"\n\n{}\n",
113-
self.sql /*
114-
sqlformat::format(
115-
self.sql,
116-
&sqlformat::QueryParams::None,
117-
sqlformat::FormatOptions::default()
118-
)*/
113+
self.sql.as_str() /*
114+
sqlformat::format(
115+
self.sql,
116+
&sqlformat::QueryParams::None,
117+
sqlformat::FormatOptions::default()
118+
)*/
119119
)
120120
} else {
121121
String::new()
@@ -158,7 +158,7 @@ impl<'q> QueryLogger<'q> {
158158
}
159159
}
160160

161-
impl<'q> Drop for QueryLogger<'q> {
161+
impl Drop for QueryLogger {
162162
fn drop(&mut self) {
163163
self.finish();
164164
}

sqlx-core/src/migrate/migration.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ use std::borrow::Cow;
22

33
use sha2::{Digest, Sha384};
44

5+
use crate::sql_str::{SqlSafeStr, SqlStr};
6+
57
use super::MigrationType;
68

79
#[derive(Debug, Clone)]
810
pub struct Migration {
911
pub version: i64,
1012
pub description: Cow<'static, str>,
1113
pub migration_type: MigrationType,
12-
pub sql: Cow<'static, str>,
14+
pub sql: SqlStr,
1315
pub checksum: Cow<'static, [u8]>,
1416
pub no_tx: bool,
1517
}
@@ -19,10 +21,13 @@ impl Migration {
1921
version: i64,
2022
description: Cow<'static, str>,
2123
migration_type: MigrationType,
22-
sql: Cow<'static, str>,
24+
sql: impl SqlSafeStr,
2325
no_tx: bool,
2426
) -> Self {
25-
let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
27+
let sql = sql.into_sql_str();
28+
let checksum = Cow::Owned(Vec::from(
29+
Sha384::digest(sql.as_str().as_bytes()).as_slice(),
30+
));
2631

2732
Migration {
2833
version,

sqlx-core/src/migrate/source.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::error::BoxDynError;
22
use crate::migrate::{Migration, MigrationType};
3+
use crate::sql_str::AssertSqlSafe;
34
use futures_core::future::BoxFuture;
45

56
use std::borrow::Cow;
@@ -131,7 +132,7 @@ pub fn resolve_blocking(path: &Path) -> Result<Vec<(Migration, PathBuf)>, Resolv
131132
version,
132133
Cow::Owned(description),
133134
migration_type,
134-
Cow::Owned(sql),
135+
AssertSqlSafe(sql),
135136
no_tx,
136137
),
137138
entry_path,

sqlx-core/src/pool/executor.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::describe::Describe;
88
use crate::error::Error;
99
use crate::executor::{Execute, Executor};
1010
use crate::pool::Pool;
11+
use crate::sql_str::SqlSafeStr;
1112

1213
impl<'p, DB: Database> Executor<'p> for &'_ Pool<DB>
1314
where
@@ -48,22 +49,30 @@ where
4849
Box::pin(async move { pool.acquire().await?.fetch_optional(query).await })
4950
}
5051

51-
fn prepare_with<'e, 'q: 'e>(
52+
fn prepare_with<'e>(
5253
self,
53-
sql: &'q str,
54+
sql: impl SqlSafeStr,
5455
parameters: &'e [<Self::Database as Database>::TypeInfo],
55-
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement, Error>> {
56+
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement, Error>>
57+
where
58+
'p: 'e,
59+
{
5660
let pool = self.clone();
61+
let sql = sql.into_sql_str();
5762

5863
Box::pin(async move { pool.acquire().await?.prepare_with(sql, parameters).await })
5964
}
6065

6166
#[doc(hidden)]
62-
fn describe<'e, 'q: 'e>(
67+
fn describe<'e>(
6368
self,
64-
sql: &'q str,
65-
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>> {
69+
sql: impl SqlSafeStr,
70+
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>>
71+
where
72+
'p: 'e,
73+
{
6674
let pool = self.clone();
75+
let sql = sql.into_sql_str();
6776

6877
Box::pin(async move { pool.acquire().await?.describe(sql).await })
6978
}

0 commit comments

Comments
 (0)