Add H2 database support, use indexes to optimize queries, add maria-specific schema, bump adventure-fabric (#465)

* Add support for H2, bundle Maria driver, bump fabric adventure

* Bundle and download H2 drivers

* Fixup H2 schema, add indexes to DBs, add maria-specific schema

* docs: Add database type documentation

* Check plugin isn't disabled before task schedule, close #463

* Bump Annotaml to 2.0.4

* docs: Mention Maria in README and Setup
This commit is contained in:
William authored and GitHub committed 2023-07-18 15:03:31 +01:00
1 parent 8d2424a7ef
commit beed438c16
28 files changed
+1443 -50

No files matched your search

@@ -55,11 +55,11 @@ public class Settings {
private boolean checkForUpdates = true;
// Database settings
@YamlComment("Type of database to use (SQLITE, MYSQL or MARIADB)")
@YamlComment("Type of database to use (SQLITE, H2, MYSQL or MARIADB)")
@YamlKey("database.type")
private Database.Type databaseType = Database.Type.SQLITE;
@YamlComment("Specify credentials here if you are using MYSQL as your database type")
@YamlComment("Specify credentials here if you are using MYSQL or MARIADB as your database type")
@YamlKey("database.mysql.credentials.host")
private String mySqlHost = "localhost";
@@ -81,7 +81,7 @@ public class Settings {
+ "&useUnicode=true"
+ "&characterEncoding=UTF-8";
@YamlComment("MYSQL database Hikari connection pool properties. "
@YamlComment("MYSQL / MARIADB database Hikari connection pool properties. "
+ "Don't modify this unless you know what you're doing!")
@YamlKey("database.mysql.connection_pool.size")
private int mySqlConnectionPoolSize = 12;
@@ -440,7 +440,8 @@ public abstract class Database {
public enum Type {
MYSQL("MySQL"),
MARIADB("MariaDB"),
SQLITE("SQLite");
SQLITE("SQLite"),
H2("H2");
private final String displayName;
File diff suppressed because it is too large. Load diff
@@ -38,16 +38,18 @@ import java.util.*;
import java.util.logging.Level;
/**
* A MySQL implementation of the plugin {@link Database}.
* A MySQL / MariaDB implementation of the plugin {@link Database}.
*/
@SuppressWarnings("DuplicatedCode")
public class MySqlDatabase extends Database {
private static final String DATA_POOL_NAME = "HuskHomesHikariPool";
private final String flavor;
private HikariDataSource dataSource;
public MySqlDatabase(@NotNull HuskHomes plugin) {
super(plugin);
this.flavor = plugin.getSettings().getDatabaseType() == Type.MARIADB ? "mariadb" : "mysql";
}
/**
@@ -64,15 +66,13 @@ public class MySqlDatabase extends Database {
public void initialize() throws IllegalStateException {
// Initialize the Hikari pooled connection
dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:" + (plugin.getSettings().getDatabaseType() == Type.MARIADB ? "mariadb" : "mysql")
+ "://"
+ plugin.getSettings().getMySqlHost()
+ ":"
+ plugin.getSettings().getMySqlPort()
+ "/"
+ plugin.getSettings().getMySqlDatabase()
+ plugin.getSettings().getMySqlConnectionParameters()
);
dataSource.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
flavor,
plugin.getSettings().getMySqlHost(),
plugin.getSettings().getMySqlPort(),
plugin.getSettings().getMySqlDatabase(),
plugin.getSettings().getMySqlConnectionParameters()
));
// Authenticate with the database
dataSource.setUsername(plugin.getSettings().getMySqlUsername());
@@ -108,7 +108,7 @@ public class MySqlDatabase extends Database {
// Prepare database schema; make tables if they don't exist
try (Connection connection = dataSource.getConnection()) {
final String[] databaseSchema = getSchemaStatements("database/mysql_schema.sql");
final String[] databaseSchema = getSchemaStatements(String.format("database/%s_schema.sql", flavor));
try (Statement statement = connection.createStatement()) {
for (String tableCreationStatement : databaseSchema) {
statement.execute(tableCreationStatement);
@@ -81,7 +81,7 @@ public class SqLiteDatabase extends Database {
plugin.log(Level.INFO, "Created the SQLite database file");
}
// Specify use of the JDBC SQLite driver
// Specify use of the JDBC SQLite driver for legacy compatibility
Class.forName("org.sqlite.JDBC");
// Set SQLite database properties
@@ -92,10 +92,11 @@ public class SqLiteDatabase extends Database {
// Establish the connection
connection = DriverManager.getConnection(
"jdbc:sqlite:" + databaseFile.getAbsolutePath(), config.toProperties()
String.format("jdbc:sqlite:%s", databaseFile.getAbsolutePath()),
config.toProperties()
);
} catch (IOException e) {
plugin.log(Level.SEVERE, "An exception occurred creating the database file", e);
plugin.log(Level.SEVERE, "An exception occurred creating the SQLite database file", e);
} catch (SQLException e) {
plugin.log(Level.SEVERE, "An SQL exception occurred initializing the SQLite database", e);
} catch (ClassNotFoundException e) {