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() { public Collection<UUID> fetchServerUUIDs() {
return queryDB(ServerQueries.fetchPlanServerInformation()).keySet(); 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.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.UUID;
/** /**
* PlanAPI extension for proxy servers. * PlanAPI extension for proxy servers.
@ -57,11 +56,6 @@ public class ProxyAPI extends CommonAPI {
hookHandler.addPluginDataSource(pluginData); hookHandler.addPluginDataSource(pluginData);
} }
@Override
public String getPlayerName(UUID uuid) {
return dbSystem.getDatabase().fetch().getPlayerName(uuid);
}
@Override @Override
public FetchOperations fetchFromPlanDB() { public FetchOperations fetchFromPlanDB() {
return dbSystem.getDatabase().fetch(); return dbSystem.getDatabase().fetch();

View File

@ -26,7 +26,6 @@ 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.UUID;
/** /**
* PlanAPI extension for Bukkit * PlanAPI extension for Bukkit
@ -56,11 +55,6 @@ public class ServerAPI extends CommonAPI {
hookHandler.addPluginDataSource(pluginData); hookHandler.addPluginDataSource(pluginData);
} }
@Override
public String getPlayerName(UUID uuid) {
return dbSystem.getDatabase().fetch().getPlayerName(uuid);
}
@Override @Override
public FetchOperations fetchFromPlanDB() { public FetchOperations fetchFromPlanDB() {
return dbSystem.getDatabase().fetch(); 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.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert; 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 com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement; 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}. * 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 @Override
public String getPlayerName(UUID playerUUID) { public String getPlayerName(UUID playerUUID) {
return usersTable.getPlayerName(playerUUID); return db.query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID)).orElse(null);
} }
@Override @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.data.container.BaseUser;
import com.djrapitops.plan.db.access.queries.objects.BaseUserQueries; 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.ServerQueries;
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.file.PlanFiles; import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.connection.ConnectionSystem; 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()) { if (Check.isBukkitAvailable() && connectionSystem.isServerAvailable()) {
return; return;
} }
String playerName = dbSystem.getDatabase().fetch().getPlayerName(uuid);
if (playerName != null) { dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID))
try { .ifPresent(playerName -> {
exportAvailablePlayerPage(uuid, playerName); try {
} catch (IOException e) { exportAvailablePlayerPage(playerUUID, playerName);
errorHandler.log(L.WARN, this.getClass(), e); } catch (IOException e) {
} errorHandler.log(L.WARN, this.getClass(), e);
} }
});
} }
public void exportPlayersPage() { public void exportPlayersPage() {

View File

@ -16,6 +16,7 @@
*/ */
package com.djrapitops.plan.system.export; 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.database.DBSystem;
import com.djrapitops.plan.system.file.PlanFiles; import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
@ -67,20 +68,21 @@ public class JSONExport extends SpecificExport {
return config.get(ExportSettings.JSON_EXPORT_PATH); return config.get(ExportSettings.JSON_EXPORT_PATH);
} }
public void exportPlayerJSON(UUID uuid) { public void exportPlayerJSON(UUID playerUUID) {
String json = responseFactory.rawPlayerPageResponse(uuid).getContent(); String json = responseFactory.rawPlayerPageResponse(playerUUID).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");
export(exportFile, Collections.singletonList(json)); dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID))
} catch (IOException e) { .ifPresent(playerName -> {
errorHandler.log(L.WARN, this.getClass(), e); 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) { public void exportServerJSON(UUID serverUUID) {