welds_sqlx_mssql/types/
str.rs1use crate::decode::Decode;
2use crate::encode::{Encode, IsNull};
3use crate::error::BoxDynError;
4use crate::io::MssqlBufMutExt;
5use crate::protocol::type_info::{Collation, CollationFlags, DataType, TypeInfo};
6use crate::{Mssql, MssqlTypeInfo, MssqlValueRef};
7use sqlx_core::types::Type;
8use std::borrow::Cow;
9
10impl Type<Mssql> for str {
11 fn type_info() -> MssqlTypeInfo {
12 MssqlTypeInfo(TypeInfo::new(DataType::NVarChar, 0))
13 }
14
15 fn compatible(ty: &MssqlTypeInfo) -> bool {
16 matches!(
17 ty.0.ty,
18 DataType::NVarChar
19 | DataType::NChar
20 | DataType::BigVarChar
21 | DataType::VarChar
22 | DataType::BigChar
23 | DataType::Char
24 )
25 }
26}
27
28impl Type<Mssql> for String {
29 fn type_info() -> MssqlTypeInfo {
30 <str as Type<Mssql>>::type_info()
31 }
32
33 fn compatible(ty: &MssqlTypeInfo) -> bool {
34 <str as Type<Mssql>>::compatible(ty)
35 }
36}
37
38impl Encode<'_, Mssql> for &'_ str {
39 fn produces(&self) -> Option<MssqlTypeInfo> {
40 Some(MssqlTypeInfo(TypeInfo {
42 ty: DataType::NVarChar,
43 size: ((self.len() * 2) as u32).max(2),
44 scale: 0,
45 precision: 0,
46 collation: Some(Collation {
47 locale: 1033,
48 flags: CollationFlags::IGNORE_CASE
49 | CollationFlags::IGNORE_WIDTH
50 | CollationFlags::IGNORE_KANA,
51 sort: 52,
52 version: 0,
53 }),
54 }))
55 }
56
57 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
58 buf.put_utf16_str(self);
59
60 IsNull::No
61 }
62}
63
64impl Encode<'_, Mssql> for String {
65 fn produces(&self) -> Option<MssqlTypeInfo> {
66 <&str as Encode<Mssql>>::produces(&self.as_str())
67 }
68
69 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
70 <&str as Encode<Mssql>>::encode_by_ref(&self.as_str(), buf)
71 }
72}
73
74impl Decode<'_, Mssql> for String {
75 fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
76 Ok(value
77 .type_info
78 .0
79 .encoding()?
80 .decode_without_bom_handling(value.as_bytes()?)
81 .0
82 .into_owned())
83 }
84}
85
86impl Type<Mssql> for Cow<'_, str> {
87 fn type_info() -> MssqlTypeInfo {
88 <&str as Type<Mssql>>::type_info()
89 }
90
91 fn compatible(ty: &MssqlTypeInfo) -> bool {
92 <&str as Type<Mssql>>::compatible(ty)
93 }
94}
95
96impl Encode<'_, Mssql> for Cow<'_, str> {
97 fn produces(&self) -> Option<MssqlTypeInfo> {
98 match self {
99 Cow::Borrowed(str) => <&str as Encode<Mssql>>::produces(str),
100 Cow::Owned(str) => <&str as Encode<Mssql>>::produces(&(str.as_ref())),
101 }
102 }
103
104 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
105 match self {
106 Cow::Borrowed(str) => <&str as Encode<Mssql>>::encode_by_ref(str, buf),
107 Cow::Owned(str) => <&str as Encode<Mssql>>::encode_by_ref(&(str.as_ref()), buf),
108 }
109 }
110}
111
112impl<'r> Decode<'r, Mssql> for Cow<'r, str> {
113 fn decode(value: MssqlValueRef<'r>) -> Result<Self, BoxDynError> {
114 Ok(Cow::Owned(
115 value
116 .type_info
117 .0
118 .encoding()?
119 .decode_without_bom_handling(value.as_bytes()?)
120 .0
121 .into_owned(),
122 ))
123 }
124}