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::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 validator(column_name: &'static str) -> Option<Box<dyn Validate>>;
45
46 fn foreign_fetcher() -> Self::ForeignFetcher {
48 Default::default()
49 }
50
51 fn fingerprint() -> TableFingerprint {
53 let mut hasher = std::hash::DefaultHasher::new();
54 std::any::TypeId::of::<Self>().hash(&mut hasher);
55 hasher.finish()
56 }
57}