burncloud_database_impl/
mysql.rs

1#[cfg(feature = "mysql")]
2use mysql_async::{Pool, Conn, prelude::Queryable, OptsBuilder};
3use burncloud_database_core::{
4    DatabaseConnection, QueryExecutor,
5    TransactionManager, Transaction, QueryContext, QueryOptions, QueryResult, QueryParam
6};
7use burncloud_database_core::error::{DatabaseResult, DatabaseError};
8use async_trait::async_trait;
9
10#[cfg(feature = "mysql")]
11pub struct MySQLConnection {
12    pool: Option<Pool>,
13    connection_string: String,
14}
15
16#[cfg(feature = "mysql")]
17impl MySQLConnection {
18    pub fn new(connection_string: String) -> Self {
19        Self {
20            pool: None,
21            connection_string,
22        }
23    }
24}
25
26#[cfg(feature = "mysql")]
27#[async_trait]
28impl DatabaseConnection for MySQLConnection {
29    async fn connect(&mut self) -> DatabaseResult<()> {
30        let pool = Pool::new(&*self.connection_string);
31        self.pool = Some(pool);
32        Ok(())
33    }
34
35    async fn disconnect(&mut self) -> DatabaseResult<()> {
36        if let Some(pool) = self.pool.take() {
37            pool.disconnect().await
38                .map_err(|e| DatabaseError::ConnectionFailed(e.to_string()))?;
39        }
40        Ok(())
41    }
42
43    async fn is_connected(&self) -> bool {
44        self.pool.is_some()
45    }
46
47    async fn ping(&self) -> DatabaseResult<()> {
48        if let Some(pool) = &self.pool {
49            let mut conn = pool.get_conn().await
50                .map_err(|e| DatabaseError::QueryFailed(e.to_string()))?;
51            conn.ping().await
52                .map_err(|e| DatabaseError::QueryFailed(e.to_string()))?;
53            Ok(())
54        } else {
55            Err(DatabaseError::ConnectionFailed("Not connected".to_string()))
56        }
57    }
58}
59
60#[cfg(feature = "mysql")]
61#[async_trait]
62impl QueryExecutor for MySQLConnection {
63    async fn execute_query(
64        &self,
65        _query: &str,
66        _params: &[&dyn QueryParam],
67        _context: &QueryContext,
68    ) -> DatabaseResult<QueryResult> {
69        Ok(QueryResult {
70            rows: Vec::new(),
71            rows_affected: 0,
72            last_insert_id: None,
73        })
74    }
75
76    async fn execute_query_with_options(
77        &self,
78        query: &str,
79        params: &[&dyn QueryParam],
80        _options: &QueryOptions,
81        context: &QueryContext,
82    ) -> DatabaseResult<QueryResult> {
83        self.execute_query(query, params, context).await
84    }
85}
86
87#[cfg(feature = "mysql")]
88pub struct MySQLTransaction {
89    // Simplified for example
90}
91
92#[cfg(feature = "mysql")]
93#[async_trait]
94impl Transaction for MySQLTransaction {
95    async fn commit(self) -> DatabaseResult<()> {
96        Ok(())
97    }
98
99    async fn rollback(self) -> DatabaseResult<()> {
100        Ok(())
101    }
102
103    async fn execute_query(
104        &self,
105        _query: &str,
106        _params: &[&dyn QueryParam],
107    ) -> DatabaseResult<QueryResult> {
108        Ok(QueryResult {
109            rows: Vec::new(),
110            rows_affected: 0,
111            last_insert_id: None,
112        })
113    }
114}
115
116#[cfg(feature = "mysql")]
117#[async_trait]
118impl TransactionManager for MySQLConnection {
119    type Transaction = MySQLTransaction;
120
121    async fn begin_transaction(&self, _context: &QueryContext) -> DatabaseResult<Self::Transaction> {
122        Ok(MySQLTransaction {})
123    }
124}