diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java
index 9b546cee2..fa6789865 100644
--- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java
+++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java
@@ -204,7 +204,8 @@ public abstract class SQLDB extends Database {
new DiskUsagePatch(this),
new WorldsOptimizationPatch(this),
new WorldTimesOptimizationPatch(this),
- new SessionsOptimizationPatch(this)
+ new SessionsOptimizationPatch(this),
+ new PingOptimizationPatch(this)
};
try {
diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/PingOptimizationPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/PingOptimizationPatch.java
new file mode 100644
index 000000000..82128d496
--- /dev/null
+++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/patches/PingOptimizationPatch.java
@@ -0,0 +1,80 @@
+/*
+ * This file is part of Player Analytics (Plan).
+ *
+ * Plan is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License v3 as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Plan is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Plan. If not, see .
+ */
+package com.djrapitops.plan.system.database.databases.sql.patches;
+
+import com.djrapitops.plan.api.exceptions.database.DBOpException;
+import com.djrapitops.plan.system.database.databases.sql.SQLDB;
+import com.djrapitops.plan.system.database.databases.sql.tables.PingTable;
+import com.djrapitops.plan.system.database.databases.sql.tables.PingTable.Col;
+
+public class PingOptimizationPatch extends Patch {
+
+ private String tempTableName;
+ private String tableName;
+
+ public PingOptimizationPatch(SQLDB db) {
+ super(db);
+ tableName = PingTable.TABLE_NAME;
+ tempTableName = "temp_ping";
+ }
+
+ @Override
+ public boolean hasBeenApplied() {
+ return hasColumn(tableName, Col.UUID.get())
+ && hasColumn(tableName, Col.SERVER_UUID.get())
+ && !hasColumn(tableName, "user_id")
+ && !hasColumn(tableName, "server_id")
+ && !hasTable(tempTableName); // If this table exists the patch has failed to finish.
+ }
+
+ @Override
+ public void apply() {
+ try {
+ tempOldTable();
+ db.getPingTable().createTable();
+
+ db.execute("INSERT INTO " + tableName + " (" +
+ Col.UUID + ", " +
+ Col.SERVER_UUID + ", " +
+ Col.ID + ", " +
+ Col.MIN_PING + ", " +
+ Col.MAX_PING + ", " +
+ Col.AVG_PING + ", " +
+ Col.DATE +
+ ") 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.MIN_PING + ", " +
+ Col.MAX_PING + ", " +
+ Col.AVG_PING + ", " +
+ Col.DATE +
+ " FROM " + tempTableName
+ );
+
+ dropTable(tempTableName);
+ } catch (Exception e) {
+ throw new DBOpException(PingOptimizationPatch.class.getSimpleName() + " failed.", e);
+ }
+ }
+
+ private void tempOldTable() {
+ if (!hasTable(tempTableName)) {
+ renameTable(tableName, tempTableName);
+ }
+ }
+}
diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/PingTable.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/PingTable.java
index 267d27f03..d39f2c5cc 100644
--- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/PingTable.java
+++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/PingTable.java
@@ -32,40 +32,42 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
-public class PingTable extends UserIDTable {
+/**
+ * Table that represents plan_ping in the database.
+ *
+ * Patches related to this table:
+ * {@link com.djrapitops.plan.system.database.databases.sql.patches.PingOptimizationPatch}
+ *
+ * @author Rsl1122
+ */
+public class PingTable extends UserUUIDTable {
public static final String TABLE_NAME = "plan_ping";
private final String insertStatement;
- private final ServerTable serverTable;
public PingTable(SQLDB db) {
super(TABLE_NAME, db);
- serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " (" +
- Col.USER_ID + ", " +
- Col.SERVER_ID + ", " +
+ Col.UUID + ", " +
+ Col.SERVER_UUID + ", " +
Col.DATE + ", " +
Col.MIN_PING + ", " +
Col.MAX_PING + ", " +
Col.AVG_PING +
- ") VALUES (" +
- usersTable.statementSelectID + ", " +
- serverTable.statementSelectServerID + ", ?, ?, ?, ?)";
+ ") VALUES (?, ?, ?, ?, ?, ?)";
}
@Override
public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(TABLE_NAME)
.primaryKeyIDColumn(supportsMySQLQueries, Col.ID)
- .column(Col.USER_ID, Sql.INT).notNull()
- .column(Col.SERVER_ID, Sql.INT).notNull()
+ .column(Col.UUID, Sql.varchar(36)).notNull()
+ .column(Col.SERVER_UUID, Sql.varchar(36)).notNull()
.column(Col.DATE, Sql.LONG).notNull()
.column(Col.MAX_PING, Sql.INT).notNull()
.column(Col.MIN_PING, Sql.INT).notNull()
.column(Col.AVG_PING, Sql.DOUBLE).notNull()
.primaryKey(supportsMySQLQueries, Col.ID)
- .foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
- .foreignKey(Col.SERVER_ID, ServerTable.TABLE_NAME, ServerTable.Col.SERVER_ID)
.toString());
}
@@ -98,9 +100,8 @@ public class PingTable extends UserIDTable {
}
public List getPing(UUID uuid) {
- Map serverUUIDs = serverTable.getServerUUIDsByID();
String sql = "SELECT * FROM " + tableName +
- " WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
+ " WHERE " + Col.UUID + "=?";
return query(new QueryStatement>(sql, 10000) {
@Override
@@ -115,7 +116,7 @@ public class PingTable extends UserIDTable {
while (set.next()) {
pings.add(new Ping(
set.getLong(Col.DATE.get()),
- serverUUIDs.get(set.getInt(Col.SERVER_ID.get())),
+ UUID.fromString(set.getString(Col.SERVER_UUID.get())),
set.getInt(Col.MIN_PING.get()),
set.getInt(Col.MAX_PING.get()),
set.getDouble(Col.AVG_PING.get())
@@ -129,28 +130,22 @@ public class PingTable extends UserIDTable {
}
public Map> getAllPings() {
- String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
- String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
- String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
- String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
String sql = "SELECT " +
Col.DATE + ", " +
Col.MAX_PING + ", " +
Col.MIN_PING + ", " +
Col.AVG_PING + ", " +
- usersUUIDColumn + ", " +
- serverUUIDColumn +
- " FROM " + tableName +
- " INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + UserInfoTable.Col.USER_ID +
- " INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + UserInfoTable.Col.SERVER_ID;
+ Col.UUID + ", " +
+ Col.SERVER_UUID +
+ " FROM " + tableName;
return query(new QueryAllStatement