sqlstr/expr/
condition.rs

1#[allow(unused_macros)]
2macro_rules! static_comparison {
3    (=) => {
4        "="
5    };
6    (!=) => {
7        "<>"
8    };
9    (>) => {
10        ">"
11    };
12    (<) => {
13        "<"
14    };
15    (>=) => {
16        ">="
17    };
18    (<=) => {
19        "<="
20    };
21}
22
23#[allow(unused_macros)]
24macro_rules! static_logical_op {
25    (NOT) => {
26        "NOT"
27    };
28    (AND) => {
29        "AND"
30    };
31    (OR) => {
32        "OR"
33    };
34    (AND_NOT) => {
35        "AND NOT"
36    };
37    (OR_NOT) => {
38        "OR NOT"
39    };
40}
41
42#[allow(unused_imports)]
43pub(super) use static_comparison;
44
45#[allow(unused_imports)]
46pub(super) use static_logical_op;
47
48#[macro_export]
49macro_rules! static_condition {
50    ($a:literal $op:tt $b:literal) => {
51        concat!(
52            $a,
53            " ",
54            $crate::expr::static_comparison!($op),
55            " ",
56            $b
57        )
58    };
59    ($pre_logic:tt $a:literal $op:tt $b:literal) => {
60        concat!(
61            $crate::expr::static_logical_op!($pre_logic),
62            " ",
63            $a,
64            " ",
65            $crate::expr::static_comparison!($op),
66            " ",
67            $b
68        )
69    };
70
71    ($a:literal $op:tt $b:literal $($logic_op:tt $ax:literal $opx:tt $bx:literal)+) => {
72        concat!(
73            $crate::expr::static_condition!($a $op $b),
74            $(
75                " ",
76                $crate::expr::static_logical_op!($logic_op),
77                " ",
78                $crate::expr::static_condition!($ax $opx $bx)
79            )+
80        )
81    };
82    ($pre_logic:tt $a:literal $op:tt $b:literal $($logic_op:tt $ax:literal $opx:tt $bx:literal)+) => {
83        concat!(
84            $crate::expr::static_logical_op!($pre_logic),
85            " ",
86            $crate::expr::static_condition!($a $op $b),
87            $(
88                " ",
89                $crate::expr::static_logical_op!($logic_op),
90                " ",
91                $crate::expr::static_condition!($ax $opx $bx)
92            )+
93        )
94    };
95}
96
97pub use static_condition;
98
99#[cfg(test)]
100mod test {
101
102    #[test]
103    fn static_condition_macro() {
104        assert_eq!(
105            static_condition!(NOT "access_history.user_id" = "user.id"),
106            "NOT access_history.user_id = user.id"
107        );
108        assert_eq!(
109            static_condition!("access_history.user_id" > "user.id"),
110            "access_history.user_id > user.id"
111        );
112        assert_eq!(
113            static_condition!(AND_NOT "access_history.user_id" >= "user.id"),
114            "AND NOT access_history.user_id >= user.id"
115        );
116        assert_eq!(
117            static_condition!("user.id" = "access_history.user_id" OR "user.updated" < "access_history.created"),
118            "user.id = access_history.user_id OR user.updated < access_history.created"
119        );
120        assert_eq!(
121            static_condition!("user.id" != "access_history.user_id"),
122            "user.id <> access_history.user_id"
123        );
124        assert_eq!(
125            static_condition!(NOT "user.id" = "access_history.user_id" AND_NOT "user.updated" < "access_history.created"),
126            "NOT user.id = access_history.user_id AND NOT user.updated < access_history.created"
127        )
128    }
129}