welds_sqlx_mssql/
value.rs

1use crate::error::{BoxDynError, UnexpectedNullError};
2use crate::{Mssql, MssqlTypeInfo};
3use sqlx_core::bytes::Bytes;
4use std::borrow::Cow;
5
6pub(crate) use sqlx_core::value::{Value, ValueRef};
7
8/// Implementation of [`ValueRef`] for MSSQL.
9#[derive(Clone)]
10pub struct MssqlValueRef<'r> {
11    pub(crate) type_info: MssqlTypeInfo,
12    pub(crate) data: Option<&'r Bytes>,
13}
14
15impl<'r> MssqlValueRef<'r> {
16    pub(crate) fn as_bytes(&self) -> Result<&'r [u8], BoxDynError> {
17        match &self.data {
18            Some(v) => Ok(v),
19            None => Err(UnexpectedNullError.into()),
20        }
21    }
22}
23
24impl ValueRef<'_> for MssqlValueRef<'_> {
25    type Database = Mssql;
26
27    fn to_owned(&self) -> MssqlValue {
28        MssqlValue {
29            data: self.data.cloned(),
30            type_info: self.type_info.clone(),
31        }
32    }
33
34    fn type_info(&self) -> Cow<'_, MssqlTypeInfo> {
35        Cow::Borrowed(&self.type_info)
36    }
37
38    fn is_null(&self) -> bool {
39        self.data.is_none() || self.type_info.0.is_null()
40    }
41}
42
43/// Implementation of [`Value`] for MSSQL.
44#[derive(Clone)]
45pub struct MssqlValue {
46    pub(crate) type_info: MssqlTypeInfo,
47    pub(crate) data: Option<Bytes>,
48}
49
50impl Value for MssqlValue {
51    type Database = Mssql;
52
53    fn as_ref(&self) -> MssqlValueRef<'_> {
54        MssqlValueRef {
55            data: self.data.as_ref(),
56            type_info: self.type_info.clone(),
57        }
58    }
59
60    fn type_info(&self) -> Cow<'_, MssqlTypeInfo> {
61        Cow::Borrowed(&self.type_info)
62    }
63
64    fn is_null(&self) -> bool {
65        self.data.is_none() || self.type_info.0.is_null()
66    }
67}