diff --git a/src/shadow_tables/lookup_table.rs b/src/shadow_tables/lookup_table.rs index fcd7b7c..f3e1f3b 100644 --- a/src/shadow_tables/lookup_table.rs +++ b/src/shadow_tables/lookup_table.rs @@ -75,8 +75,8 @@ pub trait Lookup { impl PartitionType for LookupTable { const PARTITION_NAME_COLUMN: &'static str = "partition_table"; const PARTITION_VALUE_COLUMN: &'static str = "partition_value"; - const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue = &PartitionValue::Interval; - const PARTITION_NAME_COLUMN_TYPE: &'static ValueType = &ValueType::Text; + const PARTITION_VALUE_COLUMN_TYPE: PartitionValue = PartitionValue::Interval; + const PARTITION_NAME_COLUMN_TYPE: ValueType = ValueType::Text; } impl Table for LookupTable { const POSTFIX: &'static str = "lookup"; @@ -89,8 +89,8 @@ impl Create for LookupTable { Ok(format!( "CREATE TABLE {} ({} UNIQUE, {} UNIQUE);", schema.name(), - ::partition_name_column(), - ::partition_value_column() + ::COLUMNS[0], + ::COLUMNS[1] )) } } @@ -106,11 +106,11 @@ impl LookupTable { parse_to_unix_epoch(value).map(|epoch| epoch - epoch % interval) } - pub fn partition_table_column(&self) -> ColumnDeclaration { - ::partition_name_column() + pub fn partition_table_column(&self) -> &'static ColumnDeclaration { + &::COLUMNS[0] } - pub fn partition_value_column(&self) -> ColumnDeclaration { - ::partition_value_column() + pub fn partition_value_column(&self) -> &'static ColumnDeclaration { + &::COLUMNS[1] } /// Creates a new instance of `LookupTable` with a specified base name. This involves initializing diff --git a/src/shadow_tables/mod.rs b/src/shadow_tables/mod.rs index 8572df1..cb46ec9 100644 --- a/src/shadow_tables/mod.rs +++ b/src/shadow_tables/mod.rs @@ -17,6 +17,13 @@ pub enum PartitionValue { Interval, } +impl PartitionValue { + const fn to_valuetype(partitionvalue: Self) -> ValueType { + match partitionvalue { + Self::Interval => ValueType::Integer, + } + } +} impl From for ValueType { fn from(value: PartitionValue) -> ValueType { match value { @@ -44,27 +51,23 @@ impl<'a> TryFrom<&'a ValueType> for PartitionValue { } } } - +// type IntervalPartition = ValueType::Integer; pub trait PartitionType { - const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue; + const PARTITION_VALUE_COLUMN_TYPE: PartitionValue; const PARTITION_VALUE_COLUMN: &'static str; const PARTITION_NAME_COLUMN: &'static str; - const PARTITION_NAME_COLUMN_TYPE: &'static ValueType; + const PARTITION_NAME_COLUMN_TYPE: ValueType; + const PARTITION_IDENTIFIER: ColumnDeclaration = ColumnDeclaration::new( + std::borrow::Cow::Borrowed(Self::PARTITION_NAME_COLUMN), + Self::PARTITION_NAME_COLUMN_TYPE, + ); + const PARTITION_TYPE: ColumnDeclaration = ColumnDeclaration::new( + std::borrow::Cow::Borrowed(Self::PARTITION_VALUE_COLUMN), + PartitionValue::to_valuetype(Self::PARTITION_VALUE_COLUMN_TYPE), + ); + const COLUMNS: &'static [ColumnDeclaration] = + &[Self::PARTITION_IDENTIFIER, Self::PARTITION_TYPE]; fn columns() -> ColumnDeclarations { - ColumnDeclarations(vec![ - Self::partition_name_column(), - Self::partition_value_column(), - ]) - } - fn partition_name_column() -> ColumnDeclaration { - ColumnDeclaration::new( - Self::PARTITION_NAME_COLUMN.to_string(), - *Self::PARTITION_NAME_COLUMN_TYPE, - ) - } - - fn partition_value_column() -> ColumnDeclaration { - let value_type: &ValueType = Self::PARTITION_VALUE_COLUMN_TYPE.into(); - ColumnDeclaration::new(Self::PARTITION_VALUE_COLUMN.to_string(), *value_type) + ColumnDeclarations(Self::COLUMNS.to_vec()) } } diff --git a/src/shadow_tables/root_table.rs b/src/shadow_tables/root_table.rs index 069dc44..97405ba 100644 --- a/src/shadow_tables/root_table.rs +++ b/src/shadow_tables/root_table.rs @@ -49,9 +49,9 @@ impl PartitionType for RootTable { /// integer value in seconds. E.G 3600 if the interval was set to 1 hour. const PARTITION_VALUE_COLUMN: &'static str = "partition_value"; /// The data type of the partition value column, indicating the nature of partitioning (e.g., time intervals). - const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue = &PartitionValue::Interval; + const PARTITION_VALUE_COLUMN_TYPE: PartitionValue = PartitionValue::Interval; /// The data type of the partition name column, typically text for naming partitions. - const PARTITION_NAME_COLUMN_TYPE: &'static ValueType = &ValueType::Text; + const PARTITION_NAME_COLUMN_TYPE: ValueType = ValueType::Text; } impl RootTable { @@ -77,7 +77,7 @@ impl RootTable { ) -> ExtResult { let table_name = Self::format_name(base_name); let columns = ::columns(); - let schema = ::schema(db, table_name.to_string(), columns)?; + let schema = ::schema(db, table_name, columns)?; let table = Self { partition_column, interval, @@ -114,9 +114,9 @@ impl RootTable { for index in 0..column_count { let column = row.index_mut(index); let name = column.name()?; - if name.eq(::partition_name_column().get_name()) { + if name.eq(::COLUMNS[0].get_name()) { partition_column = column.get_str()?.to_owned(); - } else if name.eq(::partition_value_column().get_name()) { + } else if name.eq(::COLUMNS[1].get_name()) { interval = column.get_i64(); } } @@ -137,8 +137,8 @@ impl RootTable { /// /// Returns a boolean indicating success of the insertion. fn insert(&self, db: &Connection) -> ExtResult { - let partition_name_column = Self::partition_name_column().get_name().to_owned(); - let partition_value_column = Self::partition_value_column().get_name().to_owned(); + let partition_name_column = Self::COLUMNS[0].get_name().to_owned(); + let partition_value_column = Self::COLUMNS[1].get_name().to_owned(); let sql = format!( "INSERT INTO {} ({partition_name_column}, {partition_value_column}) VALUES (?, ?);", self.name() diff --git a/src/types/column_declaration.rs b/src/types/column_declaration.rs index d3da03c..707f8af 100644 --- a/src/types/column_declaration.rs +++ b/src/types/column_declaration.rs @@ -1,4 +1,5 @@ use std::{ + borrow::Cow, fmt::{self, Display}, vec, }; @@ -48,7 +49,7 @@ impl<'a> From<&'a ColumnDeclaration> for PartitionColumn { /// and whether it serves as a partition column. #[derive(Clone, Debug)] pub struct ColumnDeclaration { - name: String, + name: Cow<'static, str>, data_type: ValueType, is_partition_column: bool, is_hidden: bool, @@ -56,7 +57,7 @@ pub struct ColumnDeclaration { impl ColumnDeclaration { /// Constructs a new `ColumnDeclaration`. - pub const fn new(name: String, data_type: ValueType) -> Self + pub const fn new(name: Cow<'static, str>, data_type: ValueType) -> Self where Self: Sized, { @@ -116,7 +117,7 @@ impl<'a> TryFrom<&'a str> for ColumnDeclaration { } Ok(Self { - name: tokens[0].trim().to_string(), + name: Cow::Owned(tokens[0].trim().to_string()), data_type: parse_value_type(&tokens[1].trim().to_uppercase())?, is_partition_column, is_hidden: false,