mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-26 20:16:18 +01:00
Add table prefix support to MySQL.
This commit is contained in:
parent
908b7c956b
commit
9eaab03c50
@ -27,14 +27,14 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
private void createTables() {
|
private void createTables() {
|
||||||
try (Connection conn = dbConn.openConnection()) {
|
try (Connection conn = dbConn.openConnection()) {
|
||||||
int schemaVersion;
|
int schemaVersion;
|
||||||
if (!checkTableExists("saneeconomy_schema")) {
|
if (!checkTableExists(dbConn.getTable("saneeconomy_schema"))) {
|
||||||
if (checkTableExists("player_balances")) {
|
if (checkTableExists(dbConn.getTable("player_balances"))) {
|
||||||
schemaVersion = 1;
|
schemaVersion = 1;
|
||||||
} else {
|
} else {
|
||||||
schemaVersion = 0;
|
schemaVersion = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PreparedStatement ps = conn.prepareStatement("SELECT `val` FROM saneeconomy_schema WHERE `key` = 'schema_version'");
|
PreparedStatement ps = conn.prepareStatement(String.format("SELECT `val` FROM `%s` WHERE `key` = 'schema_version'", dbConn.getTable("saneeconomy_schema")));
|
||||||
ps.executeQuery();
|
ps.executeQuery();
|
||||||
ResultSet rs = ps.getResultSet();
|
ResultSet rs = ps.getResultSet();
|
||||||
|
|
||||||
@ -47,10 +47,10 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
|
|
||||||
if (schemaVersion < 2) {
|
if (schemaVersion < 2) {
|
||||||
if (schemaVersion < 1) {
|
if (schemaVersion < 1) {
|
||||||
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `player_balances` (player_uuid CHAR(36), balance DECIMAL(18, 2))");
|
PreparedStatement ps = conn.prepareStatement(String.format("CREATE TABLE IF NOT EXISTS `%s` (player_uuid CHAR(36), balance DECIMAL(18, 2))", dbConn.getTable("player_balances")));
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
conn.prepareStatement("CREATE TABLE IF NOT EXISTS `saneeconomy_schema` (`key` VARCHAR(32) PRIMARY KEY, `val` TEXT)").executeUpdate();
|
conn.prepareStatement(String.format("CREATE TABLE IF NOT EXISTS `%s` (`key` VARCHAR(32) PRIMARY KEY, `val` TEXT)", dbConn.getTable("saneeconomy_schema"))).executeUpdate();
|
||||||
upgradeSchema1To2(conn);
|
upgradeSchema1To2(conn);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -60,9 +60,9 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
|
|
||||||
private void upgradeSchema1To2(Connection conn) throws SQLException {
|
private void upgradeSchema1To2(Connection conn) throws SQLException {
|
||||||
SaneEconomy.logger().info("Upgrading database schema from version 1 to version 2. This might take a little while...");
|
SaneEconomy.logger().info("Upgrading database schema from version 1 to version 2. This might take a little while...");
|
||||||
PreparedStatement ps = conn.prepareStatement("REPLACE INTO `saneeconomy_schema` (`key`, `val`) VALUES ('schema_version', '2')");
|
PreparedStatement ps = conn.prepareStatement(String.format("REPLACE INTO `%s` (`key`, `val`) VALUES ('schema_version', '2')", dbConn.getTable("saneeconomy_schema")));
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
conn.prepareStatement("CREATE TABLE `saneeconomy_balances` (unique_identifier VARCHAR(128) PRIMARY KEY, balance DECIMAL(18, 2))").executeUpdate();
|
conn.prepareStatement(String.format("CREATE TABLE `%s` (unique_identifier VARCHAR(128) PRIMARY KEY, balance DECIMAL(18, 2))", dbConn.getTable("saneeconomy_balances"))).executeUpdate();
|
||||||
ps = conn.prepareStatement("SELECT * FROM `player_balances`");
|
ps = conn.prepareStatement("SELECT * FROM `player_balances`");
|
||||||
ResultSet rs = ps.executeQuery();
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Entry<String, Double> e : oldBalances.entrySet()) {
|
for (Entry<String, Double> e : oldBalances.entrySet()) {
|
||||||
ps = conn.prepareStatement("INSERT INTO `saneeconomy_balances` (unique_identifier, balance) VALUES (?, ?)");
|
ps = conn.prepareStatement(String.format("INSERT INTO `%s` (unique_identifier, balance) VALUES (?, ?)", dbConn.getTable("saneeconomy_balances")));
|
||||||
ps.setString(1, "player:" + e.getKey());
|
ps.setString(1, "player:" + e.getKey());
|
||||||
ps.setDouble(2, e.getValue());
|
ps.setDouble(2, e.getValue());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
@ -100,7 +100,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
public synchronized void reloadDatabase() {
|
public synchronized void reloadDatabase() {
|
||||||
createTables();
|
createTables();
|
||||||
try (Connection conn = dbConn.openConnection()) {
|
try (Connection conn = dbConn.openConnection()) {
|
||||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM `saneeconomy_balances`");
|
PreparedStatement ps = conn.prepareStatement(String.format("SELECT * FROM `%s`", dbConn.getTable("saneeconomy_balances")));
|
||||||
ResultSet rs = ps.executeQuery();
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
balances.clear();
|
balances.clear();
|
||||||
@ -121,7 +121,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
dbConn.executeAsyncOperation((conn) -> {
|
dbConn.executeAsyncOperation((conn) -> {
|
||||||
try {
|
try {
|
||||||
ensureAccountExists(economable, conn);
|
ensureAccountExists(economable, conn);
|
||||||
PreparedStatement statement = conn.prepareStatement("UPDATE `saneeconomy_balances` SET balance = ? WHERE `unique_identifier` = ?");
|
PreparedStatement statement = conn.prepareStatement(String.format("UPDATE `%s` SET balance = ? WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
|
||||||
statement.setDouble(1, newBalance);
|
statement.setDouble(1, newBalance);
|
||||||
statement.setString(2, economable.getUniqueIdentifier());
|
statement.setString(2, economable.getUniqueIdentifier());
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
@ -134,14 +134,14 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
|||||||
|
|
||||||
private synchronized void ensureAccountExists(Economable economable, Connection conn) throws SQLException {
|
private synchronized void ensureAccountExists(Economable economable, Connection conn) throws SQLException {
|
||||||
if (!accountExists(economable, conn)) {
|
if (!accountExists(economable, conn)) {
|
||||||
PreparedStatement statement = conn.prepareStatement("INSERT INTO `saneeconomy_balances` (unique_identifier, balance) VALUES (?, 0.0)");
|
PreparedStatement statement = conn.prepareStatement(String.format("INSERT INTO `%s` (unique_identifier, balance) VALUES (?, 0.0)", dbConn.getTable("saneeconomy_balances")));
|
||||||
statement.setString(1, economable.getUniqueIdentifier());
|
statement.setString(1, economable.getUniqueIdentifier());
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized boolean accountExists(Economable economable, Connection conn) throws SQLException {
|
private synchronized boolean accountExists(Economable economable, Connection conn) throws SQLException {
|
||||||
PreparedStatement statement = conn.prepareStatement("SELECT 1 FROM `saneeconomy_balances` WHERE `unique_identifier` = ?");
|
PreparedStatement statement = conn.prepareStatement(String.format("SELECT 1 FROM `%s` WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
|
||||||
statement.setString(1, economable.getUniqueIdentifier());
|
statement.setString(1, economable.getUniqueIdentifier());
|
||||||
|
|
||||||
ResultSet rs = statement.executeQuery();
|
ResultSet rs = statement.executeQuery();
|
||||||
|
@ -23,7 +23,7 @@ public class TransactionLoggerMySQL implements TransactionLogger {
|
|||||||
private void logGeneric(String from, String to, double change, TransactionReason reason) {
|
private void logGeneric(String from, String to, double change, TransactionReason reason) {
|
||||||
this.dbConn.executeAsyncOperation((conn) -> {
|
this.dbConn.executeAsyncOperation((conn) -> {
|
||||||
try {
|
try {
|
||||||
PreparedStatement ps = conn.prepareStatement("INSERT INTO transaction_logs (`source`, `destination`, `amount`, `reason`) VALUES (?, ?, ?, ?)");
|
PreparedStatement ps = conn.prepareStatement(String.format("INSERT INTO `%s` (`source`, `destination`, `amount`, `reason`) VALUES (?, ?, ?, ?)", dbConn.getTable("transaction_logs")));
|
||||||
ps.setString(1, from);
|
ps.setString(1, from);
|
||||||
ps.setString(2, to);
|
ps.setString(2, to);
|
||||||
ps.setDouble(3, change);
|
ps.setDouble(3, change);
|
||||||
@ -46,7 +46,7 @@ public class TransactionLoggerMySQL implements TransactionLogger {
|
|||||||
|
|
||||||
private void createTables() {
|
private void createTables() {
|
||||||
try (Connection conn = dbConn.openConnection()) {
|
try (Connection conn = dbConn.openConnection()) {
|
||||||
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `transaction_logs` (`source` VARCHAR(128), `destination` VARCHAR(128), `amount` DECIMAL(18, 2), `reason` VARCHAR(128))");
|
PreparedStatement ps = conn.prepareStatement(String.format("CREATE TABLE IF NOT EXISTS `%s` (`source` VARCHAR(128), `destination` VARCHAR(128), `amount` DECIMAL(18, 2), `reason` VARCHAR(128))", dbConn.getTable("transaction_logs")));
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException("Failed to create transaction logger tables", e);
|
throw new RuntimeException("Failed to create transaction logger tables", e);
|
||||||
|
@ -10,13 +10,15 @@ public class DatabaseCredentials {
|
|||||||
private final String username;
|
private final String username;
|
||||||
private final String password;
|
private final String password;
|
||||||
private final String databaseName;
|
private final String databaseName;
|
||||||
|
private final String tablePrefix;
|
||||||
|
|
||||||
public DatabaseCredentials(String hostname, int port, String username, String password, String databaseName) {
|
public DatabaseCredentials(String hostname, int port, String username, String password, String databaseName, String tablePrefix) {
|
||||||
this.hostname = hostname;
|
this.hostname = hostname;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.databaseName = databaseName;
|
this.databaseName = databaseName;
|
||||||
|
this.tablePrefix = tablePrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHostname() {
|
public String getHostname() {
|
||||||
@ -42,4 +44,8 @@ public class DatabaseCredentials {
|
|||||||
public String getJDBCURL() {
|
public String getJDBCURL() {
|
||||||
return String.format("jdbc:mysql://%s:%d/%s", hostname, port, databaseName);
|
return String.format("jdbc:mysql://%s:%d/%s", hostname, port, databaseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTablePrefix() {
|
||||||
|
return tablePrefix;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,10 @@ public class MySQLConnection {
|
|||||||
return dbCredentials;
|
return dbCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTable(String tableName) {
|
||||||
|
return dbCredentials.getTablePrefix() + tableName;
|
||||||
|
}
|
||||||
|
|
||||||
private void waitForSlot() {
|
private void waitForSlot() {
|
||||||
while (openTransactions.get() >= MAX_OPEN_TRANSACTIONS) {
|
while (openTransactions.get() >= MAX_OPEN_TRANSACTIONS) {
|
||||||
try {
|
try {
|
||||||
|
@ -150,9 +150,10 @@ public class SaneEconomyConfiguration {
|
|||||||
String backendDb = config.getString("database");
|
String backendDb = config.getString("database");
|
||||||
String backendUser = config.getString("username");
|
String backendUser = config.getString("username");
|
||||||
String backendPass = config.getString("password");
|
String backendPass = config.getString("password");
|
||||||
|
String tablePrefix = config.getString("table_prefix", "");
|
||||||
|
|
||||||
return new DatabaseCredentials(
|
return new DatabaseCredentials(
|
||||||
backendHost, backendPort, backendUser, backendPass, backendDb
|
backendHost, backendPort, backendUser, backendPass, backendDb, tablePrefix
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user