Skip to content

feat: Add ExecuteEx::replace_arguments #3842

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

Open
wants to merge 1 commit 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
23 changes: 23 additions & 0 deletions sqlx-core/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::arguments::IntoArguments;
use crate::database::Database;
use crate::describe::Describe;
use crate::error::{BoxDynError, Error};
Expand Down Expand Up @@ -210,6 +211,16 @@ pub trait Execute<'q, DB: Database>: Send + Sized {
fn persistent(&self) -> bool;
}

pub trait ExecuteEx<'q, DB: Database, A: IntoArguments<'q, DB>>: Execute<'q, DB> {
/// Replaces the arguments to be bound against the query string.
///
/// See [`Execute::take_arguments`] for the return value considerations
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError>;
}

// 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 {
Expand Down Expand Up @@ -255,3 +266,15 @@ impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Ar
true
}
}

impl<'q, DB: Database, A: IntoArguments<'q, DB>> ExecuteEx<'q, DB, A>
for (&'q str, Option<<DB as Database>::Arguments<'q>>)
{
#[inline]
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
Ok(self.1.replace(arguments.into_arguments()))
}
}
33 changes: 32 additions & 1 deletion sqlx-core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::arguments::{Arguments, IntoArguments};
use crate::database::{Database, HasStatementCache};
use crate::encode::Encode;
use crate::error::{BoxDynError, Error};
use crate::executor::{Execute, Executor};
use crate::executor::{Execute, ExecuteEx, Executor};
use crate::statement::Statement;
use crate::types::Type;

Expand Down Expand Up @@ -72,6 +72,23 @@ where
}
}

impl<'q, DB, A> ExecuteEx<'q, DB, A> for Query<'q, DB, A>
where
DB: Database,
A: Send + IntoArguments<'q, DB>,
{
#[inline]
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
self.arguments
.replace(Ok(arguments))
.transpose()
.map(|option| option.map(IntoArguments::into_arguments))
}
}

impl<'q, DB: Database> Query<'q, DB, <DB as Database>::Arguments<'q>> {
/// Bind a value for use with this SQL query.
///
Expand Down Expand Up @@ -323,6 +340,20 @@ where
}
}

impl<'q, DB, F: Send, A: Send> ExecuteEx<'q, DB, A> for Map<'q, DB, F, A>
where
DB: Database,
A: IntoArguments<'q, DB>,
{
#[inline]
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
self.inner.replace_arguments(arguments)
}
}

impl<'q, DB, F, O, A> Map<'q, DB, F, A>
where
DB: Database,
Expand Down
16 changes: 15 additions & 1 deletion sqlx-core/src/query_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::arguments::IntoArguments;
use crate::database::{Database, HasStatementCache};
use crate::encode::Encode;
use crate::error::{BoxDynError, Error};
use crate::executor::{Execute, Executor};
use crate::executor::{Execute, ExecuteEx, Executor};
use crate::from_row::FromRow;
use crate::query::{query, query_statement, query_statement_with, query_with_result, Query};
use crate::types::Type;
Expand Down Expand Up @@ -47,6 +47,20 @@ where
}
}

impl<'q, DB, O: Send, A: Send> ExecuteEx<'q, DB, A> for QueryAs<'q, DB, O, A>
where
DB: Database,
A: 'q + IntoArguments<'q, DB>,
{
#[inline]
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
self.inner.replace_arguments(arguments)
}
}

impl<'q, DB: Database, O> QueryAs<'q, DB, O, <DB as Database>::Arguments<'q>> {
/// Bind a value for use with this SQL query.
///
Expand Down
16 changes: 15 additions & 1 deletion sqlx-core/src/query_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::arguments::IntoArguments;
use crate::database::{Database, HasStatementCache};
use crate::encode::Encode;
use crate::error::{BoxDynError, Error};
use crate::executor::{Execute, Executor};
use crate::executor::{Execute, ExecuteEx, Executor};
use crate::from_row::FromRow;
use crate::query_as::{
query_as, query_as_with_result, query_statement_as, query_statement_as_with, QueryAs,
Expand Down Expand Up @@ -44,6 +44,20 @@ where
}
}

impl<'q, DB, O: Send, A: Send> ExecuteEx<'q, DB, A> for QueryScalar<'q, DB, O, A>
where
DB: Database,
A: 'q + IntoArguments<'q, DB>,
{
#[inline]
fn replace_arguments(
&mut self,
arguments: A,
) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
self.inner.replace_arguments(arguments)
}
}

impl<'q, DB: Database, O> QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>> {
/// Bind a value for use with this SQL query.
///
Expand Down
Loading