drizzle_sqlite/traits/
column.rs

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