tweak type decoding in text mode for floats

This commit is contained in:
Ryan Leckey 2020-03-01 15:17:49 -08:00
parent 4c102f7b0e
commit f18ab2fecb
6 changed files with 29 additions and 10 deletions

View File

@ -6,8 +6,8 @@ use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, Tim
use crate::decode::Decode;
use crate::encode::Encode;
use crate::postgres::protocol::TypeId;
use crate::postgres::types::PgTypeInfo;
use crate::postgres::row::PgValue;
use crate::postgres::types::PgTypeInfo;
use crate::postgres::Postgres;
use crate::types::Type;

View File

@ -1,5 +1,11 @@
use std::convert::TryInto;
use std::str::FromStr;
use byteorder::{ReadBytesExt, NetworkEndian};
use crate::decode::Decode;
use crate::encode::Encode;
use crate::error::Error;
use crate::postgres::protocol::TypeId;
use crate::postgres::types::PgTypeInfo;
use crate::postgres::{PgValue, Postgres};
@ -25,7 +31,14 @@ impl Encode<Postgres> for f32 {
impl<'de> Decode<'de, Postgres> for f32 {
fn decode(value: Option<PgValue<'de>>) -> crate::Result<Self> {
<i32 as Decode<Postgres>>::decode(value).map(|value| f32::from_bits(value as u32))
match value.try_into()? {
PgValue::Binary(mut buf) => buf
.read_i32::<NetworkEndian>()
.map_err(Error::decode)
.map(|value| f32::from_bits(value as u32)),
PgValue::Text(s) => f32::from_str(s).map_err(Error::decode),
}
}
}
@ -49,6 +62,13 @@ impl Encode<Postgres> for f64 {
impl<'de> Decode<'de, Postgres> for f64 {
fn decode(value: Option<PgValue<'de>>) -> crate::Result<Self> {
<i64 as Decode<Postgres>>::decode(value).map(|value| f64::from_bits(value as u64))
match value.try_into()? {
PgValue::Binary(mut buf) => buf
.read_i64::<NetworkEndian>()
.map_err(Error::decode)
.map(|value| f64::from_bits(value as u64)),
PgValue::Text(s) => f64::from_str(s).map_err(Error::decode),
}
}
}

View File

@ -5,8 +5,8 @@ use uuid::Uuid;
use crate::decode::Decode;
use crate::encode::Encode;
use crate::postgres::row::PgValue;
use crate::postgres::protocol::TypeId;
use crate::postgres::row::PgValue;
use crate::postgres::types::PgTypeInfo;
use crate::postgres::Postgres;
use crate::types::Type;

View File

@ -62,7 +62,8 @@ pub(crate) fn expand_derive_decode(input: DeriveInput) -> syn::Result<proc_macro
if cfg!(feature = "postgres") {
let mut generics = generics.clone();
generics.params.insert(0, parse_quote!('de));
generics.make_where_clause()
generics
.make_where_clause()
.predicates
.push(parse_quote!(#ty: sqlx::decode::Decode<'de, sqlx::Postgres>));

View File

@ -1,5 +1,5 @@
use sqlx::{PgConnection, Connect, Row};
use sqlx::postgres::PgRow;
use sqlx::{Connect, PgConnection, Row};
async fn connect() -> anyhow::Result<PgConnection> {
Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?)

View File

@ -1,9 +1,7 @@
use futures::TryStreamExt;
use sqlx::{
postgres::PgConnection, Connect, Executor, Row,
};
use std::time::Duration;
use sqlx::postgres::{PgPool, PgRow};
use sqlx::{postgres::PgConnection, Connect, Executor, Row};
use std::time::Duration;
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]