mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-09-28 13:31:41 +00:00
feat: implement BIT
support in MSSQL
This commit is contained in:
parent
956721c96c
commit
80d545109b
@ -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",
|
||||
@ -536,6 +537,10 @@ impl TypeInfo {
|
||||
}
|
||||
}
|
||||
|
||||
DataType::BitN => {
|
||||
s.push_str("bit");
|
||||
}
|
||||
|
||||
_ => unimplemented!("fmt: unsupported data type {:?}", self.ty),
|
||||
}
|
||||
}
|
||||
|
30
sqlx-core/src/mssql/types/bool.rs
Normal file
30
sqlx-core/src/mssql/types/bool.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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
|
||||
));
|
||||
|
Loading…
x
Reference in New Issue
Block a user