Skip to content

Commit

Permalink
feat: support new types in sqlx::query_as!
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef authored and Aandreba committed Mar 31, 2023
1 parent cb6126e commit 30127eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqlx-macros-core/src/query/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?; ),
Expand Down
40 changes: 40 additions & 0 deletions tests/postgres/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down

0 comments on commit 30127eb

Please sign in to comment.