Refactored WorldTimesTable (DB Table refactor complete)

This commit is contained in:
Rsl1122 2017-10-06 17:40:27 +03:00
parent 5433116cdb
commit a268a092d7

View File

@ -7,10 +7,12 @@ import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.time.GMTimes; import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -86,10 +88,9 @@ public class WorldTimesTable extends UserIDTable {
Set<String> worldNames = worldTimesMap.keySet(); Set<String> worldNames = worldTimesMap.keySet();
db.getWorldTable().saveWorlds(worldNames); db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null; executeBatch(new ExecStatement(insertStatement) {
try (Connection connection = getConnection()) { @Override
statement = connection.prepareStatement(insertStatement); public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<String, GMTimes> entry : worldTimesMap.entrySet()) { for (Map.Entry<String, GMTimes> entry : worldTimesMap.entrySet()) {
String worldName = entry.getKey(); String worldName = entry.getKey();
GMTimes gmTimes = entry.getValue(); GMTimes gmTimes = entry.getValue();
@ -104,21 +105,14 @@ public class WorldTimesTable extends UserIDTable {
statement.setLong(7, gmTimes.getTime(gms[3])); statement.setLong(7, gmTimes.getTime(gms[3]));
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch();
commit(connection);
} finally {
close(statement);
} }
});
} }
public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException { public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID(); String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name"; String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " + String sql = "SELECT " +
columnSessionID + ", " + columnSessionID + ", " +
columnSurvival + ", " + columnSurvival + ", " +
columnCreative + ", " + columnCreative + ", " +
@ -127,11 +121,16 @@ public class WorldTimesTable extends UserIDTable {
worldNameColumn + worldNameColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId + " JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID " WHERE " + columnUserID + "=" + usersTable.statementSelectID;
);
statement.setFetchSize(2000); query(new QueryStatement<Object>(sql, 2000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); }
@Override
public Object processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
while (set.next()) { while (set.next()) {
@ -153,9 +152,9 @@ public class WorldTimesTable extends UserIDTable {
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes); session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
} }
} finally { return null;
close(set, statement);
} }
});
} }
public WorldTimes getWorldTimesOfServer() throws SQLException { public WorldTimes getWorldTimesOfServer() throws SQLException {
@ -163,14 +162,11 @@ public class WorldTimesTable extends UserIDTable {
} }
public WorldTimes getWorldTimesOfServer(UUID serverUUID) throws SQLException { public WorldTimes getWorldTimesOfServer(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID(); String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name"; String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID(); String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
String sessionServerIDColumn = sessionsTable + ".server_id"; String sessionServerIDColumn = sessionsTable + ".server_id";
statement = connection.prepareStatement("SELECT " + String sql = "SELECT " +
"SUM(" + columnSurvival + ") as survival, " + "SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " + "SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " + "SUM(" + columnAdventure + ") as adventure, " +
@ -180,11 +176,16 @@ public class WorldTimesTable extends UserIDTable {
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId + " JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + columnSessionID + " JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + columnSessionID +
" WHERE " + sessionServerIDColumn + "=" + db.getServerTable().statementSelectServerID + " WHERE " + sessionServerIDColumn + "=" + db.getServerTable().statementSelectServerID +
" GROUP BY " + columnWorldId " GROUP BY " + columnWorldId;
);
statement.setFetchSize(1000); return query(new QueryStatement<WorldTimes>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString()); statement.setString(1, serverUUID.toString());
set = statement.executeQuery(); }
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>()); WorldTimes worldTimes = new WorldTimes(new HashMap<>());
@ -201,18 +202,14 @@ public class WorldTimesTable extends UserIDTable {
worldTimes.setGMTimesForWorld(worldName, gmTimes); worldTimes.setGMTimesForWorld(worldName, gmTimes);
} }
return worldTimes; return worldTimes;
} finally {
close(set, statement);
} }
});
} }
public WorldTimes getWorldTimesOfUser(UUID uuid) throws SQLException { public WorldTimes getWorldTimesOfUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID(); String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name"; String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " + String sql = "SELECT " +
"SUM(" + columnSurvival + ") as survival, " + "SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " + "SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " + "SUM(" + columnAdventure + ") as adventure, " +
@ -221,10 +218,16 @@ public class WorldTimesTable extends UserIDTable {
" FROM " + tableName + " FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId + " JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID + " WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" GROUP BY " + columnWorldId " GROUP BY " + columnWorldId;
);
return query(new QueryStatement<WorldTimes>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); }
@Override
public WorldTimes processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
WorldTimes worldTimes = new WorldTimes(new HashMap<>()); WorldTimes worldTimes = new WorldTimes(new HashMap<>());
@ -241,9 +244,8 @@ public class WorldTimesTable extends UserIDTable {
worldTimes.setGMTimesForWorld(worldName, gmTimes); worldTimes.setGMTimesForWorld(worldName, gmTimes);
} }
return worldTimes; return worldTimes;
} finally {
close(set, statement);
} }
});
} }
public void addWorldTimesToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException { public void addWorldTimesToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException {
@ -277,16 +279,20 @@ public class WorldTimesTable extends UserIDTable {
.collect(Collectors.toList()); .collect(Collectors.toList());
db.getWorldTable().saveWorlds(worldNames); db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null; executeBatch(new ExecStatement(insertStatement) {
try (Connection connection = getConnection()) { @Override
statement = connection.prepareStatement(insertStatement); public void prepare(PreparedStatement statement) throws SQLException {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
// Every Server
for (Map<UUID, List<Session>> serverSessions : allSessions.values()) { for (Map<UUID, List<Session>> serverSessions : allSessions.values()) {
// Every User
for (Map.Entry<UUID, List<Session>> entry : serverSessions.entrySet()) { for (Map.Entry<UUID, List<Session>> entry : serverSessions.entrySet()) {
UUID uuid = entry.getKey(); UUID uuid = entry.getKey();
List<Session> sessions = entry.getValue(); List<Session> sessions = entry.getValue();
// Every Session
for (Session session : sessions) { for (Session session : sessions) {
int sessionID = session.getSessionID(); int sessionID = session.getSessionID();
// Every WorldTimes
for (Map.Entry<String, GMTimes> worldTimesEntry : session.getWorldTimes().getWorldTimes().entrySet()) { for (Map.Entry<String, GMTimes> worldTimesEntry : session.getWorldTimes().getWorldTimes().entrySet()) {
String worldName = worldTimesEntry.getKey(); String worldName = worldTimesEntry.getKey();
GMTimes gmTimes = worldTimesEntry.getValue(); GMTimes gmTimes = worldTimesEntry.getValue();
@ -302,20 +308,14 @@ public class WorldTimesTable extends UserIDTable {
} }
} }
} }
statement.executeBatch();
commit(connection);
} finally {
close(statement);
} }
});
} }
public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException { public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID(); String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name"; String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = connection.prepareStatement("SELECT " + String sql = "SELECT " +
columnSessionID + ", " + columnSessionID + ", " +
columnSurvival + ", " + columnSurvival + ", " +
columnCreative + ", " + columnCreative + ", " +
@ -323,10 +323,11 @@ public class WorldTimesTable extends UserIDTable {
columnSpectator + ", " + columnSpectator + ", " +
worldNameColumn + worldNameColumn +
" FROM " + tableName + " FROM " + tableName +
" JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId " JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId;
);
statement.setFetchSize(50000); return query(new QueryAllStatement<Map<Integer, WorldTimes>>(sql, 50000) {
set = statement.executeQuery(); @Override
public Map<Integer, WorldTimes> processResults(ResultSet set) throws SQLException {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
Map<Integer, WorldTimes> worldTimes = new HashMap<>(); Map<Integer, WorldTimes> worldTimes = new HashMap<>();
@ -347,8 +348,7 @@ public class WorldTimesTable extends UserIDTable {
worldTimes.put(sessionID, worldTOfSession); worldTimes.put(sessionID, worldTOfSession);
} }
return worldTimes; return worldTimes;
} finally { }
close(set, statement); });
}
} }
} }