use std::hash::{Hash as _, Hasher as _};
use crate::dbms::foreign_fetcher::ForeignFetcher;
use crate::dbms::table::column_def::{ColumnDef, IndexDef};
use crate::dbms::table::{InsertRecord, TableRecord, UpdateRecord};
use crate::memory::Encode;
use crate::prelude::{Sanitize, Validate};
pub type TableFingerprint = u64;
pub trait TableSchema
where
Self: Encode + 'static,
{
type Record: TableRecord<Schema = Self>;
type Insert: InsertRecord<Schema = Self>;
type Update: UpdateRecord<Schema = Self>;
type ForeignFetcher: ForeignFetcher;
fn table_name() -> &'static str;
fn columns() -> &'static [ColumnDef];
fn primary_key() -> &'static str;
fn indexes() -> &'static [IndexDef] {
&[]
}
fn to_values(self) -> Vec<(ColumnDef, crate::dbms::value::Value)>;
fn sanitizer(column_name: &'static str) -> Option<Box<dyn Sanitize>>;
fn validator(column_name: &'static str) -> Option<Box<dyn Validate>>;
fn foreign_fetcher() -> Self::ForeignFetcher {
Default::default()
}
fn fingerprint() -> TableFingerprint {
let mut hasher = std::hash::DefaultHasher::new();
std::any::TypeId::of::<Self>().hash(&mut hasher);
hasher.finish()
}
}