From 2b9199d2e23cfa425716ccf842d69a7ab4b08eed Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 15 Feb 2019 09:55:16 +0200 Subject: [PATCH] Refactored SessionsTable#getIDServerIDRelation to a query --- .../SessionIDServerIDRelationQuery.java | 37 +++++++++++++++++++ .../plan/db/patches/KillsServerIDPatch.java | 3 +- .../db/patches/WorldTimesSeverIDPatch.java | 3 +- .../plan/db/sql/tables/SessionsTable.java | 24 ------------ 4 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/schema/SessionIDServerIDRelationQuery.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/schema/SessionIDServerIDRelationQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/schema/SessionIDServerIDRelationQuery.java new file mode 100644 index 000000000..53de6bddf --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/schema/SessionIDServerIDRelationQuery.java @@ -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> { + + 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 processResults(ResultSet set) throws SQLException { + Map idServerIdMap = new HashMap<>(); + while (set.next()) { + idServerIdMap.put(set.getInt(SessionsTable.ID), set.getInt("server_id")); + } + return idServerIdMap; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/KillsServerIDPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/KillsServerIDPatch.java index 7d0e8d3d6..39676fc2a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/KillsServerIDPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/KillsServerIDPatch.java @@ -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 sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation(); + Map sessionIDServerIDRelation = query(new SessionIDServerIDRelationQuery()); String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET server_id=? WHERE " + KillsTable.SESSION_ID + "=?"; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldTimesSeverIDPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldTimesSeverIDPatch.java index dd64330ff..63bcf3a75 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldTimesSeverIDPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldTimesSeverIDPatch.java @@ -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 sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation(); + Map sessionIDServerIDRelation = query(new SessionIDServerIDRelationQuery()); String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " + "server_id=?" + diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SessionsTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SessionsTable.java index e2a7b0600..da9bd4ba0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SessionsTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/SessionsTable.java @@ -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 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>(sql, 10000) { - @Override - public Map processResults(ResultSet set) throws SQLException { - Map idServerIdMap = new HashMap<>(); - while (set.next()) { - idServerIdMap.put(set.getInt(ID), set.getInt("server_id")); - } - return idServerIdMap; - } - }); - } } \ No newline at end of file