Refactored ServerTable#insertAllServers to an executable

This commit is contained in:
Rsl1122 2019-01-24 19:38:13 +02:00
parent cf8cadeb28
commit 9d550caa14
4 changed files with 42 additions and 42 deletions

View File

@ -86,7 +86,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
}
private void copyServers() {
db.getServerTable().insertAllServers(sourceDB.query(LargeFetchQueries.fetchPlanServerInformation()).values());
copy(LargeStoreQueries::storeAllPlanServerInformation, LargeFetchQueries.fetchPlanServerInformationCollection());
}
private void copyTPS() {

View File

@ -315,6 +315,10 @@ public class LargeFetchQueries {
};
}
public static Query<Collection<Server>> fetchPlanServerInformationCollection() {
return db -> db.query(fetchPlanServerInformation()).values();
}
/**
* Query the database for Session data without kill, death or world data.
*

View File

@ -21,14 +21,13 @@ import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.NicknamesTable;
import com.djrapitops.plan.db.sql.tables.SecurityTable;
import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -139,7 +138,7 @@ public class LargeStoreQueries {
};
}
public static Executable storeAllPlanWebUsers(List<WebUser> users) {
public static Executable storeAllPlanWebUsers(Collection<WebUser> users) {
if (Verify.isEmpty(users)) {
return Executable.empty();
}
@ -160,4 +159,32 @@ public class LargeStoreQueries {
}
};
}
public static Executable storeAllPlanServerInformation(Collection<Server> servers) {
if (Verify.isEmpty(servers)) {
return Executable.empty();
}
return new ExecBatchStatement(ServerTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Server info : servers) {
UUID uuid = info.getUuid();
String name = info.getName();
String webAddress = info.getWebAddress();
if (uuid == null) {
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();
}
}
};
}
}

View File

@ -49,6 +49,10 @@ public class ServerTable extends Table {
public static final String INSTALLED = "is_installed";
public static final String MAX_PLAYERS = "max_players";
public static final String INSERT_STATEMENT = Insert.values(TABLE_NAME,
SERVER_UUID, NAME,
WEB_ADDRESS, INSTALLED, MAX_PLAYERS);
public static final String STATEMENT_SELECT_SERVER_ID =
"(SELECT " + TABLE_NAME + "." + SERVER_ID + " FROM " + TABLE_NAME +
" WHERE " + TABLE_NAME + "." + SERVER_UUID + "=?" +
@ -58,17 +62,10 @@ public class ServerTable extends Table {
super(TABLE_NAME, db);
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + SERVER_ID).where(tableName + "." + SERVER_UUID + "=?").toString() + " LIMIT 1)";
statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + NAME).where(tableName + "." + SERVER_ID + "=?").toString() + " LIMIT 1)";
insertStatement = Insert.values(tableName,
SERVER_UUID,
NAME,
WEB_ADDRESS,
INSTALLED,
MAX_PLAYERS);
}
public final String statementSelectServerID;
public final String statementSelectServerNameID;
private String insertStatement;
public static String createTableSQL(DBType dbType) {
return CreateTableParser.create(TABLE_NAME, dbType)
@ -153,7 +150,7 @@ public class ServerTable extends Table {
String webAddress = info.getWebAddress();
Verify.nullCheck(uuid, name, webAddress);
execute(new ExecStatement(insertStatement) {
execute(new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
@ -228,34 +225,6 @@ public class ServerTable extends Table {
});
}
public void insertAllServers(Collection<Server> allServer) {
if (Verify.isEmpty(allServer)) {
return;
}
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Server info : allServer) {
UUID uuid = info.getUuid();
String name = info.getName();
String webAddress = info.getWebAddress();
if (uuid == null) {
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();
}
}
});
}
public Optional<UUID> getServerUUID(String serverName) {
String sql = Select.from(tableName,
SERVER_UUID)