Skip to main content

drizzle_postgres/traits/
column.rs

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