Refactored ServerTable#setAsUninstalled to a transaction

This commit is contained in:
Rsl1122 2019-02-15 09:34:15 +02:00
parent 1810a758f2
commit 240b6cb29e
6 changed files with 79 additions and 28 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.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries; import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.commands.SetServerAsUninstalledTransaction;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
@ -91,7 +92,7 @@ public class ManageUninstalledCommand extends CommandNode {
return; return;
} }
dbSystem.getDatabase().save().setAsUninstalled(serverUUID); dbSystem.getDatabase().executeTransaction(new SetServerAsUninstalledTransaction(serverUUID));
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS)); sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
} catch (DBOpException e) { } catch (DBOpException e) {
sender.sendMessage("§cError occurred: " + e.toString()); sender.sendMessage("§cError occurred: " + e.toString());

View File

@ -0,0 +1,60 @@
/*
* 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.commands;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.WHERE;
/**
* Transaction for telling Plan that Plan has been uninstalled from the server.
*
* @author Rsl1122
*/
public class SetServerAsUninstalledTransaction extends Transaction {
private final UUID serverUUID;
public SetServerAsUninstalledTransaction(UUID serverUUID) {
this.serverUUID = serverUUID;
}
@Override
protected void performOperations() {
execute(updateServerAsUninstalled());
}
private Executable updateServerAsUninstalled() {
String sql = "UPDATE " + ServerTable.TABLE_NAME + " SET " + ServerTable.INSTALLED + "=?" +
WHERE + ServerTable.SERVER_UUID + "=?";
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setBoolean(1, false);
statement.setString(2, serverUUID.toString());
}
};
}
}

View File

@ -18,12 +18,10 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.sql.parsing.*; import com.djrapitops.plan.db.sql.parsing.*;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
@ -102,16 +100,4 @@ public class ServerTable extends Table {
} }
}); });
} }
public void setAsUninstalled(UUID serverUUID) {
String sql = "UPDATE " + tableName + " SET " + INSTALLED + "=? WHERE " + SERVER_UUID + "=?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setBoolean(1, false);
statement.setString(2, serverUUID.toString());
}
});
}
} }

View File

@ -76,7 +76,4 @@ public interface SaveOperations {
@Deprecated @Deprecated
void session(UUID uuid, Session session); void session(UUID uuid, Session session);
@Deprecated
void setAsUninstalled(UUID serverUUID);
} }

View File

@ -149,10 +149,4 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
} }
}); });
} }
@Override
public void setAsUninstalled(UUID serverUUID) {
serverTable.setAsUninstalled(serverUUID);
}
} }

View File

@ -38,10 +38,7 @@ import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction;
import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction; import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction; import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.db.access.transactions.Transaction; import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.access.transactions.commands.RegisterWebUserTransaction; import com.djrapitops.plan.db.access.transactions.commands.*;
import com.djrapitops.plan.db.access.transactions.commands.RemoveEverythingTransaction;
import com.djrapitops.plan.db.access.transactions.commands.RemovePlayerTransaction;
import com.djrapitops.plan.db.access.transactions.commands.RemoveWebUserTransaction;
import com.djrapitops.plan.db.access.transactions.events.*; import com.djrapitops.plan.db.access.transactions.events.*;
import com.djrapitops.plan.db.access.transactions.init.CleanTransaction; import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction; import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
@ -855,6 +852,22 @@ public abstract class CommonDBTest {
assertTrue("Some keys are not supported by PlayerContainer: PlayerKeys." + unsupported.toString(), unsupported.isEmpty()); assertTrue("Some keys are not supported by PlayerContainer: PlayerKeys." + unsupported.toString(), unsupported.isEmpty());
} }
@Test
public void uninstallingServerStopsItFromBeingReturnedInServerQuery() {
db.executeTransaction(new SetServerAsUninstalledTransaction(serverUUID));
Optional<Server> found = db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID));
assertFalse(found.isPresent());
}
@Test
public void uninstallingServerStopsItFromBeingReturnedInServersQuery() {
db.executeTransaction(new SetServerAsUninstalledTransaction(serverUUID));
Collection<Server> found = db.query(ServerQueries.fetchPlanServerInformationCollection());
assertTrue(found.isEmpty());
}
@Test @Test
public void serverContainerSupportsAllServerKeys() throws NoSuchAlgorithmException, IllegalAccessException { public void serverContainerSupportsAllServerKeys() throws NoSuchAlgorithmException, IllegalAccessException {
saveAllData(); saveAllData();