#[cfg(feature = "mysql")]
use crate::table::TableConfig;
use serde::{Deserialize, Serialize};
#[cfg(feature = "mysql")]
use std::hash::Hash;
#[cfg(feature = "mysql")]
pub trait TableEntity:
serde::de::DeserializeOwned
+ serde::Serialize
+ schemars::JsonSchema
+ for<'r> sqlx::FromRow<'r, sqlx::mysql::MySqlRow>
+ Send
+ Sync
+ Unpin
+ 'static
{
type Pk: serde::de::DeserializeOwned
+ serde::Serialize
+ schemars::JsonSchema
+ Send
+ Sync
+ 'static;
type Field: AsColumnName
+ serde::de::DeserializeOwned
+ serde::Serialize
+ schemars::JsonSchema
+ Copy
+ Eq
+ Hash
+ Send
+ Sync
+ 'static;
type WhereCond: IntoSqlCondition
+ serde::de::DeserializeOwned
+ schemars::JsonSchema
+ Send
+ Sync
+ 'static;
const TABLE_NAME: &'static str;
const PK_FIELD: &'static str;
fn table_config() -> &'static TableConfig;
}
pub trait AsColumnName {
fn column_name(&self) -> &'static str;
}
pub trait IntoSqlCondition {
fn into_sql_condition(self) -> SqlCondition;
}
#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
#[serde(tag = "op", content = "value", rename_all = "snake_case")]
pub enum WhereOp<V> {
Eq(V),
Ne(V),
Lt(V),
Lte(V),
Gt(V),
Gte(V),
In(Vec<V>),
NotIn(Vec<V>),
Between(V, V),
IsNull,
IsNotNull,
}
#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
#[serde(tag = "op", content = "value", rename_all = "snake_case")]
pub enum StringWhereOp {
Eq(String),
Ne(String),
Lt(String),
Lte(String),
Gt(String),
Gte(String),
In(Vec<String>),
NotIn(Vec<String>),
Between(String, String),
Like(String),
IsNull,
IsNotNull,
}
#[derive(Debug, Clone)]
pub struct SqlCondition {
pub column: &'static str,
pub op: SqlOp,
pub params: Vec<serde_json::Value>,
}
#[derive(Debug, Clone, Copy)]
pub enum SqlOp {
Eq,
Ne,
Lt,
Lte,
Gt,
Gte,
In,
NotIn,
Between,
Like,
IsNull,
IsNotNull,
}
impl SqlCondition {
pub fn into_where_condition(self) -> crate::table::WhereCondition {
use crate::table::WhereCondition as WC;
let SqlCondition {
column,
op,
mut params,
} = self;
let field = column.to_string();
let take = |params: &mut Vec<serde_json::Value>, i: usize| -> serde_json::Value {
params.get(i).cloned().unwrap_or(serde_json::Value::Null)
};
match op {
SqlOp::Eq => WC::Eq {
field,
value: take(&mut params, 0),
},
SqlOp::Ne => WC::Ne {
field,
value: take(&mut params, 0),
},
SqlOp::Lt => WC::Lt {
field,
value: take(&mut params, 0),
},
SqlOp::Lte => WC::Lte {
field,
value: take(&mut params, 0),
},
SqlOp::Gt => WC::Gt {
field,
value: take(&mut params, 0),
},
SqlOp::Gte => WC::Gte {
field,
value: take(&mut params, 0),
},
SqlOp::In => WC::In {
field,
values: params,
},
SqlOp::NotIn => WC::NotIn {
field,
values: params,
},
SqlOp::Between => {
let lo = take(&mut params, 0);
let hi = take(&mut params, 1);
WC::Between { field, lo, hi }
}
SqlOp::Like => {
let pattern = params
.first()
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
WC::Like { field, pattern }
}
SqlOp::IsNull => WC::IsNull { field },
SqlOp::IsNotNull => WC::IsNotNull { field },
}
}
}
impl<V: serde::Serialize> WhereOp<V> {
pub fn to_sql_condition(self, column: &'static str) -> SqlCondition {
match self {
WhereOp::Eq(v) => SqlCondition {
column,
op: SqlOp::Eq,
params: vec![to_v(v)],
},
WhereOp::Ne(v) => SqlCondition {
column,
op: SqlOp::Ne,
params: vec![to_v(v)],
},
WhereOp::Lt(v) => SqlCondition {
column,
op: SqlOp::Lt,
params: vec![to_v(v)],
},
WhereOp::Lte(v) => SqlCondition {
column,
op: SqlOp::Lte,
params: vec![to_v(v)],
},
WhereOp::Gt(v) => SqlCondition {
column,
op: SqlOp::Gt,
params: vec![to_v(v)],
},
WhereOp::Gte(v) => SqlCondition {
column,
op: SqlOp::Gte,
params: vec![to_v(v)],
},
WhereOp::In(vs) => SqlCondition {
column,
op: SqlOp::In,
params: vs.into_iter().map(to_v).collect(),
},
WhereOp::NotIn(vs) => SqlCondition {
column,
op: SqlOp::NotIn,
params: vs.into_iter().map(to_v).collect(),
},
WhereOp::Between(a, b) => SqlCondition {
column,
op: SqlOp::Between,
params: vec![to_v(a), to_v(b)],
},
WhereOp::IsNull => SqlCondition {
column,
op: SqlOp::IsNull,
params: vec![],
},
WhereOp::IsNotNull => SqlCondition {
column,
op: SqlOp::IsNotNull,
params: vec![],
},
}
}
}
impl StringWhereOp {
pub fn to_sql_condition(self, column: &'static str) -> SqlCondition {
match self {
StringWhereOp::Like(p) => SqlCondition {
column,
op: SqlOp::Like,
params: vec![serde_json::Value::String(p)],
},
StringWhereOp::Eq(v) => WhereOp::Eq(v).to_sql_condition(column),
StringWhereOp::Ne(v) => WhereOp::Ne(v).to_sql_condition(column),
StringWhereOp::Lt(v) => WhereOp::Lt(v).to_sql_condition(column),
StringWhereOp::Lte(v) => WhereOp::Lte(v).to_sql_condition(column),
StringWhereOp::Gt(v) => WhereOp::Gt(v).to_sql_condition(column),
StringWhereOp::Gte(v) => WhereOp::Gte(v).to_sql_condition(column),
StringWhereOp::In(vs) => WhereOp::In(vs).to_sql_condition(column),
StringWhereOp::NotIn(vs) => WhereOp::NotIn(vs).to_sql_condition(column),
StringWhereOp::Between(a, b) => WhereOp::Between(a, b).to_sql_condition(column),
StringWhereOp::IsNull => WhereOp::<String>::IsNull.to_sql_condition(column),
StringWhereOp::IsNotNull => WhereOp::<String>::IsNotNull.to_sql_condition(column),
}
}
}
fn to_v<V: serde::Serialize>(v: V) -> serde_json::Value {
serde_json::to_value(v).unwrap_or_else(|e| {
log::error!("WhereOp 参数序列化失败,降级为 Null: {}", e);
serde_json::Value::Null
})
}
#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
pub enum Filter<W> {
And {
and: Vec<Filter<W>>,
},
Or {
or: Vec<Filter<W>>,
},
Leaf(W),
}
impl<W: IntoSqlCondition> Filter<W> {
pub fn into_where_condition(self) -> crate::table::WhereCondition {
match self {
Filter::Leaf(w) => w.into_sql_condition().into_where_condition(),
Filter::And { and } => crate::table::WhereCondition::And {
conditions: and.into_iter().map(Filter::into_where_condition).collect(),
},
Filter::Or { or } => crate::table::WhereCondition::Or {
conditions: or.into_iter().map(Filter::into_where_condition).collect(),
},
}
}
}