Refactored SecurityTable#removeUser to a transaction

This commit is contained in:
Rsl1122 2019-02-02 11:50:02 +02:00
parent fa3b92f00e
commit a41fd0dc40
5 changed files with 53 additions and 19 deletions

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.command.commands.webuser;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.access.transactions.RemoveWebUserTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -84,7 +85,7 @@ public class WebDeleteCommand extends CommandNode {
sender.sendMessage("§c[Plan] User Doesn't exist.");
return;
}
db.remove().webUser(user);
db.executeTransaction(new RemoveWebUserTransaction(user));
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);

View File

@ -57,12 +57,7 @@ public class RemovePlayerTransaction extends Transaction {
}
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);
}
});
executeOther(new RemoveWebUserTransaction(username));
}
private void deleteFromTable(String tableName) {

View File

@ -0,0 +1,49 @@
/*
* 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.tables.SecurityTable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Transaction to remove a Plan {@link com.djrapitops.plan.data.WebUser} from the database.
*
* @author Rsl1122
*/
public class RemoveWebUserTransaction extends Transaction {
private final String username;
public RemoveWebUserTransaction(String username) {
this.username = username;
}
@Override
protected void performOperations() {
String sql = "DELETE FROM " + SecurityTable.TABLE_NAME + " WHERE " + SecurityTable.USERNAME + "=?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, username);
}
});
}
}

View File

@ -57,17 +57,6 @@ public class SecurityTable extends Table {
.toString();
}
public void removeUser(String user) {
String sql = "DELETE FROM " + tableName + " WHERE (" + USERNAME + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, user);
}
});
}
public void addNewUser(WebUser info) {
addNewUser(info.getName(), info.getSaltedPassHash(), info.getPermLevel());
}

View File

@ -284,7 +284,7 @@ public abstract class CommonDBTest {
@Test
public void webUserIsRemoved() throws DBInitException {
webUserIsRegistered();
db.getSecurityTable().removeUser(TestConstants.PLAYER_ONE_NAME);
db.executeTransaction(new RemoveWebUserTransaction(TestConstants.PLAYER_ONE_NAME));
assertFalse(db.query(OptionalFetchQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME)).isPresent());
}