Skip to content

sqlx-core: impl IntoArguments for Arguments #3833

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 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
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ path = "benches/sqlite/describe.rs"
harness = false
required-features = ["sqlite"]

[[test]]
name = "generic-sqlite"
path = "tests/generic/sqlite.rs"
required-features = ["sqlite", "macros"]

#
# MySQL
#
Expand Down Expand Up @@ -351,6 +356,11 @@ name = "mysql-rustsec"
path = "tests/mysql/rustsec.rs"
required-features = ["mysql"]

[[test]]
name = "generic-mysql"
path = "tests/generic/mysql.rs"
required-features = ["mysql", "macros"]

#
# PostgreSQL
#
Expand Down Expand Up @@ -405,3 +415,8 @@ required-features = ["postgres"]
name = "postgres-rustsec"
path = "tests/postgres/rustsec.rs"
required-features = ["postgres", "macros", "migrate"]

[[test]]
name = "generic-postgres"
path = "tests/generic/postgres.rs"
required-features = ["postgres", "macros"]
30 changes: 20 additions & 10 deletions sqlx-core/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,29 @@ pub trait IntoArguments<'q, DB: Database>: Sized + Send {
#[macro_export]
macro_rules! impl_into_arguments_for_arguments {
($Arguments:path) => {
impl<'q>
$crate::arguments::IntoArguments<
'q,
<$Arguments as $crate::arguments::Arguments<'q>>::Database,
> for $Arguments
{
fn into_arguments(self) -> $Arguments {
self
}
}
// impl<'q>
// $crate::arguments::IntoArguments<
// 'q,
// <$Arguments as $crate::arguments::Arguments<'q>>::Database,
// > for $Arguments
// {
// fn into_arguments(self) -> $Arguments {
// self
// }
// }
};
}

impl<'q, DB, T> IntoArguments<'q, DB> for T
where
DB: Database,
T: Arguments<'q, Database = DB> + Into<<DB as Database>::Arguments<'q>>,
{
fn into_arguments(self) -> <DB as Database>::Arguments<'q> {
self.into()
}
}

/// used by the query macros to prevent supernumerary `.bind()` calls
pub struct ImmutableArguments<'q, DB: Database>(pub <DB as Database>::Arguments<'q>);

Expand Down
15 changes: 15 additions & 0 deletions tests/generic/ansi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use sqlx::{ColumnIndex, Database, Decode, Executor, Row, Type};

pub async fn generic_it_connects<'conn, E>(e: E) -> sqlx::Result<i32>
where
E: Executor<'conn>,
E::Database: Database,
for<'row> i32: Type<E::Database> + Decode<'row, E::Database>,
<E::Database as Database>::Row: Row<Database = E::Database>,
usize: ColumnIndex<<E::Database as Database>::Row>,
{
sqlx::query("select 1 + 1")
.try_map(|row: <E::Database as Database>::Row| row.try_get::<i32, _>(0))
.fetch_one(e)
.await
}
15 changes: 15 additions & 0 deletions tests/generic/mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod ansi;

use sqlx::MySql;
use sqlx_test::{new, pool, setup_if_needed};

#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {
let mut conn = new::<MySql>().await?;

let value = ansi::generic_it_connects(&mut conn).await?;

assert_eq!(2i32, value);

Ok(())
}
15 changes: 15 additions & 0 deletions tests/generic/postgres.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod ansi;

use sqlx::Postgres;
use sqlx_test::{new, pool, setup_if_needed};

#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let value = ansi::generic_it_connects(&mut conn).await?;

assert_eq!(2i32, value);

Ok(())
}
15 changes: 15 additions & 0 deletions tests/generic/sqlite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod ansi;

use sqlx::Sqlite;
use sqlx_test::{new, pool, setup_if_needed};

#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;

let value = ansi::generic_it_connects(&mut conn).await?;

assert_eq!(2i32, value);

Ok(())
}
Loading