mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-31 17:10:17 +02:00
Refactored UsersTable#getUuidOf to a query
This commit is contained in:
parent
bb52f29bd4
commit
30bac0f6ca
|
@ -181,4 +181,32 @@ public class BaseUserQueries {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -65,34 +65,6 @@ public class UsersTable extends Table {
|
|||
.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get UUID of a player.
|
||||
*
|
||||
* @param playerName Name of a player
|
||||
* @return UUID of the player
|
||||
*/
|
||||
public UUID getUuidOf(String playerName) {
|
||||
String sql = Select.from(tableName, USER_UUID)
|
||||
.where("UPPER(" + USER_NAME + ")=UPPER(?)")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<UUID>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, playerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
String uuidS = set.getString(USER_UUID);
|
||||
return UUID.fromString(uuidS);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int getTimesKicked(UUID uuid) {
|
||||
String sql = Select.from(tableName, TIMES_KICKED)
|
||||
.where(USER_UUID + "=?")
|
||||
|
|
|
@ -85,7 +85,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
|
|||
|
||||
@Override
|
||||
public UUID getUuidOf(String playerName) {
|
||||
return usersTable.getUuidOf(playerName);
|
||||
return db.query(BaseUserQueries.fetchPlayerUUID(playerName)).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,6 +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.system.database.DBSystem;
|
||||
import com.djrapitops.plugin.api.utility.UUIDFetcher;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
|
@ -24,9 +25,17 @@ import com.djrapitops.plugin.logging.error.ErrorHandler;
|
|||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Utility for fetching a user's UUID.
|
||||
* <p>
|
||||
* Attempts are made in order:
|
||||
* - Parse UUID out of the given String
|
||||
* - Find an UUID from the database matching the player name
|
||||
* - Find an UUID from Mojang API that matches the player name
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
|
@ -49,16 +58,10 @@ public class UUIDUtility {
|
|||
*/
|
||||
public UUID getUUIDOf(String playerName) {
|
||||
UUID uuid = getUUIDFromString(playerName);
|
||||
if (uuid != null) {
|
||||
return uuid;
|
||||
}
|
||||
if (uuid != null) return uuid;
|
||||
|
||||
uuid = getUUIDFromDB(playerName);
|
||||
if (uuid != null) {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
return getUUIDViaUUIDFetcher(playerName);
|
||||
return getUUIDFromDB(playerName)
|
||||
.orElse(getUUIDViaUUIDFetcher(playerName));
|
||||
}
|
||||
|
||||
private UUID getUUIDFromString(String playerName) {
|
||||
|
@ -77,12 +80,12 @@ public class UUIDUtility {
|
|||
}
|
||||
}
|
||||
|
||||
private UUID getUUIDFromDB(String playerName) {
|
||||
private Optional<UUID> getUUIDFromDB(String playerName) {
|
||||
try {
|
||||
return dbSystem.getDatabase().fetch().getUuidOf(playerName);
|
||||
return dbSystem.getDatabase().query(BaseUserQueries.fetchPlayerUUID(playerName));
|
||||
} catch (DBOpException e) {
|
||||
errorHandler.log(L.ERROR, UUIDUtility.class, e);
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -439,20 +439,18 @@ public abstract class CommonDBTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testUsersTableUpdateName() throws DBInitException {
|
||||
public void playerNameIsUpdatedWhenPlayerLogsIn() throws DBInitException {
|
||||
saveUserOne();
|
||||
|
||||
UsersTable usersTable = db.getUsersTable();
|
||||
OptionalAssert.equals(playerUUID, db.query(BaseUserQueries.fetchPlayerUUID(TestConstants.PLAYER_ONE_NAME)));
|
||||
|
||||
assertEquals(playerUUID, usersTable.getUuidOf(TestConstants.PLAYER_ONE_NAME));
|
||||
// Updates the name
|
||||
db.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> 0, "NewName"));
|
||||
|
||||
commitTest();
|
||||
|
||||
assertNull(usersTable.getUuidOf(TestConstants.PLAYER_ONE_NAME));
|
||||
assertFalse(db.query(BaseUserQueries.fetchPlayerUUID(TestConstants.PLAYER_ONE_NAME)).isPresent());
|
||||
|
||||
assertEquals("NewName", usersTable.getPlayerName(playerUUID));
|
||||
assertEquals(playerUUID, usersTable.getUuidOf("NewName"));
|
||||
OptionalAssert.equals(playerUUID, db.query(BaseUserQueries.fetchPlayerUUID("NewName")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue
Block a user