allow slices to be passed to query!()

capture args by ref
This commit is contained in:
Austin Bonander 2019-12-27 23:20:01 -08:00
parent 98269ddd20
commit 2c424b6d63
5 changed files with 33 additions and 4 deletions

View File

@ -1,7 +1,7 @@
impl_database_ext! {
sqlx::MySql {
bool,
String | &str,
String,
i16,
i32,
i64,

View File

@ -1,7 +1,7 @@
impl_database_ext! {
sqlx::Postgres {
bool,
String | &str,
String,
i16,
i32,
i64,

View File

@ -61,7 +61,7 @@ pub fn quote_args<DB: DatabaseExt>(
let args = input.args.iter();
Ok(quote! {
let args = (#(#args),*,);
let args = (#(&#args),*,);
#args_check
})
}

View File

@ -27,10 +27,19 @@ impl<T> TyCons<Option<&'_ T>> {
}
}
impl<T> TyConsExt for TyCons<&'_ T> {
// no overlap with the following impls because of the `: Sized` bound
impl<T: Sized> TyConsExt for TyCons<&'_ T> {
type Cons = T;
}
impl TyConsExt for TyCons<&'_ str> {
type Cons = String;
}
impl<T> TyConsExt for TyCons<&'_ [T]> {
type Cons = Vec<T>;
}
impl<T> TyConsExt for TyCons<Option<T>> {
type Cons = T;
}

View File

@ -64,6 +64,26 @@ async fn test_query_file_as() -> sqlx::Result<()> {
Ok(())
}
#[async_std::test]
async fn query_by_string() -> sqlx::Result<()> {
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
let string = "Hello, world!".to_string();
let result = sqlx::query!(
"SELECT * from (VALUES('Hello, world!')) strings(string)\
where string = $1 or string = $2",
string,
string[..]
)
.fetch_one(&mut conn)
.await?;
assert_eq!(result.string, string);
Ok(())
}
#[async_std::test]
async fn test_nullable_err() -> sqlx::Result<()> {
#[derive(Debug)]