sqlx_core_guts/any/
value.rs

1use crate::any::{Any, AnyTypeInfo};
2use crate::database::HasValueRef;
3use crate::value::{Value, ValueRef};
4use std::borrow::Cow;
5
6#[cfg(feature = "postgres")]
7use crate::postgres::{PgValue, PgValueRef};
8
9#[cfg(feature = "mysql")]
10use crate::mysql::{MySqlValue, MySqlValueRef};
11
12#[cfg(feature = "sqlite")]
13use crate::sqlite::{SqliteValue, SqliteValueRef};
14
15#[cfg(feature = "mssql")]
16use crate::mssql::{MssqlValue, MssqlValueRef};
17
18pub struct AnyValue {
19    pub(crate) kind: AnyValueKind,
20    pub(crate) type_info: AnyTypeInfo,
21}
22
23pub(crate) enum AnyValueKind {
24    #[cfg(feature = "postgres")]
25    Postgres(PgValue),
26
27    #[cfg(feature = "mysql")]
28    MySql(MySqlValue),
29
30    #[cfg(feature = "sqlite")]
31    Sqlite(SqliteValue),
32
33    #[cfg(feature = "mssql")]
34    Mssql(MssqlValue),
35}
36
37pub struct AnyValueRef<'r> {
38    pub(crate) kind: AnyValueRefKind<'r>,
39    pub(crate) type_info: AnyTypeInfo,
40}
41
42pub(crate) enum AnyValueRefKind<'r> {
43    #[cfg(feature = "postgres")]
44    Postgres(PgValueRef<'r>),
45
46    #[cfg(feature = "mysql")]
47    MySql(MySqlValueRef<'r>),
48
49    #[cfg(feature = "sqlite")]
50    Sqlite(SqliteValueRef<'r>),
51
52    #[cfg(feature = "mssql")]
53    Mssql(MssqlValueRef<'r>),
54}
55
56impl Value for AnyValue {
57    type Database = Any;
58
59    fn as_ref(&self) -> <Self::Database as HasValueRef<'_>>::ValueRef {
60        match &self.kind {
61            #[cfg(feature = "postgres")]
62            AnyValueKind::Postgres(value) => value.as_ref().into(),
63
64            #[cfg(feature = "mysql")]
65            AnyValueKind::MySql(value) => value.as_ref().into(),
66
67            #[cfg(feature = "sqlite")]
68            AnyValueKind::Sqlite(value) => value.as_ref().into(),
69
70            #[cfg(feature = "mssql")]
71            AnyValueKind::Mssql(value) => value.as_ref().into(),
72        }
73    }
74
75    fn type_info(&self) -> Cow<'_, AnyTypeInfo> {
76        Cow::Borrowed(&self.type_info)
77    }
78
79    fn is_null(&self) -> bool {
80        match &self.kind {
81            #[cfg(feature = "postgres")]
82            AnyValueKind::Postgres(value) => value.is_null(),
83
84            #[cfg(feature = "mysql")]
85            AnyValueKind::MySql(value) => value.is_null(),
86
87            #[cfg(feature = "sqlite")]
88            AnyValueKind::Sqlite(value) => value.is_null(),
89
90            #[cfg(feature = "mssql")]
91            AnyValueKind::Mssql(value) => value.is_null(),
92        }
93    }
94}
95
96impl<'r> ValueRef<'r> for AnyValueRef<'r> {
97    type Database = Any;
98
99    fn to_owned(&self) -> AnyValue {
100        match &self.kind {
101            #[cfg(feature = "postgres")]
102            AnyValueRefKind::Postgres(value) => ValueRef::to_owned(value).into(),
103
104            #[cfg(feature = "mysql")]
105            AnyValueRefKind::MySql(value) => ValueRef::to_owned(value).into(),
106
107            #[cfg(feature = "sqlite")]
108            AnyValueRefKind::Sqlite(value) => ValueRef::to_owned(value).into(),
109
110            #[cfg(feature = "mssql")]
111            AnyValueRefKind::Mssql(value) => ValueRef::to_owned(value).into(),
112        }
113    }
114
115    fn type_info(&self) -> Cow<'_, AnyTypeInfo> {
116        Cow::Borrowed(&self.type_info)
117    }
118
119    fn is_null(&self) -> bool {
120        match &self.kind {
121            #[cfg(feature = "postgres")]
122            AnyValueRefKind::Postgres(value) => value.is_null(),
123
124            #[cfg(feature = "mysql")]
125            AnyValueRefKind::MySql(value) => value.is_null(),
126
127            #[cfg(feature = "sqlite")]
128            AnyValueRefKind::Sqlite(value) => value.is_null(),
129
130            #[cfg(feature = "mssql")]
131            AnyValueRefKind::Mssql(value) => value.is_null(),
132        }
133    }
134}