From abdf8223fc667a8865ace7a8200e880fbae28a2d Mon Sep 17 00:00:00 2001 From: William Date: Sun, 29 Sep 2024 13:35:28 +0100 Subject: [PATCH] refactor: adjust postgres statements --- .../husksync/database/PostgresDatabase.java | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java b/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java index 3293d138..ee002c81 100644 --- a/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java +++ b/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java @@ -137,9 +137,9 @@ public class PostgresDatabase extends Database { // Update a user's name if it has changed in the database try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE "%users_table%" - SET "username"=? - WHERE "uuid"=?"""))) { + UPDATE %users_table% + SET username=? + WHERE uuid=?;"""))) { statement.setString(1, user.getUsername()); statement.setObject(2, existingUser.getUuid()); @@ -155,7 +155,7 @@ public class PostgresDatabase extends Database { // Insert new player data into the database try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO "%users_table%" ("uuid","username") + INSERT INTO %users_table% (uuid,username) VALUES (?,?);"""))) { statement.setObject(1, user.getUuid()); @@ -174,9 +174,9 @@ public class PostgresDatabase extends Database { public Optional getUser(@NotNull UUID uuid) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT "uuid", "username" - FROM "%users_table%" - WHERE "uuid"=?"""))) { + SELECT uuid, username + FROM %users_table% + WHERE uuid=?;"""))) { statement.setObject(1, uuid); @@ -197,9 +197,9 @@ public class PostgresDatabase extends Database { public Optional getUserByName(@NotNull String username) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT "uuid", "username" - FROM "%users_table%" - WHERE "username"=?"""))) { + SELECT uuid, username + FROM %users_table% + WHERE username=?;"""))) { statement.setString(1, username); final ResultSet resultSet = statement.executeQuery(); @@ -221,8 +221,8 @@ public class PostgresDatabase extends Database { final List users = Lists.newArrayList(); try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT `uuid`, `username` - FROM `%users_table%`; + SELECT uuid, username + FROM %users_table%; """))) { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { @@ -241,10 +241,10 @@ public class PostgresDatabase extends Database { public Optional getLatestSnapshot(@NotNull User user) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT "version_uuid", "timestamp", "data" - FROM "%user_data_table%" - WHERE "player_uuid"=? - ORDER BY "timestamp" DESC + SELECT version_uuid, timestamp, data + FROM %user_data_table% + WHERE player_uuid=? + ORDER BY timestamp DESC LIMIT 1;"""))) { statement.setObject(1, user.getUuid()); final ResultSet resultSet = statement.executeQuery(); @@ -270,10 +270,10 @@ public class PostgresDatabase extends Database { final List retrievedData = Lists.newArrayList(); try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT "version_uuid", "timestamp", "data" - FROM "%user_data_table%" - WHERE "player_uuid"=? - ORDER BY "timestamp" DESC;"""))) { + SELECT version_uuid, timestamp, data + FROM %user_data_table% + WHERE player_uuid=? + ORDER BY timestamp DESC;"""))) { statement.setObject(1, user.getUuid()); final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { @@ -297,10 +297,10 @@ public class PostgresDatabase extends Database { public Optional getSnapshot(@NotNull User user, @NotNull UUID versionUuid) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - SELECT "version_uuid", "timestamp", "data" - FROM "%user_data_table%" - WHERE "player_uuid"=? AND "version_uuid"=? - ORDER BY "timestamp" DESC + SELECT version_uuid, timestamp, data + FROM %user_data_table% + WHERE player_uuid=? AND version_uuid=? + ORDER BY timestamp DESC LIMIT 1;"""))) { statement.setObject(1, user.getUuid()); statement.setObject(2, versionUuid); @@ -328,10 +328,10 @@ public class PostgresDatabase extends Database { if (unpinnedUserData.size() > maxSnapshots) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - DELETE FROM "%user_data_table%" - WHERE "player_uuid"=? - AND "pinned" = FALSE - ORDER BY "timestamp" ASC + DELETE FROM %user_data_table% + WHERE player_uuid=? + AND pinned=FALSE + ORDER BY timestamp ASC LIMIT %entry_count%;""".replace("%entry_count%", Integer.toString(unpinnedUserData.size() - maxSnapshots))))) { statement.setObject(1, user.getUuid()); @@ -348,8 +348,8 @@ public class PostgresDatabase extends Database { public boolean deleteSnapshot(@NotNull User user, @NotNull UUID versionUuid) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - DELETE FROM "%user_data_table%" - WHERE "player_uuid"=? AND "version_uuid"=? + DELETE FROM %user_data_table% + WHERE player_uuid=? AND version_uuid=? LIMIT 1;"""))) { statement.setObject(1, user.getUuid()); statement.setString(2, versionUuid.toString()); @@ -366,12 +366,12 @@ public class PostgresDatabase extends Database { protected void rotateLatestSnapshot(@NotNull User user, @NotNull OffsetDateTime within) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - DELETE FROM "%user_data_table%" - WHERE "player_uuid"=? AND "timestamp" = ( - SELECT "timestamp" - FROM "%user_data_table%" - WHERE "player_uuid"=? AND "timestamp" > ? AND "pinned" = FALSE - ORDER BY "timestamp" ASC + DELETE FROM %user_data_table% + WHERE player_uuid=? AND timestamp = ( + SELECT timestamp + FROM %user_data_table% + WHERE player_uuid=? AND timestamp > ? AND pinned=FALSE + ORDER BY timestamp ASC LIMIT 1 );"""))) { statement.setObject(1, user.getUuid()); @@ -389,8 +389,8 @@ public class PostgresDatabase extends Database { protected void createSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - INSERT INTO "%user_data_table%" - ("player_uuid","version_uuid","timestamp","save_cause","pinned","data") + INSERT INTO %user_data_table% + (player_uuid,version_uuid,timestamp,save_cause,pinned,data) VALUES (?,?,?,?,?,?);"""))) { statement.setObject(1, user.getUuid()); statement.setObject(2, data.getId()); @@ -410,9 +410,9 @@ public class PostgresDatabase extends Database { public void updateSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE "%user_data_table%" - SET "save_cause"=?,"pinned"=?,"data"=? - WHERE "player_uuid"=? AND "version_uuid"=? + UPDATE %user_data_table% + SET save_cause=?,pinned=?,data=? + WHERE player_uuid=? AND version_uuid=? LIMIT 1;"""))) { statement.setString(1, data.getSaveCause().name()); statement.setBoolean(2, data.isPinned());