Skip to main content

use_db_table/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! Table metadata primitives for `RustUse`.
5
6use use_db_name::{SchemaName, TableName};
7
8/// A table reference with optional schema qualification.
9#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct TableRef {
11    schema: Option<SchemaName>,
12    table: TableName,
13}
14
15impl TableRef {
16    /// Creates a table reference.
17    #[must_use]
18    pub const fn new(table: TableName) -> Self {
19        Self {
20            schema: None,
21            table,
22        }
23    }
24
25    /// Adds a schema qualifier.
26    #[must_use]
27    pub fn with_schema(mut self, schema: SchemaName) -> Self {
28        self.schema = Some(schema);
29        self
30    }
31
32    /// Returns the optional schema name.
33    #[must_use]
34    pub const fn schema(&self) -> Option<&SchemaName> {
35        self.schema.as_ref()
36    }
37
38    /// Returns the table name.
39    #[must_use]
40    pub const fn table(&self) -> &TableName {
41        &self.table
42    }
43}
44
45/// Broad table families.
46#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
47pub enum TableKind {
48    /// A base table.
49    #[default]
50    Base,
51    /// A view-like table.
52    View,
53    /// A temporary table.
54    Temporary,
55    /// A foreign or external table.
56    External,
57    /// Other or unspecified table kind.
58    Other,
59}
60
61/// Broad table lifecycle status.
62#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
63pub enum TableStatus {
64    /// Table is active.
65    #[default]
66    Active,
67    /// Table is archived.
68    Archived,
69    /// Table is deprecated.
70    Deprecated,
71    /// Status is unknown.
72    Unknown,
73}
74
75/// Basic table statistics.
76#[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    /// Creates table statistics.
84    #[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    /// Returns the optional row count.
93    #[must_use]
94    pub const fn row_count(self) -> Option<u64> {
95        self.row_count
96    }
97
98    /// Returns the optional byte size.
99    #[must_use]
100    pub const fn byte_size(self) -> Option<u64> {
101        self.byte_size
102    }
103}
104
105/// Table metadata.
106#[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    /// Creates table metadata.
116    #[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    /// Sets the table kind.
127    #[must_use]
128    pub const fn with_kind(mut self, kind: TableKind) -> Self {
129        self.kind = kind;
130        self
131    }
132
133    /// Sets the table status.
134    #[must_use]
135    pub const fn with_status(mut self, status: TableStatus) -> Self {
136        self.status = status;
137        self
138    }
139
140    /// Sets table statistics.
141    #[must_use]
142    pub const fn with_stats(mut self, stats: TableStats) -> Self {
143        self.stats = stats;
144        self
145    }
146
147    /// Returns the table reference.
148    #[must_use]
149    pub const fn reference(&self) -> &TableRef {
150        &self.reference
151    }
152
153    /// Returns the table kind.
154    #[must_use]
155    pub const fn kind(&self) -> TableKind {
156        self.kind
157    }
158
159    /// Returns the table status.
160    #[must_use]
161    pub const fn status(&self) -> TableStatus {
162        self.status
163    }
164
165    /// Returns the table statistics.
166    #[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}