diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/importing/importers/BukkitImporter.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/importing/importers/BukkitImporter.java index e9d1fd63f..bdfded339 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/importing/importers/BukkitImporter.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/importing/importers/BukkitImporter.java @@ -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 existingUUIDs = db.query(BaseUserQueries.fetchCommonUserUUIDs()); - Set existingUserInfoTableUUIDs = db.query(BaseUserQueries.fetchServerUserUUIDs(serverUUID.get())); + Set existingUUIDs = db.query(UserIdentifierQueries.fetchAllPlayerUUIDs()); + Set existingUserInfoTableUUIDs = db.query(UserIdentifierQueries.fetchPlayerUUIDsOfServer(serverUUID.get())); Map users = new HashMap<>(); List userInfo = new ArrayList<>(); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/api/CommonAPI.java b/Plan/common/src/main/java/com/djrapitops/plan/api/CommonAPI.java index 8c967fb96..b76c3ab3b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/api/CommonAPI.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/api/CommonAPI.java @@ -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 getKnownPlayerNames() { try { - return queryDB(BaseUserQueries.fetchPlayerNames()); + return queryDB(UserIdentifierQueries.fetchAllPlayerNames()); } catch (DBOpException e) { errorHandler.log(L.ERROR, this.getClass(), e); return new HashMap<>(); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java index c64c35def..137f94bdb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java @@ -131,7 +131,7 @@ public class AllPlayerContainersQuery implements Query> { public List executeQuery(SQLDB db) { List containers = new ArrayList<>(); - Collection users = db.query(BaseUserQueries.fetchAllCommonUserInformation()); + Collection users = db.query(BaseUserQueries.fetchAllBaseUsers()); Map> geoInfo = db.query(GeoInfoQueries.fetchAllGeoInformation()); Map> allPings = db.query(PingQueries.fetchAllPingData()); Map> allNicknames = db.query(NicknameQueries.fetchAllNicknameDataByPlayerUUIDs()); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/BaseUserQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/BaseUserQueries.java index 02809d73d..69fc945cf 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/BaseUserQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/BaseUserQueries.java @@ -43,11 +43,11 @@ public class BaseUserQueries { /** * Query database for common user information. *

- * 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> fetchAllCommonUserInformation() { + public static Query> fetchAllBaseUsers() { String sql = Select.all(UsersTable.TABLE_NAME).toString(); return new QueryAllStatement>(sql, 20000) { @@ -67,22 +67,14 @@ public class BaseUserQueries { }; } - public static Query> fetchCommonUserUUIDs() { - String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID).toString(); - - return new QueryAllStatement>(sql, 20000) { - @Override - public Set processResults(ResultSet set) throws SQLException { - Set 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. + *

+ * 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> 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. + *

+ * Only one {@link BaseUser} per player exists unlike {@link com.djrapitops.plan.data.container.UserInfo} which is available per server. + *

+ * 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> fetchServerBaseUsers(UUID serverUUID) { String sql = "SELECT " + UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " + @@ -139,74 +141,4 @@ public class BaseUserQueries { }; } - public static Query> 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>(sql, 1000) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, serverUUID.toString()); - } - - @Override - public Set processResults(ResultSet set) throws SQLException { - Set playerUUIDs = new HashSet<>(); - while (set.next()) { - UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID)); - playerUUIDs.add(playerUUID); - } - return playerUUIDs; - } - }; - } - - public static Query> fetchPlayerNames() { - String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID, UsersTable.USER_NAME).toString(); - - return new QueryAllStatement>(sql, 20000) { - @Override - public Map processResults(ResultSet set) throws SQLException { - Map 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> fetchPlayerUUID(String playerName) { - String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID) - .where("UPPER(" + UsersTable.USER_NAME + ")=UPPER(?)") - .toString(); - - return new QueryStatement>(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, playerName); - } - - @Override - public Optional processResults(ResultSet set) throws SQLException { - if (set.next()) { - String uuidS = set.getString(UsersTable.USER_UUID); - return Optional.of(UUID.fromString(uuidS)); - } - return Optional.empty(); - } - }; - } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/UserIdentifierQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/UserIdentifierQueries.java new file mode 100644 index 000000000..1f3112695 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/UserIdentifierQueries.java @@ -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 . + */ +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> fetchAllPlayerUUIDs() { + String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID).toString(); + + return new QueryAllStatement>(sql, 20000) { + @Override + public Set processResults(ResultSet set) throws SQLException { + Set 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> 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>(sql, 1000) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + } + + @Override + public Set processResults(ResultSet set) throws SQLException { + Set 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> fetchAllPlayerNames() { + String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID, UsersTable.USER_NAME).toString(); + + return new QueryAllStatement>(sql, 20000) { + @Override + public Map processResults(ResultSet set) throws SQLException { + Map 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> fetchPlayerUUIDOf(String playerName) { + String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID) + .where("UPPER(" + UsersTable.USER_NAME + ")=UPPER(?)") + .toString(); + + return new QueryStatement>(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, playerName); + } + + @Override + public Optional processResults(ResultSet set) throws SQLException { + if (set.next()) { + String uuidS = set.getString(UsersTable.USER_UUID); + return Optional.of(UUID.fromString(uuidS)); + } + return Optional.empty(); + } + }; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java index ebb97eeed..e81329e7a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java @@ -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() { 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 7422c8f27..65eaeb8bd 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 @@ -64,12 +64,12 @@ public class SQLFetchOps extends SQLOps implements FetchOperations { @Override public Set getSavedUUIDs() { - return db.query(BaseUserQueries.fetchCommonUserUUIDs()); + return db.query(UserIdentifierQueries.fetchAllPlayerUUIDs()); } @Override public Set 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 getPlayerNames() { - return db.query(BaseUserQueries.fetchPlayerNames()); + return db.query(UserIdentifierQueries.fetchAllPlayerNames()); } @Override diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/export/HtmlExport.java b/Plan/common/src/main/java/com/djrapitops/plan/system/export/HtmlExport.java index c3c574e04..28864a563 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/export/HtmlExport.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/export/HtmlExport.java @@ -139,7 +139,7 @@ public class HtmlExport extends SpecificExport { public void exportAvailablePlayers() { try { - Collection users = dbSystem.getDatabase().query(BaseUserQueries.fetchAllCommonUserInformation()); + Collection users = dbSystem.getDatabase().query(BaseUserQueries.fetchAllBaseUsers()); for (BaseUser user : users) { exportAvailablePlayerPage(user.getUuid(), user.getName()); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java index 187533c69..53492f257 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java @@ -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 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(); 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 90e59aee7..bbf38db9f 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 @@ -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());