ic_dbms_api/dbms/table/record.rs
1use candid::CandidType;
2
3use crate::dbms::table::{ColumnDef, TableSchema};
4use crate::dbms::value::Value;
5use crate::prelude::{Filter, IcDbmsResult};
6
7pub type TableColumns = Vec<(ValuesSource, Vec<(ColumnDef, Value)>)>;
8
9/// Indicates the source of the column values.
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub enum ValuesSource {
12 /// Column values belong to the current table.
13 This,
14 /// Column values belong to a foreign table.
15 Foreign { table: String, column: String },
16}
17
18/// This trait represents a record returned by a [`crate::dbms::query::Query`] for a table.
19pub trait TableRecord: CandidType + for<'de> candid::Deserialize<'de> {
20 /// The table schema associated with this record.
21 type Schema: TableSchema<Record = Self>;
22
23 /// Constructs [`TableRecord`] from a list of column values grouped by table.
24 fn from_values(values: TableColumns) -> Self;
25
26 /// Converts the record into a list of column [`Value`]s.
27 fn to_values(&self) -> Vec<(ColumnDef, Value)>;
28}
29
30/// This trait represents a record for inserting into a table.
31pub trait InsertRecord: Sized + Clone + CandidType {
32 /// The [`TableRecord`] type associated with this table schema.
33 type Record: TableRecord;
34 /// The table schema associated with this record.
35 type Schema: TableSchema<Record = Self::Record>;
36
37 /// Creates an insert record from a list of column [`Value`]s.
38 fn from_values(values: &[(ColumnDef, Value)]) -> IcDbmsResult<Self>;
39
40 /// Converts the record into a list of column [`Value`]s for insertion.
41 fn into_values(self) -> Vec<(ColumnDef, Value)>;
42
43 /// Converts the insert record into the corresponding table record.
44 fn into_record(self) -> Self::Schema;
45}
46
47/// This trait represents a record for updating a table.
48pub trait UpdateRecord: Sized + CandidType {
49 /// The [`TableRecord`] type associated with this table schema.
50 type Record: TableRecord;
51 /// The table schema associated with this record.
52 type Schema: TableSchema<Record = Self::Record>;
53
54 /// Creates an update record from a list of column [`Value`]s and an optional [`Filter`] for the where clause.
55 fn from_values(values: &[(ColumnDef, Value)], where_clause: Option<Filter>) -> Self;
56
57 /// Get the list of column [`Value`]s to be updated.
58 fn update_values(&self) -> Vec<(ColumnDef, Value)>;
59
60 /// Get the [`Filter`] condition for the update operation.
61 fn where_clause(&self) -> Option<Filter>;
62}