use crate::error::BaseError;
use crate::table::FieldConfig;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TableConfig {
pub table_name: String,
pub display_name: String,
pub primary_key: String,
pub fields: HashMap<String, FieldConfig>,
pub unique_indexes: Vec<IndexConfig>,
pub indexes: Vec<IndexConfig>,
pub default_order: Vec<(String, SortOrder)>,
pub soft_delete_field: Option<String>,
pub timestamp_fields: Option<TimestampFields>,
}
impl TableConfig {
pub fn new(table_name: impl Into<String>) -> Self {
Self {
table_name: table_name.into(),
display_name: String::new(),
primary_key: "id".to_string(),
fields: HashMap::new(),
unique_indexes: Vec::new(),
indexes: Vec::new(),
default_order: Vec::new(),
soft_delete_field: None,
timestamp_fields: None,
}
}
pub fn display_name(mut self, name: impl Into<String>) -> Self {
self.display_name = name.into();
self
}
pub fn primary_key(mut self, key: impl Into<String>) -> Self {
self.primary_key = key.into();
self
}
pub fn field(mut self, field: FieldConfig) -> Result<Self, BaseError> {
Self::validate_field_config(&field)?;
self.fields.insert(field.name.clone(), field);
Ok(self)
}
pub fn fields(mut self, fields: Vec<FieldConfig>) -> Result<Self, BaseError> {
for field in fields {
self = self.field(field)?;
}
Ok(self)
}
pub fn fields_from_iter<I>(mut self, fields: I) -> Result<Self, BaseError>
where
I: IntoIterator<Item = FieldConfig>,
{
for field in fields {
self = self.field(field)?;
}
Ok(self)
}
fn validate_field_config(field: &FieldConfig) -> Result<(), BaseError> {
if field.name.trim().is_empty() {
return Err(BaseError::ConfigError("字段名称不能为空".to_string()));
}
Ok(())
}
pub fn unique_index(mut self, fields: Vec<String>) -> Self {
self.unique_indexes.push(IndexConfig { name: None, fields });
self
}
pub fn index(mut self, fields: Vec<String>) -> Self {
self.indexes.push(IndexConfig { name: None, fields });
self
}
pub fn default_order(mut self, order: Vec<(String, SortOrder)>) -> Self {
self.default_order = order;
self
}
pub fn soft_delete_field(mut self, field: impl Into<String>) -> Self {
self.soft_delete_field = Some(field.into());
self
}
pub fn timestamps(mut self, created_at: bool, updated_at: bool, deleted_at: bool) -> Self {
self.timestamp_fields = Some(TimestampFields {
created_at: if created_at {
Some("created_at".to_string())
} else {
None
},
updated_at: if updated_at {
Some("updated_at".to_string())
} else {
None
},
deleted_at: if deleted_at {
Some("deleted_at".to_string())
} else {
None
},
});
self
}
pub fn validate_field(&self, field_name: &str) -> Result<(), BaseError> {
if self.fields.contains_key(field_name) {
Ok(())
} else {
Err(BaseError::FieldNotFound(
self.table_name.clone(),
field_name.to_string(),
))
}
}
pub fn get_field(&self, field_name: &str) -> Option<&FieldConfig> {
self.fields.get(field_name)
}
pub fn validate_query(&self, field_names: &[&str]) -> Result<(), BaseError> {
for field_name in field_names {
self.validate_field(field_name)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IndexConfig {
pub name: Option<String>,
pub fields: Vec<String>,
}
impl IndexConfig {
pub fn new(name: Option<String>, fields: Vec<String>) -> Self {
Self { name, fields }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TimestampFields {
pub created_at: Option<String>,
pub updated_at: Option<String>,
pub deleted_at: Option<String>,
}
impl TimestampFields {
pub fn new(
created_at: Option<String>,
updated_at: Option<String>,
deleted_at: Option<String>,
) -> Self {
Self {
created_at,
updated_at,
deleted_at,
}
}
}