From 6198e62d6150732394110f0d7aa799faa8d55921 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 23 Feb 2023 00:48:59 +0100 Subject: [PATCH 1/3] fix: sqlx-postgres bigdecimal feature --- sqlx-postgres/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlx-postgres/Cargo.toml b/sqlx-postgres/Cargo.toml index 987540845d..aaf01f22a6 100644 --- a/sqlx-postgres/Cargo.toml +++ b/sqlx-postgres/Cargo.toml @@ -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 From ce88ca30437adc4f44dfc39de7f3927e0d0ca96c Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 23 Feb 2023 00:53:07 +0100 Subject: [PATCH 2/3] feat: support new types in `sqlx::query_as!` --- sqlx-macros-core/src/query/output.rs | 2 +- tests/postgres/derives.rs | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/sqlx-macros-core/src/query/output.rs b/sqlx-macros-core/src/query/output.rs index f7d56646dd..2c838581b9 100644 --- a/sqlx-macros-core/src/query/output.rs +++ b/sqlx-macros-core/src/query/output.rs @@ -144,7 +144,7 @@ pub fn quote_query_as( // 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)?; ), diff --git a/tests/postgres/derives.rs b/tests/postgres/derives.rs index 5d53f3f351..ea5a6bb92e 100644 --- a/tests/postgres/derives.rs +++ b/tests/postgres/derives.rs @@ -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::().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<()> { From 54058521e7773066c30ae35ec8156813dcef675b Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 23 Feb 2023 13:49:44 +0100 Subject: [PATCH 3/3] fix test --- tests/postgres/derives.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/tests/postgres/derives.rs b/tests/postgres/derives.rs index ea5a6bb92e..c49682b870 100644 --- a/tests/postgres/derives.rs +++ b/tests/postgres/derives.rs @@ -378,21 +378,21 @@ SELECT $1 = ROW('fuzzy dice', 42, 199)::inventory_item, $1 #[sqlx_macros::test] async fn test_new_type() { struct NewType(i32); - let conn = new::().await.unwrap(); - conn.execute("CREATE TABLE new_type (id INTEGER)") - .await - .unwrap(); - conn.execute("INSERT INTO new_type (id) VALUES (1)") - .await - .unwrap(); + impl From for NewType { + fn from(value: i32) -> Self { + NewType(value) + } + } + + let mut conn = new::().await.unwrap(); struct NewTypeRow { id: NewType, } - let res = sqlx::query_as!(NewTypeRow, "SELECT id FROM new_type") - .fetch_one(&conn) + let res = sqlx::query_as!(NewTypeRow, r#"SELECT 1 as "id!""#) + .fetch_one(&mut conn) .await .unwrap(); assert_eq!(res.id.0, 1); @@ -401,17 +401,12 @@ async fn test_new_type() { id: i32, } - let res = sqlx::query_as!(NormalRow, "SELECT id FROM new_type") - .fetch_one(&conn) + let res = sqlx::query_as!(NormalRow, r#"SELECT 1 as "id!""#) + .fetch_one(&mut conn) .await .unwrap(); assert_eq!(res.id, 1); - - sqlx::query!("DROP TABLE new_type") - .execute(&conn) - .await - .unwrap(); } #[cfg(feature = "macros")]