ic_dbms_canister/dbms/schema.rs
1use ic_dbms_api::prelude::{ColumnDef, DeleteBehavior, Filter, IcDbmsResult, Value};
2
3use crate::dbms::IcDbmsDatabase;
4
5/// This trait provides the schema operation for the current database.
6///
7/// It must provide the functionalities to validate the operations and perform them using the [`Database`] instance.
8///
9/// This is required because all of the [`Database`] operations rely on `T`, a [`crate::prelude::TableSchema`], but we can't store them inside
10/// of transactions without knowing the concrete type at compile time.
11pub trait DatabaseSchema {
12 /// Returns the foreign key definitions referencing other tables for the given table name.
13 ///
14 /// So if a table `Post` has a foreign key referencing the `User` table, calling
15 /// `referenced_tables("User")` would return a list containing:
16 /// `[("Post`, &["user_id"])]`.
17 fn referenced_tables(&self, table: &'static str) -> Vec<(&'static str, Vec<&'static str>)>;
18
19 /// Performs an insert operation for the given table name and record values.
20 ///
21 /// Use [`Database::insert`] internally to perform the operation.
22 fn insert(
23 &self,
24 dbms: &IcDbmsDatabase,
25 table_name: &'static str,
26 record_values: &[(ColumnDef, Value)],
27 ) -> IcDbmsResult<()>;
28
29 /// Performs a delete operation for the given table name, delete behavior, and optional filter.
30 ///
31 /// Use [`Database::delete`] internally to perform the operation.
32 fn delete(
33 &self,
34 dbms: &IcDbmsDatabase,
35 table_name: &'static str,
36 delete_behavior: DeleteBehavior,
37 filter: Option<Filter>,
38 ) -> IcDbmsResult<u64>;
39
40 /// Performs an update operation for the given table name, patch values, and optional filter.
41 ///
42 /// Use [`Database::update`] internally to perform the operation.
43 fn update(
44 &self,
45 dbms: &IcDbmsDatabase,
46 table_name: &'static str,
47 patch_values: &[(ColumnDef, Value)],
48 filter: Option<Filter>,
49 ) -> IcDbmsResult<u64>;
50
51 /// Validates an insert operation for the given table name and record values.
52 ///
53 /// Use a [`crate::prelude::InsertIntegrityValidator`] to perform the validation.
54 fn validate_insert(
55 &self,
56 dbms: &IcDbmsDatabase,
57 table_name: &'static str,
58 record_values: &[(ColumnDef, Value)],
59 ) -> IcDbmsResult<()>;
60}