[#900] Refactored WorldTimesTable#getWorldTimesOfServer to a query:

- Changed GROUP BY to group by world_name instead, this should fix the
  issue, even if it could not be reproduced.
This commit is contained in:
Rsl1122 2019-01-27 11:42:40 +02:00
parent 0a7695a3d3
commit 4315b53ca3
4 changed files with 48 additions and 49 deletions

View File

@ -16,13 +16,12 @@
*/ */
package com.djrapitops.plan.db.sql.queries; package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.CommandUseTable; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -133,4 +132,46 @@ public class AggregateQueries {
} }
}; };
} }
public static Query<WorldTimes> totalWorldTimes(UUID serverUUID) {
String worldIDColumn = WorldTable.TABLE_NAME + "." + WorldTable.ID;
String worldNameColumn = WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as world";
String sql = "SELECT " +
"SUM(" + WorldTimesTable.SURVIVAL + ") as survival, " +
"SUM(" + WorldTimesTable.CREATIVE + ") as creative, " +
"SUM(" + WorldTimesTable.ADVENTURE + ") as adventure, " +
"SUM(" + WorldTimesTable.SPECTATOR + ") as spectator, " +
worldNameColumn +
" FROM " + WorldTimesTable.TABLE_NAME +
" INNER JOIN " + WorldTable.TABLE_NAME + " on " + worldIDColumn + "=" + WorldTimesTable.WORLD_ID +
" WHERE " + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SERVER_UUID + "=?" +
" GROUP BY world";
return new QueryStatement<WorldTimes>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
return worldTimes;
}
};
}
} }

View File

@ -76,7 +76,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
}); });
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.query(AggregateQueries.commandUsageCounts(serverUUID))); container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.query(AggregateQueries.commandUsageCounts(serverUUID)));
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID)); container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.query(AggregateQueries.totalWorldTimes(serverUUID)));
// Calculating getters // Calculating getters
container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators()); container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators());

View File

@ -184,48 +184,6 @@ public class WorldTimesTable extends Table {
}); });
} }
public WorldTimes getWorldTimesOfServer(UUID serverUUID) {
String worldIDColumn = worldTable + "." + WorldTable.ID;
String worldNameColumn = worldTable + "." + WorldTable.NAME + " as world_name";
String sql = "SELECT " +
"SUM(" + SURVIVAL + ") as survival, " +
"SUM(" + CREATIVE + ") as creative, " +
"SUM(" + ADVENTURE + ") as adventure, " +
"SUM(" + SPECTATOR + ") as spectator, " +
worldNameColumn +
" FROM " + tableName +
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + WORLD_ID +
" WHERE " + tableName + "." + SERVER_UUID + "=?" +
" GROUP BY " + WORLD_ID;
return query(new QueryStatement<WorldTimes>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world_name");
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong("survival"));
gmMap.put(gms[1], set.getLong("creative"));
gmMap.put(gms[2], set.getLong("adventure"));
gmMap.put(gms[3], set.getLong("spectator"));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
return worldTimes;
}
});
}
public WorldTimes getWorldTimesOfUser(UUID uuid) { public WorldTimes getWorldTimesOfUser(UUID uuid) {
String worldIDColumn = worldTable + "." + WorldTable.ID; String worldIDColumn = worldTable + "." + WorldTable.ID;
String worldNameColumn = worldTable + "." + WorldTable.NAME + " as world_name"; String worldNameColumn = worldTable + "." + WorldTable.NAME + " as world_name";

View File

@ -775,13 +775,13 @@ public abstract class CommonDBTest {
@Test @Test
public void testGetServerWorldTimes() { public void testGetServerWorldTimes() {
testSaveSessionsWorldTimes(); testSaveSessionsWorldTimes();
WorldTimes worldTimesOfServer = db.getWorldTimesTable().getWorldTimesOfServer(serverUUID); WorldTimes worldTimesOfServer = db.query(AggregateQueries.totalWorldTimes(serverUUID));
assertEquals(createWorldTimes(), worldTimesOfServer); assertEquals(createWorldTimes(), worldTimesOfServer);
} }
@Test @Test
public void testGetServerWorldTimesNoSessions() { public void testGetServerWorldTimesNoSessions() {
WorldTimes worldTimesOfServer = db.getWorldTimesTable().getWorldTimesOfServer(serverUUID); WorldTimes worldTimesOfServer = db.query(AggregateQueries.totalWorldTimes(serverUUID));
assertEquals(new WorldTimes(new HashMap<>()), worldTimesOfServer); assertEquals(new WorldTimes(new HashMap<>()), worldTimesOfServer);
} }