ic_dbms_api/dbms/table/
record.rs

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