use super::types::DataType;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
pub type ArcSchemaSnapshot = Arc<SchemaSnapshot>;
type TableIndex = HashMap<String, usize>;
fn new_table_index() -> Arc<OnceLock<TableIndex>> {
Arc::new(OnceLock::new())
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SchemaSnapshot {
pub tables: Vec<TableSchema>,
#[serde(default)]
pub metadata: SchemaMetadata,
#[serde(skip, default = "new_table_index")]
#[schemars(skip)]
table_index: Arc<OnceLock<TableIndex>>,
}
impl Default for SchemaSnapshot {
fn default() -> Self {
Self::new(Vec::new(), SchemaMetadata::default())
}
}
impl PartialEq for SchemaSnapshot {
fn eq(&self, other: &Self) -> bool {
self.tables == other.tables && self.metadata == other.metadata
}
}
impl Eq for SchemaSnapshot {}
impl SchemaSnapshot {
pub fn new(tables: Vec<TableSchema>, metadata: SchemaMetadata) -> Self {
let snapshot = Self {
tables,
metadata,
table_index: new_table_index(),
};
snapshot.initialize_index();
snapshot
}
pub fn set_tables(&mut self, tables: Vec<TableSchema>) {
self.tables = tables;
self.table_index = new_table_index();
self.initialize_index();
}
pub fn rebuild_index(&mut self) {
self.table_index = new_table_index();
self.initialize_index();
}
pub fn get_table(&self, name: &str) -> Option<&TableSchema> {
let index = self
.table_index
.get_or_init(|| build_table_index(&self.tables));
index
.get(name)
.and_then(|position| self.tables.get(*position))
}
pub fn get_column(&self, table: &str, column: &str) -> Option<&ColumnSchema> {
self.get_table(table)?
.columns
.iter()
.find(|candidate| candidate.name == column)
}
pub fn table_count(&self) -> usize {
self.tables.len()
}
fn initialize_index(&self) {
let _ = self.table_index.set(build_table_index(&self.tables));
}
}
fn build_table_index(tables: &[TableSchema]) -> TableIndex {
let mut index = HashMap::with_capacity(tables.len());
for (position, table) in tables.iter().enumerate() {
index.insert(table.name.clone(), position);
}
index
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SchemaMetadata {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub generated_at: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TableSchema {
pub name: String,
pub columns: Vec<ColumnSchema>,
pub description: Option<String>,
pub primary_key: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ColumnSchema {
pub name: String,
pub data_type: DataType,
pub nullable: bool,
pub description: Option<String>,
pub is_primary_key: bool,
pub foreign_key: Option<ForeignKey>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ForeignKey {
pub foreign_table: String,
pub foreign_column: String,
}