Refactored RemoveOperations#player into a Transaction

This commit is contained in:
Rsl1122 2019-01-23 18:28:11 +02:00
parent a48808adb9
commit 87a61380f1
7 changed files with 103 additions and 30 deletions

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.command.commands.manage;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -118,7 +119,7 @@ public class ManageRemoveCommand extends CommandNode {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
db.remove().player(uuid);
db.executeTransaction(new RemovePlayerTransaction(uuid));
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
} catch (DBOpException e) {

View File

@ -23,6 +23,7 @@ import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.transactions.CreateTablesTransaction;
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.db.sql.tables.*;
@ -304,7 +305,7 @@ public abstract class SQLDB extends AbstractDatabase {
.map(Map.Entry::getKey)
.collect(Collectors.toList());
for (UUID uuid : inactivePlayers) {
removeOps.player(uuid);
executeTransaction(new RemovePlayerTransaction(uuid));
}
int removed = inactivePlayers.size();
if (removed > 0) {

View File

@ -0,0 +1,76 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.transactions;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.sql.tables.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
/**
* Transaction for removing a player's data from the database.
*
* @author Rsl1122
*/
public class RemovePlayerTransaction extends Transaction {
private final UUID playerUUID;
public RemovePlayerTransaction(UUID playerUUID) {
this.playerUUID = playerUUID;
}
@Override
protected boolean shouldBeExecuted() {
return playerUUID != null;
}
@Override
protected void execute() {
query(OptionalFetchQueries.playerUserName(playerUUID)).ifPresent(this::deleteWebUser);
deleteFromTable(GeoInfoTable.TABLE_NAME);
deleteFromTable(NicknamesTable.TABLE_NAME);
deleteFromTable(KillsTable.TABLE_NAME);
deleteFromTable(WorldTimesTable.TABLE_NAME);
deleteFromTable(SessionsTable.TABLE_NAME);
deleteFromTable(PingTable.TABLE_NAME);
deleteFromTable(UserInfoTable.TABLE_NAME);
deleteFromTable(UsersTable.TABLE_NAME);
}
private void deleteWebUser(String username) {
execute(new ExecStatement("DELETE FROM " + SecurityTable.TABLE_NAME + " WHERE " + SecurityTable.USERNAME + "=?") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, username);
}
});
}
private void deleteFromTable(String tableName) {
execute(new ExecStatement("DELETE FROM " + tableName + " WHERE (uuid=?)") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
});
}
}

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import com.djrapitops.plan.system.info.server.Server;
import org.apache.commons.lang3.math.NumberUtils;
@ -75,4 +76,24 @@ public class OptionalFetchQueries {
return db -> db.query(matchingServerIdentifier("BungeeCord"));
}
public static Query<Optional<String>> playerUserName(UUID playerUUID) {
String sql = "SELECT " + UsersTable.USER_NAME +
" FROM " + UsersTable.TABLE_NAME +
" WHERE " + UsersTable.USER_UUID + "=?";
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

@ -16,12 +16,8 @@
*/
package com.djrapitops.plan.system.database.databases.operation;
import java.util.UUID;
public interface RemoveOperations {
void player(UUID uuid);
void everything();
void webUser(String name);

View File

@ -18,37 +18,14 @@ package com.djrapitops.plan.system.database.databases.sql.operation;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.tables.Table;
import com.djrapitops.plan.db.sql.tables.UserUUIDTable;
import com.djrapitops.plan.system.database.databases.operation.RemoveOperations;
import java.util.UUID;
public class SQLRemoveOps extends SQLOps implements RemoveOperations {
public SQLRemoveOps(SQLDB db) {
super(db);
}
@Override
public void player(UUID uuid) {
if (uuid == null) {
return;
}
String webUser = usersTable.getPlayerName(uuid);
for (Table t : db.getAllTablesInRemoveOrder()) {
if (!(t instanceof UserUUIDTable)) {
continue;
}
UserUUIDTable table = (UserUUIDTable) t;
table.removeUser(uuid);
}
securityTable.removeUser(webUser);
}
@Override
public void everything() {
for (Table table : db.getAllTablesInRemoveOrder()) {

View File

@ -29,6 +29,7 @@ import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.*;
@ -539,7 +540,7 @@ public abstract class CommonDBTest {
assertTrue(usersTable.isRegistered(playerUUID));
db.remove().player(playerUUID);
db.executeTransaction(new RemovePlayerTransaction(playerUUID));
assertFalse(usersTable.isRegistered(playerUUID));
assertFalse(userInfoTable.isRegistered(playerUUID));