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
#[macro_export]
macro_rules! query {
($tbl:ty;$q:expr$(,$param:expr)*) => {
format!($q, $($param,)*table = <$tbl>::table_name()).as_str()
};
}
#[macro_export]
macro_rules! params {
($($param:expr),*) => {
&[$(&$param),*]
};
}
#[macro_export]
macro_rules! table {
(
$(#[$($meta:meta),*])?
$pub:vis $name:ident
{
$(
$(#[$($inner_meta:meta),*])?
$rust_key:ident $rust_type:ty : $db_name:tt $db_type:expr
),+
}
) => {
$(#[$($meta),*])?
$pub struct $name {
$(
$(#[$($inner_meta),*])?
$rust_key:$rust_type
),+
}
impl $name{
pub fn __new($($rust_key:$rust_type),+) -> $name{
Self{
$($rust_key),+
}
}
pub fn to_create_table_str()->String{
let fields = [
$(concat!("\"",stringify!($db_name),"\""," ",$db_type)),+
];
format!("CREATE TABLE IF NOT EXISTS {} ({});",stringify!($name).to_lowercase(),fields.join(","))
}
}
impl $crate::Table for $name{
fn table_name()->String{
stringify!($name).to_lowercase()
}
}
impl From<&$crate::Row> for $name{
fn from(r:&$crate::Row)->Self{
Self{
$($rust_key:r.get(stringify!($rust_key))),+
}
}
}
impl From<$crate::Row> for $name{
fn from(r:$crate::Row)->Self{
Self::from(&r)
}
}
};
}