New worlds now saved when session is saved (temporarily):

This behavior is to be changed once World change event runs its own
transaction for storing new world names.
This commit is contained in:
Rsl1122 2019-01-27 18:21:31 +02:00
parent 69abb4d406
commit 02a3a0a58a
3 changed files with 69 additions and 14 deletions

View File

@ -19,13 +19,19 @@ package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.sql.tables.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
/**
@ -56,12 +62,7 @@ public class DataStoreQueries {
}
private static Executable updateCommandUsage(UUID serverUUID, String commandName) {
String sql = "UPDATE " + CommandUseTable.TABLE_NAME + " SET "
+ CommandUseTable.TIMES_USED + "=" + CommandUseTable.TIMES_USED + "+ 1" +
" WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
" AND " + CommandUseTable.COMMAND + "=?";
return new ExecStatement(sql) {
return new ExecStatement(CommandUseTable.UPDATE_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
@ -93,6 +94,7 @@ public class DataStoreQueries {
return connection -> {
storeSessionInformation(session).execute(connection);
storeSessionKills(session).execute(connection);
storeSessionWorldTimesWorlds(session).execute(connection);
return storeSessionWorldTimes(session).execute(connection);
};
}
@ -121,6 +123,59 @@ public class DataStoreQueries {
};
}
// TODO Remove usage after WorldChange event stores world names in its own transaction
private static Executable storeSessionWorldTimesWorlds(Session session) {
return connection -> {
UUID serverUUID = session.getUnsafe(SessionKeys.SERVER_UUID);
Collection<String> worlds = session.getValue(SessionKeys.WORLD_TIMES)
.map(WorldTimes::getWorldTimes).map(Map::keySet)
.orElse(Collections.emptySet());
for (String world : worlds) {
storeWorldName(serverUUID, world).execute(connection);
}
return false;
};
}
// TODO Remove usage after WorldChange event stores world names in its own transaction
private static Executable storeWorldName(UUID serverUUID, String worldName) {
return connection -> {
if (doesWorldNameExist(connection, serverUUID, worldName)) {
return insertWorldName(serverUUID, worldName).execute(connection);
}
return false;
};
}
// TODO Remove usage after WorldChange event stores world names in its own transaction
private static boolean doesWorldNameExist(Connection connection, UUID serverUUID, String worldName) {
String selectSQL = "SELECT COUNT(1) as c FROM " + WorldTable.TABLE_NAME +
" WHERE " + WorldTable.NAME + "=?" +
" AND " + WorldTable.SERVER_UUID + "=?";
try (PreparedStatement statement = connection.prepareStatement(selectSQL)) {
statement.setString(1, worldName);
statement.setString(2, serverUUID.toString());
try (ResultSet set = statement.executeQuery()) {
return set.next() && set.getInt("c") > 0;
}
} catch (SQLException ignored) {
// Assume it has been saved.
return true;
}
}
private static Executable insertWorldName(UUID serverUUID, String worldName) {
return new ExecStatement(WorldTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, worldName);
statement.setString(2, serverUUID.toString());
}
};
}
private static Executable storeSessionWorldTimes(Session session) {
return new ExecBatchStatement(WorldTimesTable.INSERT_STATEMENT) {
@Override

View File

@ -43,6 +43,11 @@ public class CommandUseTable {
+ SERVER_ID
+ ") VALUES (?, ?, " + ServerTable.STATEMENT_SELECT_SERVER_ID + ")";
public static final String UPDATE_STATEMENT = "UPDATE " + CommandUseTable.TABLE_NAME + " SET "
+ CommandUseTable.TIMES_USED + "=" + CommandUseTable.TIMES_USED + "+ 1" +
" WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
" AND " + CommandUseTable.COMMAND + "=?";
private CommandUseTable() {
/* Static information class */
}

View File

@ -51,6 +51,7 @@ public class WorldTable extends Table {
public static final String ID = "id";
public static final String SERVER_UUID = "server_uuid";
public static final String NAME = "world_name";
public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " ("
+ NAME + ", "
+ SERVER_UUID
@ -61,11 +62,8 @@ public class WorldTable extends Table {
" AND (" + TABLE_NAME + "." + SERVER_UUID + "=?)" +
" LIMIT 1)";
private final ServerTable serverTable;
public WorldTable(SQLDB db) {
super(TABLE_NAME, db);
serverTable = db.getServerTable();
}
public static String createTableSQL(DBType dbType) {
@ -160,15 +158,12 @@ public class WorldTable extends Table {
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WorldTable)) return false;
if (!super.equals(o)) return false;
WorldTable that = (WorldTable) o;
return Objects.equals(SELECT_WORLD_ID_STATEMENT, SELECT_WORLD_ID_STATEMENT) &&
Objects.equals(serverTable, that.serverTable);
return super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), SELECT_WORLD_ID_STATEMENT, serverTable);
return Objects.hash(super.hashCode());
}
}