From 205cd13dd874a3593d9cf4f5e2a2f81db841e4d8 Mon Sep 17 00:00:00 2001 From: Vankka Date: Mon, 7 Mar 2022 10:35:00 +0200 Subject: [PATCH] Table prefixes --- .../config/connection/StorageConfig.java | 3 +++ .../common/storage/impl/sql/SQLStorage.java | 18 +++++++++++++----- .../storage/impl/sql/file/H2Storage.java | 4 ++-- .../storage/impl/sql/hikari/MySQLStorage.java | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/config/connection/StorageConfig.java b/common/src/main/java/com/discordsrv/common/config/connection/StorageConfig.java index 6a3aa86d..36ffe220 100644 --- a/common/src/main/java/com/discordsrv/common/config/connection/StorageConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/connection/StorageConfig.java @@ -33,6 +33,9 @@ public class StorageConfig { + "- MySQL\n") public String backend = "h2"; + @Comment("SQL table prefix") + public String sqlTablePrefix = "discordsrv_"; + @Comment("Connection options for remote databases (MySQL)") public Remote remote = new Remote(); diff --git a/common/src/main/java/com/discordsrv/common/storage/impl/sql/SQLStorage.java b/common/src/main/java/com/discordsrv/common/storage/impl/sql/SQLStorage.java index 6fc271ac..513e1ada 100644 --- a/common/src/main/java/com/discordsrv/common/storage/impl/sql/SQLStorage.java +++ b/common/src/main/java/com/discordsrv/common/storage/impl/sql/SQLStorage.java @@ -40,7 +40,7 @@ public abstract class SQLStorage implements Storage { public abstract Connection getConnection(); public abstract boolean isAutoCloseConnections(); - public abstract void createTables(Connection connection, boolean linkedAccounts) throws SQLException; + public abstract void createTables(Connection connection, String tablePrefix, boolean linkedAccounts) throws SQLException; private void useConnection(CheckedConsumer connectionConsumer) throws StorageException { useConnection(connection -> { @@ -71,14 +71,18 @@ public abstract class SQLStorage implements Storage { @Override public void initialize() { - useConnection((CheckedConsumer) connection -> createTables(connection, discordSRV.linkProvider() instanceof StorageLinker)); + useConnection((CheckedConsumer) connection -> createTables( + connection, + discordSRV.connectionConfig().storage.sqlTablePrefix, + discordSRV.linkProvider() instanceof StorageLinker) + ); } @Override public @Nullable Long getUserId(@NotNull UUID player) { return useConnection(connection -> { try (PreparedStatement statement = connection.prepareStatement("select USER_ID from LINKED_ACCOUNTS where PLAYER_UUID = ?;")) { - statement.setObject(1, player); + statement.setString(1, player.toString()); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { return resultSet.getLong("USER_ID"); @@ -96,7 +100,11 @@ public abstract class SQLStorage implements Storage { statement.setLong(1, userId); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { - return resultSet.getObject("PLAYER_UUID", UUID.class); + String value = resultSet.getString("PLAYER_UUID"); + if (value == null) { + return null; + } + return UUID.fromString(value); } } } @@ -108,7 +116,7 @@ public abstract class SQLStorage implements Storage { public void createLink(@NotNull UUID player, long userId) { useConnection(connection -> { try (PreparedStatement statement = connection.prepareStatement("insert into LINKED_ACCOUNTS (PLAYER_UUID, USER_ID) values (?, ?);")) { - statement.setObject(1, player); + statement.setString(1, player.toString()); statement.setLong(2, userId); exceptEffectedRows(statement.executeUpdate(), 1); diff --git a/common/src/main/java/com/discordsrv/common/storage/impl/sql/file/H2Storage.java b/common/src/main/java/com/discordsrv/common/storage/impl/sql/file/H2Storage.java index 0a46b138..732a761f 100644 --- a/common/src/main/java/com/discordsrv/common/storage/impl/sql/file/H2Storage.java +++ b/common/src/main/java/com/discordsrv/common/storage/impl/sql/file/H2Storage.java @@ -100,11 +100,11 @@ public class H2Storage extends SQLStorage { } @Override - public void createTables(Connection connection, boolean linkedAccounts) throws SQLException { + public void createTables(Connection connection, String tablePrefix, boolean linkedAccounts) throws SQLException { if (linkedAccounts) { try (Statement statement = connection.createStatement()) { statement.execute( - "create table if not exists LINKED_ACCOUNTS " + "create table if not exists " + tablePrefix + "linked_accounts " + "(ID int not null auto_increment, " + "PLAYER_UUID varchar(36), " + "USER_ID bigint, " diff --git a/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java index e4b276f0..60a7c8fa 100644 --- a/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java +++ b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java @@ -52,11 +52,11 @@ public class MySQLStorage extends HikariStorage { } @Override - public void createTables(Connection connection, boolean linkedAccounts) throws SQLException { + public void createTables(Connection connection, String tablePrefix, boolean linkedAccounts) throws SQLException { if (linkedAccounts) { try (Statement statement = connection.createStatement()) { statement.execute( - "create table if not exists LINKED_ACCOUNTS " + "create table if not exists " + tablePrefix + "linked_accounts " + "(ID int not null auto_increment, " + "PLAYER_UUID varchar(36), " + "USER_ID bigint, "