wasm_dbms_api/dbms/table/
schema.rs1use std::hash::{Hash as _, Hasher as _};
2
3use crate::dbms::foreign_fetcher::ForeignFetcher;
4use crate::dbms::table::column_def::ColumnDef;
5use crate::dbms::table::{InsertRecord, TableRecord, UpdateRecord};
6use crate::memory::Encode;
7use crate::prelude::{Sanitize, Validate};
8
9pub type TableFingerprint = u64;
11
12pub trait TableSchema
16where
17 Self: Encode + 'static,
18{
19 type Record: TableRecord<Schema = Self>;
22 type Insert: InsertRecord<Schema = Self>;
24 type Update: UpdateRecord<Schema = Self>;
26 type ForeignFetcher: ForeignFetcher;
28
29 fn table_name() -> &'static str;
31
32 fn columns() -> &'static [ColumnDef];
34
35 fn primary_key() -> &'static str;
37
38 fn to_values(self) -> Vec<(ColumnDef, crate::dbms::value::Value)>;
40
41 fn sanitizer(column_name: &'static str) -> Option<Box<dyn Sanitize>>;
43
44 fn validator(column_name: &'static str) -> Option<Box<dyn Validate>>;
46
47 fn foreign_fetcher() -> Self::ForeignFetcher {
49 Default::default()
50 }
51
52 fn fingerprint() -> TableFingerprint {
54 let mut hasher = std::hash::DefaultHasher::new();
55 std::any::TypeId::of::<Self>().hash(&mut hasher);
56 hasher.finish()
57 }
58}