Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new type support in query_as #2369

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion sqlx-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ migrate = ["sqlx-core/migrate"]
offline = ["sqlx-core/offline"]

# Type integration features which require additional dependencies
rust_decimal = ["dep:rust_decimal", "dep:num-bigint"]
rust_decimal = ["dep:rust_decimal"]
bigdecimal = ["dep:bigdecimal", "dep:num-bigint"]

[dependencies]
# Futures crates
Expand Down
35 changes: 35 additions & 0 deletions tests/postgres/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,41 @@ 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);

impl From<i32> for NewType {
fn from(value: i32) -> Self {
NewType(value)
}
}

let mut conn = new::<Postgres>().await.unwrap();

struct NewTypeRow {
id: NewType,
}

let res = sqlx::query_as!(NewTypeRow, r#"SELECT 1 as "id!""#)
.fetch_one(&mut conn)
.await
.unwrap();
assert_eq!(res.id.0, 1);

struct NormalRow {
id: i32,
}

let res = sqlx::query_as!(NormalRow, r#"SELECT 1 as "id!""#)
.fetch_one(&mut conn)
.await
.unwrap();

assert_eq!(res.id, 1);
}

#[cfg(feature = "macros")]
#[sqlx_macros::test]
async fn test_from_row() -> anyhow::Result<()> {
Expand Down