mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-24 16:14:26 +08:00
Refactored NicknamesTable#getNicknameInformation to a query
This commit is contained in:
parent
71a5592fd1
commit
64a3bc3fe0
@ -230,4 +230,32 @@ public class PlayerFetchQueries {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<List<Nickname>> 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<List<Nickname>>(sql, 5000) {
|
||||
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, playerUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Nickname> processResults(ResultSet set) throws SQLException {
|
||||
List<Nickname> 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -55,7 +55,7 @@ public class PlayerContainerQuery implements Query<PlayerContainer> {
|
||||
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());
|
||||
|
@ -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.
|
||||
* <p>
|
||||
@ -80,32 +71,4 @@ public class NicknamesTable extends Table {
|
||||
.column(LAST_USED, Sql.LONG).notNull()
|
||||
.toString();
|
||||
}
|
||||
|
||||
public List<Nickname> getNicknameInformation(UUID uuid) {
|
||||
String sql = "SELECT " +
|
||||
NICKNAME + ", " +
|
||||
LAST_USED + ", " +
|
||||
SERVER_UUID +
|
||||
" FROM " + TABLE_NAME +
|
||||
" WHERE (" + USER_UUID + "=?)";
|
||||
|
||||
return query(new QueryStatement<List<Nickname>>(sql, 5000) {
|
||||
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Nickname> processResults(ResultSet set) throws SQLException {
|
||||
List<Nickname> 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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<String> getNicknames(UUID uuid) {
|
||||
return nicknamesTable.getNicknameInformation(uuid).stream().map(Nickname::getName).collect(Collectors.toList());
|
||||
public List<String> getNicknames(UUID playerUUID) {
|
||||
return db.query(PlayerFetchQueries.playersNicknameInformation(playerUUID)).stream()
|
||||
.map(Nickname::getName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<Nickname> nicknames = nickTable.getNicknameInformation(playerUUID);
|
||||
List<Nickname> 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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user