Skip to content

breaking: introduce SqlSafeStr API #3364

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
73 changes: 0 additions & 73 deletions sqlx-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
) -> BoxFuture<'e, Result<<Self::Database as Database>::QueryResult, Error>>
where
'c: 'e,
E: 'q + Execute<'q, Self::Database>,

Check failure on line 43 in sqlx-core/src/executor.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

cannot find trait `Execute` in this scope
{
self.execute_many(query).try_collect().boxed()
}
Expand All @@ -52,7 +52,7 @@
) -> BoxStream<'e, Result<<Self::Database as Database>::QueryResult, Error>>
where
'c: 'e,
E: 'q + Execute<'q, Self::Database>,

Check failure on line 55 in sqlx-core/src/executor.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

cannot find trait `Execute` in this scope
{
self.fetch_many(query)
.try_filter_map(|step| async move {
Expand All @@ -71,7 +71,7 @@
) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>>
where
'c: 'e,
E: 'q + Execute<'q, Self::Database>,

Check failure on line 74 in sqlx-core/src/executor.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

cannot find trait `Execute` in this scope
{
self.fetch_many(query)
.try_filter_map(|step| async move {
Expand All @@ -97,7 +97,7 @@
>
where
'c: 'e,
E: 'q + Execute<'q, Self::Database>;

Check failure on line 100 in sqlx-core/src/executor.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

cannot find trait `Execute` in this scope

/// Execute the query and return all the generated results, collected into a [`Vec`].
fn fetch_all<'e, 'q: 'e, E>(
Expand All @@ -106,7 +106,7 @@
) -> BoxFuture<'e, Result<Vec<<Self::Database as Database>::Row>, Error>>
where
'c: 'e,
E: 'q + Execute<'q, Self::Database>,

Check failure on line 109 in sqlx-core/src/executor.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

cannot find trait `Execute` in this scope
{
self.fetch(query).try_collect().boxed()
}
Expand Down Expand Up @@ -182,76 +182,3 @@
where
'c: 'e;
}

/// A type that may be executed against a database connection.
///
/// Implemented for the following:
///
/// * [`&str`](std::str)
/// * [`Query`](super::query::Query)
///
pub trait Execute<'q, DB: Database>: Send + Sized {
/// Gets the SQL that will be executed.
fn sql(&self) -> &'q str;

/// Gets the previously cached statement, if available.
fn statement(&self) -> Option<&DB::Statement<'q>>;

/// Returns the arguments to be bound against the query string.
///
/// Returning `Ok(None)` for `Arguments` indicates to use a "simple" query protocol and to not
/// prepare the query. Returning `Ok(Some(Default::default()))` is an empty arguments object that
/// will be prepared (and cached) before execution.
///
/// Returns `Err` if encoding any of the arguments failed.
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError>;

/// Returns `true` if the statement should be cached.
fn persistent(&self) -> bool;
}

// NOTE: `Execute` is explicitly not implemented for String and &String to make it slightly more
// involved to write `conn.execute(format!("SELECT {val}"))`
impl<'q, DB: Database> Execute<'q, DB> for &'q str {
#[inline]
fn sql(&self) -> &'q str {
self
}

#[inline]
fn statement(&self) -> Option<&DB::Statement<'q>> {
None
}

#[inline]
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
Ok(None)
}

#[inline]
fn persistent(&self) -> bool {
true
}
}

impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Arguments<'q>>) {
#[inline]
fn sql(&self) -> &'q str {
self.0
}

#[inline]
fn statement(&self) -> Option<&DB::Statement<'q>> {
None
}

#[inline]
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
Ok(self.1.take())
}

#[inline]
fn persistent(&self) -> bool {
true
}
}
1 change: 1 addition & 0 deletions sqlx-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub mod net;
pub mod query_as;
pub mod query_builder;
pub mod query_scalar;
pub mod sql_str;

pub mod raw_sql;
pub mod row;
Expand Down
Loading
Loading