update positions rather than set new one on home/warp update

This commit is contained in:
William committed 2022-05-31 17:44:53 +01:00
1 parent 5b8352411e
commit 8a7bb11c10
4 files changed
+140 -44

No files matched your search

@@ -181,19 +181,43 @@ public abstract class Database {
/**
* <b>(Internal use only)</b> - Sets a position to the position table in the database
*
* @param position The {@link Position} to set
* @param position The {@link Position} to set
* @param connection SQL connection
* @return The newly inserted row ID
* @throws SQLException if an SQL exception occurs doing this
*/
protected abstract int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException;
/**
* <b>(Internal use only)</b> - Updates position data
*
* @param positionId ID of the position to update
* @param position the new position
* @param connection SQL connection
* @throws SQLException if an SQL exception occurs doing this
*/
protected abstract void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException;
/**
* <b>(Internal use only)</b> - Sets position meta to the position metadata table in the database
*
* @param meta The {@link PositionMeta} to set
* @param meta The {@link PositionMeta} 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;
/**
* <b>(Internal use only)</b> - Updates position metadata
*
* @param metaId ID of the metadata to update
* @param meta the new metadata
* @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;
/**
* Ensure a {@link Player} has a {@link UserData} entry in the database and that their username is up-to-date
*
@@ -145,6 +145,32 @@ public class MySqlDatabase extends Database {
}
}
@Override
protected void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%positions_table%`
SET `x`=?,
`y`=?,
`z`=?,
`yaw`=?,
`pitch`=?,
`world_uuid`=?,
`world_name`=?,
`server_name`=?
WHERE `id`=?"""))) {
statement.setDouble(1, position.x);
statement.setDouble(2, position.y);
statement.setDouble(3, position.z);
statement.setFloat(4, position.yaw);
statement.setFloat(5, position.pitch);
statement.setString(6, position.world.uuid.toString());
statement.setString(7, position.world.name);
statement.setString(8, position.server.name);
statement.setDouble(9, positionId);
statement.executeUpdate();
}
}
@Override
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
@@ -161,6 +187,20 @@ 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();
}
}
@Override
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
@@ -776,16 +816,16 @@ public class MySqlDatabase extends Database {
return CompletableFuture.runAsync(() -> getHome(home.uuid)
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%homes_table%`
SET `position_id`=?, `metadata_id`=?, `public`=?
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `position_id`, `metadata_id` FROM `%homes_table%`
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(home, connection));
statement.setInt(2, setPositionMeta(home.meta, connection));
statement.setBoolean(3, home.isPublic);
statement.setString(4, home.uuid.toString());
statement.setString(1, home.uuid.toString());
statement.executeUpdate();
final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
updatePosition(resultSet.getInt("position_id"), home, connection);
updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection);
}
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE,
@@ -816,15 +856,16 @@ public class MySqlDatabase extends Database {
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%warps_table%`
SET `position_id`=?, `metadata_id`=?
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `position_id`, `metadata_id` FROM `%warps_table%`
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(warp, connection));
statement.setInt(2, setPositionMeta(warp.meta, connection));
statement.setString(3, warp.uuid.toString());
statement.setString(1, warp.uuid.toString());
statement.executeUpdate();
final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
updatePosition(resultSet.getInt("position_id"), warp, connection);
updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection);
}
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);
@@ -115,6 +115,32 @@ public class SqLiteDatabase extends Database {
}
}
@Override
protected void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%positions_table%`
SET `x`=?,
`y`=?,
`z`=?,
`yaw`=?,
`pitch`=?,
`world_uuid`=?,
`world_name`=?,
`server_name`=?
WHERE `id`=?"""))) {
statement.setDouble(1, position.x);
statement.setDouble(2, position.y);
statement.setDouble(3, position.z);
statement.setFloat(4, position.yaw);
statement.setFloat(5, position.pitch);
statement.setString(6, position.world.uuid.toString());
statement.setString(7, position.world.name);
statement.setString(8, position.server.name);
statement.setDouble(9, positionId);
statement.executeUpdate();
}
}
@Override
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
@@ -131,6 +157,20 @@ 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();
}
}
@Override
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
@@ -748,15 +788,15 @@ public class SqLiteDatabase extends Database {
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
try {
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%homes_table%`
SET `position_id`=?, `metadata_id`=?, `public`=?
SELECT `position_id`, `metadata_id` FROM `%homes_table%`
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(home, getConnection()));
statement.setInt(2, setPositionMeta(home.meta, getConnection()));
statement.setBoolean(3, home.isPublic);
statement.setString(4, home.uuid.toString());
statement.setString(1, home.uuid.toString());
statement.executeUpdate();
final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
updatePosition(resultSet.getInt("position_id"), home, connection);
updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection);
}
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE,
@@ -785,17 +825,18 @@ public class SqLiteDatabase extends Database {
@Override
public CompletableFuture<Void> setWarp(@NotNull Warp warp) {
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentWarp -> {
try {
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%warps_table%`
SET `position_id`=?, `metadata_id`=?
SELECT `position_id`, `metadata_id` FROM `%warps_table%`
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(warp, getConnection()));
statement.setInt(2, setPositionMeta(warp.meta, getConnection()));
statement.setString(3, warp.uuid.toString());
statement.setString(1, warp.uuid.toString());
statement.executeUpdate();
final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
updatePosition(resultSet.getInt("position_id"), warp, connection);
updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection);
}
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);