Skip to content

Fix encoding & decoding large unsigned u32 & u16 ints in MSSQL. #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sqlx-core/src/mssql/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Encode<'_, Mssql> for i8 {
impl Decode<'_, Mssql> for i8 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
let i64_val = <i64 as Decode<Mssql>>::decode(value)?;
convert_integer::<Self>(i64_val)
Ok(convert_integer::<u8>(i64_val)? as Self)
}
}

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Encode<'_, Mssql> for i16 {
impl Decode<'_, Mssql> for i16 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
let i64_val = <i64 as Decode<Mssql>>::decode(value)?;
convert_integer::<Self>(i64_val)
Ok(convert_integer::<u16>(i64_val)? as Self)
}
}

Expand All @@ -83,7 +83,7 @@ impl Encode<'_, Mssql> for i32 {
impl Decode<'_, Mssql> for i32 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
let i64_val = <i64 as Decode<Mssql>>::decode(value)?;
convert_integer::<Self>(i64_val)
Ok(convert_integer::<u32>(i64_val)? as Self)
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ fn decode_numeric(bytes: &[u8], _precision: u8, mut scale: u8) -> Result<i64, Bo
Ok(n * if negative { -1 } else { 1 })
}

fn convert_integer<T>(i64_val: i64) -> Result<T, BoxDynError>
pub(super) fn convert_integer<T>(i64_val: i64) -> Result<T, BoxDynError>
where
T: TryFrom<i64>,
T::Error: std::error::Error + Send + Sync + 'static,
Expand Down
23 changes: 7 additions & 16 deletions sqlx-core/src/mssql/types/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ impl Type<Mssql> for u16 {

impl Encode<'_, Mssql> for u16 {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
let v = i16::try_from(*self).unwrap_or_else(|_e| {
log::warn!("cannot encode {self} as a signed mssql smallint");
i16::MAX
});
let v = *self as i16;
<i16 as Encode<'_, Mssql>>::encode_by_ref(&v, buf)
}
}

impl Decode<'_, Mssql> for u16 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
let v = <i16 as Decode<'_, Mssql>>::decode(value)?;
Ok(u16::try_from(v)?)
let i64_val = <i64 as Decode<Mssql>>::decode(value)?;
super::int::convert_integer::<Self>(i64_val)
}
}

Expand All @@ -70,18 +67,15 @@ impl Type<Mssql> for u32 {

impl Encode<'_, Mssql> for u32 {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
let v = i32::try_from(*self).unwrap_or_else(|_e| {
log::warn!("cannot encode {self} as a signed mssql int");
i32::MAX
});
let v = *self as i32;
<i32 as Encode<'_, Mssql>>::encode_by_ref(&v, buf)
}
}

impl Decode<'_, Mssql> for u32 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
let v = <i32 as Decode<'_, Mssql>>::decode(value)?;
Ok(u32::try_from(v)?)
let i64_val = <i64 as Decode<Mssql>>::decode(value)?;
super::int::convert_integer::<Self>(i64_val)
}
}

Expand All @@ -97,10 +91,7 @@ impl Type<Mssql> for u64 {

impl Encode<'_, Mssql> for u64 {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
let v = i64::try_from(*self).unwrap_or_else(|_e| {
log::warn!("cannot encode {self} as a signed mssql bigint");
i64::MAX
});
let v = *self as i64;
<i64 as Encode<'_, Mssql>>::encode_by_ref(&v, buf)
}
}
Expand Down
Loading