Skip to content

Commit

Permalink
add support for f32 and f64
Browse files Browse the repository at this point in the history
  • Loading branch information
rkusa committed Nov 15, 2023
1 parent 4875b97 commit 1752fa0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postgres-macros/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ fn postgres_to_rust_type(
ty if <String as FromSql>::accepts(ty) => Some((quote!(String), quote!(str))),
ty if <i64 as FromSql>::accepts(ty) => Some((quote!(i64), quote!(i64))),
ty if <i32 as FromSql>::accepts(ty) => Some((quote!(i32), quote!(i32))),
ty if <f64 as FromSql>::accepts(ty) => Some((quote!(f64), quote!(f64))),
ty if <f32 as FromSql>::accepts(ty) => Some((quote!(f32), quote!(f32))),
ty if <bool as FromSql>::accepts(ty) => Some((quote!(bool), quote!(bool))),
ty if <Vec<u8> as FromSql>::accepts(ty) => Some((quote!(Vec<u8>), quote!([u8]))),

Expand Down
2 changes: 2 additions & 0 deletions postgres/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
54 changes: 54 additions & 0 deletions postgres/tests/primitive_type_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> = sql!("SELECT {expected}::FLOAT4").await.unwrap();
assert_eq!(val, Some(expected));
let val: Option<f32> = 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<f32> = 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<f64> = sql!("SELECT {expected}::FLOAT8").await.unwrap();
assert_eq!(val, Some(expected));
let val: Option<f64> = 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<f64> = sql!("SELECT {expected}::FLOAT8[]").await.unwrap();
assert_eq!(val, expected);
}
}

mod bool {
use super::*;

Expand Down

0 comments on commit 1752fa0

Please sign in to comment.