mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-04 08:15:39 +00:00
Merge pull request #413 from launchbadge/feature/mssql-bit-support
Feature: implement `BIT` support in MSSQL
This commit is contained in:
commit
6a26e341fb
@ -491,6 +491,7 @@ impl TypeInfo {
|
|||||||
DataType::BigInt => s.push_str("bigint"),
|
DataType::BigInt => s.push_str("bigint"),
|
||||||
DataType::Real => s.push_str("real"),
|
DataType::Real => s.push_str("real"),
|
||||||
DataType::Float => s.push_str("float"),
|
DataType::Float => s.push_str("float"),
|
||||||
|
DataType::Bit => s.push_str("bit"),
|
||||||
|
|
||||||
DataType::IntN => s.push_str(match self.size {
|
DataType::IntN => s.push_str(match self.size {
|
||||||
1 => "tinyint",
|
1 => "tinyint",
|
||||||
@ -536,6 +537,10 @@ impl TypeInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DataType::BitN => {
|
||||||
|
s.push_str("bit");
|
||||||
|
}
|
||||||
|
|
||||||
_ => unimplemented!("fmt: unsupported data type {:?}", self.ty),
|
_ => 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::protocol::type_info::{DataType, TypeInfo};
|
||||||
use crate::mssql::{Mssql, MssqlTypeInfo};
|
use crate::mssql::{Mssql, MssqlTypeInfo};
|
||||||
|
|
||||||
|
mod bool;
|
||||||
mod float;
|
mod float;
|
||||||
mod int;
|
mod int;
|
||||||
mod str;
|
mod str;
|
||||||
|
@ -35,3 +35,9 @@ test_type!(str<String>(Mssql,
|
|||||||
"'this is foo'" == "this is foo",
|
"'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