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