drizzle_postgres/traits/
column.rs

1use drizzle_core::{SQLColumn, SQLColumnInfo};
2use std::any::Any;
3
4use crate::{PostgresValue, traits::PostgresTableInfo};
5pub trait PostgresColumn<'a>: SQLColumn<'a, PostgresValue<'a>> {
6    const SERIAL: bool = false;
7    const BIGSERIAL: bool = false;
8    const GENERATED_IDENTITY: bool = false;
9}
10pub trait PostgresColumnInfo: SQLColumnInfo + Any {
11    fn table(&self) -> &dyn PostgresTableInfo;
12
13    fn is_serial(&self) -> bool;
14    fn is_bigserial(&self) -> bool;
15    fn is_generated_identity(&self) -> bool;
16    fn postgres_type(&self) -> &'static str;
17
18    /// Returns the foreign key reference if this column has one
19    fn foreign_key(&self) -> Option<&'static dyn PostgresColumnInfo> {
20        None
21    }
22
23    /// Erased access to the Postgres column info.
24    fn as_postgres_column(&self) -> &dyn PostgresColumnInfo
25    where
26        Self: Sized,
27    {
28        self
29    }
30
31    /// Core-erased foreign key reference for call sites that only need generic info.
32    fn foreign_key_core(&self) -> Option<&'static dyn SQLColumnInfo> {
33        <Self as PostgresColumnInfo>::foreign_key(self).map(|fk| fk as &dyn SQLColumnInfo)
34    }
35}
36
37// Blanket implementation for static references
38impl<T: PostgresColumnInfo> PostgresColumnInfo for &'static T {
39    fn table(&self) -> &dyn PostgresTableInfo {
40        <T as PostgresColumnInfo>::table(*self)
41    }
42
43    fn is_serial(&self) -> bool {
44        <T as PostgresColumnInfo>::is_serial(*self)
45    }
46
47    fn is_bigserial(&self) -> bool {
48        <T as PostgresColumnInfo>::is_bigserial(*self)
49    }
50
51    fn is_generated_identity(&self) -> bool {
52        <T as PostgresColumnInfo>::is_generated_identity(*self)
53    }
54
55    fn postgres_type(&self) -> &'static str {
56        <T as PostgresColumnInfo>::postgres_type(*self)
57    }
58
59    fn foreign_key(&self) -> Option<&'static dyn PostgresColumnInfo> {
60        <T as PostgresColumnInfo>::foreign_key(*self)
61    }
62}
63
64impl std::fmt::Debug for dyn PostgresColumnInfo {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct("PostgresColumnInfo")
67            .field("name", &self.name())
68            .field("type", &self.r#type())
69            .field("not_null", &self.is_not_null())
70            .field("primary_key", &self.is_primary_key())
71            .field("unique", &self.is_unique())
72            .field("table", &PostgresColumnInfo::table(self))
73            .field("has_default", &self.has_default())
74            .field("is_serial", &self.is_serial())
75            .field("is_bigserial", &self.is_bigserial())
76            .field("is_generated_identity", &self.is_generated_identity())
77            .finish()
78    }
79}