From 0c8f2eb5307e46882cb49a75b87f51b7dc87e089 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 9 Jun 2022 12:17:29 +0100 Subject: [PATCH] Change SQL schema again --- .../william278/huskhomes/config/Settings.java | 2 +- .../william278/huskhomes/data/Database.java | 38 ++-- .../huskhomes/data/MySqlDatabase.java | 138 +++++++------ .../huskhomes/data/SqLiteDatabase.java | 185 ++++++++++-------- common/src/main/resources/config.yml | 2 +- .../main/resources/database/mysql_schema.sql | 30 ++- .../main/resources/database/sqlite_schema.sql | 30 ++- 7 files changed, 232 insertions(+), 193 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 92957aff..400f8968 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -131,7 +131,7 @@ public class Settings { DATABASE_PLAYER_TABLE_NAME("data_storage_options.table_names.player_data", OptionType.STRING, "huskhomes_player_data"), DATABASE_POSITIONS_TABLE_NAME("data_storage_options.table_names.position_data", OptionType.STRING, "huskhomes_position_data"), - DATABASE_POSITIONS_META_DATA_TABLE_NAME("data_storage_options.table_names.position_meta_data", OptionType.STRING, "huskhomes_position_metadata"), + DATABASE_SAVED_POSITIONS_TABLE_NAME("data_storage_options.table_names.saved_positions_data", OptionType.STRING, "huskhomes_saved_positions_data"), DATABASE_HOMES_TABLE_NAME("data_storage_options.table_names.home_data", OptionType.STRING, "huskhomes_home_data"), DATABASE_WARPS_TABLE_NAME("data_storage_options.table_names.warp_data", OptionType.STRING, "huskhomes_warp_data"), DATABASE_TELEPORTS_TABLE_NAME("data_storage_options.table_names.teleport_data", OptionType.STRING, "huskhomes_teleport_data"), diff --git a/common/src/main/java/net/william278/huskhomes/data/Database.java b/common/src/main/java/net/william278/huskhomes/data/Database.java index bdd9db69..81282a0e 100644 --- a/common/src/main/java/net/william278/huskhomes/data/Database.java +++ b/common/src/main/java/net/william278/huskhomes/data/Database.java @@ -4,10 +4,7 @@ import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.player.Player; import net.william278.huskhomes.player.User; import net.william278.huskhomes.player.UserData; -import net.william278.huskhomes.position.Home; -import net.william278.huskhomes.position.Position; -import net.william278.huskhomes.position.PositionMeta; -import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.position.*; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.util.Logger; import net.william278.huskhomes.util.ResourceReader; @@ -62,23 +59,23 @@ public abstract class Database { protected final String positionsTableName; /** - * Stores {@link PositionMeta}; metadata about saved positions + * Stores {@link SavedPosition}s, including metadata about them *
    *
  1. id - Primary key, auto-increment; The id representing the position meta
  2. + *
  3. position_id - References the positions table; the position location data of this saved position
  4. *
  5. name - The name string of the position represented by this metadata
  6. *
  7. description - A description string of the position represented by this metadata
  8. *
  9. timestamp - A datetime timestamp representing when the position this represents was created
  10. *
*/ - protected final String positionMetadataTableName; + protected final String savedPositionsTableName; /** * Stores {@link Home} data *
    *
  1. uuid - Primary key, unique; The unique id of the home
  2. + *
  3. saved_position id - References the saved position table; The id of the home saved position data
  4. *
  5. owner_uuid - References the players table; the uuid of the person who set the home
  6. - *
  7. position_id - References the positions table; The id of the home position data
  8. - *
  9. metadata_id - References the metadata table; The id of the home metadata
  10. *
  11. is_public - Boolean value; represents if the home is set to public
  12. *
*/ @@ -88,8 +85,7 @@ public abstract class Database { * Stores {@link Warp} data *
    *
  1. uuid - Primary key, unique; The unique id of the warp
  2. - *
  3. position_id - References the positions table; The id of the warp location data
  4. - *
  5. metadata_id - References the metadata table; The id of the warp metadata
  6. + *
  7. saved_position id - References the saved position table; The id of the warp saved position data
  8. *
*/ protected final String warpsTableName; @@ -144,7 +140,7 @@ public abstract class Database { .replaceAll("%positions_table%", positionsTableName) .replaceAll("%players_table%", playerTableName) .replaceAll("%teleports_table%", teleportsTableName) - .replaceAll("%position_metadata_table%", positionMetadataTableName) + .replaceAll("%saved_positions_table%", savedPositionsTableName) .replaceAll("%homes_table%", homesTableName) .replaceAll("%warps_table%", warpsTableName); } @@ -159,8 +155,8 @@ public abstract class Database { getStringValue(Settings.ConfigOption.DATABASE_PLAYER_TABLE_NAME); this.positionsTableName = settings. getStringValue(Settings.ConfigOption.DATABASE_POSITIONS_TABLE_NAME); - this.positionMetadataTableName = settings. - getStringValue(Settings.ConfigOption.DATABASE_POSITIONS_META_DATA_TABLE_NAME); + this.savedPositionsTableName = settings. + getStringValue(Settings.ConfigOption.DATABASE_SAVED_POSITIONS_TABLE_NAME); this.homesTableName = settings. getStringValue(Settings.ConfigOption.DATABASE_HOMES_TABLE_NAME); this.warpsTableName = settings. @@ -199,24 +195,24 @@ public abstract class Database { protected abstract void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException; /** - * (Internal use only) - Sets position meta to the position metadata table in the database + * (Internal use only) - Sets a {@link SavedPosition} to the database * - * @param meta The {@link PositionMeta} to set + * @param position The {@link SavedPosition} to set * @param connection SQL connection * @return The newly inserted row ID * @throws SQLException if an SQL exception occurs doing this */ - protected abstract int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException; + protected abstract int setSavedPosition(@NotNull SavedPosition position, @NotNull Connection connection) throws SQLException; /** - * (Internal use only) - Updates position metadata + * (Internal use only) - Updates a saved position metadata * - * @param metaId ID of the metadata to update - * @param meta the new metadata - * @param connection SQL connection + * @param savedPositionId ID of the metadata to update + * @param savedPosition the new saved position + * @param connection SQL connection * @throws SQLException if an SQL exception occurs doing this */ - protected abstract void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException; + protected abstract void updateSavedPosition(int savedPositionId, @NotNull SavedPosition savedPosition, @NotNull Connection connection) throws SQLException; /** * Ensure a {@link Player} has a {@link UserData} entry in the database and that their username is up-to-date diff --git a/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java b/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java index 600f01ba..fb18859c 100644 --- a/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java @@ -172,15 +172,16 @@ public class MySqlDatabase extends Database { } @Override - protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { + protected int setSavedPosition(@NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO `%position_metadata_table%` (`name`, `description`, `timestamp`) - VALUES (?,?,?);"""), + INSERT INTO `%saved_positions_table%` (`position_id`, `name`, `description`, `timestamp`) + VALUES (?,?,?,?);"""), Statement.RETURN_GENERATED_KEYS)) { - statement.setString(1, meta.name); - statement.setString(2, meta.description); - statement.setTimestamp(3, new Timestamp(Instant.ofEpochSecond(meta.timestamp).toEpochMilli())); + statement.setInt(1, setPosition(position, connection)); + statement.setString(2, position.meta.name); + statement.setString(3, position.meta.description); + statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli())); statement.executeUpdate(); return statement.getGeneratedKeys().getInt(1); @@ -188,16 +189,27 @@ public class MySqlDatabase extends Database { } @Override - protected void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { - try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE `%position_metadata_table%` - SET `name`=?, - `description`=? - WHERE `id`=?;"""))) { - statement.setString(1, meta.name); - statement.setString(2, meta.description); - statement.setInt(3, metaId); - statement.executeUpdate(); + protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { + try (PreparedStatement selectStatement = connection.prepareStatement(""" + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `id`=?;""")) { + selectStatement.setInt(1, savedPositionId); + + final ResultSet resultSet = selectStatement.executeQuery(); + final int positionId = resultSet.getInt("position_id"); + updatePosition(positionId, position, connection); + + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%saved_positions_table%` + SET `name`=?, + `description`=? + WHERE `id`=?;"""))) { + updateStatement.setString(1, position.meta.name); + updateStatement.setString(2, position.meta.description); + updateStatement.setInt(3, savedPositionId); + updateStatement.executeUpdate(); + } } } @@ -303,9 +315,9 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `owner_uuid`=? ORDER BY `name`;"""))) { @@ -344,8 +356,8 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` ORDER BY `name`;"""))) { final ResultSet resultSet = statement.executeQuery(); @@ -379,9 +391,9 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `public`=true ORDER BY `name`;"""))) { @@ -417,12 +429,12 @@ public class MySqlDatabase extends Database { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` - FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` - WHERE `owner_uuid`=? - AND `name`=?;"""))) { + FROM `%homes_table%` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` + WHERE `owner_uuid`=? + AND `name`=?;"""))) { statement.setString(1, user.uuid.toString()); statement.setString(2, homeName); @@ -459,9 +471,9 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `%homes_table%`.`uuid`=?;"""))) { statement.setString(1, uuid.toString()); @@ -498,8 +510,8 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` WHERE `name`=?;"""))) { statement.setString(1, warpName); @@ -533,8 +545,8 @@ public class MySqlDatabase extends Database { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` WHERE `%warps_table%`.uuid=?;"""))) { statement.setString(1, uuid.toString()); @@ -816,15 +828,14 @@ public class MySqlDatabase extends Database { return CompletableFuture.runAsync(() -> getHome(home.uuid) .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { try (Connection connection = getConnection()) { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - SELECT `position_id`, `metadata_id` FROM `%homes_table%` + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `saved_position_id` FROM `%homes_table%` WHERE `uuid`=?;"""))) { statement.setString(1, home.uuid.toString()); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - updatePosition(resultSet.getInt("position_id"), home, connection); - updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection); + updateSavedPosition(resultSet.getInt("saved_position_id"), home, connection); } } } catch (SQLException e) { @@ -834,13 +845,12 @@ public class MySqlDatabase extends Database { }, () -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO `%homes_table%` (`uuid`, `owner_uuid`, `position_id`, `metadata_id`, `public`) - VALUES (?,?,?,?,?);"""))) { + INSERT INTO `%homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`) + VALUES (?,?,?,?);"""))) { statement.setString(1, home.uuid.toString()); - statement.setString(2, home.owner.uuid.toString()); - statement.setInt(3, setPosition(home, connection)); - statement.setInt(4, setPositionMeta(home.meta, connection)); - statement.setBoolean(5, home.isPublic); + statement.setInt(2, setSavedPosition(home, connection)); + statement.setString(3, home.owner.uuid.toString()); + statement.setBoolean(4, home.isPublic); statement.executeUpdate(); } @@ -854,17 +864,16 @@ public class MySqlDatabase extends Database { @Override public CompletableFuture setWarp(@NotNull Warp warp) { return CompletableFuture.runAsync(() -> getWarp(warp.uuid) - .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { + .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentWarp -> { try (Connection connection = getConnection()) { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - SELECT `position_id`, `metadata_id` FROM `%warps_table%` + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `saved_position_id` FROM `%warps_table%` WHERE `uuid`=?;"""))) { statement.setString(1, warp.uuid.toString()); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - updatePosition(resultSet.getInt("position_id"), warp, connection); - updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection); + updateSavedPosition(resultSet.getInt("saved_position_id"), warp, connection); } } } catch (SQLException e) { @@ -873,11 +882,10 @@ public class MySqlDatabase extends Database { }, () -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO `%warps_table%` (`uuid`, `position_id`, `metadata_id`) - VALUES (?,?,?);"""))) { + INSERT INTO `%warps_table%` (`uuid`, `saved_position_id`) + VALUES (?,?);"""))) { statement.setString(1, warp.uuid.toString()); - statement.setInt(2, setPosition(warp, connection)); - statement.setInt(3, setPositionMeta(warp.meta, connection)); + statement.setInt(2, setSavedPosition(warp, connection)); statement.executeUpdate(); } @@ -892,8 +900,16 @@ public class MySqlDatabase extends Database { return CompletableFuture.runAsync(() -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - DELETE FROM `%homes_table%` - WHERE `uuid`=?;"""))) { + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id`=( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id`=( + SELECT `saved_position_id` + FROM `%homes_table%` + WHERE `uuid`=? + ) + );"""))) { statement.setString(1, uuid.toString()); statement.executeUpdate(); @@ -909,8 +925,16 @@ public class MySqlDatabase extends Database { return CompletableFuture.runAsync(() -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - DELETE FROM `%warps_table%` - WHERE `uuid`=?;"""))) { + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id`=( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id`=( + SELECT `saved_position_id` + FROM `%warps_table%` + WHERE `uuid`=? + ) + );"""))) { statement.setString(1, uuid.toString()); statement.executeUpdate(); diff --git a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java index da904e74..b2e3e400 100644 --- a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java @@ -152,15 +152,16 @@ public class SqLiteDatabase extends Database { } @Override - protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { + protected int setSavedPosition(@NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO `%position_metadata_table%` (`name`, `description`, `timestamp`) - VALUES (?,?,?);"""), + INSERT INTO `%saved_positions_table%` (`position_id`, `name`, `description`, `timestamp`) + VALUES (?,?,?,?);"""), Statement.RETURN_GENERATED_KEYS)) { - statement.setString(1, meta.name); - statement.setString(2, meta.description); - statement.setTimestamp(3, new Timestamp(Instant.ofEpochSecond(meta.timestamp).toEpochMilli())); + statement.setInt(1, setPosition(position, connection)); + statement.setString(2, position.meta.name); + statement.setString(3, position.meta.description); + statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli())); statement.executeUpdate(); return statement.getGeneratedKeys().getInt(1); @@ -168,16 +169,27 @@ public class SqLiteDatabase extends Database { } @Override - protected void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { - try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE `%position_metadata_table%` - SET `name`=?, - `description`=? - WHERE `id`=?;"""))) { - statement.setString(1, meta.name); - statement.setString(2, meta.description); - statement.setInt(3, metaId); - statement.executeUpdate(); + protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { + try (PreparedStatement selectStatement = connection.prepareStatement(""" + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `id`=?;""")) { + selectStatement.setInt(1, savedPositionId); + + final ResultSet resultSet = selectStatement.executeQuery(); + final int positionId = resultSet.getInt("position_id"); + updatePosition(positionId, position, connection); + + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%saved_positions_table%` + SET `name`=?, + `description`=? + WHERE `id`=?;"""))) { + updateStatement.setString(1, position.meta.name); + updateStatement.setString(2, position.meta.description); + updateStatement.setInt(3, savedPositionId); + updateStatement.executeUpdate(); + } } } @@ -188,7 +200,7 @@ public class SqLiteDatabase extends Database { if (!existingUser.username.equals(player.getName())) { // Update a player's name if it has changed in the database try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `username`=? WHERE `uuid`=?"""))) { @@ -206,7 +218,7 @@ public class SqLiteDatabase extends Database { () -> { // Insert new player data into the database try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" INSERT INTO `%players_table%` (`uuid`,`username`) VALUES (?,?);"""))) { @@ -224,7 +236,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getUserByName(@NotNull String name) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown` FROM `%players_table%` WHERE `username`=?"""))) { @@ -251,7 +263,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getUser(@NotNull UUID uuid) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown` FROM `%players_table%` WHERE `uuid`=?"""))) { @@ -280,12 +292,12 @@ public class SqLiteDatabase extends Database { return CompletableFuture.supplyAsync(() -> { final List userHomes = new ArrayList<>(); try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `owner_uuid`=? ORDER BY `name`;"""))) { @@ -321,11 +333,11 @@ public class SqLiteDatabase extends Database { return CompletableFuture.supplyAsync(() -> { final List warps = new ArrayList<>(); try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` ORDER BY `name`;"""))) { final ResultSet resultSet = statement.executeQuery(); @@ -356,12 +368,12 @@ public class SqLiteDatabase extends Database { return CompletableFuture.supplyAsync(() -> { final List userHomes = new ArrayList<>(); try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `public`=true ORDER BY `name`;"""))) { @@ -395,12 +407,12 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getHome(@NotNull User user, @NotNull String homeName) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `owner_uuid`=? AND `name`=?;"""))) { statement.setString(1, user.uuid.toString()); @@ -436,13 +448,13 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getHome(@NotNull UUID uuid) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` - FROM `%homes_table%` - INNER JOIN `%positions_table%` ON `%homes_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` - INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` - WHERE `%homes_table%`.`uuid`=?;"""))) { + FROM `%homes_table%` + INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` + INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` + WHERE `%homes_table%`.`uuid`=?;"""))) { statement.setString(1, uuid.toString()); final ResultSet resultSet = statement.executeQuery(); @@ -465,7 +477,7 @@ public class SqLiteDatabase extends Database { } } } catch (SQLException e) { - getLogger().log(Level.SEVERE, "Failed to query a player's home by UUID", e); + getLogger().log(Level.SEVERE, "Failed to query a player's home by uuid", e); } return Optional.empty(); }); @@ -475,11 +487,11 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getWarp(@NotNull String warpName) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` WHERE `name`=?;"""))) { statement.setString(1, warpName); @@ -510,11 +522,11 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getWarp(@NotNull UUID uuid) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%warps_table%` - INNER JOIN `%positions_table%` ON `%warps_table%`.`position_id`=`%positions_table%`.`id` - INNER JOIN `%position_metadata_table%` ON `%warps_table%`.`metadata_id`=`%position_metadata_table%`.`id` + INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` + INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id` WHERE `%warps_table%`.uuid=?;"""))) { statement.setString(1, uuid.toString()); @@ -545,7 +557,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getCurrentTeleport(@NotNull User user) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%teleports_table%` INNER JOIN `%positions_table%` ON `%teleports_table%`.`destination_id` = `%positions_table%`.`id` @@ -576,7 +588,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture updateUserData(@NotNull UserData userData) { return CompletableFuture.runAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `home_slots`=?, `ignoring_requests`=?, `rtp_cooldown`=? WHERE `uuid`=?"""))) { @@ -599,7 +611,7 @@ public class SqLiteDatabase extends Database { if (teleport == null) { // Clear the user's current teleport try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" DELETE FROM `%teleports_table%` WHERE `player_uuid`=?;"""))) { statement.setString(1, user.uuid.toString()); @@ -612,11 +624,11 @@ public class SqLiteDatabase extends Database { } else { // Set the user's teleport into the database try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`) VALUES (?,?);"""))) { statement.setString(1, user.uuid.toString()); - statement.setInt(2, setPosition(teleport.target, getConnection())); + statement.setInt(2, setPosition(teleport.target, connection)); statement.executeUpdate(); } @@ -631,7 +643,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getLastPosition(@NotNull User user) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%players_table%` INNER JOIN `%positions_table%` ON `%players_table%`.`last_position` = `%positions_table%`.`id` @@ -659,14 +671,13 @@ public class SqLiteDatabase extends Database { @Override public CompletableFuture setLastPosition(@NotNull User user, @NotNull Position position) { - System.out.println("teleport b3.5"); return CompletableFuture.runAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `last_position`=? WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(position, getConnection())); + statement.setInt(1, setPosition(position, connection)); statement.setString(2, user.uuid.toString()); statement.executeUpdate(); @@ -681,7 +692,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getOfflinePosition(@NotNull User user) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%players_table%` INNER JOIN `%positions_table%` ON `%players_table%`.`offline_position` = `%positions_table%`.`id` @@ -711,11 +722,11 @@ public class SqLiteDatabase extends Database { public CompletableFuture setOfflinePosition(@NotNull User user, @NotNull Position position) { return CompletableFuture.runAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `offline_position`=? WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(position, getConnection())); + statement.setInt(1, setPosition(position, connection)); statement.setString(2, user.uuid.toString()); statement.executeUpdate(); @@ -730,7 +741,7 @@ public class SqLiteDatabase extends Database { public CompletableFuture> getRespawnPosition(@NotNull User user) { return CompletableFuture.supplyAsync(() -> { try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` FROM `%players_table%` INNER JOIN `%positions_table%` ON `%players_table%`.`respawn_position` = `%positions_table%`.`id` @@ -762,7 +773,7 @@ public class SqLiteDatabase extends Database { if (position == null) { // Clear the respawn position try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `respawn_position`=NULL WHERE `uuid`=?;"""))) { @@ -776,11 +787,11 @@ public class SqLiteDatabase extends Database { } else { // Set the respawn position try { - try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" UPDATE `%players_table%` SET `respawn_position`=? WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(position, getConnection())); + statement.setInt(1, setPosition(position, connection)); statement.setString(2, user.uuid.toString()); statement.executeUpdate(); @@ -798,14 +809,13 @@ public class SqLiteDatabase extends Database { .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - SELECT `position_id`, `metadata_id` FROM `%homes_table%` + SELECT `saved_position_id` FROM `%homes_table%` WHERE `uuid`=?;"""))) { statement.setString(1, home.uuid.toString()); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - updatePosition(resultSet.getInt("position_id"), home, connection); - updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection); + updateSavedPosition(resultSet.getInt("saved_position_id"), home, getConnection()); } } } catch (SQLException e) { @@ -815,13 +825,12 @@ public class SqLiteDatabase extends Database { }, () -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - INSERT INTO `%homes_table%` (`uuid`, `owner_uuid`, `position_id`, `metadata_id`, `public`) - VALUES (?,?,?,?,?);"""))) { + INSERT INTO `%homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`) + VALUES (?,?,?,?);"""))) { statement.setString(1, home.uuid.toString()); - statement.setString(2, home.owner.uuid.toString()); - statement.setInt(3, setPosition(home, getConnection())); - statement.setInt(4, setPositionMeta(home.meta, getConnection())); - statement.setBoolean(5, home.isPublic); + statement.setInt(2, setSavedPosition(home, getConnection())); + statement.setString(3, home.owner.uuid.toString()); + statement.setBoolean(4, home.isPublic); statement.executeUpdate(); } @@ -838,14 +847,13 @@ public class SqLiteDatabase extends Database { .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentWarp -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - SELECT `position_id`, `metadata_id` FROM `%warps_table%` + SELECT `saved_position_id` FROM `%warps_table%` WHERE `uuid`=?;"""))) { statement.setString(1, warp.uuid.toString()); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - updatePosition(resultSet.getInt("position_id"), warp, connection); - updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection); + updateSavedPosition(resultSet.getInt("saved_position_id"), warp, connection); } } } catch (SQLException e) { @@ -854,11 +862,10 @@ public class SqLiteDatabase extends Database { }, () -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - INSERT INTO `%warps_table%` (`uuid`, `position_id`, `metadata_id`) - VALUES (?,?,?);"""))) { + INSERT INTO `%warps_table%` (`uuid`, `saved_position_id`) + VALUES (?,?);"""))) { statement.setString(1, warp.uuid.toString()); - statement.setInt(2, setPosition(warp, getConnection())); - statement.setInt(3, setPositionMeta(warp.meta, getConnection())); + statement.setInt(2, setSavedPosition(warp, connection)); statement.executeUpdate(); } @@ -873,8 +880,16 @@ public class SqLiteDatabase extends Database { return CompletableFuture.runAsync(() -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - DELETE FROM `%homes_table%` - WHERE `uuid`=?;"""))) { + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id`=( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id`=( + SELECT `saved_position_id` + FROM `%homes_table%` + WHERE `uuid`=? + ) + );"""))) { statement.setString(1, uuid.toString()); statement.executeUpdate(); @@ -890,8 +905,16 @@ public class SqLiteDatabase extends Database { return CompletableFuture.runAsync(() -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - DELETE FROM `%warps_table%` - WHERE `uuid`=?;"""))) { + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id`=( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id`=( + SELECT `saved_position_id` + FROM `%warps_table%` + WHERE `uuid`=? + ) + );"""))) { statement.setString(1, uuid.toString()); statement.executeUpdate(); diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index b65250b7..af6cc5b5 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -24,7 +24,7 @@ data_storage_options: table_names: player_data: 'huskhomes_player_data' position_data: 'huskhomes_position_data' - position_meta_data: 'huskhomes_position_metadata' + saved_positions_data: 'huskhomes_saved_positions_data' home_data: 'huskhomes_home_data' warp_data: 'huskhomes_warp_data' teleport_data: 'huskhomes_teleport_data' diff --git a/common/src/main/resources/database/mysql_schema.sql b/common/src/main/resources/database/mysql_schema.sql index 5e08bafc..5c6f463d 100644 --- a/common/src/main/resources/database/mysql_schema.sql +++ b/common/src/main/resources/database/mysql_schema.sql @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS `%positions_table%` PRIMARY KEY (`id`) ); -# Create the player data table if it does not exist +# Create the players table if it does not exist CREATE TABLE IF NOT EXISTS `%players_table%` ( `uuid` char(36) NOT NULL UNIQUE, @@ -43,40 +43,38 @@ CREATE TABLE IF NOT EXISTS `%teleports_table%` FOREIGN KEY (`destination_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); -# Create the position metadata table if it does not exist -CREATE TABLE IF NOT EXISTS `%position_metadata_table%` +# Create the saved positions table if it does not exist +CREATE TABLE IF NOT EXISTS `%saved_positions_table%` ( `id` integer NOT NULL AUTO_INCREMENT, + `position_id` integer NOT NULL, `name` varchar(64) NOT NULL, `description` varchar(255) NOT NULL, `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); # Create the homes table if it does not exist CREATE TABLE IF NOT EXISTS `%homes_table%` ( - `uuid` char(36) NOT NULL UNIQUE, - `owner_uuid` char(36) NOT NULL, - `position_id` integer NOT NULL, - `metadata_id` integer NOT NULL, - `public` boolean NOT NULL DEFAULT FALSE, + `uuid` char(36) NOT NULL UNIQUE, + `saved_position_id` integer NOT NULL, + `owner_uuid` char(36) NOT NULL, + `public` boolean NOT NULL DEFAULT FALSE, PRIMARY KEY (`uuid`), FOREIGN KEY (`owner_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE, - FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION, - FOREIGN KEY (`metadata_id`) REFERENCES `%position_metadata_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION + FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); # Create the warps table if it does not exist CREATE TABLE IF NOT EXISTS `%warps_table%` ( - `uuid` char(36) NOT NULL UNIQUE, - `position_id` integer NOT NULL, - `metadata_id` integer NOT NULL, + `uuid` char(36) NOT NULL UNIQUE, + `saved_position_id` integer NOT NULL, PRIMARY KEY (`uuid`), - FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION, - FOREIGN KEY (`metadata_id`) REFERENCES `%position_metadata_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION + FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); \ No newline at end of file diff --git a/common/src/main/resources/database/sqlite_schema.sql b/common/src/main/resources/database/sqlite_schema.sql index 90694376..6633fcc2 100644 --- a/common/src/main/resources/database/sqlite_schema.sql +++ b/common/src/main/resources/database/sqlite_schema.sql @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS `%positions_table%` PRIMARY KEY (`id`) ); -/* Create the player data table if it does not exist */ +/* Create the players table if it does not exist */ CREATE TABLE IF NOT EXISTS `%players_table%` ( `uuid` char(36) NOT NULL UNIQUE, @@ -43,40 +43,38 @@ CREATE TABLE IF NOT EXISTS `%teleports_table%` FOREIGN KEY (`destination_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); -/* Create the position metadata table if it does not exist */ -CREATE TABLE IF NOT EXISTS `%position_metadata_table%` +/* Create the saved positions table if it does not exist */ +CREATE TABLE IF NOT EXISTS `%saved_positions_table%` ( `id` integer NOT NULL, + `position_id` integer NOT NULL, `name` varchar(64) NOT NULL, `description` varchar(255) NOT NULL, `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); /* Create the homes table if it does not exist */ CREATE TABLE IF NOT EXISTS `%homes_table%` ( - `uuid` char(36) NOT NULL UNIQUE, - `owner_uuid` char(36) NOT NULL, - `position_id` integer NOT NULL, - `metadata_id` integer NOT NULL, - `public` boolean NOT NULL DEFAULT FALSE, + `uuid` char(36) NOT NULL UNIQUE, + `saved_position_id` integer NOT NULL, + `owner_uuid` char(36) NOT NULL, + `public` boolean NOT NULL DEFAULT FALSE, PRIMARY KEY (`uuid`), FOREIGN KEY (`owner_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE, - FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION, - FOREIGN KEY (`metadata_id`) REFERENCES `%position_metadata_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION + FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); /* Create the warps table if it does not exist */ CREATE TABLE IF NOT EXISTS `%warps_table%` ( - `uuid` char(36) NOT NULL UNIQUE, - `position_id` integer NOT NULL, - `metadata_id` integer NOT NULL, + `uuid` char(36) NOT NULL UNIQUE, + `saved_position_id` integer NOT NULL, PRIMARY KEY (`uuid`), - FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION, - FOREIGN KEY (`metadata_id`) REFERENCES `%position_metadata_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION + FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION ); \ No newline at end of file