Skip to content

Commit

Permalink
Fix boolean & long parse errors
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <applenick@users.noreply.github.com>
  • Loading branch information
applenick committed Jun 17, 2024
1 parent ac401bc commit 80eaaab
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public CompletableFuture<List<Report>> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,8 +106,8 @@ public CompletableFuture<List<Friendship>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,13 +84,13 @@ public CompletableFuture<List<Punishment>> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,8 +65,9 @@ public CompletableFuture<Nick> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,12 +99,12 @@ public CompletableFuture<RequestProfile> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +86,7 @@ public CompletableFuture<Session> 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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,7 +113,8 @@ public CompletableFuture<LatestAddressInfo> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +78,7 @@ public CompletableFuture<UserProfile> 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);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/dev/pgm/community/utils/DatabaseUtils.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 80eaaab

Please sign in to comment.