1#[cfg(feature = "mysql")]
7use crate::table::TableConfig;
8use serde::{Deserialize, Serialize};
9#[cfg(feature = "mysql")]
10use std::hash::Hash;
11
12#[cfg(feature = "mysql")]
17pub trait TableEntity:
18 serde::de::DeserializeOwned
19 + serde::Serialize
20 + schemars::JsonSchema
21 + for<'r> sqlx::FromRow<'r, sqlx::mysql::MySqlRow>
22 + Send
23 + Sync
24 + Unpin
25 + 'static
26{
27 type Pk: serde::de::DeserializeOwned
29 + serde::Serialize
30 + schemars::JsonSchema
31 + Send
32 + Sync
33 + 'static;
34
35 type Field: AsColumnName
38 + serde::de::DeserializeOwned
39 + serde::Serialize
40 + schemars::JsonSchema
41 + Copy
42 + Eq
43 + Hash
44 + Send
45 + Sync
46 + 'static;
47
48 type WhereCond: IntoSqlCondition
50 + serde::de::DeserializeOwned
51 + schemars::JsonSchema
52 + Send
53 + Sync
54 + 'static;
55
56 const TABLE_NAME: &'static str;
58 const PK_FIELD: &'static str;
60
61 fn table_config() -> &'static TableConfig;
63}
64
65pub trait AsColumnName {
68 fn column_name(&self) -> &'static str;
70}
71
72pub trait IntoSqlCondition {
74 fn into_sql_condition(self) -> SqlCondition;
76}
77
78#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
84#[serde(tag = "op", content = "value", rename_all = "snake_case")]
85pub enum WhereOp<V> {
86 Eq(V),
88 Ne(V),
90 Lt(V),
92 Lte(V),
94 Gt(V),
96 Gte(V),
98 In(Vec<V>),
100 NotIn(Vec<V>),
102 Between(V, V),
104 IsNull,
106 IsNotNull,
108}
109
110#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
112#[serde(tag = "op", content = "value", rename_all = "snake_case")]
113pub enum StringWhereOp {
114 Eq(String),
116 Ne(String),
118 Lt(String),
120 Lte(String),
122 Gt(String),
124 Gte(String),
126 In(Vec<String>),
128 NotIn(Vec<String>),
130 Between(String, String),
132 Like(String),
134 IsNull,
136 IsNotNull,
138}
139
140#[derive(Debug, Clone)]
142pub struct SqlCondition {
143 pub column: &'static str,
145 pub op: SqlOp,
147 pub params: Vec<serde_json::Value>,
149}
150
151#[derive(Debug, Clone, Copy)]
153pub enum SqlOp {
154 Eq,
156 Ne,
158 Lt,
160 Lte,
162 Gt,
164 Gte,
166 In,
168 NotIn,
170 Between,
172 Like,
174 IsNull,
176 IsNotNull,
178}
179
180impl SqlCondition {
181 pub fn into_where_condition(self) -> crate::table::WhereCondition {
190 use crate::table::WhereCondition as WC;
191 let SqlCondition {
192 column,
193 op,
194 mut params,
195 } = self;
196 let field = column.to_string();
197 let take = |params: &mut Vec<serde_json::Value>, i: usize| -> serde_json::Value {
199 params.get(i).cloned().unwrap_or(serde_json::Value::Null)
200 };
201 match op {
202 SqlOp::Eq => WC::Eq {
203 field,
204 value: take(&mut params, 0),
205 },
206 SqlOp::Ne => WC::Ne {
207 field,
208 value: take(&mut params, 0),
209 },
210 SqlOp::Lt => WC::Lt {
211 field,
212 value: take(&mut params, 0),
213 },
214 SqlOp::Lte => WC::Lte {
215 field,
216 value: take(&mut params, 0),
217 },
218 SqlOp::Gt => WC::Gt {
219 field,
220 value: take(&mut params, 0),
221 },
222 SqlOp::Gte => WC::Gte {
223 field,
224 value: take(&mut params, 0),
225 },
226 SqlOp::In => WC::In {
227 field,
228 values: params,
229 },
230 SqlOp::NotIn => WC::NotIn {
231 field,
232 values: params,
233 },
234 SqlOp::Between => {
235 let lo = take(&mut params, 0);
236 let hi = take(&mut params, 1);
237 WC::Between { field, lo, hi }
238 }
239 SqlOp::Like => {
240 let pattern = params
241 .first()
242 .and_then(|v| v.as_str())
243 .unwrap_or("")
244 .to_string();
245 WC::Like { field, pattern }
246 }
247 SqlOp::IsNull => WC::IsNull { field },
248 SqlOp::IsNotNull => WC::IsNotNull { field },
249 }
250 }
251}
252
253impl<V: serde::Serialize> WhereOp<V> {
254 pub fn to_sql_condition(self, column: &'static str) -> SqlCondition {
256 match self {
257 WhereOp::Eq(v) => SqlCondition {
258 column,
259 op: SqlOp::Eq,
260 params: vec![to_v(v)],
261 },
262 WhereOp::Ne(v) => SqlCondition {
263 column,
264 op: SqlOp::Ne,
265 params: vec![to_v(v)],
266 },
267 WhereOp::Lt(v) => SqlCondition {
268 column,
269 op: SqlOp::Lt,
270 params: vec![to_v(v)],
271 },
272 WhereOp::Lte(v) => SqlCondition {
273 column,
274 op: SqlOp::Lte,
275 params: vec![to_v(v)],
276 },
277 WhereOp::Gt(v) => SqlCondition {
278 column,
279 op: SqlOp::Gt,
280 params: vec![to_v(v)],
281 },
282 WhereOp::Gte(v) => SqlCondition {
283 column,
284 op: SqlOp::Gte,
285 params: vec![to_v(v)],
286 },
287 WhereOp::In(vs) => SqlCondition {
288 column,
289 op: SqlOp::In,
290 params: vs.into_iter().map(to_v).collect(),
291 },
292 WhereOp::NotIn(vs) => SqlCondition {
293 column,
294 op: SqlOp::NotIn,
295 params: vs.into_iter().map(to_v).collect(),
296 },
297 WhereOp::Between(a, b) => SqlCondition {
298 column,
299 op: SqlOp::Between,
300 params: vec![to_v(a), to_v(b)],
301 },
302 WhereOp::IsNull => SqlCondition {
303 column,
304 op: SqlOp::IsNull,
305 params: vec![],
306 },
307 WhereOp::IsNotNull => SqlCondition {
308 column,
309 op: SqlOp::IsNotNull,
310 params: vec![],
311 },
312 }
313 }
314}
315
316impl StringWhereOp {
317 pub fn to_sql_condition(self, column: &'static str) -> SqlCondition {
319 match self {
320 StringWhereOp::Like(p) => SqlCondition {
321 column,
322 op: SqlOp::Like,
323 params: vec![serde_json::Value::String(p)],
324 },
325 StringWhereOp::Eq(v) => WhereOp::Eq(v).to_sql_condition(column),
327 StringWhereOp::Ne(v) => WhereOp::Ne(v).to_sql_condition(column),
328 StringWhereOp::Lt(v) => WhereOp::Lt(v).to_sql_condition(column),
329 StringWhereOp::Lte(v) => WhereOp::Lte(v).to_sql_condition(column),
330 StringWhereOp::Gt(v) => WhereOp::Gt(v).to_sql_condition(column),
331 StringWhereOp::Gte(v) => WhereOp::Gte(v).to_sql_condition(column),
332 StringWhereOp::In(vs) => WhereOp::In(vs).to_sql_condition(column),
333 StringWhereOp::NotIn(vs) => WhereOp::NotIn(vs).to_sql_condition(column),
334 StringWhereOp::Between(a, b) => WhereOp::Between(a, b).to_sql_condition(column),
335 StringWhereOp::IsNull => WhereOp::<String>::IsNull.to_sql_condition(column),
336 StringWhereOp::IsNotNull => WhereOp::<String>::IsNotNull.to_sql_condition(column),
337 }
338 }
339}
340
341fn to_v<V: serde::Serialize>(v: V) -> serde_json::Value {
342 serde_json::to_value(v).unwrap_or_else(|e| {
343 log::error!("WhereOp 参数序列化失败,降级为 Null: {}", e);
346 serde_json::Value::Null
347 })
348}
349
350#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
381#[serde(untagged)]
382pub enum Filter<W> {
383 And {
385 and: Vec<Filter<W>>,
387 },
388 Or {
390 or: Vec<Filter<W>>,
392 },
393 Leaf(W),
395}
396
397impl<W: IntoSqlCondition> Filter<W> {
398 pub fn into_where_condition(self) -> crate::table::WhereCondition {
404 match self {
405 Filter::Leaf(w) => w.into_sql_condition().into_where_condition(),
406 Filter::And { and } => crate::table::WhereCondition::And {
407 conditions: and.into_iter().map(Filter::into_where_condition).collect(),
408 },
409 Filter::Or { or } => crate::table::WhereCondition::Or {
410 conditions: or.into_iter().map(Filter::into_where_condition).collect(),
411 },
412 }
413 }
414}