Database locale stuff

This commit is contained in:
Rsl1122 2018-07-29 09:34:47 +03:00
parent 2d3ca08dfe
commit 09796cdbec
13 changed files with 60 additions and 29 deletions

View File

@ -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<UUID> uuids = copyFromDB.fetch().getSavedUUIDs();
if (uuids.isEmpty()) {
return;

View File

@ -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));

View File

@ -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();
}

View File

@ -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);

View File

@ -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> 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);

View File

@ -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> 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> 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));
}
}

View File

@ -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> locale) {
this("database", locale);
}
public SQLiteDB(String dbName) {
this(new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db"));
public SQLiteDB(String dbName, Supplier<Locale> locale) {
this(new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db"), locale);
}
public SQLiteDB(File databaseFile) {
public SQLiteDB(File databaseFile, Supplier<Locale> 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) {

View File

@ -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> locale) {
super(locale);
}
@Override
public void setupDataSource() throws DBInitException {
Optional<SqlService> sqlServiceProvider = Sponge.getServiceManager().provide(SqlService.class);

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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 -");

View File

@ -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();

View File

@ -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();