1
1
package com .eternalcode .economy .database ;
2
2
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
-
14
3
import com .google .common .base .Stopwatch ;
15
4
import com .j256 .ormlite .dao .Dao ;
16
5
import com .j256 .ormlite .dao .DaoManager ;
@@ -28,94 +17,65 @@ public class DatabaseManager {
28
17
29
18
private final Logger logger ;
30
19
private final File dataFolder ;
31
- private final DatabaseSettings databaseSettings ;
20
+ private final DatabaseSettings settings ;
32
21
private final Map <Class <?>, Dao <?, ?>> cachedDao = new ConcurrentHashMap <>();
33
22
private HikariDataSource dataSource ;
34
23
private ConnectionSource connectionSource ;
35
24
36
- public DatabaseManager (Logger logger , File dataFolder , DatabaseSettings databaseSettings ) {
25
+ public DatabaseManager (Logger logger , File dataFolder , DatabaseSettings settings ) {
37
26
this .logger = logger ;
38
27
this .dataFolder = dataFolder ;
39
- this .databaseSettings = databaseSettings ;
28
+ this .settings = settings ;
40
29
}
41
30
42
31
public void connect () {
43
32
try {
44
33
Stopwatch stopwatch = Stopwatch .createStarted ();
45
34
46
35
this .dataSource = new HikariDataSource ();
47
-
48
- DatabaseSettings settings = this .databaseSettings ;
49
-
50
36
this .dataSource .addDataSourceProperty ("cachePrepStmts" , "true" );
51
37
this .dataSource .addDataSourceProperty ("prepStmtCacheSize" , "250" );
52
38
this .dataSource .addDataSourceProperty ("prepStmtCacheSqlLimit" , "2048" );
53
39
this .dataSource .addDataSourceProperty ("useServerPrepStmts" , "true" );
54
40
55
41
this .dataSource .setMaximumPoolSize (settings .poolSize ());
56
42
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" );
116
76
}
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 );
119
79
}
120
80
}
121
81
@@ -131,19 +91,15 @@ public void close() {
131
91
132
92
@ SuppressWarnings ("unchecked" )
133
93
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
+ });
147
103
}
148
104
149
105
public ConnectionSource connectionSource () {
0 commit comments