burncloud_database_impl/
mongodb.rs1#[cfg(feature = "mongodb_support")]
2use mongodb::{Client, Database};
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;
9use std::collections::HashMap;
10
11#[cfg(feature = "mongodb_support")]
12pub struct MongoDBConnection {
13 client: Option<Client>,
14 database: Option<Database>,
15 connection_string: String,
16 database_name: String,
17}
18
19#[cfg(feature = "mongodb_support")]
20impl MongoDBConnection {
21 pub fn new(connection_string: String, database_name: String) -> Self {
22 Self {
23 client: None,
24 database: None,
25 connection_string,
26 database_name,
27 }
28 }
29}
30
31#[cfg(feature = "mongodb_support")]
32#[async_trait]
33impl DatabaseConnection for MongoDBConnection {
34 async fn connect(&mut self) -> DatabaseResult<()> {
35 let client = Client::with_uri_str(&self.connection_string).await
36 .map_err(|e| DatabaseError::ConnectionFailed(e.to_string()))?;
37
38 let database = client.database(&self.database_name);
39
40 self.client = Some(client);
41 self.database = Some(database);
42 Ok(())
43 }
44
45 async fn disconnect(&mut self) -> DatabaseResult<()> {
46 self.client = None;
47 self.database = None;
48 Ok(())
49 }
50
51 async fn is_connected(&self) -> bool {
52 self.client.is_some() && self.database.is_some()
53 }
54
55 async fn ping(&self) -> DatabaseResult<()> {
56 if let Some(database) = &self.database {
57 database.run_command(mongodb::bson::doc! { "ping": 1 }).await
58 .map_err(|e| DatabaseError::QueryFailed(e.to_string()))?;
59 Ok(())
60 } else {
61 Err(DatabaseError::ConnectionFailed("Not connected".to_string()))
62 }
63 }
64}
65
66#[cfg(feature = "mongodb_support")]
67#[async_trait]
68impl QueryExecutor for MongoDBConnection {
69 async fn execute_query(
70 &self,
71 _query: &str,
72 _params: &[&dyn QueryParam],
73 _context: &QueryContext,
74 ) -> DatabaseResult<QueryResult> {
75 Ok(QueryResult {
76 rows: Vec::new(),
77 rows_affected: 0,
78 last_insert_id: None,
79 })
80 }
81
82 async fn execute_query_with_options(
83 &self,
84 query: &str,
85 params: &[&dyn QueryParam],
86 _options: &QueryOptions,
87 context: &QueryContext,
88 ) -> DatabaseResult<QueryResult> {
89 self.execute_query(query, params, context).await
90 }
91}
92
93#[cfg(feature = "mongodb_support")]
94pub struct MongoDBTransaction {
95 }
97
98#[cfg(feature = "mongodb_support")]
99#[async_trait]
100impl Transaction for MongoDBTransaction {
101 async fn commit(self) -> DatabaseResult<()> {
102 Ok(())
103 }
104
105 async fn rollback(self) -> DatabaseResult<()> {
106 Ok(())
107 }
108
109 async fn execute_query(
110 &self,
111 _query: &str,
112 _params: &[&dyn QueryParam],
113 ) -> DatabaseResult<QueryResult> {
114 Ok(QueryResult {
115 rows: Vec::new(),
116 rows_affected: 0,
117 last_insert_id: None,
118 })
119 }
120}
121
122#[cfg(feature = "mongodb_support")]
123#[async_trait]
124impl TransactionManager for MongoDBConnection {
125 type Transaction = MongoDBTransaction;
126
127 async fn begin_transaction(&self, _context: &QueryContext) -> DatabaseResult<Self::Transaction> {
128 Ok(MongoDBTransaction {})
129 }
130}