Batch Insert for ServerTable

This commit is contained in:
Rsl1122 2017-09-02 11:47:34 +03:00
parent 4061c9c4cd
commit c990358cd4
2 changed files with 50 additions and 6 deletions

View File

@ -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<ServerInfo> 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);
}
}
}

View File

@ -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<ServerInfo> servers = serverTable.getBukkitServers();
serverTable.getBungeeInfo().ifPresent(servers::add);
toDB.db.getServerTable().insertAllServers(servers);
}
}