Refactored UsersTable#getPlayerName to a query

This commit is contained in:
Rsl1122 2019-02-16 11:50:11 +02:00
parent 57f7cb710a
commit de7a1b3286
8 changed files with 57 additions and 55 deletions

View File

@ -89,4 +89,9 @@ public abstract class CommonAPI implements PlanAPI {
public Collection<UUID> fetchServerUUIDs() {
return queryDB(ServerQueries.fetchPlanServerInformation()).keySet();
}
@Override
public String getPlayerName(UUID playerUUID) {
return queryDB(UserIdentifierQueries.fetchPlayerNameOf(playerUUID)).orElse(null);
}
}

View File

@ -26,7 +26,6 @@ import com.djrapitops.plugin.logging.error.ErrorHandler;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID;
/**
* PlanAPI extension for proxy servers.
@ -57,11 +56,6 @@ public class ProxyAPI extends CommonAPI {
hookHandler.addPluginDataSource(pluginData);
}
@Override
public String getPlayerName(UUID uuid) {
return dbSystem.getDatabase().fetch().getPlayerName(uuid);
}
@Override
public FetchOperations fetchFromPlanDB() {
return dbSystem.getDatabase().fetch();

View File

@ -26,7 +26,6 @@ import com.djrapitops.plugin.logging.error.ErrorHandler;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID;
/**
* PlanAPI extension for Bukkit
@ -56,11 +55,6 @@ public class ServerAPI extends CommonAPI {
hookHandler.addPluginDataSource(pluginData);
}
@Override
public String getPlayerName(UUID uuid) {
return dbSystem.getDatabase().fetch().getPlayerName(uuid);
}
@Override
public FetchOperations fetchFromPlanDB() {
return dbSystem.getDatabase().fetch();

View File

@ -141,4 +141,29 @@ public class UserIdentifierQueries {
}
};
}
/**
* Query database for a Player name matching a specific player's UUID.
*
* @param playerUUID UUID of the Player
* @return Optional: name if found, empty if not - Case is stored unless using a H2 database.
*/
public static Query<Optional<String>> fetchPlayerNameOf(UUID playerUUID) {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_NAME).where(UsersTable.USER_UUID + "=?").toString();
return new QueryStatement<Optional<String>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
@Override
public Optional<String> processResults(ResultSet set) throws SQLException {
if (set.next()) {
return Optional.of(set.getString(UsersTable.USER_NAME));
}
return Optional.empty();
}
};
}
}

View File

@ -22,7 +22,6 @@ import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
@ -78,25 +77,6 @@ public class UsersTable extends Table {
});
}
public String getPlayerName(UUID uuid) {
String sql = Select.from(tableName, USER_NAME).where(USER_UUID + "=?").toString();
return query(new QueryStatement<String>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public String processResults(ResultSet set) throws SQLException {
if (set.next()) {
return set.getString(USER_NAME);
}
return null;
}
});
}
/**
* Gets the names of the players which names or nicknames match {@code name}.
*

View File

@ -125,7 +125,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public String getPlayerName(UUID playerUUID) {
return usersTable.getPlayerName(playerUUID);
return db.query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID)).orElse(null);
}
@Override

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.container.BaseUser;
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.system.database.DBSystem;
import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
@ -104,18 +105,19 @@ public class HtmlExport extends SpecificExport {
});
}
public void exportPlayer(UUID uuid) {
public void exportPlayer(UUID playerUUID) {
if (Check.isBukkitAvailable() && connectionSystem.isServerAvailable()) {
return;
}
String playerName = dbSystem.getDatabase().fetch().getPlayerName(uuid);
if (playerName != null) {
try {
exportAvailablePlayerPage(uuid, playerName);
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID))
.ifPresent(playerName -> {
try {
exportAvailablePlayerPage(playerUUID, playerName);
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
});
}
public void exportPlayersPage() {

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.system.export;
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.server.ServerInfo;
@ -67,20 +68,21 @@ public class JSONExport extends SpecificExport {
return config.get(ExportSettings.JSON_EXPORT_PATH);
}
public void exportPlayerJSON(UUID uuid) {
String json = responseFactory.rawPlayerPageResponse(uuid).getContent();
String playerName = dbSystem.getDatabase().fetch().getPlayerName(uuid);
if (playerName != null) {
try {
File htmlLocation = getPlayerFolder();
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, playerName.replace(" ", "%20").replace(".", "%2E") + ".json");
public void exportPlayerJSON(UUID playerUUID) {
String json = responseFactory.rawPlayerPageResponse(playerUUID).getContent();
export(exportFile, Collections.singletonList(json));
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID))
.ifPresent(playerName -> {
try {
File htmlLocation = getPlayerFolder();
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, playerName.replace(" ", "%20").replace(".", "%2E") + ".json");
export(exportFile, Collections.singletonList(json));
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
});
}
public void exportServerJSON(UUID serverUUID) {