From cd3e4ef063ed4c9c9933b89f92495ffa5328fcf5 Mon Sep 17 00:00:00 2001 From: William278 Date: Wed, 9 Apr 2025 19:05:55 +0100 Subject: [PATCH] fix: sql syntax error with `getUnpinnedSnapshotCount` --- .../java/net/william278/husksync/database/Database.java | 7 +++---- .../william278/husksync/database/MongoDbDatabase.java | 9 +++------ .../net/william278/husksync/database/MySqlDatabase.java | 7 +++---- .../william278/husksync/database/PostgresDatabase.java | 7 +++---- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/database/Database.java b/common/src/main/java/net/william278/husksync/database/Database.java index 0fb5ad82..27ee84bf 100644 --- a/common/src/main/java/net/william278/husksync/database/Database.java +++ b/common/src/main/java/net/william278/husksync/database/Database.java @@ -140,14 +140,13 @@ public abstract class Database { public abstract List getAllSnapshots(@NotNull User user); /** - * Get the number of {@link DataSnapshot}s a user has + * Get the number of unpinned {@link DataSnapshot}s a user has * - * @param user the user to count snapshots for - * @param includePinned whether to include pinned snapshots in the search + * @param user the user to count snapshots for * @return the number of snapshots this user has saved */ @Blocking - public abstract int getSnapshotCount(@NotNull User user, boolean includePinned); + public abstract int getUnpinnedSnapshotCount(@NotNull User user); /** * Gets a specific {@link DataSnapshot} entry for a user from the database, by its UUID. diff --git a/common/src/main/java/net/william278/husksync/database/MongoDbDatabase.java b/common/src/main/java/net/william278/husksync/database/MongoDbDatabase.java index fcf7f502..c4019c4d 100644 --- a/common/src/main/java/net/william278/husksync/database/MongoDbDatabase.java +++ b/common/src/main/java/net/william278/husksync/database/MongoDbDatabase.java @@ -235,12 +235,9 @@ public class MongoDbDatabase extends Database { } @Override - public int getSnapshotCount(@NotNull User user, boolean includePinned) { + public int getUnpinnedSnapshotCount(@NotNull User user) { try { - Document filter = new Document("player_uuid", user.getUuid()); - if (!includePinned) { - filter = filter.append("pinned", false); - } + Document filter = new Document("player_uuid", user.getUuid()).append("pinned", false); return (int) mongoCollectionHelper.getCollection(userDataTable).countDocuments(filter); } catch (MongoException e) { plugin.log(Level.SEVERE, "Failed to fetch a user's current snapshot count", e); @@ -273,7 +270,7 @@ public class MongoDbDatabase extends Database { @Override protected void rotateSnapshots(@NotNull User user) { try { - final int unpinnedSnapshots = getSnapshotCount(user, false); + final int unpinnedSnapshots = getUnpinnedSnapshotCount(user); final int maxSnapshots = plugin.getSettings().getSynchronization().getMaxUserDataSnapshots(); if (unpinnedSnapshots > maxSnapshots) { Document filter = new Document("player_uuid", user.getUuid()).append("pinned", false); diff --git a/common/src/main/java/net/william278/husksync/database/MySqlDatabase.java b/common/src/main/java/net/william278/husksync/database/MySqlDatabase.java index f387c9cb..e139356f 100644 --- a/common/src/main/java/net/william278/husksync/database/MySqlDatabase.java +++ b/common/src/main/java/net/william278/husksync/database/MySqlDatabase.java @@ -305,14 +305,13 @@ public class MySqlDatabase extends Database { } @Override - public int getSnapshotCount(@NotNull User user, boolean includePinned) { + public int getUnpinnedSnapshotCount(@NotNull User user) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT COUNT(`version_uuid`) - FROM `%user_data_table%` AND `pinned`=false OR `pinned`=? + FROM `%user_data_table%` AND `pinned`=false WHERE `player_uuid`=?;"""))) { statement.setString(1, user.getUuid().toString()); - statement.setBoolean(2, includePinned); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { return resultSet.getInt(1); @@ -356,7 +355,7 @@ public class MySqlDatabase extends Database { @Blocking @Override protected void rotateSnapshots(@NotNull User user) { - final int unpinnedSnapshots = getSnapshotCount(user, false); + final int unpinnedSnapshots = getUnpinnedSnapshotCount(user); final int maxSnapshots = plugin.getSettings().getSynchronization().getMaxUserDataSnapshots(); if (unpinnedSnapshots > maxSnapshots) { try (Connection connection = getConnection()) { 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 4b57d7c6..2bfc941c 100644 --- a/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java +++ b/common/src/main/java/net/william278/husksync/database/PostgresDatabase.java @@ -294,14 +294,13 @@ public class PostgresDatabase extends Database { } @Override - public int getSnapshotCount(@NotNull User user, boolean includePinned) { + public int getUnpinnedSnapshotCount(@NotNull User user) { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" SELECT COUNT(`version_uuid`) - FROM `%user_data_table%` AND `pinned`=false OR `pinned`=? + FROM `%user_data_table%` AND `pinned`=false WHERE `player_uuid`=?;"""))) { statement.setString(1, user.getUuid().toString()); - statement.setBoolean(2, includePinned); final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { return resultSet.getInt(1); @@ -343,7 +342,7 @@ public class PostgresDatabase extends Database { @Blocking @Override protected void rotateSnapshots(@NotNull User user) { - final int unpinnedSnapshots = getSnapshotCount(user, false); + final int unpinnedSnapshots = getUnpinnedSnapshotCount(user); final int maxSnapshots = plugin.getSettings().getSynchronization().getMaxUserDataSnapshots(); if (unpinnedSnapshots > maxSnapshots) { try (Connection connection = getConnection()) {