Skip to content

Commit ce88ca3

Browse files
committed
feat: support new types in sqlx::query_as!
1 parent 6198e62 commit ce88ca3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

sqlx-macros-core/src/query/output.rs

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

tests/postgres/derives.rs

+40
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,46 @@ SELECT $1 = ROW('fuzzy dice', 42, 199)::inventory_item, $1
374374
Ok(())
375375
}
376376

377+
#[cfg(feature = "macros")]
378+
#[sqlx_macros::test]
379+
async fn test_new_type() {
380+
struct NewType(i32);
381+
let conn = new::<Postgres>().await.unwrap();
382+
conn.execute("CREATE TABLE new_type (id INTEGER)")
383+
.await
384+
.unwrap();
385+
386+
conn.execute("INSERT INTO new_type (id) VALUES (1)")
387+
.await
388+
.unwrap();
389+
390+
struct NewTypeRow {
391+
id: NewType,
392+
}
393+
394+
let res = sqlx::query_as!(NewTypeRow, "SELECT id FROM new_type")
395+
.fetch_one(&conn)
396+
.await
397+
.unwrap();
398+
assert_eq!(res.id.0, 1);
399+
400+
struct NormalRow {
401+
id: i32,
402+
}
403+
404+
let res = sqlx::query_as!(NormalRow, "SELECT id FROM new_type")
405+
.fetch_one(&conn)
406+
.await
407+
.unwrap();
408+
409+
assert_eq!(res.id, 1);
410+
411+
sqlx::query!("DROP TABLE new_type")
412+
.execute(&conn)
413+
.await
414+
.unwrap();
415+
}
416+
377417
#[cfg(feature = "macros")]
378418
#[sqlx_macros::test]
379419
async fn test_from_row() -> anyhow::Result<()> {

0 commit comments

Comments
 (0)