From 64a3bc3fe07db60222d1f7dccc4a41b9ddc6049c Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 5 Feb 2019 12:08:58 +0200 Subject: [PATCH] Refactored NicknamesTable#getNicknameInformation to a query --- .../db/access/queries/PlayerFetchQueries.java | 28 ++++++++++++++ .../containers/PlayerContainerQuery.java | 2 +- .../plan/db/sql/tables/NicknamesTable.java | 37 ------------------- .../databases/sql/operation/SQLFetchOps.java | 6 ++- .../com/djrapitops/plan/db/CommonDBTest.java | 7 +--- 5 files changed, 35 insertions(+), 45 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerFetchQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerFetchQueries.java index f22c5f09b..34413a803 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerFetchQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerFetchQueries.java @@ -230,4 +230,32 @@ public class PlayerFetchQueries { } }; } + + public static Query> playersNicknameInformation(UUID playerUUID) { + String sql = "SELECT " + + NicknamesTable.NICKNAME + ", " + + NicknamesTable.LAST_USED + ", " + + NicknamesTable.SERVER_UUID + + " FROM " + NicknamesTable.TABLE_NAME + + " WHERE (" + NicknamesTable.USER_UUID + "=?)"; + + return new QueryStatement>(sql, 5000) { + + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, playerUUID.toString()); + } + + @Override + public List processResults(ResultSet set) throws SQLException { + List nicknames = new ArrayList<>(); + while (set.next()) { + UUID serverUUID = UUID.fromString(set.getString(NicknamesTable.SERVER_UUID)); + String nickname = set.getString(NicknamesTable.NICKNAME); + nicknames.add(new Nickname(nickname, set.getLong(NicknamesTable.LAST_USED), serverUUID)); + } + return nicknames; + } + }; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java index 6af7d9122..bbfd07f44 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java @@ -55,7 +55,7 @@ public class PlayerContainerQuery implements Query { container.putAll(db.getUsersTable().getUserInformation(uuid)); container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(PlayerFetchQueries.playerGeoInfo(uuid))); container.putCachingSupplier(PlayerKeys.PING, () -> db.query(PlayerFetchQueries.playerPingData(uuid))); - container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.getNicknamesTable().getNicknameInformation(uuid)); + container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.query(PlayerFetchQueries.playersNicknameInformation(uuid))); container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> db.query(new PerServerContainerQuery(uuid))); container.putSupplier(PlayerKeys.BANNED, () -> new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).isBanned()); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java index 78f3e4c64..a3f4a8cc2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java @@ -16,23 +16,14 @@ */ package com.djrapitops.plan.db.sql.tables; -import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.SQLDB; -import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.patches.NicknameLastSeenPatch; import com.djrapitops.plan.db.patches.NicknamesOptimizationPatch; import com.djrapitops.plan.db.patches.Version10Patch; import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Sql; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - /** * Table that is in charge of storing nickname data. *

@@ -80,32 +71,4 @@ public class NicknamesTable extends Table { .column(LAST_USED, Sql.LONG).notNull() .toString(); } - - public List getNicknameInformation(UUID uuid) { - String sql = "SELECT " + - NICKNAME + ", " + - LAST_USED + ", " + - SERVER_UUID + - " FROM " + TABLE_NAME + - " WHERE (" + USER_UUID + "=?)"; - - return query(new QueryStatement>(sql, 5000) { - - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, uuid.toString()); - } - - @Override - public List processResults(ResultSet set) throws SQLException { - List nicknames = new ArrayList<>(); - while (set.next()) { - UUID serverUUID = UUID.fromString(set.getString(SERVER_UUID)); - String nickname = set.getString(NICKNAME); - nicknames.add(new Nickname(nickname, set.getLong(LAST_USED), serverUUID)); - } - return nicknames; - } - }); - } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java index a35fece85..20891a936 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java @@ -28,6 +28,7 @@ import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.access.queries.LargeFetchQueries; import com.djrapitops.plan.db.access.queries.OptionalFetchQueries; +import com.djrapitops.plan.db.access.queries.PlayerFetchQueries; import com.djrapitops.plan.db.access.queries.ServerAggregateQueries; import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries; import com.djrapitops.plan.system.database.databases.operation.FetchOperations; @@ -135,8 +136,9 @@ public class SQLFetchOps extends SQLOps implements FetchOperations { } @Override - public List getNicknames(UUID uuid) { - return nicknamesTable.getNicknameInformation(uuid).stream().map(Nickname::getName).collect(Collectors.toList()); + public List getNicknames(UUID playerUUID) { + return db.query(PlayerFetchQueries.playersNicknameInformation(playerUUID)).stream() + .map(Nickname::getName).collect(Collectors.toList()); } @Override diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java index b32afaf25..2cfd56a1a 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java @@ -252,14 +252,13 @@ public abstract class CommonDBTest { @Test public void testNicknamesTable() throws DBInitException { saveUserOne(); - NicknamesTable nickTable = db.getNicknamesTable(); Nickname expected = new Nickname("TestNickname", System.currentTimeMillis(), serverUUID); db.executeTransaction(new NicknameStoreTransaction(playerUUID, expected)); db.executeTransaction(new NicknameStoreTransaction(playerUUID, expected)); commitTest(); - List nicknames = nickTable.getNicknameInformation(playerUUID); + List nicknames = db.query(PlayerFetchQueries.playersNicknameInformation(playerUUID)); assertEquals(1, nicknames.size()); assertEquals(expected, nicknames.get(0)); } @@ -466,9 +465,7 @@ public abstract class CommonDBTest { public void testRemovalSingleUser() { saveUserTwo(); - UserInfoTable userInfoTable = db.getUserInfoTable(); SessionsTable sessionsTable = db.getSessionsTable(); - NicknamesTable nicknamesTable = db.getNicknamesTable(); db.executeTransaction(new PlayerServerRegisterTransaction(playerUUID, () -> 223456789L, "Test_name", serverUUID)); saveTwoWorlds(); @@ -488,7 +485,7 @@ public abstract class CommonDBTest { assertFalse(db.query(PlayerFetchQueries.isPlayerRegistered(playerUUID))); assertFalse(db.query(PlayerFetchQueries.isPlayerRegisteredOnServer(playerUUID, serverUUID))); - assertTrue(nicknamesTable.getNicknameInformation(playerUUID).isEmpty()); + assertTrue(db.query(PlayerFetchQueries.playersNicknameInformation(playerUUID)).isEmpty()); assertTrue(db.query(PlayerFetchQueries.playerGeoInfo(playerUUID)).isEmpty()); assertTrue(sessionsTable.getSessions(playerUUID).isEmpty()); }