use super::FieldConfig;
use crate::datasource::DatasourceId;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub(crate) struct TableConfig {
pub(crate) table_name: String,
pub(crate) datasource: DatasourceId,
#[cfg_attr(not(feature = "mysql"), allow(dead_code))]
pub(crate) table_ref: yang_db::TableRef,
pub(crate) display_name: String,
pub(crate) primary_key: String,
pub(crate) primary_keys: Vec<String>,
pub(crate) fields: HashMap<String, FieldConfig>,
#[cfg_attr(not(any(feature = "mysql", feature = "postgres")), allow(dead_code))]
pub(crate) opaque_columns: Vec<OpaqueColumnConfig>,
#[cfg_attr(not(feature = "mysql"), allow(dead_code))]
pub(crate) field_refs: HashMap<String, yang_db::FieldRef>,
#[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
pub(crate) unique_indexes: Vec<IndexConfig>,
#[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
pub(crate) indexes: Vec<IndexConfig>,
#[cfg_attr(not(feature = "mysql"), allow(dead_code))]
pub(crate) checks: Vec<CheckConfig>,
#[cfg_attr(not(feature = "mysql"), allow(dead_code))]
pub(crate) foreign_keys: Vec<ForeignKeyConfig>,
#[cfg_attr(not(any(feature = "mysql", test)), allow(dead_code))]
pub(crate) default_order: Vec<(String, SortOrder)>,
pub(crate) soft_delete_field: Option<String>,
pub(crate) timestamp_fields: Option<TimestampFields>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CheckConfig {
pub(crate) name: String,
pub(crate) expression: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ForeignKeyConfig {
pub(crate) name: String,
pub(crate) columns: Vec<String>,
pub(crate) referenced_table: String,
pub(crate) referenced_columns: Vec<String>,
}
impl TableConfig {
pub(crate) fn get_field(&self, field_name: &str) -> Option<&FieldConfig> {
self.fields.get(field_name)
}
#[cfg(feature = "mysql")]
pub(crate) fn get_field_ref(&self, field_name: &str) -> Option<&yang_db::FieldRef> {
self.field_refs.get(field_name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OpaqueColumnConfig {
pub(crate) name: String,
pub(crate) sql_type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct IndexConfig {
pub(crate) name: Option<String>,
pub(crate) fields: Vec<String>,
pub(crate) expressions: Vec<String>,
pub(crate) predicate: Option<String>,
pub(crate) nulls_not_distinct: bool,
}
impl IndexConfig {
pub(crate) fn new(name: Option<String>, fields: Vec<String>, nulls_not_distinct: bool) -> Self {
Self {
name,
fields,
expressions: Vec::new(),
predicate: None,
nulls_not_distinct,
}
}
pub(crate) fn with_expressions(mut self, expressions: Vec<String>) -> Self {
self.expressions = expressions;
self
}
pub(crate) fn with_predicate(mut self, predicate: Option<String>) -> Self {
self.predicate = predicate;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct TimestampFields {
pub(crate) created_at: Option<String>,
pub(crate) updated_at: Option<String>,
pub(crate) deleted_at: Option<String>,
}
impl TimestampFields {
pub(crate) fn new(
created_at: Option<String>,
updated_at: Option<String>,
deleted_at: Option<String>,
) -> Self {
Self {
created_at,
updated_at,
deleted_at,
}
}
}