From 1752fa02ca92c1a26a0864ea235ee3247b9bdbb2 Mon Sep 17 00:00:00 2001 From: Markus Ast Date: Wed, 15 Nov 2023 13:29:34 +0100 Subject: [PATCH] add support for f32 and f64 --- postgres-macros/src/sql.rs | 2 + postgres/src/types.rs | 2 + postgres/tests/primitive_type_test.rs | 54 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/postgres-macros/src/sql.rs b/postgres-macros/src/sql.rs index 0e47705..0a21436 100644 --- a/postgres-macros/src/sql.rs +++ b/postgres-macros/src/sql.rs @@ -384,6 +384,8 @@ fn postgres_to_rust_type( ty if ::accepts(ty) => Some((quote!(String), quote!(str))), ty if ::accepts(ty) => Some((quote!(i64), quote!(i64))), ty if ::accepts(ty) => Some((quote!(i32), quote!(i32))), + ty if ::accepts(ty) => Some((quote!(f64), quote!(f64))), + ty if ::accepts(ty) => Some((quote!(f32), quote!(f32))), ty if ::accepts(ty) => Some((quote!(bool), quote!(bool))), ty if as FromSql>::accepts(ty) => Some((quote!(Vec), quote!([u8]))), diff --git a/postgres/src/types.rs b/postgres/src/types.rs index 1dc1b43..f00da24 100644 --- a/postgres/src/types.rs +++ b/postgres/src/types.rs @@ -110,6 +110,8 @@ macro_rules! impl_type { impl_type!(i32); impl_type!(i64); +impl_type!(f32); +impl_type!(f64); impl_type!(bool); impl_type!(String); #[cfg(feature = "json")] diff --git a/postgres/tests/primitive_type_test.rs b/postgres/tests/primitive_type_test.rs index 339123a..fea0bdf 100644 --- a/postgres/tests/primitive_type_test.rs +++ b/postgres/tests/primitive_type_test.rs @@ -104,6 +104,60 @@ mod i32 { } } +mod f32 { + use super::*; + + #[tokio::test] + async fn test_f32() { + let expected = 42.0f32; + let val: f32 = sql!("SELECT {expected}::FLOAT4").await.unwrap(); + assert_eq!(val, expected); + } + + #[tokio::test] + async fn test_f32_option() { + let expected = 42.0f32; + let val: Option = sql!("SELECT {expected}::FLOAT4").await.unwrap(); + assert_eq!(val, Some(expected)); + let val: Option = sql!("SELECT NULL::FLOAT4").await.unwrap(); + assert_eq!(val, None); + } + + #[tokio::test] + async fn test_f32_vec() { + let expected = vec![4.0f32, 2.0f32]; + let val: Vec = sql!("SELECT {expected}::FLOAT4[]").await.unwrap(); + assert_eq!(val, expected); + } +} + +mod f64 { + use super::*; + + #[tokio::test] + async fn test_f64() { + let expected = 42.0f64; + let val: f64 = sql!("SELECT {expected}::FLOAT8").await.unwrap(); + assert_eq!(val, expected); + } + + #[tokio::test] + async fn test_f64_option() { + let expected = 42.0f64; + let val: Option = sql!("SELECT {expected}::FLOAT8").await.unwrap(); + assert_eq!(val, Some(expected)); + let val: Option = sql!("SELECT NULL::FLOAT8").await.unwrap(); + assert_eq!(val, None); + } + + #[tokio::test] + async fn test_f64_vec() { + let expected = vec![4.0f64, 2.0f64]; + let val: Vec = sql!("SELECT {expected}::FLOAT8[]").await.unwrap(); + assert_eq!(val, expected); + } +} + mod bool { use super::*;