From 9de70d2e7aed97a79d8d006208474db48636e342 Mon Sep 17 00:00:00 2001 From: Tobias Tschinkowitz Date: Thu, 1 Sep 2022 03:10:29 +0200 Subject: [PATCH] fix: mssql uses unsigned for tinyint instead of signed (#2074) --- sqlx-core/src/mssql/types/mod.rs | 1 + sqlx-core/src/mssql/types/uint.rs | 30 ++++++++++++++++++++++++++++++ tests/mssql/types.rs | 7 +++++++ 3 files changed, 38 insertions(+) create mode 100644 sqlx-core/src/mssql/types/uint.rs diff --git a/sqlx-core/src/mssql/types/mod.rs b/sqlx-core/src/mssql/types/mod.rs index 148eaeae85..0d90e375b4 100644 --- a/sqlx-core/src/mssql/types/mod.rs +++ b/sqlx-core/src/mssql/types/mod.rs @@ -6,6 +6,7 @@ mod bool; mod float; mod int; mod str; +mod uint; impl<'q, T: 'q + Encode<'q, Mssql>> Encode<'q, Mssql> for Option { fn encode(self, buf: &mut Vec) -> IsNull { diff --git a/sqlx-core/src/mssql/types/uint.rs b/sqlx-core/src/mssql/types/uint.rs new file mode 100644 index 0000000000..a36cae3a6c --- /dev/null +++ b/sqlx-core/src/mssql/types/uint.rs @@ -0,0 +1,30 @@ +use crate::decode::Decode; +use crate::encode::{Encode, IsNull}; +use crate::error::BoxDynError; +use crate::mssql::protocol::type_info::{DataType, TypeInfo}; +use crate::mssql::{Mssql, MssqlTypeInfo, MssqlValueRef}; +use crate::types::Type; + +impl Type for u8 { + fn type_info() -> MssqlTypeInfo { + MssqlTypeInfo(TypeInfo::new(DataType::IntN, 1)) + } + + fn compatible(ty: &MssqlTypeInfo) -> bool { + matches!(ty.0.ty, DataType::TinyInt | DataType::IntN) && ty.0.size == 1 + } +} + +impl Encode<'_, Mssql> for u8 { + fn encode_by_ref(&self, buf: &mut Vec) -> IsNull { + buf.extend(&self.to_le_bytes()); + + IsNull::No + } +} + +impl Decode<'_, Mssql> for u8 { + fn decode(value: MssqlValueRef<'_>) -> Result { + Ok(value.as_bytes()?[0] as u8) + } +} diff --git a/tests/mssql/types.rs b/tests/mssql/types.rs index c3bb1076a5..3e9ae7395b 100644 --- a/tests/mssql/types.rs +++ b/tests/mssql/types.rs @@ -5,6 +5,13 @@ test_type!(null>(Mssql, "CAST(NULL as INT)" == None:: )); +test_type!(u8( + Mssql, + "CAST(5 AS TINYINT)" == 5_u8, + "CAST(0 AS TINYINT)" == 0_u8, + "CAST(255 AS TINYINT)" == 255_u8, +)); + test_type!(i8( Mssql, "CAST(5 AS TINYINT)" == 5_i8,