Skip to main content

wasm_dbms_api/dbms/
database.rs

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