1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_db_name::{SchemaName, TableName};
7
8#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct TableRef {
11 schema: Option<SchemaName>,
12 table: TableName,
13}
14
15impl TableRef {
16 #[must_use]
18 pub const fn new(table: TableName) -> Self {
19 Self {
20 schema: None,
21 table,
22 }
23 }
24
25 #[must_use]
27 pub fn with_schema(mut self, schema: SchemaName) -> Self {
28 self.schema = Some(schema);
29 self
30 }
31
32 #[must_use]
34 pub const fn schema(&self) -> Option<&SchemaName> {
35 self.schema.as_ref()
36 }
37
38 #[must_use]
40 pub const fn table(&self) -> &TableName {
41 &self.table
42 }
43}
44
45#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
47pub enum TableKind {
48 #[default]
50 Base,
51 View,
53 Temporary,
55 External,
57 Other,
59}
60
61#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
63pub enum TableStatus {
64 #[default]
66 Active,
67 Archived,
69 Deprecated,
71 Unknown,
73}
74
75#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
77pub struct TableStats {
78 row_count: Option<u64>,
79 byte_size: Option<u64>,
80}
81
82impl TableStats {
83 #[must_use]
85 pub const fn new(row_count: Option<u64>, byte_size: Option<u64>) -> Self {
86 Self {
87 row_count,
88 byte_size,
89 }
90 }
91
92 #[must_use]
94 pub const fn row_count(self) -> Option<u64> {
95 self.row_count
96 }
97
98 #[must_use]
100 pub const fn byte_size(self) -> Option<u64> {
101 self.byte_size
102 }
103}
104
105#[derive(Clone, Debug, Eq, PartialEq)]
107pub struct TableMetadata {
108 reference: TableRef,
109 kind: TableKind,
110 status: TableStatus,
111 stats: TableStats,
112}
113
114impl TableMetadata {
115 #[must_use]
117 pub const fn new(reference: TableRef) -> Self {
118 Self {
119 reference,
120 kind: TableKind::Base,
121 status: TableStatus::Active,
122 stats: TableStats::new(None, None),
123 }
124 }
125
126 #[must_use]
128 pub const fn with_kind(mut self, kind: TableKind) -> Self {
129 self.kind = kind;
130 self
131 }
132
133 #[must_use]
135 pub const fn with_status(mut self, status: TableStatus) -> Self {
136 self.status = status;
137 self
138 }
139
140 #[must_use]
142 pub const fn with_stats(mut self, stats: TableStats) -> Self {
143 self.stats = stats;
144 self
145 }
146
147 #[must_use]
149 pub const fn reference(&self) -> &TableRef {
150 &self.reference
151 }
152
153 #[must_use]
155 pub const fn kind(&self) -> TableKind {
156 self.kind
157 }
158
159 #[must_use]
161 pub const fn status(&self) -> TableStatus {
162 self.status
163 }
164
165 #[must_use]
167 pub const fn stats(&self) -> TableStats {
168 self.stats
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::{TableKind, TableMetadata, TableRef, TableStats, TableStatus};
175 use use_db_name::{SchemaName, TableName};
176
177 #[test]
178 fn stores_table_metadata() -> Result<(), Box<dyn std::error::Error>> {
179 let table = TableRef::new(TableName::new("users")?).with_schema(SchemaName::new("public")?);
180 let metadata = TableMetadata::new(table)
181 .with_kind(TableKind::Base)
182 .with_status(TableStatus::Active)
183 .with_stats(TableStats::new(Some(10), Some(2048)));
184
185 assert_eq!(
186 metadata.reference().schema().expect("schema").as_str(),
187 "public"
188 );
189 assert_eq!(metadata.stats().row_count(), Some(10));
190 assert_eq!(metadata.kind(), TableKind::Base);
191 Ok(())
192 }
193}