axion_db/introspection/
mod.rs

1// axion-db/src/introspection/mod.rs
2use crate::{
3    client::DbClient,
4    config::DatabaseType,
5    error::{DbError, DbResult},
6    metadata::{DatabaseMetadata, EnumMetadata, SchemaMetadata, TableMetadata, ViewMetadata},
7};
8use std::{collections::HashMap, sync::Arc};
9
10// --- Implementations for each dialect ---
11pub mod postgres;
12// pub mod mysql; // Future
13
14/// The main Introspector trait that all database-specific introspectors must implement.
15#[async_trait::async_trait]
16pub trait Introspector: Send + Sync {
17    async fn list_user_schemas(&self) -> DbResult<Vec<String>>;
18    async fn introspect(&self, schemas: &[String]) -> DbResult<DatabaseMetadata>;
19    async fn introspect_schema(&self, schema_name: &str) -> DbResult<SchemaMetadata>;
20    async fn introspect_table(
21        &self,
22        schema_name: &str,
23        table_name: &str,
24    ) -> DbResult<TableMetadata>;
25    async fn introspect_view(&self, schema_name: &str, view_name: &str) -> DbResult<ViewMetadata>;
26    async fn introspect_enums_for_schema(
27        &self,
28        schema_name: &str,
29    ) -> DbResult<HashMap<String, EnumMetadata>>;
30}
31
32// ==============================================================================
33//  The Dispatcher Macro and Enum
34// ==============================================================================
35
36/// A factory function that creates the correct, boxed introspector based on the database dialect.
37pub fn new_introspector(client: Arc<DbClient>) -> DbResult<Box<dyn Introspector>> {
38    match client.config.db_type {
39        DatabaseType::Postgres => Ok(Box::new(postgres::PostgresIntrospector::new(client))),
40        // Future dialects would be added here:
41        // DatabaseType::Mysql => Ok(Box::new(mysql::MySqlIntrospector::new(client))),
42        _ => Err(DbError::UnsupportedDbType(
43            "This database type is not yet supported for introspection.".to_string(),
44        )),
45    }
46}