diff --git a/common/pom.xml b/common/pom.xml index 40ea3b2e..88d762d3 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -147,6 +147,20 @@ 2.7.3 provided + + + com.h2database + h2 + 1.4.196 + provided + + + + org.xerial + sqlite-jdbc + 3.21.0 + provided + redis.clients diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/FlatfileConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/FlatfileConnectionFactory.java index 29ea7a60..30cbb498 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/FlatfileConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/FlatfileConnectionFactory.java @@ -28,62 +28,41 @@ package me.lucko.luckperms.common.storage.dao.sql.connection.file; import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory; import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.text.DecimalFormat; -import java.util.concurrent.locks.ReentrantLock; +import java.util.LinkedHashMap; +import java.util.Map; abstract class FlatfileConnectionFactory extends AbstractConnectionFactory { protected static final DecimalFormat DF = new DecimalFormat("#.##"); protected final File file; - private final ReentrantLock lock = new ReentrantLock(); - private NonClosableConnection connection; FlatfileConnectionFactory(String name, File file) { super(name); this.file = file; } - protected abstract String getDriverClass(); - protected abstract String getDriverId(); - @Override public void init() { } - @Override - public void shutdown() throws Exception { - if (this.connection != null) { - this.connection.shutdown(); - } + protected File getWriteFile() { + return this.file; } @Override - public Connection getConnection() throws SQLException { - this.lock.lock(); - try { - if (this.connection == null || this.connection.isClosed()) { - try { - Class.forName(getDriverClass()); - } catch (ClassNotFoundException ignored) {} + public Map getMeta() { + Map ret = new LinkedHashMap<>(); - Connection connection = DriverManager.getConnection(getDriverId() + ":" + this.file.getAbsolutePath()); - if (connection != null) { - this.connection = new NonClosableConnection(connection); - } - } - - } finally { - this.lock.unlock(); + File databaseFile = getWriteFile(); + if (databaseFile.exists()) { + double size = databaseFile.length() / 1048576D; + ret.put("File Size", DF.format(size) + "MB"); + } else { + ret.put("File Size", "0MB"); } - if (this.connection == null) { - throw new SQLException("Unable to get a connection."); - } - - return this.connection; + return ret; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java index 63b4c385..c13f5ce3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/H2ConnectionFactory.java @@ -25,43 +25,59 @@ package me.lucko.luckperms.common.storage.dao.sql.connection.file; +import org.h2.Driver; + import java.io.File; -import java.util.LinkedHashMap; -import java.util.Map; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.locks.ReentrantLock; public class H2ConnectionFactory extends FlatfileConnectionFactory { + private final ReentrantLock lock = new ReentrantLock(); + private NonClosableConnection connection; + public H2ConnectionFactory(File file) { super("H2", file); // backwards compat File data = new File(file.getParent(), "luckperms.db.mv.db"); if (data.exists()) { - data.renameTo(new File(file.getParent(), "luckperms-h2.mv.db")); + data.renameTo(new File(file.getParent(), file.getName() + ".mv.db")); } } @Override - public Map getMeta() { - Map ret = new LinkedHashMap<>(); - - File databaseFile = new File(super.file.getParent(), "luckperms-h2.mv.db"); - if (databaseFile.exists()) { - double size = databaseFile.length() / 1048576D; - ret.put("File Size", DF.format(size) + "MB"); - } else { - ret.put("File Size", "0MB"); + public Connection getConnection() throws SQLException { + this.lock.lock(); + try { + if (this.connection == null || this.connection.isClosed()) { + Connection connection = Driver.load().connect("jdbc:h2:" + this.file.getAbsolutePath(), new Properties()); + if (connection != null) { + this.connection = new NonClosableConnection(connection); + } + } + } finally { + this.lock.unlock(); } - return ret; + if (this.connection == null) { + throw new SQLException("Unable to get a connection."); + } + + return this.connection; } @Override - protected String getDriverClass() { - return "org.h2.Driver"; + public void shutdown() throws Exception { + if (this.connection != null) { + this.connection.shutdown(); + } } @Override - protected String getDriverId() { - return "jdbc:h2"; + protected File getWriteFile() { + // h2 appends this to the end of the database file + return new File(super.file.getParent(), super.file.getName() + ".mv.db"); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java index b0e2bd38..57224df3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/file/SQLiteConnectionFactory.java @@ -25,43 +25,55 @@ package me.lucko.luckperms.common.storage.dao.sql.connection.file; +import org.sqlite.JDBC; + import java.io.File; -import java.util.LinkedHashMap; -import java.util.Map; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.locks.ReentrantLock; public class SQLiteConnectionFactory extends FlatfileConnectionFactory { + private final ReentrantLock lock = new ReentrantLock(); + private NonClosableConnection connection; + public SQLiteConnectionFactory(File file) { super("SQLite", file); // backwards compat File data = new File(file.getParent(), "luckperms.sqlite"); if (data.exists()) { - data.renameTo(new File(file.getParent(), "luckperms-sqlite.db")); + data.renameTo(file); } } @Override - public Map getMeta() { - Map ret = new LinkedHashMap<>(); + public Connection getConnection() throws SQLException { + this.lock.lock(); + try { + if (this.connection == null || this.connection.isClosed()) { + Connection connection = JDBC.createConnection("jdbc:sqlite:" + this.file.getAbsolutePath(), new Properties()); + if (connection != null) { + this.connection = new NonClosableConnection(connection); + } + } - File databaseFile = new File(super.file.getParent(), "luckperms-sqlite.db"); - if (databaseFile.exists()) { - double size = databaseFile.length() / 1048576D; - ret.put("File Size", DF.format(size) + "MB"); - } else { - ret.put("File Size", "0MB"); + } finally { + this.lock.unlock(); } - return ret; + if (this.connection == null) { + throw new SQLException("Unable to get a connection."); + } + + return this.connection; } @Override - protected String getDriverClass() { - return "org.sqlite.JDBC"; + public void shutdown() throws Exception { + if (this.connection != null) { + this.connection.shutdown(); + } } - @Override - protected String getDriverId() { - return "jdbc:sqlite"; - } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java index f8aea26e..6e301bb3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java @@ -100,7 +100,6 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory config.setInitializationFailFast(false); } - this.hikari = new HikariDataSource(config); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java index 37f7d705..11d4f5e1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java @@ -40,7 +40,7 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory { @Override protected String getDriverClass() { - return classExists("org.mariadb.jdbc.MariaDbDataSource") ? "org.mariadb.jdbc.MariaDbDataSource" : "org.mariadb.jdbc.MySQLDataSource"; + return "org.mariadb.jdbc.MariaDbDataSource"; } @Override @@ -57,13 +57,4 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory { config.addDataSourceProperty("properties", propertiesString); } - private static boolean classExists(String clazz) { - try { - Class.forName(clazz); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } - } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java index 7251e502..97397889 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java @@ -35,16 +35,8 @@ public class MySqlConnectionFactory extends HikariConnectionFactory { } @Override - protected void appendConfigurationInfo(HikariConfig config) { - String address = this.configuration.getAddress(); - String[] addressSplit = address.split(":"); - address = addressSplit[0]; - String port = addressSplit.length > 1 ? addressSplit[1] : "3306"; - String database = this.configuration.getDatabase(); - - config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + database); - config.setUsername(this.configuration.getUsername()); - config.setPassword(this.configuration.getPassword()); + protected String getDriverClass() { + return "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"; } @Override