ic_dbms_api/dbms/database.rs
1use crate::prelude::{
2 DeleteBehavior, Filter, IcDbmsResult, InsertRecord, Query, TableSchema, UpdateRecord,
3};
4
5/// This module defines the Database trait and related database functionalities.
6pub trait Database {
7 /// Executes a SELECT query and returns the results.
8 ///
9 /// # Arguments
10 ///
11 /// - `query` - The SELECT [`Query`] to be executed.
12 ///
13 /// # Returns
14 ///
15 /// The returned results are a vector of [`table::TableRecord`] matching the query.
16 fn select<T>(&self, query: Query<T>) -> IcDbmsResult<Vec<T::Record>>
17 where
18 T: TableSchema;
19
20 /// Executes an INSERT query.
21 ///
22 /// # Arguments
23 ///
24 /// - `record` - The INSERT record to be executed.
25 fn insert<T>(&self, record: T::Insert) -> IcDbmsResult<()>
26 where
27 T: TableSchema,
28 T::Insert: InsertRecord<Schema = T>;
29
30 /// Executes an UPDATE query.
31 ///
32 /// # Arguments
33 ///
34 /// - `patch` - The UPDATE patch to be applied.
35 /// - `filter` - An optional [`Filter`] to specify which records to update.
36 ///
37 /// # Returns
38 ///
39 /// The number of rows updated.
40 fn update<T>(&self, patch: T::Update) -> IcDbmsResult<u64>
41 where
42 T: TableSchema,
43 T::Update: UpdateRecord<Schema = T>;
44
45 /// Executes a DELETE query.
46 ///
47 /// # Arguments
48 ///
49 /// - `behaviour` - The [`DeleteBehavior`] to apply for foreign key constraints.
50 /// - `filter` - An optional [`Filter`] to specify which records to delete.
51 ///
52 /// # Returns
53 ///
54 /// The number of rows deleted.
55 fn delete<T>(&self, behaviour: DeleteBehavior, filter: Option<Filter>) -> IcDbmsResult<u64>
56 where
57 T: TableSchema;
58
59 /// Commits the current transaction.
60 ///
61 /// The transaction is consumed.
62 ///
63 /// Any error during commit will trap the canister to ensure consistency.
64 fn commit(&mut self) -> IcDbmsResult<()>;
65
66 /// Rolls back the current transaction.
67 ///
68 /// The transaction is consumed.
69 fn rollback(&mut self) -> IcDbmsResult<()>;
70}