#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use use_db_name::{SchemaName, TableName};
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TableRef {
schema: Option<SchemaName>,
table: TableName,
}
impl TableRef {
#[must_use]
pub const fn new(table: TableName) -> Self {
Self {
schema: None,
table,
}
}
#[must_use]
pub fn with_schema(mut self, schema: SchemaName) -> Self {
self.schema = Some(schema);
self
}
#[must_use]
pub const fn schema(&self) -> Option<&SchemaName> {
self.schema.as_ref()
}
#[must_use]
pub const fn table(&self) -> &TableName {
&self.table
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum TableKind {
#[default]
Base,
View,
Temporary,
External,
Other,
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum TableStatus {
#[default]
Active,
Archived,
Deprecated,
Unknown,
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TableStats {
row_count: Option<u64>,
byte_size: Option<u64>,
}
impl TableStats {
#[must_use]
pub const fn new(row_count: Option<u64>, byte_size: Option<u64>) -> Self {
Self {
row_count,
byte_size,
}
}
#[must_use]
pub const fn row_count(self) -> Option<u64> {
self.row_count
}
#[must_use]
pub const fn byte_size(self) -> Option<u64> {
self.byte_size
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TableMetadata {
reference: TableRef,
kind: TableKind,
status: TableStatus,
stats: TableStats,
}
impl TableMetadata {
#[must_use]
pub const fn new(reference: TableRef) -> Self {
Self {
reference,
kind: TableKind::Base,
status: TableStatus::Active,
stats: TableStats::new(None, None),
}
}
#[must_use]
pub const fn with_kind(mut self, kind: TableKind) -> Self {
self.kind = kind;
self
}
#[must_use]
pub const fn with_status(mut self, status: TableStatus) -> Self {
self.status = status;
self
}
#[must_use]
pub const fn with_stats(mut self, stats: TableStats) -> Self {
self.stats = stats;
self
}
#[must_use]
pub const fn reference(&self) -> &TableRef {
&self.reference
}
#[must_use]
pub const fn kind(&self) -> TableKind {
self.kind
}
#[must_use]
pub const fn status(&self) -> TableStatus {
self.status
}
#[must_use]
pub const fn stats(&self) -> TableStats {
self.stats
}
}
#[cfg(test)]
mod tests {
use super::{TableKind, TableMetadata, TableRef, TableStats, TableStatus};
use use_db_name::{SchemaName, TableName};
#[test]
fn stores_table_metadata() -> Result<(), Box<dyn std::error::Error>> {
let table = TableRef::new(TableName::new("users")?).with_schema(SchemaName::new("public")?);
let metadata = TableMetadata::new(table)
.with_kind(TableKind::Base)
.with_status(TableStatus::Active)
.with_stats(TableStats::new(Some(10), Some(2048)));
assert_eq!(
metadata.reference().schema().expect("schema").as_str(),
"public"
);
assert_eq!(metadata.stats().row_count(), Some(10));
assert_eq!(metadata.kind(), TableKind::Base);
Ok(())
}
}