Skip to main content

drizzle_postgres/traits/
table.rs

1use drizzle_core::{SQLTable, SQLTableInfo};
2
3use super::PostgresColumnInfo;
4use crate::common::PostgresSchemaType;
5use crate::values::PostgresValue;
6
7pub trait PostgresTable<'a>:
8    SQLTable<'a, PostgresSchemaType, PostgresValue<'a>> + PostgresTableInfo
9{
10}
11
12pub trait PostgresTableInfo: SQLTableInfo {
13    fn r#type(&self) -> &PostgresSchemaType;
14    fn postgres_columns(&self) -> &'static [&'static dyn PostgresColumnInfo];
15
16    /// Returns all tables this table depends on via foreign keys.
17    fn postgres_dependencies(&self) -> &'static [&'static dyn PostgresTableInfo];
18}
19
20// Blanket implementation for static references
21impl<T: PostgresTableInfo> PostgresTableInfo for &'static T {
22    fn r#type(&self) -> &PostgresSchemaType {
23        (*self).r#type()
24    }
25
26    fn postgres_columns(&self) -> &'static [&'static dyn PostgresColumnInfo] {
27        (*self).postgres_columns()
28    }
29
30    fn postgres_dependencies(&self) -> &'static [&'static dyn PostgresTableInfo] {
31        (*self).postgres_dependencies()
32    }
33}
34
35impl std::fmt::Debug for dyn PostgresTableInfo {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("PostgresTableInfo")
38            .field("name", &self.name())
39            .field("type", &self.r#type())
40            .field("columns", &PostgresTableInfo::postgres_columns(self))
41            .field(
42                "dependencies",
43                &PostgresTableInfo::postgres_dependencies(self),
44            )
45            .finish()
46    }
47}