forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathencode.rs
More file actions
85 lines (71 loc) · 2.52 KB
/
encode.rs
File metadata and controls
85 lines (71 loc) · 2.52 KB
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
use crate::encode::Encode;
use crate::types::Type;
#[cfg(feature = "odbc")]
use crate::odbc::Odbc;
#[cfg(feature = "postgres")]
use crate::postgres::Postgres;
#[cfg(feature = "mysql")]
use crate::mysql::MySql;
#[cfg(feature = "mssql")]
use crate::mssql::Mssql;
#[cfg(feature = "sqlite")]
use crate::sqlite::Sqlite;
// Implements Encode for any T where T supports Encode for any database that has support currently
// compiled into SQLx
macro_rules! impl_any_encode {
($ty:ty) => {
impl<'q> crate::encode::Encode<'q, crate::any::Any> for $ty
where
$ty: crate::any::AnyEncode<'q>,
{
fn encode_by_ref(
&self,
buf: &mut crate::any::AnyArgumentBuffer<'q>,
) -> crate::encode::IsNull {
match &mut buf.0 {
#[cfg(feature = "postgres")]
crate::any::arguments::AnyArgumentBufferKind::Postgres(args, _) => {
args.add(self)
}
#[cfg(feature = "mysql")]
crate::any::arguments::AnyArgumentBufferKind::MySql(args, _) => args.add(self),
#[cfg(feature = "mssql")]
crate::any::arguments::AnyArgumentBufferKind::Mssql(args, _) => args.add(self),
#[cfg(feature = "sqlite")]
crate::any::arguments::AnyArgumentBufferKind::Sqlite(args) => args.add(self),
#[cfg(feature = "odbc")]
crate::any::arguments::AnyArgumentBufferKind::Odbc(args, _) => {
let _ =
<$ty as crate::encode::Encode<'q, crate::odbc::Odbc>>::encode_by_ref(
self,
&mut args.values,
);
}
}
// unused
crate::encode::IsNull::No
}
}
};
}
// Callback macro that generates the actual trait and impl
macro_rules! impl_any_encode_for_databases {
($($db:ident),+) => {
pub trait AnyEncode<'q>: $(Encode<'q, $db> + Type<$db> +)+ Send {}
impl<'q, T> AnyEncode<'q> for T
where
T: $(Encode<'q, $db> + Type<$db> +)+ Send
{}
};
}
// Generate all combinations
for_all_feature_combinations! {
entries: [
("postgres", Postgres),
("mysql", MySql),
("mssql", Mssql),
("sqlite", Sqlite),
("odbc", Odbc),
],
callback: impl_any_encode_for_databases
}