11package com .eternalcode .economy .database ;
22
3- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .H2_DRIVER ;
4- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .H2_JDBC_URL ;
5- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .MARIADB_DRIVER ;
6- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .MARIADB_JDBC_URL ;
7- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .MYSQL_DRIVER ;
8- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .MYSQL_JDBC_URL ;
9- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .POSTGRESQL_DRIVER ;
10- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .POSTGRESQL_JDBC_URL ;
11- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .SQLITE_DRIVER ;
12- import static com .eternalcode .economy .database .DatabaseConnectionDriverConstant .SQLITE_JDBC_URL ;
13-
143import com .google .common .base .Stopwatch ;
154import com .j256 .ormlite .dao .Dao ;
165import com .j256 .ormlite .dao .DaoManager ;
@@ -28,94 +17,65 @@ public class DatabaseManager {
2817
2918 private final Logger logger ;
3019 private final File dataFolder ;
31- private final DatabaseSettings databaseSettings ;
20+ private final DatabaseSettings settings ;
3221 private final Map <Class <?>, Dao <?, ?>> cachedDao = new ConcurrentHashMap <>();
3322 private HikariDataSource dataSource ;
3423 private ConnectionSource connectionSource ;
3524
36- public DatabaseManager (Logger logger , File dataFolder , DatabaseSettings databaseSettings ) {
25+ public DatabaseManager (Logger logger , File dataFolder , DatabaseSettings settings ) {
3726 this .logger = logger ;
3827 this .dataFolder = dataFolder ;
39- this .databaseSettings = databaseSettings ;
28+ this .settings = settings ;
4029 }
4130
4231 public void connect () {
4332 try {
4433 Stopwatch stopwatch = Stopwatch .createStarted ();
4534
4635 this .dataSource = new HikariDataSource ();
47-
48- DatabaseSettings settings = this .databaseSettings ;
49-
5036 this .dataSource .addDataSourceProperty ("cachePrepStmts" , "true" );
5137 this .dataSource .addDataSourceProperty ("prepStmtCacheSize" , "250" );
5238 this .dataSource .addDataSourceProperty ("prepStmtCacheSqlLimit" , "2048" );
5339 this .dataSource .addDataSourceProperty ("useServerPrepStmts" , "true" );
5440
5541 this .dataSource .setMaximumPoolSize (settings .poolSize ());
5642 this .dataSource .setConnectionTimeout (settings .timeout ());
57- this .dataSource .setUsername (settings .getUsername ());
58- this .dataSource .setPassword (settings .getPassword ());
59-
60- DatabaseDriverType driverType = settings .getDriverType ();
61- switch (driverType ) {
62- case MY_SQL -> {
63- this .dataSource .setDriverClassName (MYSQL_DRIVER );
64- this .dataSource .setJdbcUrl (String .format (
65- MYSQL_JDBC_URL ,
66- settings .getHostname (),
67- settings .getPort (),
68- settings .getDatabase (),
69- this .databaseSettings .isSSL (),
70- this .databaseSettings .isSSL ())
71- );
72- }
73-
74- case MARIA_DB -> {
75- this .dataSource .setDriverClassName (MARIADB_DRIVER );
76- this .dataSource .setJdbcUrl (String .format (
77- MARIADB_JDBC_URL ,
78- settings .getHostname (),
79- settings .getPort (),
80- settings .getDatabase (),
81- this .databaseSettings .isSSL (),
82- this .databaseSettings .isSSL ())
83- );
84- }
85-
86- case H2 -> {
87- this .dataSource .setDriverClassName (H2_DRIVER );
88- this .dataSource .setJdbcUrl (String .format (
89- H2_JDBC_URL ,
90- this .dataFolder )
91- );
92- }
93-
94- case SQLITE -> {
95- this .dataSource .setDriverClassName (SQLITE_DRIVER );
96- this .dataSource .setJdbcUrl (String .format (
97- SQLITE_JDBC_URL ,
98- this .dataFolder )
99- );
100- }
101-
102- case POSTGRE_SQL -> {
103- this .dataSource .setDriverClassName (POSTGRESQL_DRIVER );
104- this .dataSource .setJdbcUrl (String .format (
105- POSTGRESQL_JDBC_URL ,
106- settings .getHostname (), settings .getPort (), this .databaseSettings .isSSL ())
107- );
108- }
109-
110- default -> throw new DatabaseException ("SQL type '" + driverType + "' not found" );
111- }
112-
113- this .connectionSource = new DataSourceConnectionSource (this .dataSource , this .dataSource .getJdbcUrl ());
114- this .logger .info ("Loaded database " + driverType + " in " +
115- stopwatch .elapsed (TimeUnit .MILLISECONDS ) + "ms" );
43+ this .dataSource .setUsername (settings .username ());
44+ this .dataSource .setPassword (settings .password ());
45+
46+ DatabaseDriverType type = settings .databaseType ();
47+ this .dataSource .setDriverClassName (type .getDriver ());
48+
49+ String jdbcUrl = switch (type ) {
50+ case H2 , SQLITE -> type .formatUrl (dataFolder );
51+ case POSTGRESQL -> type .formatUrl (
52+ settings .hostname (),
53+ settings .port (),
54+ settings .database (),
55+ DatabaseConnectionDriverConstant .sslParamForPostgreSQL (settings .ssl ())
56+ );
57+ case MYSQL -> type .formatUrl (
58+ settings .hostname (),
59+ settings .port (),
60+ settings .database (),
61+ DatabaseConnectionDriverConstant .sslParamForMySQL (settings .ssl ())
62+ );
63+ case MARIADB -> type .formatUrl (
64+ settings .hostname (),
65+ settings .port (),
66+ settings .database (),
67+ String .valueOf (settings .ssl ())
68+ );
69+ };
70+
71+ this .dataSource .setJdbcUrl (jdbcUrl );
72+
73+ this .connectionSource = new DataSourceConnectionSource (this .dataSource , jdbcUrl );
74+
75+ this .logger .info ("Loaded database " + type + " in " + stopwatch .elapsed (TimeUnit .MILLISECONDS ) + "ms" );
11676 }
117- catch (DatabaseException | SQLException exception ) {
118- throw new RuntimeException ("Failed to connect to the database" , exception );
77+ catch (Exception exception ) {
78+ throw new DatabaseException ("Failed to connect to the database" , exception );
11979 }
12080 }
12181
@@ -131,19 +91,15 @@ public void close() {
13191
13292 @ SuppressWarnings ("unchecked" )
13393 public <T , ID > Dao <T , ID > getDao (Class <T > type ) {
134- try {
135- Dao <?, ?> dao = this .cachedDao .get (type );
136-
137- if (dao == null ) {
138- dao = DaoManager .createDao (this .connectionSource , type );
139- this .cachedDao .put (type , dao );
140- }
141-
142- return (Dao <T , ID >) dao ;
143- }
144- catch (SQLException exception ) {
145- throw new RuntimeException (exception );
146- }
94+ return (Dao <T , ID >) this .cachedDao .computeIfAbsent (
95+ type , clazz -> {
96+ try {
97+ return DaoManager .createDao (this .connectionSource , clazz );
98+ }
99+ catch (SQLException exception ) {
100+ throw new DatabaseException ("Failed to create DAO for " + clazz .getName (), exception );
101+ }
102+ });
147103 }
148104
149105 public ConnectionSource connectionSource () {
0 commit comments