@@ -6,84 +6,99 @@ use crate::core::mysql::pool::MySqlConnectionManager;
6
6
use crate :: core:: postgres:: pool:: PostgresConnectionManager ;
7
7
use crate :: ts_generator:: information_schema:: DBSchema ;
8
8
use clap:: Parser ;
9
- use lazy_static :: lazy_static ;
9
+ use std :: sync :: LazyLock ;
10
10
use std:: { collections:: HashMap , sync:: Arc } ;
11
11
use tokio:: { runtime:: Handle , sync:: Mutex , task} ;
12
12
13
13
// The file contains all implicitly dependent variables or state that files need for the logic
14
14
// We have a lot of states that we need to drill down into each methods
15
- lazy_static ! {
16
- pub static ref CLI_ARGS : Cli = Cli :: parse( ) ;
17
- pub static ref CONFIG : Config = Config :: new( ) ;
15
+ pub static CLI_ARGS : LazyLock < Cli > = LazyLock :: new ( Cli :: parse) ;
16
+ pub static CONFIG : LazyLock < Config > = LazyLock :: new ( Config :: new) ;
18
17
19
- // This is a holder for shared DBSChema used to fetch information for information_schema table
20
- // By having a singleton, we can think about caching the result if we are fetching a query too many times
21
- pub static ref DB_SCHEMA : Arc <Mutex <DBSchema >> = Arc :: new( Mutex :: new( DBSchema :: new( ) ) ) ;
22
- pub static ref ERR_DB_CONNECTION_ISSUE : String = "Unable to connect to the database, please check the connection configuration again https://jasonshin.github.io/sqlx-ts/api/1.connecting-to-db.html" . to_string( ) ;
18
+ // This is a holder for shared DBSChema used to fetch information for information_schema table
19
+ // By having a singleton, we can think about caching the result if we are fetching a query too many times
20
+ pub static DB_SCHEMA : LazyLock < Arc < Mutex < DBSchema > > > = LazyLock :: new ( || Arc :: new ( Mutex :: new ( DBSchema :: new ( ) ) ) ) ;
21
+ // TODO: move this to errors.rs
22
+ pub static ERR_DB_CONNECTION_ISSUE : LazyLock < String > = LazyLock :: new ( || {
23
+ "Unable to connect to the database, please check the connection configuration again https://jasonshin.github.io/sqlx-ts/api/1.connecting-to-db.html" . to_string ( )
24
+ } ) ;
23
25
24
- // This variable holds database connections for each connection name that is defined in the config
25
- // We are using lazy_static to initialize the connections once and use them throughout the application
26
- static ref DB_CONN_CACHE : HashMap <String , Arc <Mutex <DBConn >>> = {
27
- let mut cache = HashMap :: new( ) ;
28
- for connection in CONFIG . connections. keys( ) {
29
- let connection_config = CONFIG . connections. get( connection)
30
- . unwrap_or_else( || panic!( "Failed to find a correct connection from the configuration - {connection}" ) ) ;
31
- let db_type = connection_config. db_type. to_owned( ) ;
32
- let conn = match db_type {
33
- DatabaseType :: Mysql => {
34
- task:: block_in_place( || Handle :: current( ) . block_on( async {
35
- let mysql_cred = CONFIG . get_mysql_cred_str( connection_config) ;
36
- let mysql_cred = mysql_cred. as_str( ) ;
37
- let manager = MySqlConnectionManager :: new( mysql_cred. to_string( ) ) ;
38
- let pool = bb8:: Pool :: builder( ) . max_size( connection_config. pool_size) . build( manager)
39
- . await
40
- . expect( & ERR_DB_CONNECTION_ISSUE ) ;
41
- DBConn :: MySQLPooledConn ( Mutex :: new( pool) )
42
- } ) )
43
- }
44
- DatabaseType :: Postgres => {
45
- task:: block_in_place( || Handle :: current( ) . block_on( async {
46
- let postgres_cred = CONFIG . get_postgres_cred( connection_config) ;
47
- let manager = PostgresConnectionManager :: new( postgres_cred) ;
48
- let pool = bb8:: Pool :: builder( ) . max_size( 10 ) . build( manager)
49
- . await
50
- . expect( & ERR_DB_CONNECTION_ISSUE ) ;
51
- let db_conn = DBConn :: PostgresConn ( Mutex :: new( pool) ) ;
26
+ // This variable holds database connections for each connection name that is defined in the config
27
+ // We are using lazy_static to initialize the connections once and use them throughout the application
28
+ pub static DB_CONN_CACHE : LazyLock < HashMap < String , Arc < Mutex < DBConn > > > > = LazyLock :: new ( || {
29
+ let mut cache = HashMap :: new ( ) ;
30
+ for connection in CONFIG . connections . keys ( ) {
31
+ let connection_config = CONFIG
32
+ . connections
33
+ . get ( connection)
34
+ . unwrap_or_else ( || panic ! ( "Invalid connection name - {connection}" ) ) ;
35
+ let db_type = connection_config. db_type . to_owned ( ) ;
36
+ let conn = match db_type {
37
+ DatabaseType :: Mysql => task:: block_in_place ( || {
38
+ Handle :: current ( ) . block_on ( async {
39
+ let mysql_cred = CONFIG . get_mysql_cred_str ( connection_config) ;
40
+ let mysql_cred = mysql_cred. as_str ( ) ;
41
+ let manager = MySqlConnectionManager :: new ( mysql_cred. to_string ( ) , connection. to_string ( ) ) ;
42
+ let pool = bb8:: Pool :: builder ( )
43
+ . max_size ( connection_config. pool_size )
44
+ . connection_timeout ( std:: time:: Duration :: from_secs ( connection_config. connection_timeout ) )
45
+ . build ( manager)
46
+ . await
47
+ . expect ( & ERR_DB_CONNECTION_ISSUE ) ;
52
48
53
- let conn = match & db_conn {
54
- DBConn :: PostgresConn ( conn) => conn,
55
- _ => panic!( "Invalid connection type" ) ,
56
- } ;
49
+ DBConn :: MySQLPooledConn ( Mutex :: new ( pool) )
50
+ } )
51
+ } ) ,
52
+ DatabaseType :: Postgres => task:: block_in_place ( || {
53
+ Handle :: current ( ) . block_on ( async {
54
+ let postgres_cred = CONFIG . get_postgres_cred ( connection_config) ;
55
+ let manager = PostgresConnectionManager :: new ( postgres_cred) ;
56
+ let pool = bb8:: Pool :: builder ( )
57
+ . max_size ( connection_config. pool_size )
58
+ . connection_timeout ( std:: time:: Duration :: from_secs ( connection_config. connection_timeout ) )
59
+ . build ( manager)
60
+ . await
61
+ . expect ( & ERR_DB_CONNECTION_ISSUE ) ;
57
62
58
- if connection_config. pg_search_path. is_some( ) {
59
- let search_path_query = format!( "SET search_path TO {}" , & connection_config. pg_search_path. clone( ) . unwrap( ) . as_str( ) ) ;
60
- {
61
- let conn = conn. lock( ) . await ;
62
- let conn = conn
63
- . get( )
64
- . await
65
- . expect( & ERR_DB_CONNECTION_ISSUE ) ;
63
+ let db_conn = DBConn :: PostgresConn ( Mutex :: new ( pool) ) ;
66
64
67
- let ERR_SEARCH_PATH_QUERY = format!( "Failed to execute the search_path query {:?}" , search_path_query. as_str( ) ) ;
68
- let ERR_SEARCH_PATH_QUERY = ERR_SEARCH_PATH_QUERY . as_str( ) ;
69
- conn. execute( & search_path_query, & [ ] ) . await
70
- . expect( ERR_SEARCH_PATH_QUERY ) ;
71
- }
72
- }
73
- db_conn
74
- } ) )
65
+ let conn = match & db_conn {
66
+ DBConn :: PostgresConn ( conn) => conn,
67
+ _ => panic ! ( "Invalid connection type" ) ,
68
+ } ;
75
69
76
- }
77
- } ;
78
- cache. insert( connection. to_owned( ) , Arc :: new( Mutex :: new( conn) ) ) ;
79
- } ;
80
- cache
81
- } ;
70
+ if connection_config. pg_search_path . is_some ( ) {
71
+ let search_path_query = format ! (
72
+ "SET search_path TO {}" ,
73
+ & connection_config. pg_search_path. clone( ) . unwrap( ) . as_str( )
74
+ ) ;
75
+ {
76
+ let conn = conn. lock ( ) . await ;
77
+ let conn = conn. get ( ) . await . expect ( & ERR_DB_CONNECTION_ISSUE ) ;
82
78
83
- // This variable holds a singleton of DBConnections that is used to get a DBConn from the cache
84
- // DBConn is used to access the raw connection to the database or run `prepare` statement against each connection
85
- pub static ref DB_CONNECTIONS : Arc <Mutex <DBConnections <' static >>> = {
86
- let db_connections = DBConnections :: new( & DB_CONN_CACHE ) ;
87
- Arc :: new( Mutex :: new( db_connections) )
79
+ let err_search_path_query = format ! (
80
+ "Failed to execute the search_path query {:?}" ,
81
+ search_path_query. as_str( )
82
+ ) ;
83
+ let err_search_path_query = err_search_path_query. as_str ( ) ;
84
+ conn
85
+ . execute ( & search_path_query, & [ ] )
86
+ . await
87
+ . expect ( err_search_path_query) ;
88
+ }
89
+ }
90
+ db_conn
91
+ } )
92
+ } ) ,
88
93
} ;
89
- }
94
+ cache. insert ( connection. to_owned ( ) , Arc :: new ( Mutex :: new ( conn) ) ) ;
95
+ }
96
+ cache
97
+ } ) ;
98
+
99
+ // This variable holds a singleton of DBConnections that is used to get a DBConn from the cache
100
+ // DBConn is used to access the raw connection to the database or run `prepare` statement against each connection
101
+ pub static DB_CONNECTIONS : LazyLock < Arc < Mutex < DBConnections < ' static > > > > = LazyLock :: new ( || {
102
+ let db_connections = DBConnections :: new ( & DB_CONN_CACHE ) ;
103
+ Arc :: new ( Mutex :: new ( db_connections) )
104
+ } ) ;
0 commit comments