From a8825fbba308e2a3791b935aca0e0cb39170b5cf Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 24 Jan 2019 15:11:53 +0200 Subject: [PATCH] Removed SessionsTable.Col --- .../db/patches/BadAFKThresholdValuePatch.java | 24 +-- .../plan/db/patches/SessionAFKTimePatch.java | 4 +- .../db/patches/SessionsOptimizationPatch.java | 33 ++--- .../plan/db/patches/WorldsServerIDPatch.java | 22 +-- .../plan/db/sql/tables/SessionsTable.java | 139 +++++++----------- .../plan/db/sql/tables/WorldTimesTable.java | 17 +-- 6 files changed, 93 insertions(+), 146 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/BadAFKThresholdValuePatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/BadAFKThresholdValuePatch.java index 7bbc8ae91..4b139d0c1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/BadAFKThresholdValuePatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/BadAFKThresholdValuePatch.java @@ -17,10 +17,10 @@ package com.djrapitops.plan.db.patches; import com.djrapitops.plan.db.SQLDB; -import com.djrapitops.plan.db.access.QueryAllStatement; +import com.djrapitops.plan.db.access.CountQueryStatement; import com.djrapitops.plan.db.sql.tables.SessionsTable; -import java.sql.ResultSet; +import java.sql.PreparedStatement; import java.sql.SQLException; /** @@ -48,13 +48,13 @@ public class BadAFKThresholdValuePatch extends Patch { // where |afk - session_length| < 5 String sql = "SELECT COUNT(1) as found FROM " + SessionsTable.TABLE_NAME + " WHERE ABS(" + - SessionsTable.Col.AFK_TIME + - " - (" + SessionsTable.Col.SESSION_END + "-" + SessionsTable.Col.SESSION_START + - ")) < 5 AND " + SessionsTable.Col.AFK_TIME + "!=0"; - return query(new QueryAllStatement(sql) { + SessionsTable.AFK_TIME + + " - (" + SessionsTable.SESSION_END + "-" + SessionsTable.SESSION_START + + ")) < 5 AND " + SessionsTable.AFK_TIME + "!=0"; + return query(new CountQueryStatement(sql, "found") { @Override - public Boolean processResults(ResultSet set) throws SQLException { - return set.next() && set.getInt("found") > 0; + public void prepare(PreparedStatement statement) throws SQLException { + /* Nothing to prepare */ } }); } @@ -62,10 +62,10 @@ public class BadAFKThresholdValuePatch extends Patch { @Override protected void applyPatch() { // where |afk - session_length| < 5 - String sql = "UPDATE " + SessionsTable.TABLE_NAME + " SET " + SessionsTable.Col.AFK_TIME + "=0 WHERE ABS(" + - SessionsTable.Col.AFK_TIME + - " - (" + SessionsTable.Col.SESSION_END + "-" + SessionsTable.Col.SESSION_START + - ")) < 5 AND " + SessionsTable.Col.AFK_TIME + "!=0"; + String sql = "UPDATE " + SessionsTable.TABLE_NAME + " SET " + SessionsTable.AFK_TIME + "=0 WHERE ABS(" + + SessionsTable.AFK_TIME + + " - (" + SessionsTable.SESSION_END + "-" + SessionsTable.SESSION_START + + ")) < 5 AND " + SessionsTable.AFK_TIME + "!=0"; execute(sql); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionAFKTimePatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionAFKTimePatch.java index 5563fe6fa..c06d8dd47 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionAFKTimePatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionAFKTimePatch.java @@ -27,13 +27,13 @@ public class SessionAFKTimePatch extends Patch { @Override public boolean hasBeenApplied() { - return hasColumn(SessionsTable.TABLE_NAME, SessionsTable.Col.AFK_TIME.get()); + return hasColumn(SessionsTable.TABLE_NAME, SessionsTable.AFK_TIME); } @Override protected void applyPatch() { addColumn(SessionsTable.TABLE_NAME, - SessionsTable.Col.AFK_TIME + " bigint NOT NULL DEFAULT 0" + SessionsTable.AFK_TIME + " bigint NOT NULL DEFAULT 0" ); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionsOptimizationPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionsOptimizationPatch.java index c6cebb21d..c58a030dc 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionsOptimizationPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/SessionsOptimizationPatch.java @@ -19,7 +19,6 @@ package com.djrapitops.plan.db.patches; import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.sql.tables.SessionsTable; -import com.djrapitops.plan.db.sql.tables.SessionsTable.Col; public class SessionsOptimizationPatch extends Patch { @@ -34,8 +33,8 @@ public class SessionsOptimizationPatch extends Patch { @Override public boolean hasBeenApplied() { - return hasColumn(tableName, Col.UUID.get()) - && hasColumn(tableName, Col.SERVER_UUID.get()) + return hasColumn(tableName, SessionsTable.USER_UUID) + && hasColumn(tableName, SessionsTable.SERVER_UUID) && !hasColumn(tableName, "user_id") && !hasColumn(tableName, "server_id") && !hasTable(tempTableName); // If this table exists the patch has failed to finish. @@ -52,23 +51,23 @@ public class SessionsOptimizationPatch extends Patch { db.getSessionsTable().createTable(); execute("INSERT INTO " + tableName + " (" + - Col.UUID + ", " + - Col.SERVER_UUID + ", " + - Col.ID + ", " + - Col.SESSION_START + ", " + - Col.SESSION_END + ", " + - Col.MOB_KILLS + ", " + - Col.DEATHS + ", " + - Col.AFK_TIME + + SessionsTable.USER_UUID + ", " + + SessionsTable.SERVER_UUID + ", " + + SessionsTable.ID + ", " + + SessionsTable.SESSION_START + ", " + + SessionsTable.SESSION_END + ", " + + SessionsTable.MOB_KILLS + ", " + + SessionsTable.DEATHS + ", " + + SessionsTable.AFK_TIME + ") SELECT " + "(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " + "(SELECT plan_servers.uuid FROM plan_servers WHERE plan_servers.id = " + tempTableName + ".server_id LIMIT 1), " + - Col.ID + ", " + - Col.SESSION_START + ", " + - Col.SESSION_END + ", " + - Col.MOB_KILLS + ", " + - Col.DEATHS + ", " + - Col.AFK_TIME + + SessionsTable.ID + ", " + + SessionsTable.SESSION_START + ", " + + SessionsTable.SESSION_END + ", " + + SessionsTable.MOB_KILLS + ", " + + SessionsTable.DEATHS + ", " + + SessionsTable.AFK_TIME + " FROM " + tempTableName ); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldsServerIDPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldsServerIDPatch.java index 84fbe80e6..3cd385d2d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldsServerIDPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/WorldsServerIDPatch.java @@ -89,15 +89,15 @@ public class WorldsServerIDPatch extends Patch { WorldTimesTable worldTimesTable = db.getWorldTimesTable(); SessionsTable sessionsTable = db.getSessionsTable(); - String worldIDColumn = worldTimesTable + "." + WorldTimesTable.Col.WORLD_ID; - String worldSessionIDColumn = worldTimesTable + "." + WorldTimesTable.Col.SESSION_ID; - String sessionIDColumn = sessionsTable + "." + SessionsTable.Col.ID; - String sessionServerUUIDColumn = sessionsTable + "." + SessionsTable.Col.SERVER_UUID; + String worldIDColumn = worldTimesTable + "." + WorldTimesTable.WORLD_ID; + String worldSessionIDColumn = worldTimesTable + "." + WorldTimesTable.SESSION_ID; + String sessionIDColumn = sessionsTable + "." + SessionsTable.ID; + String sessionServerUUIDColumn = sessionsTable + "." + SessionsTable.SERVER_UUID; String sql = "SELECT DISTINCT " + - WorldTable.Col.NAME + " FROM " + + WorldTable.NAME + " FROM " + WorldTable.TABLE_NAME + - " INNER JOIN " + worldTimesTable + " on " + worldIDColumn + "=" + WorldTable.TABLE_NAME + "." + WorldTable.Col.ID + + " INNER JOIN " + worldTimesTable + " on " + worldIDColumn + "=" + WorldTable.TABLE_NAME + "." + WorldTable.ID + " INNER JOIN " + sessionsTable + " on " + worldSessionIDColumn + "=" + sessionIDColumn + " WHERE " + sessionServerUUIDColumn + "=?"; @@ -111,7 +111,7 @@ public class WorldsServerIDPatch extends Patch { public Set processResults(ResultSet set) throws SQLException { Set worldNames = new HashSet<>(); while (set.next()) { - worldNames.add(set.getString(WorldTable.Col.NAME.get())); + worldNames.add(set.getString(WorldTable.NAME)); } return worldNames; } @@ -133,8 +133,8 @@ public class WorldsServerIDPatch extends Patch { WorldTimesTable worldTimesTable = db.getWorldTimesTable(); String sql = "UPDATE " + worldTimesTable + " SET " + - WorldTimesTable.Col.WORLD_ID + "=?" + - " WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" + + WorldTimesTable.WORLD_ID + "=?" + + " WHERE " + WorldTimesTable.WORLD_ID + "=?" + " AND " + "server_id=?"; executeBatch(new ExecStatement(sql) { @Override @@ -159,9 +159,9 @@ public class WorldsServerIDPatch extends Patch { public List processResults(ResultSet set) throws SQLException { List objects = new ArrayList<>(); while (set.next()) { - int worldID = set.getInt(WorldTable.Col.ID.get()); + int worldID = set.getInt(WorldTable.ID); int serverID = set.getInt("server_id"); - String worldName = set.getString(WorldTable.Col.NAME.get()); + String worldName = set.getString(WorldTable.NAME); objects.add(new WorldObj(worldID, serverID, worldName)); } return objects; 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 b27198c7f..7d292985e 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 @@ -27,7 +27,6 @@ import com.djrapitops.plan.db.access.QueryStatement; 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.Column; import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Select; import com.djrapitops.plan.db.sql.parsing.Sql; @@ -69,13 +68,13 @@ public class SessionsTable extends UserUUIDTable { public SessionsTable(SQLDB db) { super(TABLE_NAME, db); insertStatement = "INSERT INTO " + tableName + " (" - + Col.UUID + ", " - + Col.SESSION_START + ", " - + Col.SESSION_END + ", " - + Col.DEATHS + ", " - + Col.MOB_KILLS + ", " - + Col.AFK_TIME + ", " - + Col.SERVER_UUID + + USER_UUID + ", " + + SESSION_START + ", " + + SESSION_END + ", " + + DEATHS + ", " + + MOB_KILLS + ", " + + AFK_TIME + ", " + + SERVER_UUID + ") VALUES (?, ?, ?, ?, ?, ?, ?)"; } @@ -105,10 +104,10 @@ public class SessionsTable extends UserUUIDTable { * @return ID of the inserted session or -1 if session has not been inserted. */ private int getSessionID(UUID uuid, Session session) { - String sql = "SELECT " + Col.ID + " FROM " + tableName + - " WHERE " + Col.UUID + "=?" + - " AND " + Col.SESSION_START + "=?" + - " AND " + Col.SESSION_END + "=?"; + String sql = "SELECT " + ID + " FROM " + tableName + + " WHERE " + USER_UUID + "=?" + + " AND " + SESSION_START + "=?" + + " AND " + SESSION_END + "=?"; return query(new QueryStatement(sql) { @Override @@ -121,7 +120,7 @@ public class SessionsTable extends UserUUIDTable { @Override public Integer processResults(ResultSet set) throws SQLException { if (set.next()) { - return set.getInt(Col.ID.get()); + return set.getInt(ID); } return -1; } @@ -181,7 +180,7 @@ public class SessionsTable extends UserUUIDTable { */ private Map> getSessionInformation(UUID uuid) { String sql = Select.from(tableName, "*") - .where(Col.UUID + "=?") + .where(USER_UUID + "=?") .toString(); return query(new QueryStatement>>(sql, 10000) { @@ -194,15 +193,15 @@ public class SessionsTable extends UserUUIDTable { public Map> processResults(ResultSet set) throws SQLException { Map> sessionsByServer = new HashMap<>(); while (set.next()) { - int id = set.getInt(Col.ID.get()); - long start = set.getLong(Col.SESSION_START.get()); - long end = set.getLong(Col.SESSION_END.get()); - UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get())); + int id = set.getInt(ID); + long start = set.getLong(SESSION_START); + long end = set.getLong(SESSION_END); + UUID serverUUID = UUID.fromString(set.getString(SERVER_UUID)); - long timeAFK = set.getLong(Col.AFK_TIME.get()); + long timeAFK = set.getLong(AFK_TIME); - int deaths = set.getInt(Col.DEATHS.get()); - int mobKills = set.getInt(Col.MOB_KILLS.get()); + int deaths = set.getInt(DEATHS); + int mobKills = set.getInt(MOB_KILLS); List sessions = sessionsByServer.getOrDefault(serverUUID, new ArrayList<>()); sessions.add(new Session(id, uuid, serverUUID, start, end, mobKills, deaths, timeAFK)); sessionsByServer.put(serverUUID, sessions); @@ -222,11 +221,11 @@ public class SessionsTable extends UserUUIDTable { */ public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) { String sql = "SELECT" + - " (SUM(" + Col.SESSION_END + ") - SUM(" + Col.SESSION_START + ")) as playtime" + + " (SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" + " FROM " + tableName + - " WHERE " + Col.SESSION_START + ">?" + - " AND " + Col.UUID + "=?" + - " AND " + Col.SERVER_UUID + "=?"; + " WHERE " + SESSION_START + ">?" + + " AND " + USER_UUID + "=?" + + " AND " + SERVER_UUID + "=?"; return query(new QueryStatement(sql) { @Override @@ -299,10 +298,10 @@ public class SessionsTable extends UserUUIDTable { */ public long getPlaytimeOfServer(UUID serverUUID, long afterDate) { String sql = "SELECT" + - " (SUM(" + Col.SESSION_END + ") - SUM(" + Col.SESSION_START + ")) as playtime" + + " (SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" + " FROM " + tableName + - " WHERE " + Col.SESSION_START + ">?" + - " AND " + Col.SERVER_UUID + "=?"; + " WHERE " + SESSION_START + ">?" + + " AND " + SERVER_UUID + "=?"; return query(new QueryStatement(sql) { @Override @@ -343,9 +342,9 @@ public class SessionsTable extends UserUUIDTable { String sql = "SELECT" + " COUNT(*) as logintimes" + " FROM " + tableName + - " WHERE (" + Col.SESSION_START + " >= ?)" + - " AND " + Col.UUID + "=?" + - " AND " + Col.SERVER_UUID + "=?"; + " WHERE (" + SESSION_START + " >= ?)" + + " AND " + USER_UUID + "=?" + + " AND " + SERVER_UUID + "=?"; return query(new QueryStatement(sql) { @Override @@ -388,15 +387,15 @@ public class SessionsTable extends UserUUIDTable { public Map> getSessionInfoOfServer(UUID serverUUID) { String sql = "SELECT " + - tableName + "." + Col.ID + ", " + - Col.UUID + ", " + - Col.SESSION_START + ", " + - Col.SESSION_END + ", " + - Col.DEATHS + ", " + - Col.MOB_KILLS + ", " + - Col.AFK_TIME + + tableName + "." + ID + ", " + + USER_UUID + ", " + + SESSION_START + ", " + + SESSION_END + ", " + + DEATHS + ", " + + MOB_KILLS + ", " + + AFK_TIME + " FROM " + tableName + - " WHERE " + Col.SERVER_UUID + "=?"; + " WHERE " + SERVER_UUID + "=?"; return query(new QueryStatement>>(sql, 5000) { @Override @@ -408,15 +407,15 @@ public class SessionsTable extends UserUUIDTable { public Map> processResults(ResultSet set) throws SQLException { Map> sessionsByUser = new HashMap<>(); while (set.next()) { - int id = set.getInt(Col.ID.get()); - UUID uuid = UUID.fromString(set.getString(Col.UUID.get())); - long start = set.getLong(Col.SESSION_START.get()); - long end = set.getLong(Col.SESSION_END.get()); + int id = set.getInt(ID); + UUID uuid = UUID.fromString(set.getString(USER_UUID)); + long start = set.getLong(SESSION_START); + long end = set.getLong(SESSION_END); - int deaths = set.getInt(Col.DEATHS.get()); - int mobKills = set.getInt(Col.MOB_KILLS.get()); + int deaths = set.getInt(DEATHS); + int mobKills = set.getInt(MOB_KILLS); - long timeAFK = set.getLong(Col.AFK_TIME.get()); + long timeAFK = set.getLong(AFK_TIME); List sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>()); sessions.add(new Session(id, uuid, serverUUID, start, end, mobKills, deaths, timeAFK)); @@ -433,10 +432,10 @@ public class SessionsTable extends UserUUIDTable { public Map getLastSeenForAllPlayers() { String sql = "SELECT" + - " MAX(" + Col.SESSION_END + ") as last_seen, " + - Col.UUID + + " MAX(" + SESSION_END + ") as last_seen, " + + USER_UUID + " FROM " + tableName + - " GROUP BY " + Col.UUID; + " GROUP BY " + USER_UUID; return query(new QueryAllStatement>(sql, 20000) { @Override @@ -550,8 +549,8 @@ public class SessionsTable extends UserUUIDTable { public Map getIDServerIDRelation() { String sql = "SELECT " + - Col.ID + ", " + - "(SELECT plan_servers.id FROM plan_servers WHERE plan_servers.uuid=" + Col.SERVER_UUID + ") as server_id" + + 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) { @@ -559,46 +558,10 @@ public class SessionsTable extends UserUUIDTable { public Map processResults(ResultSet set) throws SQLException { Map idServerIdMap = new HashMap<>(); while (set.next()) { - idServerIdMap.put(set.getInt(Col.ID.get()), set.getInt("server_id")); + idServerIdMap.put(set.getInt(ID), set.getInt("server_id")); } return idServerIdMap; } }); } - - @Deprecated - public enum Col implements Column { - @Deprecated - UUID(UserUUIDTable.Col.UUID.get()), - @Deprecated - ID("id"), - @Deprecated - SERVER_UUID("server_uuid"), - @Deprecated - SESSION_START("session_start"), - @Deprecated - SESSION_END("session_end"), - @Deprecated - MOB_KILLS("mob_kills"), - @Deprecated - DEATHS("deaths"), - @Deprecated - AFK_TIME("afk_time"); - - private final String column; - - Col(String column) { - this.column = column; - } - - @Override - public String get() { - return toString(); - } - - @Override - public String toString() { - return column; - } - } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java index fd152d496..bc7d97eb9 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java @@ -33,7 +33,6 @@ import com.djrapitops.plan.db.patches.WorldsServerIDPatch; import com.djrapitops.plan.db.sql.parsing.Column; import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Sql; -import com.djrapitops.plan.db.sql.parsing.TableSqlParser; import com.djrapitops.plugin.utilities.Verify; import java.sql.PreparedStatement; @@ -111,21 +110,7 @@ public class WorldTimesTable extends UserUUIDTable { @Override public void createTable() throws DBInitException { - createTable(TableSqlParser.createTable(tableName) - .primaryKeyIDColumn(supportsMySQLQueries, Col.ID) - .column(Col.UUID, Sql.varchar(36)).notNull() - .column(Col.WORLD_ID, Sql.INT).notNull() - .column(Col.SERVER_UUID, Sql.varchar(36)).notNull() - .column(Col.SESSION_ID, Sql.INT).notNull() - .column(Col.SURVIVAL, Sql.LONG).notNull().defaultValue("0") - .column(Col.CREATIVE, Sql.LONG).notNull().defaultValue("0") - .column(Col.ADVENTURE, Sql.LONG).notNull().defaultValue("0") - .column(Col.SPECTATOR, Sql.LONG).notNull().defaultValue("0") - .primaryKey(supportsMySQLQueries, Col.ID) - .foreignKey(Col.WORLD_ID, worldTable.getTableName(), WorldTable.Col.ID) - .foreignKey(Col.SESSION_ID, sessionsTable.getTableName(), SessionsTable.Col.ID) - .toString() - ); + createTable(createTableSQL(db.getType())); } public void addWorldTimesToSessions(UUID uuid, Map sessions) {