mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-14 15:57:50 +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();
|
.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) {
|
public int getTimesKicked(UUID uuid) {
|
||||||
String sql = Select.from(tableName, TIMES_KICKED)
|
String sql = Select.from(tableName, TIMES_KICKED)
|
||||||
.where(USER_UUID + "=?")
|
.where(USER_UUID + "=?")
|
||||||
|
@ -85,7 +85,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UUID getUuidOf(String playerName) {
|
public UUID getUuidOf(String playerName) {
|
||||||
return usersTable.getUuidOf(playerName);
|
return db.query(BaseUserQueries.fetchPlayerUUID(playerName)).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package com.djrapitops.plan.utilities.uuid;
|
package com.djrapitops.plan.utilities.uuid;
|
||||||
|
|
||||||
import com.djrapitops.plan.api.exceptions.database.DBOpException;
|
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.plan.system.database.DBSystem;
|
||||||
import com.djrapitops.plugin.api.utility.UUIDFetcher;
|
import com.djrapitops.plugin.api.utility.UUIDFetcher;
|
||||||
import com.djrapitops.plugin.logging.L;
|
import com.djrapitops.plugin.logging.L;
|
||||||
@ -24,9 +25,17 @@ import com.djrapitops.plugin.logging.error.ErrorHandler;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
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
|
* @author Rsl1122
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
@ -49,16 +58,10 @@ public class UUIDUtility {
|
|||||||
*/
|
*/
|
||||||
public UUID getUUIDOf(String playerName) {
|
public UUID getUUIDOf(String playerName) {
|
||||||
UUID uuid = getUUIDFromString(playerName);
|
UUID uuid = getUUIDFromString(playerName);
|
||||||
if (uuid != null) {
|
if (uuid != null) return uuid;
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid = getUUIDFromDB(playerName);
|
return getUUIDFromDB(playerName)
|
||||||
if (uuid != null) {
|
.orElse(getUUIDViaUUIDFetcher(playerName));
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
return getUUIDViaUUIDFetcher(playerName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private UUID getUUIDFromString(String 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 {
|
try {
|
||||||
return dbSystem.getDatabase().fetch().getUuidOf(playerName);
|
return dbSystem.getDatabase().query(BaseUserQueries.fetchPlayerUUID(playerName));
|
||||||
} catch (DBOpException e) {
|
} catch (DBOpException e) {
|
||||||
errorHandler.log(L.ERROR, UUIDUtility.class, e);
|
errorHandler.log(L.ERROR, UUIDUtility.class, e);
|
||||||
return null;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -439,20 +439,18 @@ public abstract class CommonDBTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUsersTableUpdateName() throws DBInitException {
|
public void playerNameIsUpdatedWhenPlayerLogsIn() throws DBInitException {
|
||||||
saveUserOne();
|
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"));
|
db.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> 0, "NewName"));
|
||||||
|
|
||||||
commitTest();
|
commitTest();
|
||||||
|
|
||||||
assertNull(usersTable.getUuidOf(TestConstants.PLAYER_ONE_NAME));
|
assertFalse(db.query(BaseUserQueries.fetchPlayerUUID(TestConstants.PLAYER_ONE_NAME)).isPresent());
|
||||||
|
|
||||||
assertEquals("NewName", usersTable.getPlayerName(playerUUID));
|
OptionalAssert.equals(playerUUID, db.query(BaseUserQueries.fetchPlayerUUID("NewName")));
|
||||||
assertEquals(playerUUID, usersTable.getUuidOf("NewName"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user