ic_dbms_api/dbms/table/
schema.rs1use std::hash::{Hash as _, Hasher as _};
2
3use candid::CandidType;
4
5use crate::dbms::foreign_fetcher::ForeignFetcher;
6use crate::dbms::table::column_def::ColumnDef;
7use crate::dbms::table::{InsertRecord, TableRecord, UpdateRecord};
8use crate::memory::Encode;
9use crate::prelude::{Sanitize, Validate};
10
11pub type TableFingerprint = u64;
13
14pub trait TableSchema
18where
19 Self: Encode + CandidType + 'static,
20{
21 type Record: TableRecord<Schema = Self>;
24 type Insert: InsertRecord<Schema = Self>;
26 type Update: UpdateRecord<Schema = Self>;
28 type ForeignFetcher: ForeignFetcher;
30
31 fn table_name() -> &'static str;
33
34 fn columns() -> &'static [ColumnDef];
36
37 fn primary_key() -> &'static str;
39
40 fn to_values(self) -> Vec<(ColumnDef, crate::dbms::value::Value)>;
42
43 fn sanitizer(column_name: &'static str) -> Option<Box<dyn Sanitize>>;
45
46 fn validator(column_name: &'static str) -> Option<Box<dyn Validate>>;
48
49 fn foreign_fetcher() -> Self::ForeignFetcher {
51 Default::default()
52 }
53
54 fn fingerprint() -> TableFingerprint {
56 let mut hasher = std::hash::DefaultHasher::new();
57 std::any::TypeId::of::<Self>().hash(&mut hasher);
58 hasher.finish()
59 }
60}