COUNT() always return something, statement found would be always true.

This commit is contained in:
Raizer 2021-09-02 00:32:33 +00:00 committed by ArpyAge
parent be3207eca0
commit e76c2de94a

View File

@ -55,16 +55,18 @@ public class GiveBadge extends RCONMessage<GiveBadge.GiveBadgeJSON> {
} else { } else {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
for (String badgeCode : json.badge.split(";")) { for (String badgeCode : json.badge.split(";")) {
boolean found; int numberOfRows = 0;
try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(slot_id) FROM users_badges INNER JOIN users ON users.id = user_id WHERE users.id = ? AND badge_code = ? LIMIT 1")) { try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(slot_id) FROM users_badges INNER JOIN users ON users.id = user_id WHERE users.id = ? AND badge_code = ? LIMIT 1")) {
statement.setInt(1, json.user_id); statement.setInt(1, json.user_id);
statement.setString(2, badgeCode); statement.setString(2, badgeCode);
try (ResultSet set = statement.executeQuery()) { try (ResultSet set = statement.executeQuery()) {
found = set.next(); if (set.next()){
numberOfRows = set.getInt(1);
}
} }
} }
if (found) { if (numberOfRows != 0) {
this.status = RCONMessage.STATUS_ERROR; this.status = RCONMessage.STATUS_ERROR;
this.message += Emulator.getTexts().getValue("commands.error.cmd_badge.already_owns").replace("%user%", username).replace("%badge%", badgeCode) + "\r"; this.message += Emulator.getTexts().getValue("commands.error.cmd_badge.already_owns").replace("%user%", username).replace("%badge%", badgeCode) + "\r";
} else { } else {
@ -92,4 +94,4 @@ public class GiveBadge extends RCONMessage<GiveBadge.GiveBadgeJSON> {
public String badge; public String badge;
} }
} }