@@ -14,15 +14,90 @@ pub use sql::ParsedSqlFile;
1414use sql:: { DbPlaceHolder , DB_PLACEHOLDERS } ;
1515use sqlx:: any:: AnyKind ;
1616
17+ /// Supported database types in `SQLPage`
18+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
19+ pub enum SupportedDatabase {
20+ Sqlite ,
21+ Postgres ,
22+ MySql ,
23+ Mssql ,
24+ Odbc ,
25+ }
26+
27+ impl SupportedDatabase {
28+ /// Detect the database type from a connection's `dbms_name`
29+ #[ must_use]
30+ pub fn from_dbms_name ( dbms_name : & str ) -> Option < Self > {
31+ match dbms_name. to_lowercase ( ) . as_str ( ) {
32+ "sqlite" | "sqlite3" => Some ( Self :: Sqlite ) ,
33+ "postgres" | "postgresql" => Some ( Self :: Postgres ) ,
34+ "mysql" | "mariadb" => Some ( Self :: MySql ) ,
35+ "mssql" | "sql server" | "microsoft sql server" => Some ( Self :: Mssql ) ,
36+ "odbc" => Some ( Self :: Odbc ) ,
37+ _ => None ,
38+ }
39+ }
40+
41+ /// Convert from sqlx `AnyKind` to our enum
42+ #[ must_use]
43+ pub fn from_any_kind ( kind : AnyKind ) -> Self {
44+ match kind {
45+ AnyKind :: Sqlite => Self :: Sqlite ,
46+ AnyKind :: Postgres => Self :: Postgres ,
47+ AnyKind :: MySql => Self :: MySql ,
48+ AnyKind :: Mssql => Self :: Mssql ,
49+ AnyKind :: Odbc => Self :: Odbc ,
50+ }
51+ }
52+
53+ /// Get the display name for the database
54+ #[ must_use]
55+ pub fn display_name ( self ) -> & ' static str {
56+ match self {
57+ Self :: Sqlite => "SQLite" ,
58+ Self :: Postgres => "PostgreSQL" ,
59+ Self :: MySql => "MySQL" ,
60+ Self :: Mssql => "Microsoft SQL Server" ,
61+ Self :: Odbc => "ODBC" ,
62+ }
63+ }
64+ }
65+
1766pub struct Database {
1867 pub connection : sqlx:: AnyPool ,
1968}
69+
2070impl Database {
2171 pub async fn close ( & self ) -> anyhow:: Result < ( ) > {
2272 log:: info!( "Closing all database connections..." ) ;
2373 self . connection . close ( ) . await ;
2474 Ok ( ( ) )
2575 }
76+
77+ /// Detect the database type using the connection's `dbms_name`
78+ pub async fn detect_database_type ( & self ) -> anyhow:: Result < SupportedDatabase > {
79+ let mut conn = self . connection . acquire ( ) . await ?;
80+ let dbms_name = conn. dbms_name ( ) . await ?;
81+
82+ if let Some ( db_type) = SupportedDatabase :: from_dbms_name ( & dbms_name) {
83+ log:: debug!(
84+ "Detected database type: {} from dbms_name: {}" ,
85+ db_type. display_name( ) ,
86+ dbms_name
87+ ) ;
88+ Ok ( db_type)
89+ } else {
90+ log:: warn!( "Unknown database type from dbms_name: {dbms_name}" ) ;
91+ // Fallback to AnyKind detection
92+ Ok ( SupportedDatabase :: from_any_kind ( self . connection . any_kind ( ) ) )
93+ }
94+ }
95+
96+ /// Get the database type using the fallback method (`AnyKind`)
97+ #[ must_use]
98+ pub fn get_database_type ( & self ) -> SupportedDatabase {
99+ SupportedDatabase :: from_any_kind ( self . connection . any_kind ( ) )
100+ }
26101}
27102
28103#[ derive( Debug ) ]
0 commit comments