ic_dbms_api/dbms/table/
schema.rs

1use 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;
7
8/// A type representing a unique fingerprint for a table schema.
9pub type TableFingerprint = u64;
10
11/// Table schema representation.
12///
13/// It is used to define the structure of a database table.
14pub trait TableSchema
15where
16    Self: Encode + 'static,
17{
18    /// The [`TableRecord`] type associated with this table schema;
19    /// which is the data returned by a query.
20    type Record: TableRecord<Schema = Self>;
21    /// The [`InsertRecord`] type associated with this table schema.
22    type Insert: InsertRecord<Schema = Self>;
23    /// The [`UpdateRecord`] type associated with this table schema.
24    type Update: UpdateRecord<Schema = Self>;
25    /// The [`ForeignFetcher`] type associated with this table schema.
26    type ForeignFetcher: ForeignFetcher;
27
28    /// Returns the name of the table.
29    fn table_name() -> &'static str;
30
31    /// Returns the column definitions of the table.
32    fn columns() -> &'static [ColumnDef];
33
34    /// Returns the name of the primary key column.
35    fn primary_key() -> &'static str;
36
37    /// Converts itself into a vector of column-value pairs.
38    fn to_values(self) -> Vec<(ColumnDef, crate::dbms::value::Value)>;
39
40    /// Returns an instance of the [`ForeignFetcher`] for this table schema.
41    fn foreign_fetcher() -> Self::ForeignFetcher {
42        Default::default()
43    }
44
45    /// Returns the fingerprint of the table schema.
46    fn fingerprint() -> TableFingerprint {
47        let mut hasher = std::hash::DefaultHasher::new();
48        std::any::TypeId::of::<Self>().hash(&mut hasher);
49        hasher.finish()
50    }
51}