quill_sql/utils/
table_ref.rs

1#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2pub enum TableReference {
3    /// An unqualified table reference, e.g. "table"
4    Bare {
5        /// The table name
6        table: String,
7    },
8    /// A partially resolved table reference, e.g. "schema.table"
9    Partial {
10        /// The schema containing the table
11        schema: String,
12        /// The table name
13        table: String,
14    },
15    /// A fully resolved table reference, e.g. "catalog.schema.table"
16    Full {
17        /// The catalog (aka database) containing the table
18        catalog: String,
19        /// The schema containing the table
20        schema: String,
21        /// The table name
22        table: String,
23    },
24}
25
26impl TableReference {
27    pub fn table(&self) -> &str {
28        match self {
29            Self::Full { table, .. } | Self::Partial { table, .. } | Self::Bare { table } => table,
30        }
31    }
32
33    pub fn schema(&self) -> Option<&str> {
34        match self {
35            Self::Full { schema, .. } | Self::Partial { schema, .. } => Some(schema),
36            _ => None,
37        }
38    }
39
40    pub fn catalog(&self) -> Option<&str> {
41        match self {
42            Self::Full { catalog, .. } => Some(catalog),
43            _ => None,
44        }
45    }
46
47    pub fn resolved_eq(&self, other: &Self) -> bool {
48        match self {
49            TableReference::Bare { table } => table == other.table(),
50            TableReference::Partial { schema, table } => {
51                table == other.table() && other.schema().map_or(true, |s| s == schema)
52            }
53            TableReference::Full {
54                catalog,
55                schema,
56                table,
57            } => {
58                table == other.table()
59                    && other.schema().map_or(true, |s| s == schema)
60                    && other.catalog().map_or(true, |c| c == catalog)
61            }
62        }
63    }
64
65    pub fn to_log_string(&self) -> String {
66        match self {
67            TableReference::Bare { table } => table.clone(),
68            TableReference::Partial { schema, table } => format!("{schema}.{table}"),
69            TableReference::Full {
70                catalog,
71                schema,
72                table,
73            } => format!("{catalog}.{schema}.{table}"),
74        }
75    }
76}
77
78impl std::fmt::Display for TableReference {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            TableReference::Bare { table } => write!(f, "{table}"),
82            TableReference::Partial { schema, table } => {
83                write!(f, "{schema}.{table}")
84            }
85            TableReference::Full {
86                catalog,
87                schema,
88                table,
89            } => write!(f, "{catalog}.{schema}.{table}"),
90        }
91    }
92}