From 80eaaab754ea3720334578bd06923a10acb40dcd Mon Sep 17 00:00:00 2001 From: applenick Date: Wed, 12 Jun 2024 23:44:01 -0700 Subject: [PATCH] Fix boolean & long parse errors Signed-off-by: applenick --- .../services/SQLAssistanceService.java | 3 +- .../services/SQLFriendshipService.java | 5 +-- .../services/SQLModerationService.java | 9 ++--- .../nick/services/SQLNickService.java | 6 ++-- .../requests/services/SQLRequestService.java | 7 ++-- .../sessions/services/SQLSessionService.java | 3 +- .../users/services/AddressHistoryService.java | 4 ++- .../users/services/SQLUserService.java | 3 +- .../pgm/community/utils/DatabaseUtils.java | 34 +++++++++++++++++++ 9 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/main/java/dev/pgm/community/utils/DatabaseUtils.java diff --git a/src/main/java/dev/pgm/community/assistance/services/SQLAssistanceService.java b/src/main/java/dev/pgm/community/assistance/services/SQLAssistanceService.java index 917ceb1f..03b9c52e 100644 --- a/src/main/java/dev/pgm/community/assistance/services/SQLAssistanceService.java +++ b/src/main/java/dev/pgm/community/assistance/services/SQLAssistanceService.java @@ -9,6 +9,7 @@ import dev.pgm.community.Community; import dev.pgm.community.assistance.Report; import dev.pgm.community.feature.SQLFeatureBase; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.List; import java.util.UUID; @@ -64,7 +65,7 @@ public CompletableFuture> queryList(String target) { String id = row.getString("id"); String sender = row.getString("sender"); String reason = row.getString("reason"); - long time = Long.parseLong(row.getString("time")); + long time = DatabaseUtils.parseLong(row, "time"); reports .getReports() .add( diff --git a/src/main/java/dev/pgm/community/friends/services/SQLFriendshipService.java b/src/main/java/dev/pgm/community/friends/services/SQLFriendshipService.java index 6b14882c..a686b360 100644 --- a/src/main/java/dev/pgm/community/friends/services/SQLFriendshipService.java +++ b/src/main/java/dev/pgm/community/friends/services/SQLFriendshipService.java @@ -9,6 +9,7 @@ import dev.pgm.community.feature.SQLFeatureBase; import dev.pgm.community.friends.Friendship; import dev.pgm.community.friends.Friendship.FriendshipStatus; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -105,8 +106,8 @@ public CompletableFuture> queryList(String target) { String requester = row.getString("requester"); String requested = row.getString("requested"); String status = row.getString("status"); - long requestDate = Long.parseLong(row.getString("requestDate")); - long updateDate = Long.parseLong(row.getString("updateDate")); + long requestDate = DatabaseUtils.parseLong(row, "requestDate"); + long updateDate = DatabaseUtils.parseLong(row, "updateDate"); Instant requestInstant = Instant.ofEpochMilli(requestDate); Instant updateInstant = Instant.ofEpochMilli(updateDate); diff --git a/src/main/java/dev/pgm/community/moderation/services/SQLModerationService.java b/src/main/java/dev/pgm/community/moderation/services/SQLModerationService.java index 25234266..6811646e 100644 --- a/src/main/java/dev/pgm/community/moderation/services/SQLModerationService.java +++ b/src/main/java/dev/pgm/community/moderation/services/SQLModerationService.java @@ -11,6 +11,7 @@ import dev.pgm.community.moderation.punishments.Punishment; import dev.pgm.community.moderation.punishments.PunishmentType; import dev.pgm.community.moderation.punishments.types.ExpirablePunishment; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Duration; import java.time.Instant; import java.util.List; @@ -83,13 +84,13 @@ public CompletableFuture> queryList(String target) { String issuer = row.getString("issuer"); String reason = row.getString("reason"); String type = row.getString("type"); - long time = Long.parseLong(row.getString("time")); - long expires = Long.parseLong(row.getString("expires")); + long time = DatabaseUtils.parseLong(row, "time"); + long expires = DatabaseUtils.parseLong(row, "expires"); Instant timeIssued = Instant.ofEpochMilli(time); Duration length = Duration.between(Instant.ofEpochMilli(time), Instant.ofEpochMilli(expires)); - boolean active = row.get("active"); - long lastUpdateTime = Long.parseLong(row.getString("last_updated")); + boolean active = DatabaseUtils.parseBoolean(row, "active"); + long lastUpdateTime = DatabaseUtils.parseLong(row, "last_updated"); Instant lastUpdate = Instant.ofEpochMilli(lastUpdateTime); String lastUpdateBy = row.getString("updated_by"); String service = row.getString("service"); diff --git a/src/main/java/dev/pgm/community/nick/services/SQLNickService.java b/src/main/java/dev/pgm/community/nick/services/SQLNickService.java index ae54ef12..c3b58c3d 100644 --- a/src/main/java/dev/pgm/community/nick/services/SQLNickService.java +++ b/src/main/java/dev/pgm/community/nick/services/SQLNickService.java @@ -8,6 +8,7 @@ import dev.pgm.community.nick.Nick; import dev.pgm.community.nick.NickConfig; import dev.pgm.community.nick.NickImpl; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.List; import java.util.UUID; @@ -64,8 +65,9 @@ public CompletableFuture query(String target) { row -> { if (row != null) { String nickName = row.getString("nickname"); - Instant date = Instant.ofEpochMilli(Long.parseLong(row.getString("date"))); - boolean enabled = row.get("enabled"); + long time = DatabaseUtils.parseLong(row, "date"); + Instant date = Instant.ofEpochMilli(time); + boolean enabled = DatabaseUtils.parseBoolean(row, "enabled"); nick.setNick(new NickImpl(playerId, nickName, date, enabled)); } nick.setLoaded(true); diff --git a/src/main/java/dev/pgm/community/requests/services/SQLRequestService.java b/src/main/java/dev/pgm/community/requests/services/SQLRequestService.java index 935a41ad..d631f085 100644 --- a/src/main/java/dev/pgm/community/requests/services/SQLRequestService.java +++ b/src/main/java/dev/pgm/community/requests/services/SQLRequestService.java @@ -7,6 +7,7 @@ import com.google.common.collect.Lists; import dev.pgm.community.feature.SQLFeatureBase; import dev.pgm.community.requests.RequestProfile; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.List; import java.util.UUID; @@ -98,12 +99,12 @@ public CompletableFuture query(String target) { result -> { if (result != null) { final UUID id = UUID.fromString(result.getString("id")); - final long lastRequest = Long.parseLong(result.getString("last_request_time")); + final long lastRequest = DatabaseUtils.parseLong(result, "last_request_time"); final String lastRequestMap = result.getString("last_request_map"); - final long lastSponsor = Long.parseLong(result.getString("last_sponsor_time")); + final long lastSponsor = DatabaseUtils.parseLong(result, "last_sponsor_time"); final String lastSponsorMap = result.getString("last_sponsor_map"); final int tokens = result.getInt("tokens"); - final long lastToken = Long.parseLong(result.getString("last_token_refresh")); + final long lastToken = DatabaseUtils.parseLong(result, "last_token_refresh"); final Instant lastRequestTime = lastRequest == -1 ? null : Instant.ofEpochMilli(lastRequest); diff --git a/src/main/java/dev/pgm/community/sessions/services/SQLSessionService.java b/src/main/java/dev/pgm/community/sessions/services/SQLSessionService.java index d321172e..266b9788 100644 --- a/src/main/java/dev/pgm/community/sessions/services/SQLSessionService.java +++ b/src/main/java/dev/pgm/community/sessions/services/SQLSessionService.java @@ -8,6 +8,7 @@ import dev.pgm.community.feature.SQLFeatureBase; import dev.pgm.community.sessions.Session; import dev.pgm.community.sessions.SessionQuery; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.List; import java.util.UUID; @@ -85,7 +86,7 @@ public CompletableFuture query(SessionQuery target) { String id = result.getString("id"); String player = result.getString("player"); - boolean disguised = result.get("disguised"); + boolean disguised = DatabaseUtils.parseBoolean(result, "disguised"); String server = result.getString("server"); diff --git a/src/main/java/dev/pgm/community/users/services/AddressHistoryService.java b/src/main/java/dev/pgm/community/users/services/AddressHistoryService.java index 08b718a9..91f92acd 100644 --- a/src/main/java/dev/pgm/community/users/services/AddressHistoryService.java +++ b/src/main/java/dev/pgm/community/users/services/AddressHistoryService.java @@ -7,6 +7,7 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.Sets; import dev.pgm.community.database.Query; +import dev.pgm.community.utils.DatabaseUtils; import java.time.Instant; import java.util.List; import java.util.Set; @@ -112,7 +113,8 @@ public CompletableFuture getLatestAddressInfo(UUID playerId) result -> { if (result != null) { String address = result.getString(IP_ADDRESS_FIELD); - Instant date = Instant.ofEpochMilli(Long.parseLong(result.getString(DATE_FIELD))); + long time = DatabaseUtils.parseLong(result, DATE_FIELD); + Instant date = Instant.ofEpochMilli(time); info.setAddress(address); info.setDate(date); } diff --git a/src/main/java/dev/pgm/community/users/services/SQLUserService.java b/src/main/java/dev/pgm/community/users/services/SQLUserService.java index 612d2c5b..0bedd784 100644 --- a/src/main/java/dev/pgm/community/users/services/SQLUserService.java +++ b/src/main/java/dev/pgm/community/users/services/SQLUserService.java @@ -7,6 +7,7 @@ import dev.pgm.community.feature.SQLFeatureBase; import dev.pgm.community.users.UserProfile; import dev.pgm.community.users.UserProfileImpl; +import dev.pgm.community.utils.DatabaseUtils; import dev.pgm.community.utils.NameUtils; import java.time.Instant; import java.util.List; @@ -77,7 +78,7 @@ public CompletableFuture query(String target) { if (result != null) { final UUID id = UUID.fromString(result.getString("id")); final String username = result.getString("name"); - final long firstJoin = Long.parseLong(result.getString("first_join")); + final long firstJoin = DatabaseUtils.parseLong(result, "first_join"); final int joinCount = result.getInt("join_count"); UserData loadedData = new UserData(id); diff --git a/src/main/java/dev/pgm/community/utils/DatabaseUtils.java b/src/main/java/dev/pgm/community/utils/DatabaseUtils.java new file mode 100644 index 00000000..c32db574 --- /dev/null +++ b/src/main/java/dev/pgm/community/utils/DatabaseUtils.java @@ -0,0 +1,34 @@ +package dev.pgm.community.utils; + +import co.aikar.idb.DbRow; + +public class DatabaseUtils { + + public static boolean parseBoolean(DbRow row, String fieldName) throws ClassCastException { + Object obj = row.get(fieldName); + if (obj instanceof Integer) { + int activeInt = (Integer) obj; + return (activeInt != 0); + } else if (obj instanceof Boolean) { + return (Boolean) obj; + } else { + throw new ClassCastException( + "Unexpected type for '" + fieldName + "': " + obj.getClass().getName()); + } + } + + public static long parseLong(DbRow row, String fieldName) throws ClassCastException { + Object obj = row.get(fieldName); + if (obj instanceof String) { + String rawLong = (String) obj; + return Long.parseLong(rawLong); + } else if (obj instanceof Long) { + return (Long) obj; + } else if (obj instanceof Integer) { + return (Integer) obj; + } else { + throw new ClassCastException( + "Unexpected type for '" + fieldName + "': " + obj.getClass().getName()); + } + } +}