mybatis_sql/
template.rs

1use once_cell::sync::Lazy;
2
3pub static TEMPLATE: Lazy<SqlTemplates> = Lazy::new(|| SqlTemplates::default());
4
5#[inline]
6fn string_to_static_str(s: String) -> &'static str {
7    Box::leak(s.into_boxed_str())
8}
9
10#[derive(Clone, Debug)]
11pub struct Keywords {
12    pub value: &'static str,
13    pub left_space: &'static str,
14    pub right_space: &'static str,
15    pub left_right_space: &'static str,
16}
17
18macro_rules! gen_template {
19     ({ $($key:ident:$value:tt$(,)?)+ }) => {
20           /// Most of the SQL keywords used by the mybatis
21           #[derive(Clone,Debug)]
22           pub struct SqlTemplates {
23               $(pub $key:Keywords,
24               )+
25           }
26           impl Default for SqlTemplates {
27               fn default() -> Self {
28               if cfg!(feature = "upper_case_sql_keyword") {
29                 Self{
30                  $(
31                    $key:Keywords{
32                        value: string_to_static_str($value.to_uppercase()),
33                        left_space:string_to_static_str(" ".to_string()+$value.to_uppercase().as_str()),
34                        right_space:string_to_static_str($value.to_uppercase()+" "),
35                        left_right_space:string_to_static_str(format!(" {} ",$value.to_uppercase())),
36                    },
37                  )+
38                 }
39               }else{
40                 Self{
41                  $(
42                    $key:Keywords{
43                        value:concat!("",$value,""),
44                        left_space:concat!(" ",$value,""),
45                        right_space:concat!("",$value," "),
46                        left_right_space:concat!(" ",$value," "),
47                    },
48                  )+
49                  }
50               }
51
52
53               }
54          }
55     }
56}
57
58gen_template!({
59            r#where: "where",
60            and: "and",
61            or: "or",
62            r#in: "in",
63            having: "having",
64            order_by: "order by",
65            group_by: "group by",
66            asc: "asc",
67            desc: "desc",
68            between: "between",
69            not: "not",
70            like: "like",
71            is: "is",
72            null: "NULL",
73            insert_into: "insert into",
74            values: "values",
75            limit: "limit",
76            set: "set",
77            update: "update",
78            select: "select",
79            delete_from: "delete from",
80            from: "from",
81            r#as: "as",
82            offset: "offset",
83            rows_fetch_next: "rows fetch next",
84            rows_only: "rows only",
85});
86
87#[test]
88fn test_gen() {
89    let t = SqlTemplates::default();
90    println!("{:?}", t);
91}