From a1f2e6165b97404960dada12a5909bf3acd6dddb Mon Sep 17 00:00:00 2001 From: lovasoa Date: Thu, 7 Nov 2024 20:49:41 +0100 Subject: [PATCH] delegate unsigned codec to signed equivalents in postgres --- sqlx-core/src/postgres/types/int.rs | 47 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/sqlx-core/src/postgres/types/int.rs b/sqlx-core/src/postgres/types/int.rs index c962eee70d..6d91a4b0a4 100644 --- a/sqlx-core/src/postgres/types/int.rs +++ b/sqlx-core/src/postgres/types/int.rs @@ -130,85 +130,82 @@ impl Decode<'_, Postgres> for i64 { impl Type for u16 { fn type_info() -> PgTypeInfo { - PgTypeInfo::INT4 + >::type_info() } } impl PgHasArrayType for u16 { fn array_type_info() -> PgTypeInfo { - PgTypeInfo::INT4_ARRAY + ::array_type_info() } } impl Encode<'_, Postgres> for u16 { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { - buf.extend(&i32::from(*self).to_be_bytes()); - IsNull::No + let v = i32::from(*self); + >::encode_by_ref(&v, buf) } } impl Decode<'_, Postgres> for u16 { fn decode(value: PgValueRef<'_>) -> Result { - let decoded = match value.format() { - PgValueFormat::Binary => BigEndian::read_i32(value.as_bytes()?), - PgValueFormat::Text => value.as_str()?.parse::()?, - }; - Ok(u16::try_from(decoded)?) + let v = >::decode(value)?; + Ok(u16::try_from(v)?) } } impl Type for u32 { fn type_info() -> PgTypeInfo { - PgTypeInfo::INT8 + >::type_info() } } impl PgHasArrayType for u32 { fn array_type_info() -> PgTypeInfo { - PgTypeInfo::INT8_ARRAY + ::array_type_info() } } impl Encode<'_, Postgres> for u32 { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { - buf.extend(&i64::from(*self).to_be_bytes()); - IsNull::No + let v = i64::from(*self); + >::encode_by_ref(&v, buf) } } impl Decode<'_, Postgres> for u32 { fn decode(value: PgValueRef<'_>) -> Result { - let decoded = match value.format() { - PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?), - PgValueFormat::Text => value.as_str()?.parse::()?, - }; - Ok(u32::try_from(decoded)?) + let v = >::decode(value)?; + Ok(u32::try_from(v)?) } } impl Type for u64 { fn type_info() -> PgTypeInfo { - PgTypeInfo::NUMERIC + >::type_info() } } impl PgHasArrayType for u64 { fn array_type_info() -> PgTypeInfo { - PgTypeInfo::NUMERIC_ARRAY + ::array_type_info() } } impl Encode<'_, Postgres> for u64 { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { - let numeric_str = self.to_string(); - buf.extend(numeric_str.as_bytes()); - IsNull::No + let v = i64::try_from(*self).unwrap_or_else(|_| { + let v = i64::MAX; + log::warn!("cannot encode {self} as a signed postgres bigint, encoding as {v} instead"); + v + }); + >::encode_by_ref(&v, buf) } } impl Decode<'_, Postgres> for u64 { fn decode(value: PgValueRef<'_>) -> Result { - let decoded = value.as_str()?.parse::()?; - Ok(decoded) + let v = >::decode(value)?; + Ok(u64::try_from(v)?) } }