Skip to content

Commit 4074e11

Browse files
vLuckyyygemini-code-assist[bot]CitralFlo
authored
GH-967 Refactor database. (#1072)
* GH-967 Refactor database. Based on EternalEconomy. * Fix. * Update eternalcore-core/src/main/java/com/eternalcode/core/database/DatabaseConnectionDriverConstant.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update eternalcore-core/src/main/java/com/eternalcode/core/database/DatabaseConnectionDriverConstant.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Fix. * Fix. * Fix. * Update eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java Co-authored-by: Michał Wojtas <[email protected]> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Michał Wojtas <[email protected]>
1 parent a3475ab commit 4074e11

File tree

11 files changed

+344
-126
lines changed

11 files changed

+344
-126
lines changed

eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eternalcode.core.configuration.implementation;
22

33
import com.eternalcode.core.configuration.AbstractConfigurationFile;
4+
import com.eternalcode.core.database.DatabaseConfig;
45
import com.eternalcode.core.database.DatabaseType;
56
import com.eternalcode.core.feature.afk.AfkSettings;
67
import com.eternalcode.core.feature.automessage.AutoMessageSettings;
@@ -16,6 +17,7 @@
1617
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
1718
import eu.okaeri.configs.OkaeriConfig;
1819
import eu.okaeri.configs.annotation.Comment;
20+
import eu.okaeri.configs.annotation.Header;
1921
import org.bukkit.Material;
2022
import org.bukkit.Sound;
2123

@@ -25,42 +27,31 @@
2527
import java.util.Map;
2628
import java.util.Set;
2729

30+
@Header({
31+
"#",
32+
"# This is the main configuration file for EternalCore.",
33+
"#",
34+
"# If you need help with the configuration or have any questions related to EternalCore, join our discord, or create an issue on our GitHub.",
35+
"#",
36+
"# Issues: https://github.com/EternalCodeTeam/EternalCore/issues",
37+
"# Discord: https://discord.gg/FQ7jmGBd6c",
38+
"# Website: https://eternalcode.pl/",
39+
"# Source Code: https://github.com/EternalCodeTeam/EternalCore",
40+
"#",
41+
})
42+
2843
@ConfigurationFile
2944
public class PluginConfiguration extends AbstractConfigurationFile {
3045

31-
@Comment({
32-
"#",
33-
"# This is the main configuration file for EternalCore.",
34-
"#",
35-
"# If you need help with the configuration or have any questions related to EternalCore, join us in our discord, or create an issue on our GitHub.",
36-
"#",
37-
"# Issues: https://github.com/EternalCodeTeam/EternalCore/issues",
38-
"# Discord: https://discord.gg/FQ7jmGBd6c",
39-
"# Website: https://eternalcode.pl/",
40-
"# Source Code: https://github.com/EternalCodeTeam/EternalCore",
41-
"#",
42-
})
43-
4446
@Comment("# Whether the player should receive information about new plugin updates upon joining the server")
4547
public boolean shouldReceivePluginUpdates = true;
4648

47-
@Comment({ " ", "# Database Section" })
48-
public Database database = new Database();
49-
50-
public static class Database extends OkaeriConfig {
51-
@Comment({
52-
"# SQL Drivers and ports:",
53-
"# MySQL (3306), MariaDB (3306), PostgresQL (5432)",
54-
"# SQLite, H2"
55-
})
56-
public DatabaseType databaseType = DatabaseType.SQLITE;
49+
@Comment
50+
@Comment({
51+
"Settings responsible for the database connection."
52+
})
53+
public DatabaseConfig database = new DatabaseConfig();
5754

58-
public String hostname = "127.0.0.1";
59-
public String database = "database";
60-
public String username = "root";
61-
public String password = "U5eStr0ngP4ssw0rd";
62-
public int port = 3306;
63-
}
6455

6556
@Comment({ "", "# Join settings" })
6657
public Join join = new Join();

eternalcore-core/src/main/java/com/eternalcode/core/database/AbstractRepositoryOrmLite.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.eternalcode.core.database;
22

33
import com.eternalcode.commons.scheduler.Scheduler;
4+
import com.eternalcode.core.util.ThrowingFunction;
45
import com.j256.ormlite.dao.Dao;
5-
import panda.std.function.ThrowingFunction;
6-
76
import java.sql.SQLException;
87
import java.util.List;
98
import java.util.Optional;
@@ -51,21 +50,32 @@ protected <T> CompletableFuture<List<T>> selectAll(Class<T> type) {
5150
return this.action(type, Dao::queryForAll);
5251
}
5352

54-
protected <T, ID, R> CompletableFuture<R> action(Class<T> type, ThrowingFunction<Dao<T, ID>, R, SQLException> action) {
55-
CompletableFuture<R> completableFuture = new CompletableFuture<>();
53+
protected <T, ID, R> CompletableFuture<R> action(
54+
Class<T> type,
55+
ThrowingFunction<Dao<T, ID>, R, SQLException> action
56+
) {
57+
CompletableFuture<R> future = new CompletableFuture<>();
5658

5759
this.scheduler.runAsync(() -> {
58-
Dao<T, ID> dao = this.databaseManager.getDao(type);
60+
Dao<T, ID> dao;
61+
try {
62+
dao = this.databaseManager.getDao(type);
63+
}
64+
catch (Exception exception) {
65+
future.completeExceptionally(new DatabaseException(
66+
"Failed to get DAO for class: " + type.getName(), exception));
67+
return;
68+
}
5969

6070
try {
61-
completableFuture.complete(action.apply(dao));
71+
future.complete(action.apply(dao));
6272
}
6373
catch (Throwable throwable) {
64-
completableFuture.completeExceptionally(throwable);
74+
future.completeExceptionally(new DatabaseException(
75+
"Database action failed for: " + type.getSimpleName(), throwable));
6576
}
6677
});
6778

68-
return completableFuture;
79+
return future;
6980
}
70-
7181
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.eternalcode.core.database;
2+
3+
import eu.okaeri.configs.OkaeriConfig;
4+
import eu.okaeri.configs.annotation.Comment;
5+
6+
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
7+
public class DatabaseConfig extends OkaeriConfig implements DatabaseSettings {
8+
9+
@Comment({"Type of the database driver (e.g., SQLITE, H2, MYSQL, MARIADB, POSTGRESQL).", "Determines the "
10+
+ "database type "
11+
+ "to be used."})
12+
public DatabaseDriverType databaseType = DatabaseDriverType.SQLITE;
13+
14+
@Comment({"Hostname of the database server.", "For local databases, this is usually 'localhost'."})
15+
public String hostname = "localhost";
16+
17+
@Comment({"Port number of the database server. Common ports:",
18+
" - MySQL: 3306",
19+
" - PostgreSQL: 5432",
20+
" - H2: Not applicable (file-based)",
21+
" - SQLite: Not applicable (file-based)"})
22+
public int port = 3306;
23+
24+
@Comment("Name of the database to connect to. This is the name of the specific database instance.")
25+
public String database = "eternalcore";
26+
27+
@Comment("Username for the database connection. This is the user account used to authenticate with the database.")
28+
public String username = "root";
29+
30+
@Comment("Password for the database connection. This is the password for the specified user account.")
31+
public String password = "password";
32+
33+
@Comment("Enable SSL for the database connection. Set to true to use SSL/TLS for secure connections.")
34+
public boolean ssl = false;
35+
36+
@Comment("Connection pool size. This determines the maximum number of connections in the pool.")
37+
public int poolSize = 16;
38+
39+
@Comment("Connection timeout in milliseconds. This is the maximum time to wait for a connection from the pool.")
40+
public int timeout = 30000;
41+
42+
@Override
43+
public DatabaseDriverType getDriverType() {
44+
return this.databaseType;
45+
}
46+
47+
@Override
48+
public String getHostname() {
49+
return this.hostname;
50+
}
51+
52+
@Override
53+
public int getPort() {
54+
return this.port;
55+
}
56+
57+
@Override
58+
public String getDatabase() {
59+
return this.database;
60+
}
61+
62+
@Override
63+
public String getUsername() {
64+
return this.username;
65+
}
66+
67+
@Override
68+
public String getPassword() {
69+
return this.password;
70+
}
71+
72+
@Override
73+
public boolean isSSL() {
74+
return this.ssl;
75+
}
76+
77+
@Override
78+
public int poolSize() {
79+
return this.poolSize;
80+
}
81+
82+
@Override
83+
public int timeout() {
84+
return this.timeout;
85+
}
86+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.eternalcode.core.database;
2+
3+
final class DatabaseConnectionDriverConstant {
4+
5+
private DatabaseConnectionDriverConstant() {}
6+
7+
// mysql
8+
static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";
9+
static final String MYSQL_JDBC_URL = "jdbc:mysql://%s:%s/%s?sslMode=%s";
10+
11+
// mariadb — accepts "true"/"false" as valid sslMode values (aliases for "verify-full" and "disable")
12+
static final String MARIADB_DRIVER = "org.mariadb.jdbc.Driver";
13+
static final String MARIADB_JDBC_URL = "jdbc:mariadb://%s:%s/%s?sslMode=%s";
14+
15+
// h2
16+
static final String H2_DRIVER = "org.h2.Driver";
17+
static final String H2_JDBC_URL = "jdbc:h2:./%s/database";
18+
19+
// sqlite
20+
static final String SQLITE_DRIVER = "org.sqlite.JDBC";
21+
static final String SQLITE_JDBC_URL = "jdbc:sqlite:%s/database.db";
22+
23+
// postgresql
24+
static final String POSTGRESQL_DRIVER = "org.postgresql.Driver";
25+
static final String POSTGRESQL_JDBC_URL = "jdbc:postgresql://%s:%s/%s?sslmode=%s";
26+
27+
static String sslParamForMySQL(boolean enabled) {
28+
return enabled ? "REQUIRED" : "DISABLED";
29+
}
30+
31+
static String sslParamForPostgreSQL(boolean enabled) {
32+
return enabled ? "require" : "disable";
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.eternalcode.core.database;
2+
3+
import static com.eternalcode.core.database.DatabaseConnectionDriverConstant.*;
4+
5+
public enum DatabaseDriverType {
6+
7+
MYSQL(MYSQL_DRIVER, MYSQL_JDBC_URL),
8+
MARIADB(MARIADB_DRIVER, MARIADB_JDBC_URL),
9+
POSTGRESQL(POSTGRESQL_DRIVER, POSTGRESQL_JDBC_URL),
10+
H2(H2_DRIVER, H2_JDBC_URL),
11+
SQLITE(SQLITE_DRIVER, SQLITE_JDBC_URL);
12+
13+
private final String driver;
14+
private final String urlFormat;
15+
16+
DatabaseDriverType(String driver, String urlFormat) {
17+
this.driver = driver;
18+
this.urlFormat = urlFormat;
19+
}
20+
21+
public String getDriver() {
22+
return driver;
23+
}
24+
25+
public String formatUrl(Object... args) {
26+
return String.format(urlFormat, args);
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.eternalcode.core.database;
2+
3+
public class DatabaseException extends RuntimeException {
4+
public DatabaseException(String message) {
5+
super(message);
6+
}
7+
8+
public DatabaseException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
}

0 commit comments

Comments
 (0)