Table prefixes

This commit is contained in:
Vankka 2022-03-07 10:35:00 +02:00
parent 373e866346
commit 205cd13dd8
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 20 additions and 9 deletions

View File

@ -33,6 +33,9 @@ public class StorageConfig {
+ "- MySQL\n") + "- MySQL\n")
public String backend = "h2"; public String backend = "h2";
@Comment("SQL table prefix")
public String sqlTablePrefix = "discordsrv_";
@Comment("Connection options for remote databases (MySQL)") @Comment("Connection options for remote databases (MySQL)")
public Remote remote = new Remote(); public Remote remote = new Remote();

View File

@ -40,7 +40,7 @@ public abstract class SQLStorage implements Storage {
public abstract Connection getConnection(); public abstract Connection getConnection();
public abstract boolean isAutoCloseConnections(); 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<Connection> connectionConsumer) throws StorageException { private void useConnection(CheckedConsumer<Connection> connectionConsumer) throws StorageException {
useConnection(connection -> { useConnection(connection -> {
@ -71,14 +71,18 @@ public abstract class SQLStorage implements Storage {
@Override @Override
public void initialize() { public void initialize() {
useConnection((CheckedConsumer<Connection>) connection -> createTables(connection, discordSRV.linkProvider() instanceof StorageLinker)); useConnection((CheckedConsumer<Connection>) connection -> createTables(
connection,
discordSRV.connectionConfig().storage.sqlTablePrefix,
discordSRV.linkProvider() instanceof StorageLinker)
);
} }
@Override @Override
public @Nullable Long getUserId(@NotNull UUID player) { public @Nullable Long getUserId(@NotNull UUID player) {
return useConnection(connection -> { return useConnection(connection -> {
try (PreparedStatement statement = connection.prepareStatement("select USER_ID from LINKED_ACCOUNTS where PLAYER_UUID = ?;")) { 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()) { try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getLong("USER_ID"); return resultSet.getLong("USER_ID");
@ -96,7 +100,11 @@ public abstract class SQLStorage implements Storage {
statement.setLong(1, userId); statement.setLong(1, userId);
try (ResultSet resultSet = statement.executeQuery()) { try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) { 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) { public void createLink(@NotNull UUID player, long userId) {
useConnection(connection -> { useConnection(connection -> {
try (PreparedStatement statement = connection.prepareStatement("insert into LINKED_ACCOUNTS (PLAYER_UUID, USER_ID) values (?, ?);")) { 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); statement.setLong(2, userId);
exceptEffectedRows(statement.executeUpdate(), 1); exceptEffectedRows(statement.executeUpdate(), 1);

View File

@ -100,11 +100,11 @@ public class H2Storage extends SQLStorage {
} }
@Override @Override
public void createTables(Connection connection, boolean linkedAccounts) throws SQLException { public void createTables(Connection connection, String tablePrefix, boolean linkedAccounts) throws SQLException {
if (linkedAccounts) { if (linkedAccounts) {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
statement.execute( statement.execute(
"create table if not exists LINKED_ACCOUNTS " "create table if not exists " + tablePrefix + "linked_accounts "
+ "(ID int not null auto_increment, " + "(ID int not null auto_increment, "
+ "PLAYER_UUID varchar(36), " + "PLAYER_UUID varchar(36), "
+ "USER_ID bigint, " + "USER_ID bigint, "

View File

@ -52,11 +52,11 @@ public class MySQLStorage extends HikariStorage {
} }
@Override @Override
public void createTables(Connection connection, boolean linkedAccounts) throws SQLException { public void createTables(Connection connection, String tablePrefix, boolean linkedAccounts) throws SQLException {
if (linkedAccounts) { if (linkedAccounts) {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
statement.execute( statement.execute(
"create table if not exists LINKED_ACCOUNTS " "create table if not exists " + tablePrefix + "linked_accounts "
+ "(ID int not null auto_increment, " + "(ID int not null auto_increment, "
+ "PLAYER_UUID varchar(36), " + "PLAYER_UUID varchar(36), "
+ "USER_ID bigint, " + "USER_ID bigint, "