Skip to main content

ignis_db/database/
mod.rs

1//! Multi-platform database abstraction layer
2//!
3//! This module provides the core abstraction traits and implementations
4//! for supporting multiple databases in Ignis-DB.
5
6pub mod mysql;
7pub mod postgres;
8pub mod schema_utils;
9pub mod traits;
10
11pub use mysql::MySQL;
12pub use postgres::PostgreSQL;
13pub use traits::*;
14
15/// Database connection configuration
16#[derive(Debug, Clone)]
17pub struct DatabaseConfig {
18    pub host: String,
19    pub port: u16,
20    pub database: String,
21    pub username: String,
22    pub password: String,
23    pub ssl_mode: Option<String>,
24}
25
26impl DatabaseConfig {
27    /// Create a new database configuration
28    pub fn new(
29        host: impl Into<String>,
30        port: u16,
31        database: impl Into<String>,
32        username: impl Into<String>,
33        password: impl Into<String>,
34    ) -> Self {
35        Self {
36            host: host.into(),
37            port,
38            database: database.into(),
39            username: username.into(),
40            password: password.into(),
41            ssl_mode: None,
42        }
43    }
44
45    /// Create a PostgreSQL configuration
46    pub fn postgres(
47        host: impl Into<String>,
48        port: u16,
49        database: impl Into<String>,
50        username: impl Into<String>,
51        password: impl Into<String>,
52    ) -> Self {
53        Self::new(host, port, database, username, password)
54    }
55
56    /// Create a MySQL configuration
57    pub fn mysql(
58        host: impl Into<String>,
59        port: u16,
60        database: impl Into<String>,
61        username: impl Into<String>,
62        password: impl Into<String>,
63    ) -> Self {
64        Self::new(host, port, database, username, password)
65    }
66}
67
68/// Database type enumeration for runtime database selection
69#[derive(Debug, Clone, PartialEq)]
70pub enum DatabaseType {
71    PostgreSQL,
72    MySQL,
73    SQLite,
74    MSSQL,
75}
76
77impl DatabaseType {
78    /// Get the default port for the database type
79    pub fn default_port(&self) -> u16 {
80        match self {
81            DatabaseType::PostgreSQL => 5432,
82            DatabaseType::MySQL => 3306,
83            DatabaseType::SQLite => 0, // SQLite doesn't use ports
84            DatabaseType::MSSQL => 1433,
85        }
86    }
87
88    /// Get the database name for display purposes
89    pub fn display_name(&self) -> &'static str {
90        match self {
91            DatabaseType::PostgreSQL => "PostgreSQL",
92            DatabaseType::MySQL => "MySQL",
93            DatabaseType::SQLite => "SQLite",
94            DatabaseType::MSSQL => "Microsoft SQL Server",
95        }
96    }
97}