From c990358cd4608f1658bbc8549d26025bcc3b2f25 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 2 Sep 2017 11:47:34 +0300 Subject: [PATCH] Batch Insert for ServerTable --- .../plan/database/tables/ServerTable.java | 46 ++++++++++++++++--- .../tables/move/BatchOperationTable.java | 10 ++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java index f57cf7911..d6698a0dc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java @@ -40,11 +40,18 @@ public class ServerTable extends Table { private final String columnWebserverAddress = "web_address"; private final String columnInstalled = "is_installed"; private final String columnMaxPlayers = "max_players"; + private String insertStatement; public ServerTable(SQLDB db, boolean usingMySQL) { super("plan_servers", db, usingMySQL); statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(columnServerUUID + "=?").toString() + ")"; statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(columnServerID + "=?").toString() + ")"; + insertStatement = Insert.values(tableName, + columnServerUUID, + columnServerName, + columnWebserverAddress, + columnInstalled, + columnMaxPlayers); } @Override @@ -109,12 +116,7 @@ public class ServerTable extends Table { Verify.nullCheck(uuid, name, webAddress); PreparedStatement statement = null; try { - statement = prepareStatement(Insert.values(tableName, - columnServerUUID, - columnServerName, - columnWebserverAddress, - columnInstalled, - columnMaxPlayers)); + statement = prepareStatement(insertStatement); statement.setString(1, uuid.toString()); statement.setString(2, name); @@ -268,4 +270,36 @@ public class ServerTable extends Table { public String getColumnUUID() { return columnServerUUID; } + + public void insertAllServers(List allServerInfo) throws SQLException { + if (Verify.isEmpty(allServerInfo)) { + return; + } + PreparedStatement statement = null; + try { + statement = prepareStatement(insertStatement); + + for (ServerInfo info : allServerInfo) { + UUID uuid = info.getUuid(); + String name = info.getName(); + String webAddress = info.getWebAddress(); + + if (Verify.notNull(uuid, name, webAddress)) { + continue; + } + + statement.setString(1, uuid.toString()); + statement.setString(2, name); + statement.setString(3, webAddress); + statement.setBoolean(4, true); + statement.setInt(5, info.getMaxPlayers()); + statement.addBatch(); + } + + statement.executeBatch(); + commit(statement.getConnection()); + } finally { + close(statement); + } + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java index 658d0fd61..de3fb2481 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/move/BatchOperationTable.java @@ -7,10 +7,13 @@ package main.java.com.djrapitops.plan.database.tables.move; import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException; import main.java.com.djrapitops.plan.api.exceptions.DatabaseException; import main.java.com.djrapitops.plan.database.databases.SQLDB; +import main.java.com.djrapitops.plan.database.tables.ServerTable; import main.java.com.djrapitops.plan.database.tables.Table; +import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; import java.sql.SQLException; import java.util.Collection; +import java.util.List; import java.util.UUID; /** @@ -68,4 +71,11 @@ public class BatchOperationTable extends Table { public void copyWebUsers(BatchOperationTable toDB) throws SQLException { toDB.db.getSecurityTable().addUsers(db.getSecurityTable().getUsers()); } + + public void copyServers(BatchOperationTable toDB) throws SQLException { + ServerTable serverTable = db.getServerTable(); + List servers = serverTable.getBukkitServers(); + serverTable.getBungeeInfo().ifPresent(servers::add); + toDB.db.getServerTable().insertAllServers(servers); + } } \ No newline at end of file