Add the array types to the query! macro

This still needs some work, as the inferance and error messages are not
quite where we want them to be
This commit is contained in:
Oliver Bøving 2020-02-25 21:56:07 +01:00
parent 11b36fc7e5
commit 0de4b5186b
3 changed files with 41 additions and 0 deletions

View file

@ -30,6 +30,11 @@ impl HasSqlType<String> for Postgres {
<Self as HasSqlType<str>>::type_info()
}
}
impl HasSqlType<[String]> for Postgres {
fn type_info() -> PgTypeInfo {
<Self as HasSqlType<[&'_ str]>>::type_info()
}
}
impl HasSqlType<Vec<String>> for Postgres {
fn type_info() -> PgTypeInfo {
<Self as HasSqlType<Vec<&'_ str>>>::type_info()

View file

@ -25,6 +25,16 @@ impl_database_ext! {
#[cfg(feature = "chrono")]
sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc> | sqlx::types::chrono::DateTime<_>,
// Arrays
Vec<bool>, [bool],
Vec<String>, [String],
Vec<i16>, [i16],
Vec<i32>, [i32],
Vec<i64>, [i64],
Vec<f32>, [f32],
Vec<f64>, [f64],
},
ParamChecking::Strong
}

View file

@ -176,6 +176,32 @@ async fn test_many_args() -> anyhow::Result<()> {
Ok(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_array_from_slice() -> anyhow::Result<()> {
let mut conn = connect().await?;
let list: &[i32] = &[1, 2, 3, 4i32];
let result = sqlx::query!("SELECT $1::int[] as my_array", *list)
.fetch_one(&mut conn)
.await?;
assert_eq!(result.my_array, vec![1, 2, 3, 4]);
println!("result ID: {:?}", result.my_array);
let account = sqlx::query!("SELECT ARRAY[4,3,2,1] as my_array")
.fetch_one(&mut conn)
.await?;
assert_eq!(account.my_array, vec![4, 3, 2, 1]);
println!("account ID: {:?}", account.my_array);
Ok(())
}
async fn connect() -> anyhow::Result<PgConnection> {
let _ = dotenv::dotenv();
let _ = env_logger::try_init();