Skip to main content

db_cores/builder/
macros.rs

1
2
3#[macro_export]
4macro_rules! wheres {
5    (
6        $(
7            $column:literal $op:tt $value:expr        //$column:literal
8        ),* $(,)?
9    ) => {
10        {
11            let mut vec = Vec::new();
12
13            $(
14                // 重点使用 stringify!($op) 把操作符 =、!= 等转换成字符串,这样就能在 match 中匹配 "=" 了。
15                let operator = match stringify!($op) {
16                    "=" => $crate::db_common::Operator::Eq,
17                    "!=" => $crate::db_common::Operator::Ne,
18                    ">" => $crate::db_common::Operator::Gt,
19                    ">=" => $crate::db_common::Operator::Gte,
20                    "<" => $crate::db_common::Operator::Lt,
21                    "<=" => $crate::db_common::Operator::Lte,
22                    "in" => $crate::db_common::Operator::In,
23                    "like" => $crate::db_common::Operator::Like,
24                    "not_in" => $crate::db_common::Operator::NotIn,
25                    "btw" => $crate::db_common::Operator::Between,
26                    "not_btw" => $crate::db_common::Operator::NotBetween,
27                    "is_null" => $crate::db_common::Operator::IsNull,
28                    "not_null" => $crate::db_common::Operator::NotNull,
29                    _ => panic!("Unknown operator: {}", stringify!($op)),
30                };
31                vec.push( $crate::db_common::BuildConditionItem {
32                    column: $column.to_string(),
33                    operator,
34                    values: $value.into(),
35                    logical: $crate::db_common::Logical::And,
36                });
37            )*
38             vec
39        }
40    };
41}
42
43#[macro_export]
44macro_rules! wheres_s {
45    (
46        $(
47            $column:literal $op:tt $value:expr        //$column:literal
48        ),* $(,)?
49    ) => {
50        {
51            let mut vec = Vec::new();
52
53            $(
54                // 重点使用 stringify!($op) 把操作符 =、!= 等转换成字符串,这样就能在 match 中匹配 "=" 了。
55                let operator = match stringify!($op) {
56                    "=" => $crate::db_common::Operator::Eq,
57                    "!=" => $crate::db_common::Operator::Ne,
58                    ">" => $crate::db_common::Operator::Gt,
59                    ">=" => $crate::db_common::Operator::Gte,
60                    "<" => $crate::db_common::Operator::Lt,
61                    "<=" => $crate::db_common::Operator::Lte,
62                    "in" => $crate::db_common::Operator::In,
63                    "like" => $crate::db_common::Operator::Like,
64                    "not_in" => $crate::db_common::Operator::NotIn,
65                    "btw" => $crate::db_common::Operator::Between,
66                    "not_btw" => $crate::db_common::Operator::NotBetween,
67                    "is_null" => $crate::db_common::Operator::IsNull,
68                    "not_null" => $crate::db_common::Operator::NotNull,
69                    _ => panic!("Unknown operator: {}", stringify!($op)),
70                };
71                vec.push( $crate::db_common::BuildConditionItem {
72                    column: $column.to_string(),
73                    operator,
74                    values: $value.into(),
75                    logical: $crate::db_common::Logical::And,
76                });
77            )*
78             Some(vec)
79        }
80    };
81}
82
83
84/// orderby!("name",true,"age",false) ture 是降序,false 是升序
85#[macro_export]
86macro_rules! orderby {
87    (
88        $(
89            $column:expr , $value:expr
90        ),* $(,)?
91    ) => {
92        {
93
94            vec![
95                $(
96                    $crate::query_build::OrderItem {
97                        column: $column.to_string(),
98                        direction: $value,
99                    }
100                ),*
101            ]
102        }
103    };
104}
105
106#[macro_export]
107macro_rules! bind_params {
108    (
109        $($bind_value:expr),* $(,)?
110    ) => {
111         vec![
112            $(
113
114                ($bind_value).into()
115            ),*
116        ]
117
118    };
119}
120
121#[macro_export]
122macro_rules! update_items {
123    (
124        $(
125            $column:literal $op:tt $value:expr
126        ),* $(,)?
127    ) => {
128        {
129            vec![
130                $(
131                   $crate::query_build::UpdateItem {
132                        column: $column.to_string(),
133                        value: $value.into(),
134                    }
135                ),*
136            ]
137        }
138    };
139}
140
141#[macro_export]
142macro_rules! insert_items {
143    (
144        $(
145            $column:literal $op:tt $value:expr
146        ),* $(,)?
147    ) => {
148        {
149             let mut columns = Vec::new();
150            $(
151                columns.push($column.to_string(),);
152            )*
153
154            let mut values = Vec::new();
155
156            $(
157                values.push($value.into(),);
158            )*
159
160            $crate::query_build::InsertItems {
161                columns: columns,
162                values:vec![values],
163            }
164        }
165    };
166}
167
168