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]
#[doc(hidden)]
macro_rules! sql_literal {
($($name:ident)+ => $text:literal) => {
$text
};
($($name:ident)+ => $text:literal : $param:ident) => {
::std::concat!( $text, '$', ::include_sql::index_of!($param in [ $( $name ),+ ] + 1) )
};
($($name:ident)+ => $text:literal : $param:ident $($tail:tt)+) => {
::std::concat!(
$text, '$', ::include_sql::index_of!($param in [ $( $name ),+ ] + 1),
$crate::sql_literal!($($name)+ => $($tail)+)
)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! num_args {
() => { 0 };
(: $head:ident $($tail:tt)*) => { 1 + $crate::num_args!($($tail)*) };
(# $head:ident $($tail:tt)*) => { $head.len() + $crate::num_args!($($tail)*) };
}
#[macro_export]
#[doc(hidden)]
macro_rules! sql_len {
() => { 0 };
($text:literal $($tail:tt)*) => { $text.len() + $crate::sql_len!($($tail)*) };
(: $head:ident $($tail:tt)*) => { 3 + $crate::sql_len!($($tail)*) };
(# $head:ident $($tail:tt)*) => { $head.len() * 5 + $crate::sql_len!($($tail)*) };
}
#[macro_export]
#[doc(hidden)]
macro_rules! dynamic_sql {
($stmt:ident $args:ident $i:ident) => {};
($stmt:ident $args:ident $i:ident $text:literal $($tail:tt)*) => {
$stmt.push_str($text);
$crate::dynamic_sql!($stmt $args $i $($tail)*);
};
($stmt:ident $args:ident $i:ident : $param:ident $($tail:tt)*) => {
$i += 1;
$stmt.push_str(&::std::format!("${}", $i));
$args.push(&$param);
$crate::dynamic_sql!($stmt $args $i $($tail)*);
};
($stmt:ident $args:ident $i:ident # $param:ident $($tail:tt)*) => {
let mut iter = $param.into_iter();
if let Some(arg) = iter.next() {
$i += 1;
$stmt.push_str(&::std::format!("${}", $i));
$args.push(arg);
while let Some(arg) = iter.next() {
$i += 1;
$stmt.push_str(&::std::format!(", ${}", $i));
$args.push(arg);
}
} else {
$stmt.push_str("NULL");
}
$crate::dynamic_sql!($stmt $args $i $($tail)*);
};
}