Refactored SessionsTable#getIDServerIDRelation to a query

This commit is contained in:
Rsl1122 2019-02-15 09:55:16 +02:00
parent f4adf4a52c
commit 2b9199d2e2
4 changed files with 41 additions and 26 deletions

View File

@ -0,0 +1,37 @@
package com.djrapitops.plan.db.access.queries.schema;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.sql.tables.SessionsTable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import static com.djrapitops.plan.db.sql.parsing.Sql.FROM;
import static com.djrapitops.plan.db.sql.parsing.Sql.SELECT;
/**
* Query to fetch server id for each session, used by 2 patches.
*
* @author Rsl1122
* @see com.djrapitops.plan.db.patches.KillsServerIDPatch
* @see com.djrapitops.plan.db.patches.WorldTimesSeverIDPatch
*/
public class SessionIDServerIDRelationQuery extends QueryAllStatement<Map<Integer, Integer>> {
public SessionIDServerIDRelationQuery() {
super(SELECT + SessionsTable.ID + ", " +
"(SELECT plan_servers.id FROM plan_servers WHERE plan_servers.uuid=" + SessionsTable.SERVER_UUID + ") as server_id" +
FROM + SessionsTable.TABLE_NAME, 50000);
}
@Override
public Map<Integer, Integer> processResults(ResultSet set) throws SQLException {
Map<Integer, Integer> idServerIdMap = new HashMap<>();
while (set.next()) {
idServerIdMap.put(set.getInt(SessionsTable.ID), set.getInt("server_id"));
}
return idServerIdMap;
}
}

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.queries.schema.SessionIDServerIDRelationQuery;
import com.djrapitops.plan.db.sql.tables.KillsTable;
import java.sql.PreparedStatement;
@ -65,7 +66,7 @@ public class KillsServerIDPatch extends Patch {
addColumn(KillsTable.TABLE_NAME, "server_id integer NOT NULL DEFAULT 0");
Map<Integer, Integer> sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation();
Map<Integer, Integer> sessionIDServerIDRelation = query(new SessionIDServerIDRelationQuery());
String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET server_id=? WHERE " + KillsTable.SESSION_ID + "=?";

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.queries.schema.SessionIDServerIDRelationQuery;
import com.djrapitops.plan.db.sql.tables.WorldTimesTable;
import java.sql.PreparedStatement;
@ -60,7 +61,7 @@ public class WorldTimesSeverIDPatch extends Patch {
@Override
protected void applyPatch() {
Map<Integer, Integer> sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation();
Map<Integer, Integer> sessionIDServerIDRelation = query(new SessionIDServerIDRelationQuery());
String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " +
"server_id=?" +

View File

@ -18,18 +18,12 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.patches.SessionAFKTimePatch;
import com.djrapitops.plan.db.patches.SessionsOptimizationPatch;
import com.djrapitops.plan.db.patches.Version10Patch;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
@ -87,22 +81,4 @@ public class SessionsTable extends Table {
.column(AFK_TIME, Sql.LONG).notNull()
.toString();
}
public Map<Integer, Integer> getIDServerIDRelation() {
String sql = SELECT +
ID + ", " +
"(SELECT plan_servers.id FROM plan_servers WHERE plan_servers.uuid=" + SERVER_UUID + ") as server_id" +
FROM + tableName;
return query(new QueryAllStatement<Map<Integer, Integer>>(sql, 10000) {
@Override
public Map<Integer, Integer> processResults(ResultSet set) throws SQLException {
Map<Integer, Integer> idServerIdMap = new HashMap<>();
while (set.next()) {
idServerIdMap.put(set.getInt(ID), set.getInt("server_id"));
}
return idServerIdMap;
}
});
}
}