Skip to main content

drizzle_sqlite/traits/
column.rs

1use drizzle_core::{SQLColumn, SQLColumnInfo};
2
3use crate::traits::SQLiteTableInfo;
4use crate::values::SQLiteValue;
5
6pub trait SQLiteColumn<'a>: SQLColumn<'a, SQLiteValue<'a>> {
7    const AUTOINCREMENT: bool = false;
8}
9
10pub trait SQLiteColumnInfo: SQLColumnInfo {
11    fn is_autoincrement(&self) -> bool;
12    fn table(&self) -> &dyn SQLiteTableInfo;
13
14    /// Returns the foreign key reference if this column has one.
15    fn foreign_key(&self) -> Option<&'static dyn SQLiteColumnInfo> {
16        None
17    }
18
19    /// Erased access to the SQLite column info.
20    fn as_sqlite_column(&self) -> &dyn SQLiteColumnInfo
21    where
22        Self: Sized,
23    {
24        self
25    }
26
27    /// Core-erased foreign key reference for call sites that only need generic info.
28    fn foreign_key_core(&self) -> Option<&'static dyn SQLColumnInfo> {
29        <Self as SQLiteColumnInfo>::foreign_key(self).map(|fk| fk as &dyn SQLColumnInfo)
30    }
31}
32
33// Blanket implementation for static references
34impl<T: SQLiteColumnInfo> SQLiteColumnInfo for &'static T {
35    fn is_autoincrement(&self) -> bool {
36        <T as SQLiteColumnInfo>::is_autoincrement(*self)
37    }
38
39    fn table(&self) -> &dyn SQLiteTableInfo {
40        <T as SQLiteColumnInfo>::table(*self)
41    }
42
43    fn foreign_key(&self) -> Option<&'static dyn SQLiteColumnInfo> {
44        <T as SQLiteColumnInfo>::foreign_key(*self)
45    }
46}
47
48impl std::fmt::Debug for dyn SQLiteColumnInfo {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        f.debug_struct("SQLiteColumnInfo")
51            .field("name", &self.name())
52            .field("type", &self.r#type())
53            .field("not_null", &self.is_not_null())
54            .field("primary_key", &self.is_primary_key())
55            .field("unique", &self.is_unique())
56            .field("table", &SQLiteColumnInfo::table(self))
57            .field("has_default", &self.has_default())
58            .field("is_autoincrement", &self.is_autoincrement())
59            .finish()
60    }
61}