drizzle_postgres/traits/
column.rs

1use drizzle_core::{SQLColumn, SQLColumnInfo};
2use std::any::Any;
3
4use crate::{PostgresValue, traits::PostgresTableInfo};
5
6pub trait PostgresColumn<'a>: SQLColumn<'a, PostgresValue<'a>> {}
7impl<'a, T: SQLColumn<'a, PostgresValue<'a>>> PostgresColumn<'a> for T {}
8
9pub trait PostgresColumnInfo: SQLColumnInfo + Any {
10    fn table(&self) -> &dyn PostgresTableInfo;
11
12    fn is_serial(&self) -> bool;
13    fn is_bigserial(&self) -> bool;
14    fn is_generated_identity(&self) -> bool;
15    fn postgres_type(&self) -> &'static str;
16
17    /// Returns the foreign key reference if this column has one
18    fn foreign_key(&self) -> Option<&'static dyn PostgresColumnInfo> {
19        None
20    }
21}
22
23pub trait AsColumnInfo: SQLColumnInfo {
24    fn as_column(&self) -> &dyn PostgresColumnInfo;
25}
26
27impl<T: PostgresColumnInfo> AsColumnInfo for T {
28    fn as_column(&self) -> &dyn PostgresColumnInfo {
29        self
30    }
31}
32
33impl std::fmt::Debug for dyn PostgresColumnInfo {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("PostgresColumnInfo")
36            .field("name", &self.name())
37            .field("type", &self.r#type())
38            .field("not_null", &self.is_not_null())
39            .field("primary_key", &self.is_primary_key())
40            .field("unique", &self.is_unique())
41            .field("table", &PostgresColumnInfo::table(self))
42            .field("has_default", &self.has_default())
43            .field("is_serial", &self.is_serial())
44            .field("is_bigserial", &self.is_bigserial())
45            .field("is_generated_identity", &self.is_generated_identity())
46            .finish()
47    }
48}