Split identifier (UUID, name) related queries to own class.

This commit is contained in:
Rsl1122 2019-02-16 11:45:07 +02:00
parent c57021a116
commit 57f7cb710a
10 changed files with 183 additions and 107 deletions

View File

@ -25,7 +25,7 @@ import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.LargeStoreQueries;
import com.djrapitops.plan.db.access.queries.objects.BaseUserQueries;
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.system.cache.GeolocationCache;
import com.djrapitops.plan.system.database.DBSystem;
@ -126,8 +126,8 @@ public abstract class BukkitImporter implements Importer {
Database db = dbSystem.getDatabase();
Set<UUID> existingUUIDs = db.query(BaseUserQueries.fetchCommonUserUUIDs());
Set<UUID> existingUserInfoTableUUIDs = db.query(BaseUserQueries.fetchServerUserUUIDs(serverUUID.get()));
Set<UUID> existingUUIDs = db.query(UserIdentifierQueries.fetchAllPlayerUUIDs());
Set<UUID> existingUserInfoTableUUIDs = db.query(UserIdentifierQueries.fetchPlayerUUIDsOfServer(serverUUID.get()));
Map<UUID, BaseUser> users = new HashMap<>();
List<UserInfo> userInfo = new ArrayList<>();

View File

@ -21,8 +21,8 @@ import com.djrapitops.plan.api.data.ServerContainer;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
import com.djrapitops.plan.db.access.queries.objects.BaseUserQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.utilities.uuid.UUIDUtility;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
@ -66,7 +66,7 @@ public abstract class CommonAPI implements PlanAPI {
@Override
public Map<UUID, String> getKnownPlayerNames() {
try {
return queryDB(BaseUserQueries.fetchPlayerNames());
return queryDB(UserIdentifierQueries.fetchAllPlayerNames());
} catch (DBOpException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
return new HashMap<>();

View File

@ -131,7 +131,7 @@ public class AllPlayerContainersQuery implements Query<List<PlayerContainer>> {
public List<PlayerContainer> executeQuery(SQLDB db) {
List<PlayerContainer> containers = new ArrayList<>();
Collection<BaseUser> users = db.query(BaseUserQueries.fetchAllCommonUserInformation());
Collection<BaseUser> users = db.query(BaseUserQueries.fetchAllBaseUsers());
Map<UUID, List<GeoInfo>> geoInfo = db.query(GeoInfoQueries.fetchAllGeoInformation());
Map<UUID, List<Ping>> allPings = db.query(PingQueries.fetchAllPingData());
Map<UUID, List<Nickname>> allNicknames = db.query(NicknameQueries.fetchAllNicknameDataByPlayerUUIDs());

View File

@ -43,11 +43,11 @@ public class BaseUserQueries {
/**
* Query database for common user information.
* <p>
* This is the base for any user information.
* Only one {@link BaseUser} per player exists unlike {@link com.djrapitops.plan.data.container.UserInfo} which is available per server.
*
* @return Map: Player UUID - BaseUser
*/
public static Query<Collection<BaseUser>> fetchAllCommonUserInformation() {
public static Query<Collection<BaseUser>> fetchAllBaseUsers() {
String sql = Select.all(UsersTable.TABLE_NAME).toString();
return new QueryAllStatement<Collection<BaseUser>>(sql, 20000) {
@ -67,22 +67,14 @@ public class BaseUserQueries {
};
}
public static Query<Set<UUID>> fetchCommonUserUUIDs() {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID).toString();
return new QueryAllStatement<Set<UUID>>(sql, 20000) {
@Override
public Set<UUID> processResults(ResultSet set) throws SQLException {
Set<UUID> playerUUIDs = new HashSet<>();
while (set.next()) {
UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID));
playerUUIDs.add(playerUUID);
}
return playerUUIDs;
}
};
}
/**
* Query database for common user information of a player.
* <p>
* Only one {@link BaseUser} per player exists unlike {@link com.djrapitops.plan.data.container.UserInfo} which is available per server.
*
* @param playerUUID UUID of the Player.
* @return Optional: BaseUser if found, empty if not.
*/
public static Query<Optional<BaseUser>> fetchBaseUserOfPlayer(UUID playerUUID) {
String sql = Select.all(UsersTable.TABLE_NAME).where(UsersTable.USER_UUID + "=?").toString();
@ -107,6 +99,16 @@ public class BaseUserQueries {
};
}
/**
* Query database for common user information for players that have played on a specific server.
* <p>
* Only one {@link BaseUser} per player exists unlike {@link com.djrapitops.plan.data.container.UserInfo} which is available per server.
* <p>
* This will fetch BaseUsers for which UserInfo object also exists on the server.
*
* @param serverUUID UUID of the Plan server.
* @return Collection: BaseUsers
*/
public static Query<Collection<BaseUser>> fetchServerBaseUsers(UUID serverUUID) {
String sql = "SELECT " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " +
@ -139,74 +141,4 @@ public class BaseUserQueries {
};
}
public static Query<Set<UUID>> fetchServerUserUUIDs(UUID serverUUID) {
String sql = "SELECT " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " +
" FROM " + UsersTable.TABLE_NAME +
" INNER JOIN " + UserInfoTable.TABLE_NAME + " on " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + "." + UserInfoTable.USER_UUID +
" WHERE " + UserInfoTable.SERVER_UUID + "=?";
return new QueryStatement<Set<UUID>>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public Set<UUID> processResults(ResultSet set) throws SQLException {
Set<UUID> playerUUIDs = new HashSet<>();
while (set.next()) {
UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID));
playerUUIDs.add(playerUUID);
}
return playerUUIDs;
}
};
}
public static Query<Map<UUID, String>> fetchPlayerNames() {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID, UsersTable.USER_NAME).toString();
return new QueryAllStatement<Map<UUID, String>>(sql, 20000) {
@Override
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString((UsersTable.USER_UUID)));
String name = set.getString((UsersTable.USER_NAME));
names.put(uuid, name);
}
return names;
}
};
}
/**
* Query database for a Player UUID matching a specific player's name.
*
* @param playerName Name of the player, case does not matter.
* @return Optional: UUID if found, empty if not.
*/
public static Query<Optional<UUID>> fetchPlayerUUID(String playerName) {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID)
.where("UPPER(" + UsersTable.USER_NAME + ")=UPPER(?)")
.toString();
return new QueryStatement<Optional<UUID>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerName);
}
@Override
public Optional<UUID> processResults(ResultSet set) throws SQLException {
if (set.next()) {
String uuidS = set.getString(UsersTable.USER_UUID);
return Optional.of(UUID.fromString(uuidS));
}
return Optional.empty();
}
};
}
}

