Skip to main content

drizzle_sqlite/traits/
table.rs

1use drizzle_core::{SQLTable, SQLTableInfo};
2
3use crate::common::SQLiteSchemaType;
4use crate::traits::SQLiteColumnInfo;
5use crate::values::SQLiteValue;
6
7pub trait SQLiteTable<'a>:
8    SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>> + SQLiteTableInfo
9{
10    const WITHOUT_ROWID: bool;
11    const STRICT: bool;
12}
13
14pub trait SQLiteTableInfo: SQLTableInfo {
15    fn r#type(&self) -> &SQLiteSchemaType;
16    fn without_rowid(&self) -> bool;
17    fn strict(&self) -> bool;
18
19    fn sqlite_columns(&self) -> &'static [&'static dyn SQLiteColumnInfo];
20
21    /// Returns all tables this table depends on via foreign keys
22    fn sqlite_dependencies(&self) -> &'static [&'static dyn SQLiteTableInfo];
23}
24
25// Blanket implementation for static references
26impl<T: SQLiteTableInfo> SQLiteTableInfo for &'static T {
27    fn r#type(&self) -> &SQLiteSchemaType {
28        (*self).r#type()
29    }
30
31    fn without_rowid(&self) -> bool {
32        (*self).without_rowid()
33    }
34
35    fn strict(&self) -> bool {
36        (*self).strict()
37    }
38
39    fn sqlite_columns(&self) -> &'static [&'static dyn SQLiteColumnInfo] {
40        (*self).sqlite_columns()
41    }
42
43    fn sqlite_dependencies(&self) -> &'static [&'static dyn SQLiteTableInfo] {
44        (*self).sqlite_dependencies()
45    }
46}
47
48impl std::fmt::Debug for dyn SQLiteTableInfo {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        f.debug_struct("SQLiteTableInfo")
51            .field("name", &self.name())
52            .field("type", &self.r#type())
53            .field("columns", &SQLiteTableInfo::sqlite_columns(self))
54            .field("without_rowid", &self.without_rowid())
55            .field("strict", &self.strict())
56            .field("dependencies", &SQLiteTableInfo::sqlite_dependencies(self))
57            .finish()
58    }
59}