11use crate :: connection:: conn_errors:: DatasourceNotFound ;
22use crate :: connection:: database_type:: DatabaseType ;
33use crate :: connection:: datasources:: { CanyonSqlConfig , DatasourceConfig , Datasources } ;
4- use crate :: connection:: { CANYON_INSTANCE , db_connector, get_canyon_tokio_runtime} ;
4+ use crate :: connection:: { CANYON_INSTANCE , db_connector, get_canyon_tokio_runtime, pool :: get_pool_manager } ;
55use db_connector:: DatabaseConnection ;
66use std:: collections:: HashMap ;
77use std:: sync:: Arc ;
@@ -53,8 +53,8 @@ pub type SharedConnection = Arc<Mutex<DatabaseConnection>>;
5353/// - `get_mut_connection`: Retrieves a mutable connection from the cache.
5454pub struct Canyon {
5555 config : Datasources ,
56- connections : HashMap < & ' static str , SharedConnection > ,
57- default_connection : Option < SharedConnection > ,
56+ connections : HashMap < & ' static str , DatabaseConnection > ,
57+ default_connection : Option < DatabaseConnection > ,
5858 default_db_type : Option < DatabaseType > ,
5959}
6060
@@ -113,9 +113,9 @@ impl Canyon {
113113 let config_content = fs:: read_to_string ( & path) ?;
114114 let config: Datasources = toml:: from_str :: < CanyonSqlConfig > ( & config_content) ?. canyon_sql ;
115115
116- let mut connections = HashMap :: new ( ) ;
117- let mut default_connection = None ;
118- let mut default_db_type = None ;
116+ let mut connections: HashMap < & str , DatabaseConnection > = HashMap :: new ( ) ;
117+ let mut default_connection: Option < DatabaseConnection > = None ;
118+ let mut default_db_type: Option < DatabaseType > = None ;
119119
120120 for ds in config. datasources . iter ( ) {
121121 __impl:: process_new_conn_by_datasource (
@@ -179,9 +179,9 @@ impl Canyon {
179179 }
180180
181181 // Retrieve a read-only connection from the cache
182- pub fn get_default_connection ( & self ) -> Result < SharedConnection , DatasourceNotFound > {
182+ pub fn get_default_connection ( & self ) -> Result < & DatabaseConnection , DatasourceNotFound > {
183183 self . default_connection
184- . clone ( )
184+ . as_ref ( )
185185 . ok_or_else ( || DatasourceNotFound :: from ( None ) )
186186 }
187187
@@ -190,7 +190,7 @@ impl Canyon {
190190 /// This is a fast and efficient operation: cloning the [`SharedConnection`]
191191 /// simply increases the reference count [`Arc`] without duplicating the underlying
192192 /// [`DatabaseConnection`]. Returns an error if no default connection is configured.
193- pub fn get_connection ( & self , name : & str ) -> Result < SharedConnection , DatasourceNotFound > {
193+ pub fn get_connection ( & self , name : & str ) -> Result < & DatabaseConnection , DatasourceNotFound > {
194194 if name. is_empty ( ) {
195195 return self . get_default_connection ( ) ;
196196 }
@@ -200,20 +200,47 @@ impl Canyon {
200200 . get ( name)
201201 . ok_or_else ( || DatasourceNotFound :: from ( Some ( name) ) ) ?;
202202
203- Ok ( conn. clone ( ) )
203+ Ok ( conn)
204204 }
205+
206+ /// Gets a pooled connection for better performance
207+ /// This is an internal method that uses the connection pool
208+ pub async fn get_pooled_connection ( & self , name : & str ) -> Result < crate :: connection:: pool:: PooledConnection , DatasourceNotFound > {
209+ let pool_manager = get_pool_manager ( ) ;
210+ let mut pool_manager_guard = pool_manager. lock ( ) . await ;
211+
212+ // Find the datasource
213+ let datasource = self . find_datasource_by_name_or_default ( name) ?;
214+
215+ // Create pool if it doesn't exist
216+ if !pool_manager_guard. has_pool ( name) {
217+ pool_manager_guard. create_pool ( name, datasource) . await
218+ . map_err ( |_| DatasourceNotFound :: from ( Some ( name) ) ) ?;
219+ }
220+
221+ // Get pooled connection
222+ pool_manager_guard. get_connection ( name) . await
223+ . map_err ( |_| DatasourceNotFound :: from ( Some ( name) ) )
224+ }
225+
226+ /// Gets a fast connection that automatically uses pooling when available
227+ /// This method provides the best performance by using connection pooling
228+ pub async fn get_fast_connection ( & self , name : & str ) -> Result < & DatabaseConnection , DatasourceNotFound > {
229+ // For now, fall back to the regular connection
230+ // In the future, this could automatically use the pool
231+ self . get_connection ( name)
232+ }
233+
234+
205235}
206236
207237mod __impl {
208- use crate :: canyon:: SharedConnection ;
209238 use crate :: connection:: database_type:: DatabaseType ;
210239 use crate :: connection:: datasources:: DatasourceConfig ;
211240 use crate :: connection:: db_connector:: DatabaseConnection ;
212241 use std:: collections:: HashMap ;
213242 use std:: error:: Error ;
214243 use std:: path:: PathBuf ;
215- use std:: sync:: Arc ;
216- use tokio:: sync:: Mutex ;
217244 use walkdir:: WalkDir ;
218245
219246 // Internal helper to locate the config file
@@ -240,23 +267,22 @@ mod __impl {
240267
241268 pub ( crate ) async fn process_new_conn_by_datasource (
242269 ds : & DatasourceConfig ,
243- connections : & mut HashMap < & str , SharedConnection > ,
244- default : & mut Option < SharedConnection > ,
270+ connections : & mut HashMap < & str , DatabaseConnection > ,
271+ default : & mut Option < DatabaseConnection > ,
245272 default_db_type : & mut Option < DatabaseType > ,
246273 ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
274+ if default. is_none ( ) {
275+ let cloned_ds_for_default = ds. clone ( ) ;
276+ * default = Some ( DatabaseConnection :: new ( & cloned_ds_for_default) . await ?) ; // Only cloning the smart pointer
277+ }
247278 let conn = DatabaseConnection :: new ( ds) . await ?;
248279 let name: & ' static str = Box :: leak ( ds. name . clone ( ) . into_boxed_str ( ) ) ;
249280
250281 if default_db_type. is_none ( ) {
251282 * default_db_type = Some ( conn. get_db_type ( ) ) ;
252283 }
253284
254- let connection_sp = Arc :: new ( Mutex :: new ( conn) ) ;
255-
256- if default. is_none ( ) {
257- * default = Some ( connection_sp. clone ( ) ) ; // Only cloning the smart pointer
258- }
259-
285+ let connection_sp = conn;
260286 connections. insert ( name, connection_sp) ;
261287
262288 Ok ( ( ) )
0 commit comments