View File

@ -0,0 +1,144 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.queries.objects;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
/**
* Queries for fetching different user identifiers in the database.
*
* @author Rsl1122
*/
public class UserIdentifierQueries {
private UserIdentifierQueries() {
/* Static method class */
}
/**
* Query database for all player UUIDs stored in the Plan database.
*
* @return Set of UUIDs.
*/
public static Query<Set<UUID>> fetchAllPlayerUUIDs() {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID).toString();
return new QueryAllStatement<Set<UUID>>(sql, 20000) {
@Override
public Set<UUID> processResults(ResultSet set) throws SQLException {
Set<UUID> playerUUIDs = new HashSet<>();
while (set.next()) {
UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID));
playerUUIDs.add(playerUUID);
}
return playerUUIDs;
}
};
}
/**
* Query database for all player UUIDs that have joined a server.
*
* @param serverUUID UUID of the Plan server.
* @return Set of UUIDs.
*/
public static Query<Set<UUID>> fetchPlayerUUIDsOfServer(UUID serverUUID) {
String sql = "SELECT " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " +
" FROM " + UsersTable.TABLE_NAME +
" INNER JOIN " + UserInfoTable.TABLE_NAME + " on " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + "." + UserInfoTable.USER_UUID +
" WHERE " + UserInfoTable.SERVER_UUID + "=?";
return new QueryStatement<Set<UUID>>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public Set<UUID> processResults(ResultSet set) throws SQLException {
Set<UUID> playerUUIDs = new HashSet<>();
while (set.next()) {
UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID));
playerUUIDs.add(playerUUID);
}
return playerUUIDs;
}
};
}
/**
* Query database for a Map for all UUIDs and Player names.
*
* @return Map: Player UUID - Player name
*/
public static Query<Map<UUID, String>> fetchAllPlayerNames() {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID, UsersTable.USER_NAME).toString();
return new QueryAllStatement<Map<UUID, String>>(sql, 20000) {
@Override
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString((UsersTable.USER_UUID)));
String name = set.getString((UsersTable.USER_NAME));
names.put(uuid, name);
}
return names;
}
};
}
/**
* Query database for a Player UUID matching a specific player's name.
*
* @param playerName Name of the player, case does not matter.
* @return Optional: UUID if found, empty if not.
*/
public static Query<Optional<UUID>> fetchPlayerUUIDOf(String playerName) {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID)
.where("UPPER(" + UsersTable.USER_NAME + ")=UPPER(?)")
.toString();
return new QueryStatement<Optional<UUID>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerName);
}
@Override
public Optional<UUID> processResults(ResultSet set) throws SQLException {
if (set.next()) {
String uuidS = set.getString(UsersTable.USER_UUID);
return Optional.of(UUID.fromString(uuidS));
}
return Optional.empty();
}
};
}
}

View File

