Skip to content

Commit

Permalink
feat: implement BIT support in MSSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
janaakhterov committed Jun 16, 2020
1 parent 956721c commit 80d5451
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sqlx-core/src/mssql/protocol/type_info.rs
Expand Up @@ -491,6 +491,7 @@ impl TypeInfo {
DataType::BigInt => s.push_str("bigint"),
DataType::Real => s.push_str("real"),
DataType::Float => s.push_str("float"),
DataType::Bit => s.push_str("bit"),

DataType::IntN => s.push_str(match self.size {
1 => "tinyint",
Expand Down Expand Up @@ -536,6 +537,10 @@ impl TypeInfo {
}
}

DataType::BitN => {
s.push_str("bit");
}

_ => unimplemented!("fmt: unsupported data type {:?}", self.ty),
}
}
Expand Down
30 changes: 30 additions & 0 deletions sqlx-core/src/mssql/types/bool.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<Mssql> for bool {
fn type_info() -> MssqlTypeInfo {
MssqlTypeInfo(TypeInfo::new(DataType::BitN, 1))
}
}

impl Encode<'_, Mssql> for bool {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
buf.push(if *self { 1 } else { 0 });

IsNull::No
}
}

impl Decode<'_, Mssql> for bool {
fn accepts(ty: &MssqlTypeInfo) -> bool {
matches!(ty.0.ty, DataType::Bit | DataType::BitN)
}

fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(value.as_bytes()?[0] == 1)
}
}
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/types/mod.rs
Expand Up @@ -2,6 +2,7 @@ use crate::encode::{Encode, IsNull};
use crate::mssql::protocol::type_info::{DataType, TypeInfo};
use crate::mssql::{Mssql, MssqlTypeInfo};

mod bool;
mod float;
mod int;
mod str;
Expand Down
6 changes: 6 additions & 0 deletions tests/mssql/types.rs
Expand Up @@ -35,3 +35,9 @@ test_type!(str<String>(Mssql,
"'this is foo'" == "this is foo",
"''" == "",
));

test_type!(bool(
Mssql,
"CAST(1 as BIT)" == true,
"CAST(0 as BIT)" == false
));

0 comments on commit 80d5451

Please sign in to comment.