1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::error::{BoxDynError, UnexpectedNullError};
use crate::mssql::{Mssql, MssqlTypeInfo};
use crate::value::{Value, ValueRef};
use bytes::Bytes;
use std::borrow::Cow;

/// Implementation of [`ValueRef`] for MSSQL.
#[derive(Clone)]
pub struct MssqlValueRef<'r> {
    pub(crate) type_info: MssqlTypeInfo,
    pub(crate) data: Option<&'r Bytes>,
}

impl<'r> MssqlValueRef<'r> {
    pub(crate) fn as_bytes(&self) -> Result<&'r [u8], BoxDynError> {
        match &self.data {
            Some(v) => Ok(v),
            None => Err(UnexpectedNullError.into()),
        }
    }
}

impl ValueRef<'_> for MssqlValueRef<'_> {
    type Database = Mssql;

    fn to_owned(&self) -> MssqlValue {
        MssqlValue {
            data: self.data.cloned(),
            type_info: self.type_info.clone(),
        }
    }

    fn type_info(&self) -> Cow<'_, MssqlTypeInfo> {
        Cow::Borrowed(&self.type_info)
    }

    fn is_null(&self) -> bool {
        self.data.is_none() || self.type_info.0.is_null()
    }
}

#[cfg(feature = "any")]
impl<'r> From<MssqlValueRef<'r>> for crate::any::AnyValueRef<'r> {
    #[inline]
    fn from(value: MssqlValueRef<'r>) -> Self {
        crate::any::AnyValueRef {
            type_info: value.type_info.clone().into(),
            kind: crate::any::value::AnyValueRefKind::Mssql(value),
        }
    }
}

/// Implementation of [`Value`] for MSSQL.
#[derive(Clone)]
pub struct MssqlValue {
    pub(crate) type_info: MssqlTypeInfo,
    pub(crate) data: Option<Bytes>,
}

impl Value for MssqlValue {
    type Database = Mssql;

    fn as_ref(&self) -> MssqlValueRef<'_> {
        MssqlValueRef {
            data: self.data.as_ref(),
            type_info: self.type_info.clone(),
        }
    }

    fn type_info(&self) -> Cow<'_, MssqlTypeInfo> {
        Cow::Borrowed(&self.type_info)
    }

    fn is_null(&self) -> bool {
        self.data.is_none() || self.type_info.0.is_null()
    }
}

#[cfg(feature = "any")]
impl From<MssqlValue> for crate::any::AnyValue {
    #[inline]
    fn from(value: MssqlValue) -> Self {
        crate::any::AnyValue {
            type_info: value.type_info.clone().into(),
            kind: crate::any::value::AnyValueKind::Mssql(value),
        }
    }
}