diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java new file mode 100644 index 000000000..d1e77f532 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/BackupCopyTransaction.java @@ -0,0 +1,105 @@ +/* + * 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 . + */ +package com.djrapitops.plan.db.access.transactions; + +import com.djrapitops.plan.db.Database; +import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; +import com.djrapitops.plan.db.sql.tables.UsersTable; + +/** + * Transaction that performs a clear + copy operation to duplicate a source database in the current one. + * + * @author Rsl1122 + */ +public class BackupCopyTransaction extends RemoveEverythingTransaction { + + private final Database sourceDB; + + public BackupCopyTransaction(Database sourceDB) { + this.sourceDB = sourceDB; + } + + @Override + protected boolean shouldBeExecuted() { + return !sourceDB.equals(db) && sourceDB.isOpen(); + } + + @Override + protected void execute() { + super.execute(); + + copyServers(); + copyUsers(); + copyWorlds(); + copyTPS(); + copyWebUsers(); + copyCommandUse(); + copyIPsAndGeolocs(); + copyNicknames(); + copySessions(); + copyUserInfo(); + copyPings(); + } + + private void copyPings() { + db.getPingTable().insertAllPings(sourceDB.query(LargeFetchQueries.fetchAllPingData())); + } + + private void copyCommandUse() { + db.getCommandUseTable().insertCommandUsage(sourceDB.query(LargeFetchQueries.fetchAllCommandUsageData())); + } + + private void copyIPsAndGeolocs() { + db.getGeoInfoTable().insertAllGeoInfo(sourceDB.query(LargeFetchQueries.fetchAllGeoInfoData())); + } + + private void copyNicknames() { + db.getNicknamesTable().insertNicknames(sourceDB.query(LargeFetchQueries.fetchAllNicknameData())); + } + + private void copyWebUsers() { + db.getSecurityTable().addUsers(sourceDB.query(LargeFetchQueries.fetchAllPlanWebUsers())); + } + + private void copyServers() { + db.getServerTable().insertAllServers(sourceDB.query(LargeFetchQueries.fetchPlanServerInformation()).values()); + } + + private void copyTPS() { + db.getTpsTable().insertAllTPS(sourceDB.query(LargeFetchQueries.fetchAllTPSData())); + } + + private void copyUserInfo() { + db.getUserInfoTable().insertUserInfo(sourceDB.query(LargeFetchQueries.fetchPerServerUserInformation())); + } + + private void copyWorlds() { + db.getWorldTable().saveWorlds(sourceDB.query(LargeFetchQueries.fetchAllWorldNames())); + } + + private void copyUsers() { + UsersTable fromTable = db.getUsersTable(); + UsersTable toTable = db.getUsersTable(); + + toTable.insertUsers(sourceDB.query(LargeFetchQueries.fetchAllCommonUserInformation())); + toTable.updateKicked(fromTable.getAllTimesKicked()); + } + + private void copySessions() { + db.getSessionsTable().insertSessions(sourceDB.query(LargeFetchQueries.fetchAllSessionsWithKillAndWorldData()), true); + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java index 070ab46af..7962496aa 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java @@ -35,7 +35,7 @@ import java.sql.Savepoint; */ public abstract class Transaction { - private SQLDB db; + SQLDB db; private Connection connection; private Savepoint savepoint; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java deleted file mode 100644 index 82a362105..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/move/BatchOperationTable.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 . - */ -package com.djrapitops.plan.db.sql.tables.move; - -import com.djrapitops.plan.db.SQLDB; -import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction; -import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; -import com.djrapitops.plan.db.sql.tables.Table; -import com.djrapitops.plan.db.sql.tables.UsersTable; - -/** - * A Fake table used to store a lot of big table operations. - *

- * To use this table create a new BatchOperationTable with both SQLDB objects. - * {@code SQLDB from; SQLDB to;} - * {@code fromT = new BatchOperationTable(from);} - * {@code toT = new BatchOperationTable(to);} - * {@code fromT.copy(toT);} - *

- * The copy methods assume that the table has been cleared, or that no duplicate data will be entered for a user. - *

- * Server and User tables should be copied first. - * - * @author Rsl1122 - */ -public class BatchOperationTable extends Table { - - /** - * Constructor. - *

- * Call to access copy functionality. - * - * @param database Database to copy things from - * @throws IllegalStateException if database.init has not been called. - * @throws ClassCastException if database is not SQLDB. - */ - public BatchOperationTable(SQLDB database) { - super("", database); - if (!db.isOpen()) { - throw new IllegalStateException("Given Database had not been initialized."); - } - } - - public void copyEverything(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.executeTransaction(new RemoveEverythingTransaction()); - - copyServers(toDB); - copyUsers(toDB); - copyWorlds(toDB); - copyTPS(toDB); - copyWebUsers(toDB); - copyCommandUse(toDB); - copyIPsAndGeolocs(toDB); - copyNicknames(toDB); - copySessions(toDB); - copyUserInfo(toDB); - copyPings(toDB); - } - - public void copyPings(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getPingTable().insertAllPings(db.query(LargeFetchQueries.fetchAllPingData())); - } - - public void copyCommandUse(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getCommandUseTable().insertCommandUsage(db.query(LargeFetchQueries.fetchAllCommandUsageData())); - } - - public void copyIPsAndGeolocs(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getGeoInfoTable().insertAllGeoInfo(db.query(LargeFetchQueries.fetchAllGeoInfoData())); - } - - public void copyNicknames(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getNicknamesTable().insertNicknames(db.query(LargeFetchQueries.fetchAllNicknameData())); - } - - public void copyWebUsers(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getSecurityTable().addUsers(db.query(LargeFetchQueries.fetchAllPlanWebUsers())); - } - - public void copyServers(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getServerTable().insertAllServers(db.query(LargeFetchQueries.fetchPlanServerInformation()).values()); - } - - public void copyTPS(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getTpsTable().insertAllTPS(db.query(LargeFetchQueries.fetchAllTPSData())); - } - - public void copyUserInfo(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getUserInfoTable().insertUserInfo(db.query(LargeFetchQueries.fetchPerServerUserInformation())); - } - - public void copyWorlds(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getWorldTable().saveWorlds(db.query(LargeFetchQueries.fetchAllWorldNames())); - } - - public void copyUsers(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - UsersTable fromTable = db.getUsersTable(); - UsersTable toTable = toDB.db.getUsersTable(); - - toTable.insertUsers(db.query(LargeFetchQueries.fetchAllCommonUserInformation())); - toTable.updateKicked(fromTable.getAllTimesKicked()); - } - - public void copySessions(BatchOperationTable toDB) { - if (toDB.equals(this)) { - return; - } - toDB.db.getSessionsTable().insertSessions(db.query(LargeFetchQueries.fetchAllSessionsWithKillAndWorldData()), true); - } -} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLBackupOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLBackupOps.java index d13d08b8d..c0cb6b67c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLBackupOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLBackupOps.java @@ -18,7 +18,7 @@ package com.djrapitops.plan.system.database.databases.sql.operation; import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.SQLDB; -import com.djrapitops.plan.db.sql.tables.move.BatchOperationTable; +import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction; import com.djrapitops.plan.system.database.databases.operation.BackupOperations; public class SQLBackupOps extends SQLOps implements BackupOperations { @@ -29,18 +29,11 @@ public class SQLBackupOps extends SQLOps implements BackupOperations { @Override public void backup(Database toDatabase) { - if (toDatabase instanceof SQLDB) { - BatchOperationTable toDB = new BatchOperationTable((SQLDB) toDatabase); - BatchOperationTable fromDB = new BatchOperationTable(db); - - fromDB.copyEverything(toDB); - } else { - throw new IllegalArgumentException("Database was not a SQL database - backup not implemented."); - } + toDatabase.executeTransaction(new BackupCopyTransaction(db)); } @Override public void restore(Database fromDatabase) { - fromDatabase.backup().backup(db); + db.executeTransaction(new BackupCopyTransaction(fromDatabase)); } }