drizzle_core/traits/
column.rs

1use core::any::Any;
2
3use crate::{SQLParam, SQLSchema, SQLSchemaType, SQLTable, SQLTableInfo};
4
5pub trait SQLColumnInfo: Any + Send + Sync {
6    fn is_not_null(&self) -> bool;
7    fn is_primary_key(&self) -> bool;
8    fn is_unique(&self) -> bool;
9    fn name(&self) -> &str;
10    fn r#type(&self) -> &str;
11    fn has_default(&self) -> bool;
12
13    fn table(&self) -> &dyn SQLTableInfo;
14    /// Returns the foreign key reference if this column has one
15    fn foreign_key(&self) -> Option<&'static dyn SQLColumnInfo> {
16        None
17    }
18}
19
20pub trait AsColumnInfo: Sized + SQLColumnInfo {
21    fn as_column(&self) -> &dyn SQLColumnInfo {
22        self
23    }
24}
25impl<T: SQLColumnInfo> AsColumnInfo for T {}
26
27pub trait SQLColumn<'a, Value: SQLParam + 'a>:
28    SQLColumnInfo + Default + SQLSchema<'a, &'a str, Value>
29{
30    type Table: SQLTable<'a, Self::TableType, Value>;
31    type TableType: SQLSchemaType;
32    type Type: TryInto<Value>;
33
34    const PRIMARY_KEY: bool = false;
35    const NOT_NULL: bool = false;
36    const UNIQUE: bool = false;
37    const DEFAULT: Option<Self::Type> = None;
38
39    fn default_fn(&'a self) -> Option<impl Fn() -> Self::Type> {
40        None::<fn() -> Self::Type>
41    }
42}
43
44impl core::fmt::Debug for dyn SQLColumnInfo {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        f.debug_struct("SQLColumnInfo")
47            .field("name", &self.name())
48            .field("type", &self.r#type())
49            .field("not_null", &self.is_not_null())
50            .field("primary_key", &self.is_primary_key())
51            .field("unique", &self.is_unique())
52            .field("table", &self.table().name())
53            .field("has_default", &self.has_default())
54            .finish()
55    }
56}