sqlx_core_oldapi/any/
encode.rs

1use crate::encode::Encode;
2use crate::types::Type;
3
4#[cfg(feature = "odbc")]
5use crate::odbc::Odbc;
6
7#[cfg(feature = "postgres")]
8use crate::postgres::Postgres;
9
10#[cfg(feature = "mysql")]
11use crate::mysql::MySql;
12
13#[cfg(feature = "mssql")]
14use crate::mssql::Mssql;
15
16#[cfg(feature = "sqlite")]
17use crate::sqlite::Sqlite;
18
19// Implements Encode for any T where T supports Encode for any database that has support currently
20// compiled into SQLx
21macro_rules! impl_any_encode {
22    ($ty:ty) => {
23        impl<'q> crate::encode::Encode<'q, crate::any::Any> for $ty
24        where
25            $ty: crate::any::AnyEncode<'q>,
26        {
27            fn encode_by_ref(
28                &self,
29                buf: &mut crate::any::AnyArgumentBuffer<'q>,
30            ) -> crate::encode::IsNull {
31                match &mut buf.0 {
32                    #[cfg(feature = "postgres")]
33                    crate::any::arguments::AnyArgumentBufferKind::Postgres(args, _) => {
34                        args.add(self)
35                    }
36
37                    #[cfg(feature = "mysql")]
38                    crate::any::arguments::AnyArgumentBufferKind::MySql(args, _) => args.add(self),
39
40                    #[cfg(feature = "mssql")]
41                    crate::any::arguments::AnyArgumentBufferKind::Mssql(args, _) => args.add(self),
42
43                    #[cfg(feature = "sqlite")]
44                    crate::any::arguments::AnyArgumentBufferKind::Sqlite(args) => args.add(self),
45
46                    #[cfg(feature = "odbc")]
47                    crate::any::arguments::AnyArgumentBufferKind::Odbc(args, _) => {
48                        let _ =
49                            <$ty as crate::encode::Encode<'q, crate::odbc::Odbc>>::encode_by_ref(
50                                self,
51                                &mut args.values,
52                            );
53                    }
54                }
55
56                // unused
57                crate::encode::IsNull::No
58            }
59        }
60    };
61}
62
63// Callback macro that generates the actual trait and impl
64macro_rules! impl_any_encode_for_databases {
65    ($($db:ident),+) => {
66        pub trait AnyEncode<'q>: $(Encode<'q, $db> + Type<$db> +)+ Send {}
67
68        impl<'q, T> AnyEncode<'q> for T
69        where
70            T: $(Encode<'q, $db> + Type<$db> +)+ Send
71        {}
72    };
73}
74
75// Generate all combinations
76for_all_feature_combinations! {
77    entries: [
78        ("postgres", Postgres),
79        ("mysql", MySql),
80        ("mssql", Mssql),
81        ("sqlite", Sqlite),
82        ("odbc", Odbc),
83    ],
84    callback: impl_any_encode_for_databases
85}