From 09796cdbec5d24604d64e655dbf00c72c062a49a Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 29 Jul 2018 09:34:47 +0300 Subject: [PATCH] Database locale stuff --- .../commands/manage/ManageBackupCommand.java | 2 +- .../commands/manage/ManageRestoreCommand.java | 2 +- .../plan/system/database/BungeeDBSystem.java | 2 +- .../plan/system/database/ServerDBSystem.java | 4 ++-- .../database/databases/sql/MySQLDB.java | 8 +++++-- .../system/database/databases/sql/SQLDB.java | 18 +++++++++++----- .../database/databases/sql/SQLiteDB.java | 21 ++++++++++--------- .../database/databases/sql/SpongeMySQLDB.java | 6 ++++++ .../plan/system/locale/lang/PluginLang.java | 10 ++++++++- .../system/database/databases/MySQLTest.java | 5 +++-- .../system/database/databases/SQLiteTest.java | 5 +++-- .../settings/network/NetworkSettingsTest.java | 3 ++- .../java/utilities/TestDatabaseCreator.java | 3 ++- 13 files changed, 60 insertions(+), 29 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java index 24b8b9b87..ac3a6479b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java @@ -97,7 +97,7 @@ public class ManageBackupCommand extends CommandNode { try { String timeStamp = Formatters.iso8601NoClock().apply(System::currentTimeMillis); String fileName = dbName + "-backup-" + timeStamp; - backupDB = new SQLiteDB(fileName); + backupDB = new SQLiteDB(fileName, () -> locale); Collection uuids = copyFromDB.fetch().getSavedUUIDs(); if (uuids.isEmpty()) { return; diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java index b8e43df0b..142570d3b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java @@ -90,7 +90,7 @@ public class ManageRestoreCommand extends CommandNode { backupDBName = backupDBName.substring(0, backupDBName.length() - 3); } - SQLiteDB backupDB = new SQLiteDB(backupDBName); + SQLiteDB backupDB = new SQLiteDB(backupDBName, () -> locale); backupDB.init(); sender.sendMessage(locale.getString(ManageLang.PROGRESS_START)); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/BungeeDBSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/database/BungeeDBSystem.java index 5abe1c34d..2cc2534b3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/BungeeDBSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/BungeeDBSystem.java @@ -23,7 +23,7 @@ public class BungeeDBSystem extends DBSystem { @Override protected void initDatabase() throws DBInitException { - db = new MySQLDB(); + db = new MySQLDB(locale); databases.add(db); db.init(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/ServerDBSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/database/ServerDBSystem.java index caf95d6ae..f04ac673b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/ServerDBSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/ServerDBSystem.java @@ -27,8 +27,8 @@ public class ServerDBSystem extends DBSystem { @Override protected void initDatabase() throws DBInitException { - databases.add(Check.isSpongeAvailable() ? new SpongeMySQLDB() : new MySQLDB()); - databases.add(new SQLiteDB()); + databases.add(Check.isSpongeAvailable() ? new SpongeMySQLDB(locale) : new MySQLDB(locale)); + databases.add(new SQLiteDB(locale)); String dbType = Settings.DB_TYPE.toString().toLowerCase().trim(); db = getActiveDatabaseByName(dbType); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/MySQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/MySQLDB.java index 94d222e19..44eff19c7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/MySQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/MySQLDB.java @@ -1,6 +1,8 @@ package com.djrapitops.plan.system.database.databases.sql; import com.djrapitops.plan.api.exceptions.database.DBInitException; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plan.system.locale.lang.PluginLang; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.api.utility.log.Log; import com.zaxxer.hikari.HikariConfig; @@ -10,6 +12,7 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.util.Objects; +import java.util.function.Supplier; /** * @author Rsl1122 @@ -18,7 +21,8 @@ public class MySQLDB extends SQLDB { protected DataSource dataSource; - public MySQLDB() { + public MySQLDB(Supplier locale) { + super(locale); } /** @@ -33,8 +37,8 @@ public class MySQLDB extends SQLDB { String database = Settings.DB_DATABASE.toString(); String launchOptions = Settings.DB_LAUNCH_OPTIONS.toString(); if (launchOptions.isEmpty() || !launchOptions.startsWith("?") || launchOptions.endsWith("&")) { - Log.error("Launch Options were faulty, using default (?rewriteBatchedStatements=true&useSSL=false)"); launchOptions = "?rewriteBatchedStatements=true&useSSL=false"; + Log.error(locale.get().getString(PluginLang.DB_MYSQL_LAUNCH_OPTIONS_FAIL, launchOptions)); } config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database + launchOptions); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java index d1c55ce93..c13289c26 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java @@ -10,6 +10,8 @@ import com.djrapitops.plan.system.database.databases.sql.patches.*; import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement; import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement; import com.djrapitops.plan.system.database.databases.sql.tables.*; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plan.system.locale.lang.PluginLang; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.utility.log.Log; @@ -25,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.UUID; +import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -35,6 +38,8 @@ import java.util.stream.Collectors; */ public abstract class SQLDB extends Database { + protected final Supplier locale; + private final UsersTable usersTable; private final UserInfoTable userInfoTable; private final KillsTable killsTable; @@ -62,7 +67,8 @@ public abstract class SQLDB extends Database { private final boolean usingMySQL; private ITask dbCleanTask; - public SQLDB() { + public SQLDB(Supplier locale) { + this.locale = locale; usingMySQL = getName().equals("MySQL"); serverTable = new ServerTable(this); @@ -159,15 +165,17 @@ public abstract class SQLDB extends Database { for (Patch patch : patches) { if (!patch.hasBeenApplied()) { String patchName = patch.getClass().getSimpleName(); - Log.info("Applying Patch: " + patchName + ".."); + Log.info(locale.get().getString(PluginLang.DB_APPLY_PATCH, patchName)); patch.apply(); applied = true; } } - Log.info(applied ? "All database patches applied successfully." : "All database patches already applied."); + Log.info(locale.get().getString( + applied ? PluginLang.DB_APPLIED_PATCHES : PluginLang.DB_APPLIED_PATCHES_ALREADY + )); } catch (Exception e) { Log.error("----------------------------------------------------"); - Log.error("Database Patching failed, plugin has to be disabled. Please report this issue"); + Log.error(locale.get().getString(PluginLang.ENABLE_FAIL_DB_PATCH)); Log.error("----------------------------------------------------"); Log.toLog(this.getClass(), e); PlanPlugin.getInstance().onDisable(); @@ -246,7 +254,7 @@ public abstract class SQLDB extends Database { } int removed = inactivePlayers.size(); if (removed > 0) { - Log.info("Removed data of " + removed + " players."); + Log.info(locale.get().getString(PluginLang.DB_NOTIFY_CLEAN, removed)); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java index 2fb9798a3..0e15d32f3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java @@ -2,6 +2,8 @@ package com.djrapitops.plan.system.database.databases.sql; import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.api.exceptions.database.DBInitException; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plan.system.locale.lang.PluginLang; import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.task.AbsRunnable; @@ -11,6 +13,7 @@ import com.djrapitops.plugin.task.RunnableFactory; import java.io.File; import java.sql.*; import java.util.Objects; +import java.util.function.Supplier; /** * @author Rsl1122 @@ -22,18 +25,16 @@ public class SQLiteDB extends SQLDB { private Connection connection; private ITask connectionPingTask; - /** - * Class Constructor. - */ - public SQLiteDB() { - this("database"); + public SQLiteDB(Supplier locale) { + this("database", locale); } - public SQLiteDB(String dbName) { - this(new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db")); + public SQLiteDB(String dbName, Supplier locale) { + this(new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db"), locale); } - public SQLiteDB(File databaseFile) { + public SQLiteDB(File databaseFile, Supplier locale) { + super(locale); dbName = databaseFile.getName(); this.databaseFile = databaseFile; } @@ -68,7 +69,7 @@ public class SQLiteDB extends SQLDB { try { return DriverManager.getConnection("jdbc:sqlite:" + dbFilePath + "?journal_mode=WAL"); } catch (SQLException ignored) { - Log.info("SQLite WAL mode not supported on this server version, using default. This may or may not affect performance."); + Log.info(locale.get().getString(PluginLang.DB_NOTIFY_SQLITE_WAL)); return DriverManager.getConnection("jdbc:sqlite:" + dbFilePath); } } @@ -88,7 +89,7 @@ public class SQLiteDB extends SQLDB { resultSet = statement.executeQuery("/* ping */ SELECT 1"); } } catch (SQLException e) { - Log.debug("Something went wrong during Ping task."); + Log.debug("Something went wrong during SQLite Connection upkeep task."); try { connection = getNewConnection(databaseFile); } catch (SQLException e1) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java index f1fcb6b4d..7a9409d6b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java @@ -1,6 +1,7 @@ package com.djrapitops.plan.system.database.databases.sql; import com.djrapitops.plan.api.exceptions.database.DBInitException; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.api.utility.log.Log; import org.spongepowered.api.Sponge; @@ -9,6 +10,7 @@ import org.spongepowered.api.service.sql.SqlService; import java.sql.Connection; import java.sql.SQLException; import java.util.Optional; +import java.util.function.Supplier; /** * MySQLDB implementation for Sponge since default driver is not available. @@ -17,6 +19,10 @@ import java.util.Optional; */ public class SpongeMySQLDB extends MySQLDB { + public SpongeMySQLDB(Supplier locale) { + super(locale); + } + @Override public void setupDataSource() throws DBInitException { Optional sqlServiceProvider = Sponge.getServiceManager().provide(SqlService.class); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/PluginLang.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/PluginLang.java index 24211fbba..e6a2d99fb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/PluginLang.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/PluginLang.java @@ -18,6 +18,7 @@ public enum PluginLang implements Lang { ENABLE_FAIL_DB("Enable FAIL - Database", "${0}-Database Connection failed: ${1}"), ENABLE_FAIL_WRONG_DB("Enable FAIL - Wrong Database Type", "${0} is not a supported Database"), + ENABLE_FAIL_DB_PATCH("Enable FAIL - Database Patch", "Database Patching failed, plugin has to be disabled. Please report this issue"), ENABLE_FAIL_NO_WEB_SERVER_BUNGEE("Enable FAIL - WebServer (Bungee)", "WebServer did not initialize!"), ENABLE_FAIL_GEODB_WRITE("Enable FAIL - GeoDB Write", "Something went wrong saving the downloaded GeoLite2 Geolocation database"), @@ -36,7 +37,14 @@ public enum PluginLang implements Lang { VERSION_AVAILABLE_SPIGOT("Version - New (old)", "New Version is available at ${0}"), VERSION_AVAILABLE_DEV("Version - DEV", " This is a DEV release."), VERSION_FAIL_READ_VERSIONS("Version FAIL - Read versions.txt", "Version information could not be loaded from Github/versions.txt"), - VERSION_FAIL_READ_OLD("Version FAIL - Read info (old)", "Failed to check newest version number"); + VERSION_FAIL_READ_OLD("Version FAIL - Read info (old)", "Failed to check newest version number"), + + DB_APPLY_PATCH("Database - Apply Patch", "Applying Patch: ${0}.."), + DB_APPLIED_PATCHES("Database - Patches Applied", "All database patches applied successfully."), + DB_APPLIED_PATCHES_ALREADY("Database - Patches Applied", "All database patches already applied."), + DB_NOTIFY_CLEAN("Database Notify - Clean", "Removed data of ${0} players."), + DB_NOTIFY_SQLITE_WAL("Database Notify - SQLite No WAL", "SQLite WAL mode not supported on this server version, using default. This may or may not affect performance."), + DB_MYSQL_LAUNCH_OPTIONS_FAIL("Database MySQL - Launch Options Error", "Launch Options were faulty, using default (${0})"); private final String identifier; private final String defaultValue; diff --git a/Plan/src/test/java/com/djrapitops/plan/system/database/databases/MySQLTest.java b/Plan/src/test/java/com/djrapitops/plan/system/database/databases/MySQLTest.java index 1a3a8942f..855d869aa 100644 --- a/Plan/src/test/java/com/djrapitops/plan/system/database/databases/MySQLTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/system/database/databases/MySQLTest.java @@ -5,6 +5,7 @@ package com.djrapitops.plan.system.database.databases; import com.djrapitops.plan.system.database.databases.sql.MySQLDB; +import com.djrapitops.plan.system.locale.Locale; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -18,12 +19,12 @@ public class MySQLTest { @Test public void testMySQLGetConfigName() { - assertEquals("mysql", new MySQLDB().getConfigName()); + assertEquals("mysql", new MySQLDB(Locale::new).getConfigName()); } @Test public void testMySQLGetName() { - assertEquals("MySQL", new MySQLDB().getName()); + assertEquals("MySQL", new MySQLDB(Locale::new).getName()); } } diff --git a/Plan/src/test/java/com/djrapitops/plan/system/database/databases/SQLiteTest.java b/Plan/src/test/java/com/djrapitops/plan/system/database/databases/SQLiteTest.java index bd4f02b5f..06078f5db 100644 --- a/Plan/src/test/java/com/djrapitops/plan/system/database/databases/SQLiteTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/system/database/databases/SQLiteTest.java @@ -24,6 +24,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLiteDB; import com.djrapitops.plan.system.database.databases.sql.tables.*; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.processing.processors.player.RegisterProcessor; import com.djrapitops.plan.utilities.Base64Util; import com.djrapitops.plan.utilities.SHA256Hash; @@ -68,7 +69,7 @@ public class SQLiteTest { System.out.println("--- Test Class Setup ---"); SystemMockUtil mockUtil = SystemMockUtil.setUp(temporaryFolder.getRoot()) .enableConfigSystem(); - db = new SQLiteDB(); + db = new SQLiteDB(Locale::new); mockUtil.enableDatabaseSystem(db) .enableServerInfoSystem(); StaticHolder.saveInstance(SQLDB.class, Plan.class); @@ -748,7 +749,7 @@ public class SQLiteTest { @Test public void testBackupAndRestore() throws DBException, UnsupportedEncodingException, NoSuchAlgorithmException { System.out.println("- Creating Backup Database -"); - SQLiteDB backup = new SQLiteDB("debug-backup" + System.currentTimeMillis()); + SQLiteDB backup = new SQLiteDB("debug-backup" + System.currentTimeMillis(), Locale::new); backup.init(); System.out.println("- Backup Database Created -"); diff --git a/Plan/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java b/Plan/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java index 4b52c362e..6504005b5 100644 --- a/Plan/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java @@ -8,6 +8,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLiteDB; import com.djrapitops.plan.system.database.databases.sql.tables.ServerTable; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.api.utility.log.Log; @@ -33,7 +34,7 @@ public class NetworkSettingsTest { SystemMockUtil mockUtil = SystemMockUtil.setUp(temporaryFolder.getRoot()) .enableConfigSystem(); - db = new SQLiteDB(); + db = new SQLiteDB(Locale::new); mockUtil.enableDatabaseSystem(db) .enableServerInfoSystem(); diff --git a/Plan/src/test/java/utilities/TestDatabaseCreator.java b/Plan/src/test/java/utilities/TestDatabaseCreator.java index 471f860fa..5f4bbcc4e 100644 --- a/Plan/src/test/java/utilities/TestDatabaseCreator.java +++ b/Plan/src/test/java/utilities/TestDatabaseCreator.java @@ -7,6 +7,7 @@ import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.system.database.databases.sql.SQLDB; import com.djrapitops.plan.system.database.databases.sql.SQLiteDB; import com.djrapitops.plan.system.info.server.Server; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plugin.api.TimeAmount; import java.io.File; @@ -37,7 +38,7 @@ public class TestDatabaseCreator { boolean oldDB = testDB.exists(); - db = new SQLiteDB(testDB); + db = new SQLiteDB(testDB, Locale::new); db.init(); r = new Random();