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