diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index 9d6c03dbd..30ffa0f67 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -277,9 +277,9 @@ public abstract class SQLDB extends Database { boolean success = userId != -1 && ipsTable.removeUserIPs(userId) && nicknamesTable.removeUserNicknames(userId) - && sessionsTable.removeUserSessions(userId) && killsTable.removeUserKillsAndVictims(userId) && worldTimesTable.removeUserWorldTimes(userId) + && sessionsTable.removeUserSessions(userId) && usersTable.removeUser(uuid); if (success) { commit(); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java index 96b3fd710..501679155 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java @@ -7,31 +7,36 @@ import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.TableSqlParser; -import main.java.com.djrapitops.plan.utilities.Benchmark; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.Function; +import java.util.stream.Collectors; /** * @author Rsl1122 */ -public class KillsTable extends Table { +public class KillsTable extends UserIDTable { private final String columnKillerUserID = "killer_id"; private final String columnVictimUserID = "victim_id"; private final String columnWeapon = "weapon"; private final String columnDate = "date"; - private final String columnServerID = "server_id"; //TODO private final String columnSessionID = "session_id"; //TODO + private final SessionsTable sessionsTable; + /** * @param db * @param usingMySQL */ public KillsTable(SQLDB db, boolean usingMySQL) { super("plan_kills", db, usingMySQL); + sessionsTable = db.getSessionsTable(); } /** @@ -39,15 +44,16 @@ public class KillsTable extends Table { */ @Override public boolean createTable() { - UsersTable usersTable = db.getUsersTable(); try { execute(TableSqlParser.createTable(tableName) .column(columnKillerUserID, Sql.INT).notNull() .column(columnVictimUserID, Sql.INT).notNull() .column(columnWeapon, Sql.varchar(30)).notNull() .column(columnDate, Sql.LONG).notNull() + .column(columnSessionID, Sql.LONG).notNull() .foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID()) .foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID()) + .foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID()) .toString() ); return true; @@ -77,164 +83,70 @@ public class KillsTable extends Table { } } - /** - * @param userId - * @return - * @throws SQLException - */ - public List getPlayerKills(int userId) throws SQLException { - UsersTable usersTable = db.getUsersTable(); - PreparedStatement statement = null; - ResultSet set = null; - try { - statement = prepareStatement("SELECT * FROM " + tableName + " WHERE (" + columnKillerUserID + "=?)"); - statement.setInt(1, userId); - set = statement.executeQuery(); - List killData = new ArrayList<>(); - while (set.next()) { - UUID victimUUID = null; // TODO Victim UUID Retrieval - killData.add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate))); - } - return killData; - } finally { - close(set); - close(statement); - } - } - - /** - * @param userId - * @param kills - * @throws SQLException - */ - public void savePlayerKills(int userId, List kills) throws SQLException { - if (Verify.isEmpty(kills)) { + public void savePlayerKills(UUID uuid, long sessionID, List playerKills) throws SQLException { + if (Verify.isEmpty(playerKills)) { return; } - Benchmark.start("Save Kills"); - kills.removeAll(getPlayerKills(userId)); PreparedStatement statement = null; try { statement = prepareStatement("INSERT INTO " + tableName + " (" + columnKillerUserID + ", " + columnVictimUserID + ", " - + columnWeapon + ", " - + columnDate - + ") VALUES (?, ?, ?, ?)"); - boolean commitRequired = false; - for (KillData kill : kills) { - if (kill == null) { - continue; - } - statement.setInt(1, userId); - statement.setInt(2, -1); // TODO Victim ID Retrieval - statement.setString(3, kill.getWeapon()); - statement.setLong(4, kill.getTime()); + + columnSessionID + ", " + + columnDate + ", " + + columnWeapon + + ") VALUES (" + + usersTable.statementSelectID + ", " + + usersTable.statementSelectID + ", " + + "?, ?, ?)"); + for (KillData kill : playerKills) { + UUID victim = kill.getVictim(); + long date = kill.getTime(); + String weapon = kill.getWeapon(); + statement.setString(1, uuid.toString()); + statement.setString(2, victim.toString()); + statement.setLong(3, sessionID); + statement.setLong(4, date); + statement.setString(5, weapon); statement.addBatch(); - commitRequired = true; - } - - if (commitRequired) { - statement.executeBatch(); } + statement.executeBatch(); } finally { close(statement); - Benchmark.stop("Database", "Save Kills"); } } - /** - * @param ids - * @param uuids - * @return - * @throws SQLException - */ - public Map> getPlayerKills(Collection ids, Map uuids) throws SQLException { - if (ids == null || ids.isEmpty()) { - return new HashMap<>(); - } - Benchmark.start("Get Kills multiple"); + public void addKillsToSessions(UUID uuid, List allSessions) throws SQLException { PreparedStatement statement = null; ResultSet set = null; + Map sessions = allSessions.stream().collect(Collectors.toMap(Session::getSessionID, Function.identity())); try { - Map> kills = new HashMap<>(); - statement = prepareStatement("SELECT * FROM " + tableName); + String usersTableID = usersTable + "." + usersTable.getColumnID(); + String usersTableUUID = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid"; + statement = prepareStatement("SELECT " + + columnSessionID + ", " + + columnDate + ", " + + columnWeapon + ", " + + usersTableUUID + ", " + + " FROM " + tableName + + " WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID + + " JOIN " + usersTable + " on " + usersTableID + "=" + columnVictimUserID); // Might not work TODO TEST + statement.setString(1, uuid.toString()); set = statement.executeQuery(); - for (Integer id : ids) { - kills.put(id, new ArrayList<>()); - } while (set.next()) { - int killerID = set.getInt(columnKillerUserID); - if (!ids.contains(killerID)) { + long sessionID = set.getLong(columnSessionID); + Session session = sessions.get(sessionID); + if (session == null) { continue; } - UUID victimUUID = null; // TODO Victim UUID Retrieval - kills.get(killerID).add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate))); - } - return kills; - } finally { - close(set); - close(statement); - Benchmark.stop("Database", "Get Kills multiple"); - } - } - - /** - * @param kills - * @param uuids - * @throws SQLException - */ - public void savePlayerKills(Map> kills, Map uuids) throws SQLException { - if (Verify.isEmpty(kills)) { - return; - } - - Benchmark.start("Save Kills multiple"); - Map> saved = getPlayerKills(kills.keySet(), uuids); - - PreparedStatement statement = null; - try { - statement = prepareStatement("INSERT INTO " + tableName + " (" - + columnKillerUserID + ", " - + columnVictimUserID + ", " - + columnWeapon + ", " - + columnDate - + ") VALUES (?, ?, ?, ?)"); - boolean commitRequired = false; - for (Map.Entry> entrySet : kills.entrySet()) { - Integer id = entrySet.getKey(); - List playerKills = entrySet.getValue(); - playerKills.removeIf(Objects::isNull); - List s = saved.get(id); - - if (s != null) { - playerKills.removeAll(s); - } - - for (KillData kill : playerKills) { - statement.setInt(1, id); - statement.setInt(2, -1); // TODO Victim ID Retrieval - statement.setString(3, kill.getWeapon()); - statement.setLong(4, kill.getTime()); - statement.addBatch(); - commitRequired = true; - } - - if (commitRequired) { - statement.executeBatch(); - } + String uuidS = set.getString("victim_uuid"); + UUID victim = UUID.fromString(uuidS); + long date = set.getLong(columnDate); + String weapon = set.getString(columnWeapon); + session.getPlayerKills().add(new KillData(victim, weapon, date)); } } finally { - close(statement); - Benchmark.stop("Database", "Save Kills multiple"); + close(set, statement); } } - - public void savePlayerKills(UUID uuid, List playerKills) { - // TODO savePlayerKills - } - - public void addKillsToSessions(List allSessions) { - // TODO addKillsToSessions - } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java index 8472cc3ff..854071e53 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java @@ -19,7 +19,7 @@ import java.util.stream.Collectors; */ public class SessionsTable extends UserIDTable { - private final String columnSessionID = "id"; + private final String columnID = "id"; private final String columnSessionStart = "session_start"; private final String columnSessionEnd = "session_end"; private final String columnServerID = "server_id"; @@ -46,7 +46,7 @@ public class SessionsTable extends UserIDTable { String serverTableName = serverTable.getTableName(); String serverTableID = serverTable.getColumnID(); String sql = TableSqlParser.createTable(this.tableName) - .primaryKeyIDColumn(usingMySQL, columnSessionID, Sql.LONG) + .primaryKeyIDColumn(usingMySQL, columnID, Sql.LONG) .column(columnUserID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull() .column(columnSessionStart, Sql.LONG).notNull() @@ -55,7 +55,7 @@ public class SessionsTable extends UserIDTable { .column(columnDeaths, Sql.INT).notNull() .foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID()) .foreignKey(columnServerID, serverTableName, serverTableID) - .primaryKey(usingMySQL, columnSessionID) + .primaryKey(usingMySQL, columnID) .toString(); System.out.println(sql); execute(sql); @@ -92,9 +92,8 @@ public class SessionsTable extends UserIDTable { if (sessionID == -1) { throw new IllegalStateException("Session was not Saved!"); } - session.setSessionID(sessionID); - db.getWorldTimesTable().saveWorldTimes(session.getWorldTimes()); - db.getKillsTable().savePlayerKills(uuid, session.getPlayerKills()); + db.getWorldTimesTable().saveWorldTimes(sessionID, session.getWorldTimes()); + db.getKillsTable().savePlayerKills(uuid, sessionID, session.getPlayerKills()); } /** @@ -117,7 +116,7 @@ public class SessionsTable extends UserIDTable { + columnMobKills + ", " + columnServerID + ") VALUES (" - + columnUserID + "=" + usersTable.statementSelectID + ", " + + usersTable.statementSelectID + ", " + "?, ?, ?, ?, " + serverTable.statementSelectServerID + ")"); statement.setString(1, uuid.toString()); @@ -145,7 +144,7 @@ public class SessionsTable extends UserIDTable { PreparedStatement statement = null; ResultSet set = null; try { - statement = prepareStatement("SELECT " + columnSessionID + " FROM " + tableName + + statement = prepareStatement("SELECT " + columnID + " FROM " + tableName + " WHERE " + columnUserID + "=" + usersTable.statementSelectID + " AND " + columnSessionStart + "=?" + " AND " + columnSessionEnd + "=?"); @@ -154,7 +153,7 @@ public class SessionsTable extends UserIDTable { statement.setLong(3, session.getSessionEnd()); set = statement.executeQuery(); if (set.next()) { - return set.getLong(columnSessionID); + return set.getLong(columnID); } return -1L; } finally { @@ -184,7 +183,7 @@ public class SessionsTable extends UserIDTable { statement.setString(1, uuid.toString()); set = statement.executeQuery(); while (set.next()) { - long id = set.getLong(columnSessionID); + long id = set.getLong(columnID); long start = set.getLong(columnSessionStart); long end = set.getLong(columnSessionEnd); String serverName = serverNames.get(set.getInt(columnServerID)); @@ -203,8 +202,8 @@ public class SessionsTable extends UserIDTable { public Map> getSessions(UUID uuid) throws SQLException { Map> sessions = getSessionInformation(uuid); List allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toList()); - db.getKillsTable().addKillsToSessions(allSessions); - db.getWorldTimesTable().addWorldTimesToSessions(allSessions); + db.getKillsTable().addKillsToSessions(uuid, allSessions); + db.getWorldTimesTable().addWorldTimesToSessions(uuid, allSessions); return sessions; } @@ -397,7 +396,7 @@ public class SessionsTable extends UserIDTable { ResultSet set = null; try { statement = prepareStatement("SELECT" + - " (COUNT(" + columnSessionID + ") - SUM(" + columnSessionStart + ")) as logintimes" + + " (COUNT(" + columnID + ") - SUM(" + columnSessionStart + ")) as logintimes" + " FROM " + tableName + " WHERE " + columnSessionStart + ">?" + " AND " + columnUserID + "=" + usersTable.statementSelectID + @@ -414,4 +413,8 @@ public class SessionsTable extends UserIDTable { close(set, statement); } } + + public String getColumnID() { + return columnID; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index 9219773e9..e8140395b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -377,4 +377,8 @@ public class UsersTable extends Table { // TODO Rewrite method for new UserData objects. return new ArrayList<>(); } + + public String getColumnUUID() { + return columnUUID; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java index ca09b7a06..1b37d3615 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTimesTable.java @@ -11,10 +11,7 @@ import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Table class representing database table plan_world_times. @@ -316,11 +313,11 @@ public class WorldTimesTable extends UserIDTable { } } - public void saveWorldTimes(WorldTimes worldTimes) { + public void saveWorldTimes(long sessionID, WorldTimes worldTimes) { // TODO saveWorldTimes (INSERT) } - public void addWorldTimesToSessions(List allSessions) { + public void addWorldTimesToSessions(UUID uuid, List allSessions) { // TODO saveWorldTimes } }