use crate::migrate::Migration;
use crate::schema::{CompositeType, Grant, Index, Role, Table, View};
use anyhow::Result;
use std::collections::HashMap;
pub trait Database: Send + Sync {
fn database_type(&self) -> crate::database::DatabaseType;
fn name(&self) -> &str;
fn type_mapping(&self) -> &dyn TypeMapping;
fn sql_generator(&self) -> &dyn SqlGenerator;
fn introspector(&self) -> &dyn Introspector;
fn connection_manager(&self) -> &dyn ConnectionManager;
fn migration_manager(&self) -> &dyn MigrationManager;
}
pub trait TypeMapping: Send + Sync {
fn rust_to_sql_type(&self, rust_type: &str) -> String;
fn sql_to_rust_type(&self, sql_type: &str) -> String;
fn default_value(&self, sql_type: &str) -> Option<String>;
fn supports_auto_increment(&self, sql_type: &str) -> bool;
fn max_string_length(&self, sql_type: &str) -> Option<usize>;
}
pub trait SqlGenerator: Send + Sync {
fn create_table_sql(&self, table: &Table) -> String;
fn create_view_sql(&self, view: &View) -> String;
fn create_index_sql(&self, index: &Index) -> String;
fn create_type_sql(&self, composite_type: &CompositeType) -> String;
fn create_schema_sql(&self, schema_name: &str) -> String;
fn alter_table_sql(&self, table: &Table, changes: &[String]) -> String;
fn drop_table_sql(&self, table_name: &str) -> String;
fn drop_view_sql(&self, view_name: &str) -> String;
fn drop_index_sql(&self, index_name: &str) -> String;
fn insert_sql(&self, table_name: &str, columns: &[String], values: &[String]) -> String;
fn update_sql(&self, table_name: &str, set_clause: &str, where_clause: &str) -> String;
fn delete_sql(&self, table_name: &str, where_clause: &str) -> String;
fn select_sql(
&self,
table_name: &str,
columns: &[String],
where_clause: Option<&str>,
) -> String;
fn quote_identifier(&self, identifier: &str) -> String;
fn quote_string(&self, value: &str) -> String;
fn current_timestamp_function(&self) -> &str;
fn auto_increment_keyword(&self) -> &str;
fn create_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
fn reset_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
fn get_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
fn list_guc_settings_sql(&self) -> String;
fn set_jwt_context_sql(
&self,
user_id: &str,
tenant_id: Option<&str>,
role: Option<&str>,
) -> String;
fn clear_jwt_context_sql(&self) -> String;
}
pub trait Introspector: Send + Sync {
fn get_tables(&self) -> Result<Vec<Table>>;
fn get_table(&self, table_name: &str) -> Result<Option<Table>>;
fn get_views(&self) -> Result<Vec<View>>;
fn get_view(&self, view_name: &str) -> Result<Option<View>>;
fn get_indexes(&self) -> Result<Vec<Index>>;
fn get_table_indexes(&self, table_name: &str) -> Result<Vec<Index>>;
fn get_composite_types(&self) -> Result<Vec<CompositeType>>;
fn get_composite_type(&self, type_name: &str) -> Result<Option<CompositeType>>;
fn get_roles(&self) -> Result<Vec<Role>>;
fn get_grants(&self) -> Result<Vec<Grant>>;
fn get_schema_version(&self) -> Result<Option<String>>;
fn table_exists(&self, table_name: &str) -> Result<bool>;
fn view_exists(&self, view_name: &str) -> Result<bool>;
fn index_exists(&self, index_name: &str) -> Result<bool>;
}
pub trait ConnectionManager: Send + Sync {
fn create_pool(
&self,
config: &crate::database::DatabaseConfig,
) -> Result<Box<dyn ConnectionPool>>;
fn test_connection(&self, config: &crate::database::DatabaseConfig) -> Result<()>;
fn connection_string(&self, config: &crate::database::DatabaseConfig) -> String;
fn connection_options(
&self,
config: &crate::database::DatabaseConfig,
) -> Result<Box<dyn std::fmt::Debug>>;
}
pub trait ConnectionPool: Send + Sync {
fn execute_query(
&self,
sql: &str,
params: &[&dyn std::fmt::Display],
) -> Result<Vec<HashMap<String, String>>>;
fn execute_command(&self, sql: &str, params: &[&dyn std::fmt::Display]) -> Result<u64>;
fn begin_transaction(&self) -> Result<Box<dyn Transaction>>;
fn active_connections(&self) -> u32;
fn max_connections(&self) -> u32;
}
pub trait Transaction: Send + Sync {
fn execute_query(
&self,
sql: &str,
params: &[&dyn std::fmt::Display],
) -> Result<Vec<HashMap<String, String>>>;
fn execute_command(&self, sql: &str, params: &[&dyn std::fmt::Display]) -> Result<u64>;
fn commit(self: Box<Self>) -> Result<()>;
fn rollback(self: Box<Self>) -> Result<()>;
}
pub trait MigrationManager: Send + Sync {
fn get_applied_migrations(&self) -> Result<Vec<Migration>>;
fn apply_migration(&self, migration: &Migration) -> Result<()>;
fn rollback_migration(&self, migration: &Migration) -> Result<()>;
fn is_migration_applied(&self, migration_id: &str) -> Result<bool>;
fn create_migrations_table(&self) -> Result<()>;
fn get_current_version(&self) -> Result<Option<String>>;
fn set_schema_version(&self, version: &str) -> Result<()>;
}