Skip to content
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

fix: mssql uses unsigned for tinyint instead of signed #2074

Merged
merged 1 commit into from
Sep 1, 2022
Merged
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
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
fn encode(self, buf: &mut Vec<u8>) -> IsNull {
Expand Down
30 changes: 30 additions & 0 deletions sqlx-core/src/mssql/types/uint.rs
Original file line number Diff line number Diff line change
@@ -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<Mssql> 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<u8>) -> IsNull {
buf.extend(&self.to_le_bytes());

IsNull::No
}
}

impl Decode<'_, Mssql> for u8 {
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(value.as_bytes()?[0] as u8)
}
}
7 changes: 7 additions & 0 deletions tests/mssql/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ test_type!(null<Option<i32>>(Mssql,
"CAST(NULL as INT)" == None::<i32>
));

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,
Expand Down