mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 15:55:45 +00:00
tweak type decoding in text mode for floats
This commit is contained in:
parent
4c102f7b0e
commit
f18ab2fecb
@ -6,8 +6,8 @@ use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, Tim
|
|||||||
use crate::decode::Decode;
|
use crate::decode::Decode;
|
||||||
use crate::encode::Encode;
|
use crate::encode::Encode;
|
||||||
use crate::postgres::protocol::TypeId;
|
use crate::postgres::protocol::TypeId;
|
||||||
use crate::postgres::types::PgTypeInfo;
|
|
||||||
use crate::postgres::row::PgValue;
|
use crate::postgres::row::PgValue;
|
||||||
|
use crate::postgres::types::PgTypeInfo;
|
||||||
use crate::postgres::Postgres;
|
use crate::postgres::Postgres;
|
||||||
use crate::types::Type;
|
use crate::types::Type;
|
||||||
|
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
|
use std::convert::TryInto;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use byteorder::{ReadBytesExt, NetworkEndian};
|
||||||
|
|
||||||
use crate::decode::Decode;
|
use crate::decode::Decode;
|
||||||
use crate::encode::Encode;
|
use crate::encode::Encode;
|
||||||
|
use crate::error::Error;
|
||||||
use crate::postgres::protocol::TypeId;
|
use crate::postgres::protocol::TypeId;
|
||||||
use crate::postgres::types::PgTypeInfo;
|
use crate::postgres::types::PgTypeInfo;
|
||||||
use crate::postgres::{PgValue, Postgres};
|
use crate::postgres::{PgValue, Postgres};
|
||||||
@ -25,7 +31,14 @@ impl Encode<Postgres> for f32 {
|
|||||||
|
|
||||||
impl<'de> Decode<'de, Postgres> for f32 {
|
impl<'de> Decode<'de, Postgres> for f32 {
|
||||||
fn decode(value: Option<PgValue<'de>>) -> crate::Result<Self> {
|
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 {
|
impl<'de> Decode<'de, Postgres> for f64 {
|
||||||
fn decode(value: Option<PgValue<'de>>) -> crate::Result<Self> {
|
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),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::decode::Decode;
|
use crate::decode::Decode;
|
||||||
use crate::encode::Encode;
|
use crate::encode::Encode;
|
||||||
use crate::postgres::row::PgValue;
|
|
||||||
use crate::postgres::protocol::TypeId;
|
use crate::postgres::protocol::TypeId;
|
||||||
|
use crate::postgres::row::PgValue;
|
||||||
use crate::postgres::types::PgTypeInfo;
|
use crate::postgres::types::PgTypeInfo;
|
||||||
use crate::postgres::Postgres;
|
use crate::postgres::Postgres;
|
||||||
use crate::types::Type;
|
use crate::types::Type;
|
||||||
|
@ -62,7 +62,8 @@ pub(crate) fn expand_derive_decode(input: DeriveInput) -> syn::Result<proc_macro
|
|||||||
if cfg!(feature = "postgres") {
|
if cfg!(feature = "postgres") {
|
||||||
let mut generics = generics.clone();
|
let mut generics = generics.clone();
|
||||||
generics.params.insert(0, parse_quote!('de));
|
generics.params.insert(0, parse_quote!('de));
|
||||||
generics.make_where_clause()
|
generics
|
||||||
|
.make_where_clause()
|
||||||
.predicates
|
.predicates
|
||||||
.push(parse_quote!(#ty: sqlx::decode::Decode<'de, sqlx::Postgres>));
|
.push(parse_quote!(#ty: sqlx::decode::Decode<'de, sqlx::Postgres>));
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use sqlx::{PgConnection, Connect, Row};
|
|
||||||
use sqlx::postgres::PgRow;
|
use sqlx::postgres::PgRow;
|
||||||
|
use sqlx::{Connect, PgConnection, Row};
|
||||||
|
|
||||||
async fn connect() -> anyhow::Result<PgConnection> {
|
async fn connect() -> anyhow::Result<PgConnection> {
|
||||||
Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?)
|
Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?)
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use sqlx::{
|
|
||||||
postgres::PgConnection, Connect, Executor, Row,
|
|
||||||
};
|
|
||||||
use std::time::Duration;
|
|
||||||
use sqlx::postgres::{PgPool, PgRow};
|
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-async-std", async_std::test)]
|
||||||
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user