feat: support new types in sqlx::query_as!

This commit is contained in:
Vladimir 2023-02-23 00:53:07 +01:00 committed by Austin Bonander
parent d81b6fc08d
commit 4b8fa7a4d7
2 changed files with 41 additions and 1 deletions

View File

@ -144,7 +144,7 @@ pub fn quote_query_as<DB: DatabaseExt>(
// binding to a `let` avoids confusing errors about
// "try expression alternatives have incompatible types"
// it doesn't seem to hurt inference in the other branches
let #var_name = row.try_get_unchecked::<#type_, _>(#i)?;
let #var_name = row.try_get_unchecked::<#type_, _>(#i)?.into();
},
// type was overridden to be a wildcard so we fallback to the runtime check
(true, ColumnType::Wildcard) => quote! ( let #var_name = row.try_get(#i)?; ),

View File

@ -374,6 +374,46 @@ SELECT $1 = ROW('fuzzy dice', 42, 199)::inventory_item, $1
Ok(())
}
#[cfg(feature = "macros")]
#[sqlx_macros::test]
async fn test_new_type() {
struct NewType(i32);
let conn = new::<Postgres>().await.unwrap();
conn.execute("CREATE TABLE new_type (id INTEGER)")
.await
.unwrap();
conn.execute("INSERT INTO new_type (id) VALUES (1)")
.await
.unwrap();
struct NewTypeRow {
id: NewType,
}
let res = sqlx::query_as!(NewTypeRow, "SELECT id FROM new_type")
.fetch_one(&conn)
.await
.unwrap();
assert_eq!(res.id.0, 1);
struct NormalRow {
id: i32,
}
let res = sqlx::query_as!(NormalRow, "SELECT id FROM new_type")
.fetch_one(&conn)
.await
.unwrap();
assert_eq!(res.id, 1);
sqlx::query!("DROP TABLE new_type")
.execute(&conn)
.await
.unwrap();
}
#[cfg(feature = "macros")]
#[sqlx_macros::test]
async fn test_from_row() -> anyhow::Result<()> {