@ -104,7 +104,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
}
private void copyCommonUserInformation() {
copy(LargeStoreQueries::storeAllCommonUserInformation, BaseUserQueries.fetchAllCommonUserInformation());
copy(LargeStoreQueries::storeAllCommonUserInformation, BaseUserQueries.fetchAllBaseUsers());
}
private void copySessionsWithKillAndWorldData() {

View File

@ -64,12 +64,12 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public Set<UUID> getSavedUUIDs() {
return db.query(BaseUserQueries.fetchCommonUserUUIDs());
return db.query(UserIdentifierQueries.fetchAllPlayerUUIDs());
}
@Override
public Set<UUID> getSavedUUIDs(UUID serverUUID) {
return db.query(BaseUserQueries.fetchServerUserUUIDs(serverUUID));
return db.query(UserIdentifierQueries.fetchPlayerUUIDsOfServer(serverUUID));
}
@Override
@ -85,7 +85,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public UUID getUuidOf(String playerName) {
return db.query(BaseUserQueries.fetchPlayerUUID(playerName)).orElse(null);
return db.query(UserIdentifierQueries.fetchPlayerUUIDOf(playerName)).orElse(null);
}
@Override
@ -120,7 +120,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public Map<UUID, String> getPlayerNames() {
return db.query(BaseUserQueries.fetchPlayerNames());
return db.query(UserIdentifierQueries.fetchAllPlayerNames());
}
@Override

View File

@ -139,7 +139,7 @@ public class HtmlExport extends SpecificExport {
public void exportAvailablePlayers() {
try {
Collection<BaseUser> users = dbSystem.getDatabase().query(BaseUserQueries.fetchAllCommonUserInformation());
Collection<BaseUser> users = dbSystem.getDatabase().query(BaseUserQueries.fetchAllBaseUsers());
for (BaseUser user : users) {
exportAvailablePlayerPage(user.getUuid(), user.getName());
}

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.utilities.uuid;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.access.queries.objects.BaseUserQueries;
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plugin.api.utility.UUIDFetcher;
import com.djrapitops.plugin.logging.L;
@ -82,7 +82,7 @@ public class UUIDUtility {
private Optional<UUID> getUUIDFromDB(String playerName) {
try {
return dbSystem.getDatabase().query(BaseUserQueries.fetchPlayerUUID(playerName));
return dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerUUIDOf(playerName));
} catch (DBOpException e) {
errorHandler.log(L.ERROR, UUIDUtility.class, e);
return Optional.empty();

View File

@ -442,15 +442,15 @@ public abstract class CommonDBTest {
public void playerNameIsUpdatedWhenPlayerLogsIn() throws DBInitException {
saveUserOne();
OptionalAssert.equals(playerUUID, db.query(BaseUserQueries.fetchPlayerUUID(TestConstants.PLAYER_ONE_NAME)));
OptionalAssert.equals(playerUUID, db.query(UserIdentifierQueries.fetchPlayerUUIDOf(TestConstants.PLAYER_ONE_NAME)));
// Updates the name
db.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> 0, "NewName"));
commitTest();
assertFalse(db.query(BaseUserQueries.fetchPlayerUUID(TestConstants.PLAYER_ONE_NAME)).isPresent());
assertFalse(db.query(UserIdentifierQueries.fetchPlayerUUIDOf(TestConstants.PLAYER_ONE_NAME)).isPresent());
OptionalAssert.equals(playerUUID, db.query(BaseUserQueries.fetchPlayerUUID("NewName")));
OptionalAssert.equals(playerUUID, db.query(UserIdentifierQueries.fetchPlayerUUIDOf("NewName")));
}
@Test
@ -501,7 +501,7 @@ public abstract class CommonDBTest {
db.executeTransaction(new RemoveEverythingTransaction());
assertTrue(db.query(BaseUserQueries.fetchAllCommonUserInformation()).isEmpty());
assertTrue(db.query(BaseUserQueries.fetchAllBaseUsers()).isEmpty());
assertQueryIsEmpty(db, UserInfoQueries.fetchAllUserInformation());
assertQueryIsEmpty(db, NicknameQueries.fetchAllNicknameData());
assertQueryIsEmpty(db, GeoInfoQueries.fetchAllGeoInformation());
@ -661,7 +661,7 @@ public abstract class CommonDBTest {
backup.executeTransaction(new BackupCopyTransaction(db));
assertQueryResultIsEqual(db, backup, BaseUserQueries.fetchAllCommonUserInformation());
assertQueryResultIsEqual(db, backup, BaseUserQueries.fetchAllBaseUsers());
assertQueryResultIsEqual(db, backup, UserInfoQueries.fetchAllUserInformation());
assertQueryResultIsEqual(db, backup, NicknameQueries.fetchAllNicknameData());
assertQueryResultIsEqual(db, backup, GeoInfoQueries.fetchAllGeoInformation());