Bunch of code smells:

- Sorted out SessionQueries ORDER BY usefulness with a TreeMap

Level Critical (SonarCloud):
- Smell: Duplicated String literals in the Queries: " FROM ", " WHERE ",
  " AND ", etc
- Smell: GeoInfoStoreTransaction static value assignment
- Smell: Patch - Duplicate switch case
- Smell: DataCache - Empty constructor without comment
This commit is contained in:
Rsl1122 2019-02-14 22:27:24 +02:00
parent 23799d303f
commit 4eb8c6476a
17 changed files with 201 additions and 185 deletions

View File

@ -167,6 +167,7 @@ public abstract class BukkitImporter implements Importer {
SaveOperations save = dbSystem.getDatabase().save();
// TODO Replace with a transaction
save.insertUsers(users);
submitTo(service, () -> save.insertSessions(ImmutableMap.of(serverUUID.get(), sessions), true));
submitTo(service, () -> save.kickAmount(timesKicked));

View File

@ -28,6 +28,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Static method class for queries that count together counts for a player on a per server basis.
* <p>
@ -51,9 +53,9 @@ public class PerServerAggregateQueries {
public static Query<Map<UUID, Long>> lastSeenOnServers(UUID playerUUID) {
String sql = "SELECT MAX(" + SessionsTable.SESSION_END + ") as last_seen, " +
SessionsTable.SERVER_UUID +
" FROM " + SessionsTable.TABLE_NAME +
" WHERE " + SessionsTable.USER_UUID + "=?" +
" GROUP BY " + SessionsTable.SERVER_UUID;
FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.USER_UUID + "=?" +
GROUP_BY + SessionsTable.SERVER_UUID;
return new QueryStatement<Map<UUID, Long>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -80,9 +82,9 @@ public class PerServerAggregateQueries {
* @return Map: Server UUID - Player kill count
*/
public static Query<Map<UUID, Integer>> playerKillCountOnServers(UUID playerUUID) {
String sql = "SELECT COUNT(1) as kill_count, " + KillsTable.SERVER_UUID + " FROM " + KillsTable.TABLE_NAME +
" WHERE " + KillsTable.KILLER_UUID + "=?" +
" GROUP BY " + KillsTable.SERVER_UUID;
String sql = "SELECT COUNT(1) as kill_count, " + KillsTable.SERVER_UUID + FROM + KillsTable.TABLE_NAME +
WHERE + KillsTable.KILLER_UUID + "=?" +
GROUP_BY + KillsTable.SERVER_UUID;
return new QueryStatement<Map<UUID, Integer>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -110,9 +112,9 @@ public class PerServerAggregateQueries {
*/
public static Query<Map<UUID, Integer>> mobKillCountOnServers(UUID playerUUID) {
String sql = "SELECT SUM(" + SessionsTable.MOB_KILLS + ") as kill_count, " +
SessionsTable.SERVER_UUID + " FROM " + SessionsTable.TABLE_NAME +
" WHERE " + SessionsTable.USER_UUID + "=?" +
" GROUP BY " + SessionsTable.SERVER_UUID;
SessionsTable.SERVER_UUID + FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.USER_UUID + "=?" +
GROUP_BY + SessionsTable.SERVER_UUID;
return new QueryStatement<Map<UUID, Integer>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -139,9 +141,9 @@ public class PerServerAggregateQueries {
* @return Map: Server UUID - Mob kill count
*/
public static Query<Map<UUID, Integer>> playerDeathCountOnServers(UUID playerUUID) {
String sql = "SELECT COUNT(1) as death_count, " + KillsTable.SERVER_UUID + " FROM " + KillsTable.TABLE_NAME +
" WHERE " + KillsTable.VICTIM_UUID + "=?" +
" GROUP BY " + KillsTable.SERVER_UUID;
String sql = "SELECT COUNT(1) as death_count, " + KillsTable.SERVER_UUID + FROM + KillsTable.TABLE_NAME +
WHERE + KillsTable.VICTIM_UUID + "=?" +
GROUP_BY + KillsTable.SERVER_UUID;
return new QueryStatement<Map<UUID, Integer>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -163,9 +165,9 @@ public class PerServerAggregateQueries {
public static Query<Map<UUID, Integer>> totalDeathCountOnServers(UUID playerUUID) {
String sql = "SELECT SUM(" + SessionsTable.DEATHS + ") as death_count, " +
SessionsTable.SERVER_UUID + " FROM " + SessionsTable.TABLE_NAME +
" WHERE " + SessionsTable.USER_UUID + "=?" +
" GROUP BY " + SessionsTable.SERVER_UUID;
SessionsTable.SERVER_UUID + FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.USER_UUID + "=?" +
GROUP_BY + SessionsTable.SERVER_UUID;
return new QueryStatement<Map<UUID, Integer>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {

View File

@ -28,6 +28,8 @@ import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Static method class for queries that return information related to a single player.
*
@ -47,8 +49,8 @@ public class PlayerFetchQueries {
*/
public static Query<Optional<String>> playerUserName(UUID playerUUID) {
String sql = "SELECT " + UsersTable.USER_NAME +
" FROM " + UsersTable.TABLE_NAME +
" WHERE " + UsersTable.USER_UUID + "=?";
FROM + UsersTable.TABLE_NAME +
WHERE + UsersTable.USER_UUID + "=?";
return new QueryStatement<Optional<String>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -73,7 +75,7 @@ public class PlayerFetchQueries {
*/
public static Query<Boolean> isPlayerRegistered(UUID playerUUID) {
String sql = "SELECT COUNT(1) as c FROM " + UsersTable.TABLE_NAME +
" WHERE " + UsersTable.USER_UUID + "=?";
WHERE + UsersTable.USER_UUID + "=?";
return new HasMoreThanZeroQueryStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -91,8 +93,8 @@ public class PlayerFetchQueries {
*/
public static Query<Boolean> isPlayerRegisteredOnServer(UUID playerUUID, UUID serverUUID) {
String sql = "SELECT COUNT(1) as c FROM " + UserInfoTable.TABLE_NAME +
" WHERE " + UserInfoTable.USER_UUID + "=?" +
" AND " + UserInfoTable.SERVER_UUID + "=?";
WHERE + UserInfoTable.USER_UUID + "=?" +
AND + UserInfoTable.SERVER_UUID + "=?";
return new HasMoreThanZeroQueryStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {

View File

@ -28,6 +28,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Static method class for queries that count how many entries of particular kinds there are for a server.
*
@ -62,7 +64,7 @@ public class ServerAggregateQueries {
*/
public static Query<Integer> serverUserCount(UUID serverUUID) {
String sql = "SELECT COUNT(1) as c FROM " + UserInfoTable.TABLE_NAME +
" WHERE " + UserInfoTable.SERVER_UUID + "=?";
WHERE + UserInfoTable.SERVER_UUID + "=?";
return new QueryStatement<Integer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -85,9 +87,9 @@ public class ServerAggregateQueries {
* @return Map: Server UUID - Count of users registered to that server
*/
public static Query<Map<UUID, Integer>> serverUserCounts() {
String sql = "SELECT COUNT(1) as c, " + UserInfoTable.SERVER_UUID + " FROM " + UserInfoTable.TABLE_NAME +
" WHERE " + UserInfoTable.SERVER_UUID + "=?" +
" GROUP BY " + UserInfoTable.SERVER_UUID;
String sql = "SELECT COUNT(1) as c, " + UserInfoTable.SERVER_UUID + FROM + UserInfoTable.TABLE_NAME +
WHERE + UserInfoTable.SERVER_UUID + "=?" +
GROUP_BY + UserInfoTable.SERVER_UUID;
return new QueryAllStatement<Map<UUID, Integer>>(sql, 100) {
@Override
public Map<UUID, Integer> processResults(ResultSet set) throws SQLException {
@ -109,8 +111,8 @@ public class ServerAggregateQueries {
* @return Map: Lowercase used command - Count of use times.
*/
public static Query<Map<String, Integer>> commandUsageCounts(UUID serverUUID) {
String sql = "SELECT " + CommandUseTable.COMMAND + ", " + CommandUseTable.TIMES_USED + " FROM " + CommandUseTable.TABLE_NAME +
" WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID;
String sql = SELECT + CommandUseTable.COMMAND + ", " + CommandUseTable.TIMES_USED + FROM + CommandUseTable.TABLE_NAME +
WHERE + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID;
return new QueryStatement<Map<String, Integer>>(sql, 5000) {
@Override
@ -132,21 +134,21 @@ public class ServerAggregateQueries {
}
public static Query<Map<String, Integer>> networkGeolocationCounts() {
String subQuery1 = "SELECT " +
String subQuery1 = SELECT +
GeoInfoTable.USER_UUID + ", " +
GeoInfoTable.GEOLOCATION + ", " +
GeoInfoTable.LAST_USED +
" FROM " + GeoInfoTable.TABLE_NAME;
String subQuery2 = "SELECT " +
FROM + GeoInfoTable.TABLE_NAME;
String subQuery2 = SELECT +
GeoInfoTable.USER_UUID + ", " +
"MAX(" + GeoInfoTable.LAST_USED + ") as m" +
" FROM " + GeoInfoTable.TABLE_NAME +
" GROUP BY " + GeoInfoTable.USER_UUID;
String sql = "SELECT " + GeoInfoTable.GEOLOCATION + ", COUNT(1) as c FROM (" +
FROM + GeoInfoTable.TABLE_NAME +
GROUP_BY + GeoInfoTable.USER_UUID;
String sql = SELECT + GeoInfoTable.GEOLOCATION + ", COUNT(1) as c FROM (" +
"(" + subQuery1 + ") AS q1" +
" INNER JOIN (" + subQuery2 + ") AS q2 ON q1.uuid = q2.uuid)" +
" WHERE " + GeoInfoTable.LAST_USED + "=m" +
" GROUP BY " + GeoInfoTable.GEOLOCATION;
" INNER JOIN (" + subQuery2 + ") AS q2 ON q1.uuid = q2.uuid)" +
WHERE + GeoInfoTable.LAST_USED + "=m" +
GROUP_BY + GeoInfoTable.GEOLOCATION;
return new QueryAllStatement<Map<String, Integer>>(sql) {
@Override

View File

@ -27,6 +27,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Queries for {@link com.djrapitops.plan.data.store.objects.Nickname} objects.
*
@ -44,12 +46,12 @@ public class NicknameQueries {
* @return Multimap: Server UUID - (Player UUID - List of nicknames)
*/
public static Query<Map<UUID, Map<UUID, List<Nickname>>>> fetchAllNicknameData() {
String sql = "SELECT " +
String sql = SELECT +
NicknamesTable.NICKNAME + ", " +
NicknamesTable.LAST_USED + ", " +
NicknamesTable.USER_UUID + ", " +
NicknamesTable.SERVER_UUID +
" FROM " + NicknamesTable.TABLE_NAME;
FROM + NicknamesTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, Map<UUID, List<Nickname>>>>(sql, 5000) {
@Override
@ -78,14 +80,15 @@ public class NicknameQueries {
public static Query<Optional<Nickname>> fetchLastSeenNicknameOfPlayer(UUID playerUUID, UUID serverUUID) {
String subQuery = "SELECT MAX(" + NicknamesTable.LAST_USED + ") FROM " + NicknamesTable.TABLE_NAME +
" WHERE " + NicknamesTable.USER_UUID + "=?" +
" AND " + NicknamesTable.SERVER_UUID + "=?" +
" GROUP BY " + NicknamesTable.USER_UUID;
String sql = "SELECT " + NicknamesTable.LAST_USED + ", " +
NicknamesTable.NICKNAME + " FROM " + NicknamesTable.TABLE_NAME +
" WHERE " + NicknamesTable.USER_UUID + "=?" +
" AND " + NicknamesTable.SERVER_UUID + "=?" +
" AND " + NicknamesTable.LAST_USED + "=(" + subQuery + ")";
WHERE + NicknamesTable.USER_UUID + "=?" +
AND + NicknamesTable.SERVER_UUID + "=?" +
GROUP_BY + NicknamesTable.USER_UUID;
String sql = SELECT +
NicknamesTable.LAST_USED + ", " + NicknamesTable.NICKNAME +
FROM + NicknamesTable.TABLE_NAME +
WHERE + NicknamesTable.USER_UUID + "=?" +
AND + NicknamesTable.SERVER_UUID + "=?" +
AND + NicknamesTable.LAST_USED + "=(" + subQuery + ")";
return new QueryStatement<Optional<Nickname>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -110,12 +113,12 @@ public class NicknameQueries {
}
public static Query<List<Nickname>> fetchNicknameDataOfPlayer(UUID playerUUID) {
String sql = "SELECT " +
String sql = SELECT +
NicknamesTable.NICKNAME + ", " +
NicknamesTable.LAST_USED + ", " +
NicknamesTable.SERVER_UUID +
" FROM " + NicknamesTable.TABLE_NAME +
" WHERE (" + NicknamesTable.USER_UUID + "=?)";
FROM + NicknamesTable.TABLE_NAME +
WHERE + NicknamesTable.USER_UUID + "=?";
return new QueryStatement<List<Nickname>>(sql, 5000) {
@ -143,12 +146,12 @@ public class NicknameQueries {
* @return Map: Player UUID - List of nicknames.
*/
public static Query<Map<UUID, List<Nickname>>> fetchAllNicknameDataByPlayerUUIDs() {
String sql = "SELECT " +
String sql = SELECT +
NicknamesTable.NICKNAME + ", " +
NicknamesTable.LAST_USED + ", " +
NicknamesTable.USER_UUID + ", " +
NicknamesTable.SERVER_UUID +
" FROM " + NicknamesTable.TABLE_NAME;
FROM + NicknamesTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, List<Nickname>>>(sql, 5000) {
@Override
public Map<UUID, List<Nickname>> processResults(ResultSet set) throws SQLException {
@ -173,12 +176,12 @@ public class NicknameQueries {
* @return Map: Player UUID - List of Nicknames on the server.
*/
public static Query<Map<UUID, List<Nickname>>> fetchNicknameDataOfServer(UUID serverUUID) {
String sql = "SELECT " +
String sql = SELECT +
NicknamesTable.NICKNAME + ", " +
NicknamesTable.LAST_USED + ", " +
NicknamesTable.USER_UUID + ", " +
NicknamesTable.SERVER_UUID +
" FROM " + NicknamesTable.TABLE_NAME;
FROM + NicknamesTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, List<Nickname>>>(sql, 5000) {
@Override

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.data.container.PlayerKill;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.store.objects.DateHolder;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query;
@ -34,6 +35,8 @@ import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
import static com.djrapitops.plan.db.sql.parsing.Sql.WHERE;
/**
* Queries for {@link com.djrapitops.plan.data.container.Session} objects.
*
@ -69,6 +72,8 @@ public class SessionQueries {
" INNER JOIN " + WorldTimesTable.TABLE_NAME + " ON " + SessionsTable.TABLE_NAME + "." + SessionsTable.ID + "=" + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SESSION_ID +
" INNER JOIN " + WorldTable.TABLE_NAME + " ON " + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.WORLD_ID + "=" + WorldTable.TABLE_NAME + "." + WorldTable.ID;
private static final String ORDER_BY_SESSION_START_DESC = " ORDER BY " + SessionsTable.SESSION_START + " DESC";
/**
* Query the database for Session data without kill, death or world data.
*
@ -122,7 +127,8 @@ public class SessionQueries {
* @return List of sessions
*/
public static Query<List<Session>> fetchAllSessions() {
String sql = SELECT_SESSIONS_STATEMENT + " ORDER BY " + SessionsTable.SESSION_START + " DESC";
String sql = SELECT_SESSIONS_STATEMENT +
ORDER_BY_SESSION_START_DESC;
return new QueryAllStatement<List<Session>>(sql, 50000) {
@Override
public List<Session> processResults(ResultSet set) throws SQLException {
@ -138,8 +144,9 @@ public class SessionQueries {
* @return Map: Player UUID - List of sessions on the server.
*/
public static Query<Map<UUID, List<Session>>> fetchSessionsOfServer(UUID serverUUID) {
String sql = SELECT_SESSIONS_STATEMENT + " WHERE " + SessionsTable.TABLE_NAME + "." + SessionsTable.SERVER_UUID + "=?" +
" ORDER BY " + SessionsTable.SESSION_START + " DESC";
String sql = SELECT_SESSIONS_STATEMENT +
WHERE + SessionsTable.TABLE_NAME + "." + SessionsTable.SERVER_UUID + "=?" +
ORDER_BY_SESSION_START_DESC;
return new QueryStatement<Map<UUID, List<Session>>>(sql, 50000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -161,8 +168,9 @@ public class SessionQueries {
* @return Map: Server UUID - List of sessions on the server.
*/
public static Query<Map<UUID, List<Session>>> fetchSessionsOfPlayer(UUID playerUUID) {
String sql = SELECT_SESSIONS_STATEMENT + " WHERE " + SessionsTable.TABLE_NAME + "." + SessionsTable.USER_UUID + "=?" +
" ORDER BY " + SessionsTable.SESSION_START + " DESC";
String sql = SELECT_SESSIONS_STATEMENT +
WHERE + SessionsTable.TABLE_NAME + "." + SessionsTable.USER_UUID + "=?" +
ORDER_BY_SESSION_START_DESC;
return new QueryStatement<Map<UUID, List<Session>>>(sql, 50000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -179,18 +187,19 @@ public class SessionQueries {
private static List<Session> extractDataFromSessionSelectStatement(ResultSet set) throws SQLException {
// Server UUID - Player UUID - Session Start - Session
Map<UUID, Map<UUID, Map<Long, Session>>> tempSessionMap = new HashMap<>();
Map<UUID, Map<UUID, SortedMap<Long, Session>>> tempSessionMap = new HashMap<>();
// Utilities
String[] gms = GMTimes.getGMKeyArray();
DateHolderRecentComparator dateColderRecentComparator = new DateHolderRecentComparator();
Comparator<DateHolder> dateColderRecentComparator = new DateHolderRecentComparator();
Comparator<Long> longRecentComparator = (one, two) -> Long.compare(two, one); // Descending order, most recent first.
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString(SessionsTable.SERVER_UUID));
Map<UUID, Map<Long, Session>> serverSessions = tempSessionMap.getOrDefault(serverUUID, new HashMap<>());
Map<UUID, SortedMap<Long, Session>> serverSessions = tempSessionMap.getOrDefault(serverUUID, new HashMap<>());
UUID playerUUID = UUID.fromString(set.getString(SessionsTable.USER_UUID));
Map<Long, Session> playerSessions = serverSessions.getOrDefault(playerUUID, new HashMap<>());
SortedMap<Long, Session> playerSessions = serverSessions.getOrDefault(playerUUID, new TreeMap<>(longRecentComparator));
long sessionStart = set.getLong(SessionsTable.SESSION_START);
// id, uuid, serverUUID, sessionStart, sessionEnd, mobKills, deaths, afkTime
@ -236,7 +245,7 @@ public class SessionQueries {
return tempSessionMap.values().stream()
.map(Map::values)
.flatMap(Collection::stream)
.map(Map::values)
.map(SortedMap::values)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

View File

@ -27,6 +27,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Queries for {@link com.djrapitops.plan.data.container.UserInfo} objects.
*
@ -46,13 +48,13 @@ public class UserInfoQueries {
* @return Map: Server UUID - List of user information
*/
public static Query<Map<UUID, List<UserInfo>>> fetchAllUserInformation() {
String sql = "SELECT " +
String sql = SELECT +
UserInfoTable.REGISTERED + ", " +
UserInfoTable.BANNED + ", " +
UserInfoTable.OP + ", " +
UserInfoTable.USER_UUID + ", " +
UserInfoTable.SERVER_UUID +
" FROM " + UserInfoTable.TABLE_NAME;
FROM + UserInfoTable.TABLE_NAME;
return new QueryAllStatement<Map<UUID, List<UserInfo>>>(sql, 50000) {
@Override
@ -83,13 +85,13 @@ public class UserInfoQueries {
* @return List of UserInfo objects, one for each server where the player has played.
*/
public static Query<List<UserInfo>> fetchUserInformationOfUser(UUID playerUUID) {
String sql = "SELECT " +
String sql = SELECT +
UserInfoTable.TABLE_NAME + "." + UserInfoTable.REGISTERED + ", " +
UserInfoTable.BANNED + ", " +
UserInfoTable.OP + ", " +
UserInfoTable.SERVER_UUID +
" FROM " + UserInfoTable.TABLE_NAME +
" WHERE " + UserInfoTable.TABLE_NAME + "." + UserInfoTable.USER_UUID + "=?";
FROM + UserInfoTable.TABLE_NAME +
WHERE + UserInfoTable.TABLE_NAME + "." + UserInfoTable.USER_UUID + "=?";
return new QueryStatement<List<UserInfo>>(sql) {
@Override
@ -113,14 +115,14 @@ public class UserInfoQueries {
}
public static Query<Map<UUID, UserInfo>> fetchUserInformationOfServer(UUID serverUUID) {
String sql = "SELECT " +
String sql = SELECT +
UserInfoTable.REGISTERED + ", " +
UserInfoTable.BANNED + ", " +
UserInfoTable.OP + ", " +
UserInfoTable.USER_UUID + ", " +
UserInfoTable.SERVER_UUID +
" FROM " + UserInfoTable.TABLE_NAME +
" WHERE " + UserInfoTable.SERVER_UUID + "=?";
FROM + UserInfoTable.TABLE_NAME +
WHERE + UserInfoTable.SERVER_UUID + "=?";
return new QueryStatement<Map<UUID, UserInfo>>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {

View File

@ -30,6 +30,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Queries for {@link com.djrapitops.plan.data.time.WorldTimes} objects.
*
@ -37,6 +39,16 @@ import java.util.UUID;
*/
public class WorldTimesQueries {
private static String worldColumn = "world";
private static final String SELECT_WORLD_TIMES_STATEMENT_START = "SELECT " +
"SUM(" + WorldTimesTable.SURVIVAL + ") as survival, " +
"SUM(" + WorldTimesTable.CREATIVE + ") as creative, " +
"SUM(" + WorldTimesTable.ADVENTURE + ") as adventure, " +
"SUM(" + WorldTimesTable.SPECTATOR + ") as spectator, " +
WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as " + worldColumn +
FROM + WorldTimesTable.TABLE_NAME +
" INNER JOIN " + WorldTable.TABLE_NAME + " on " + WorldTable.TABLE_NAME + "." + WorldTable.ID + "=" + WorldTimesTable.WORLD_ID;
private WorldTimesQueries() {
/* Static method class */
}
@ -48,18 +60,9 @@ public class WorldTimesQueries {
* @return WorldTimes with world name - playtime ms information.
*/
public static Query<WorldTimes> fetchServerTotalWorldTimes(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";
String sql = SELECT_WORLD_TIMES_STATEMENT_START +
WHERE + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SERVER_UUID + "=?" +
GROUP_BY + worldColumn;
return new QueryStatement<WorldTimes>(sql, 1000) {
@Override
@ -73,14 +76,9 @@ public class WorldTimesQueries {
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world");
String worldName = set.getString(worldColumn);
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);
GMTimes gmTimes = extractGMTimes(set, gms);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
@ -96,18 +94,9 @@ public class WorldTimesQueries {
* @return WorldTimes with world name - playtime ms information.
*/
public static Query<WorldTimes> fetchPlayerTotalWorldTimes(UUID playerUUID) {
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.USER_UUID + "=?" +
" GROUP BY world";
String sql = SELECT_WORLD_TIMES_STATEMENT_START +
WHERE + WorldTimesTable.USER_UUID + "=?" +
GROUP_BY + worldColumn;
return new QueryStatement<WorldTimes>(sql) {
@Override
@ -121,14 +110,9 @@ public class WorldTimesQueries {
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
while (set.next()) {
String worldName = set.getString("world");
String worldName = set.getString(worldColumn);
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);
GMTimes gmTimes = extractGMTimes(set, gms);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
@ -144,19 +128,9 @@ public class WorldTimesQueries {
* @return Map: Server UUID - WorldTimes total for the server
*/
public static Query<Map<UUID, WorldTimes>> fetchPlayerWorldTimesOnServers(UUID playerUUID) {
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, " +
WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SERVER_UUID + ", " +
worldNameColumn +
" FROM " + WorldTimesTable.TABLE_NAME +
" INNER JOIN " + WorldTable.TABLE_NAME + " on " + worldIDColumn + "=" + WorldTimesTable.WORLD_ID +
" WHERE " + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.USER_UUID + "=?" +
" GROUP BY world, " + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SERVER_UUID;
String sql = SELECT_WORLD_TIMES_STATEMENT_START +
WHERE + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.USER_UUID + "=?" +
GROUP_BY + worldColumn + ", " + WorldTimesTable.TABLE_NAME + "." + WorldTimesTable.SERVER_UUID;
return new QueryStatement<Map<UUID, WorldTimes>>(sql, 1000) {
@Override
@ -172,14 +146,9 @@ public class WorldTimesQueries {
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString(WorldTimesTable.SERVER_UUID));
WorldTimes worldTimes = worldTimesMap.getOrDefault(serverUUID, new WorldTimes(new HashMap<>()));
String worldName = set.getString("world");
String worldName = set.getString(worldColumn);
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);
GMTimes gmTimes = extractGMTimes(set, gms);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
worldTimesMap.put(serverUUID, worldTimes);
@ -188,4 +157,12 @@ public class WorldTimesQueries {
}
};
}
private static GMTimes extractGMTimes(ResultSet set, String[] gms) throws SQLException {
Map<String, Long> gmMap = new HashMap<>();
for (String gameMode : gms) {
gmMap.put(gameMode, set.getLong(gameMode));
}
return new GMTimes(gmMap);
}
}

View File

@ -63,6 +63,15 @@ public class GeoInfoStoreTransaction extends Transaction {
return !hasFailed;
}
public static void setAsFailed() {
hasFailed = true;
}
private GeoInfo createGeoInfo() throws NoSuchAlgorithmException {
String country = geolocationFunction.apply(ip.getHostAddress());
return new GeoInfo(ip, country, time);
}
@Override
protected void performOperations() {
try {
@ -71,12 +80,7 @@ public class GeoInfoStoreTransaction extends Transaction {
execute(DataStoreQueries.storeGeoInfo(playerUUID, geoInfo));
} catch (NoSuchAlgorithmException noSHA256Available) {
// SHA256 not available.
hasFailed = true;
setAsFailed();
}
}
private GeoInfo createGeoInfo() throws NoSuchAlgorithmException {
String country = geolocationFunction.apply(ip.getHostAddress());
return new GeoInfo(ip, country, time);
}
}

View File

@ -117,14 +117,15 @@ public class IPAnonPatch extends Patch {
}
execute(GeoInfoTable.createTableSQL(dbType));
boolean hasUserIdColumn = hasColumn(tempTableName, "user_id");
String identifiers = hasUserIdColumn ? "user_id" : "id, uuid";
String userIdColumn = "user_id";
boolean hasUserIdColumn = hasColumn(tempTableName, userIdColumn);
String identifiers = hasUserIdColumn ? userIdColumn : "id, uuid";
execute("INSERT INTO plan_ips (" +
identifiers + ", ip, ip_hash, geolocation, last_used" +
") SELECT " +
identifiers + ", ip, ip_hash, geolocation, MAX(last_used) FROM plan_ips_temp GROUP BY ip_hash, " +
(hasUserIdColumn ? "user_id" : "uuid") +
(hasUserIdColumn ? userIdColumn : "uuid") +
", ip, geolocation");
dropTable(tempTableName);
}

View File

@ -33,6 +33,7 @@ public abstract class Patch extends OperationCriticalTransaction {
protected final SQLDB db;
protected final DBType dbType;
private static final String ALTER_TABLE = "ALTER TABLE ";
public Patch(SQLDB db) {
setDb(db);
@ -96,7 +97,7 @@ public abstract class Patch extends OperationCriticalTransaction {
}
protected void addColumn(String tableName, String columnInfo) {
execute("ALTER TABLE " + tableName + " ADD " + (dbType.supportsMySQLQueries() ? "" : "COLUMN ") + columnInfo);
execute(ALTER_TABLE + tableName + " ADD " + (dbType.supportsMySQLQueries() ? "" : "COLUMN ") + columnInfo);
}
protected void dropTable(String name) {
@ -110,11 +111,10 @@ public abstract class Patch extends OperationCriticalTransaction {
private String getRenameTableSQL(String from, String to) {
switch (dbType) {
case SQLITE:
return "ALTER TABLE " + from + " RENAME TO " + to;
case H2:
return ALTER_TABLE + from + " RENAME TO " + to;
case MYSQL:
return "RENAME TABLE " + from + " TO " + to;
case H2:
return "ALTER TABLE " + from + " RENAME TO " + to;
default:
throw new IllegalArgumentException("DBType: " + dbType.getName() + " does not have rename table sql");
}
@ -129,7 +129,7 @@ public abstract class Patch extends OperationCriticalTransaction {
for (MySQLSchemaQueries.ForeignKeyConstraint constraint : constraints) {
// Uses information from https://stackoverflow.com/a/34574758
execute("ALTER TABLE " + constraint.getTable() +
execute(ALTER_TABLE + constraint.getTable() +
" DROP FOREIGN KEY " + constraint.getConstraintName());
}
}

View File

@ -23,6 +23,8 @@ import com.djrapitops.plan.db.sql.tables.*;
import java.util.Optional;
import static com.djrapitops.plan.db.sql.parsing.Sql.FROM;
public class Version10Patch extends Patch {
private Integer serverID;
@ -55,13 +57,13 @@ public class Version10Patch extends Patch {
copyTPS();
dropTable("plan_user_info");
dropTable(UserInfoTable.TABLE_NAME);
copyUsers();
dropTable("plan_ips");
dropTable(GeoInfoTable.TABLE_NAME);
execute(GeoInfoTable.createTableSQL(dbType));
dropTable("plan_world_times");
dropTable("plan_worlds");
dropTable(WorldTimesTable.TABLE_NAME);
dropTable(WorldTable.TABLE_NAME);
execute(WorldTable.createTableSQL(dbType));
execute(WorldTimesTable.createTableSQL(dbType));
@ -73,7 +75,7 @@ public class Version10Patch extends Patch {
private void copyUsers() throws DBInitException {
String tempTableName = "temp_users";
renameTable("plan_users", tempTableName);
renameTable(UsersTable.TABLE_NAME, tempTableName);
String tempNickTableName = "temp_nicks";
renameTable(NicknamesTable.TABLE_NAME, tempNickTableName);
@ -83,7 +85,7 @@ public class Version10Patch extends Patch {
execute(UsersTable.createTableSQL(dbType));
execute(NicknamesTable.createTableSQL(dbType));
dropTable("plan_sessions");
dropTable(SessionsTable.TABLE_NAME);
execute(SessionsTable.createTableSQL(dbType));
execute(KillsTable.createTableSQL(dbType));
@ -92,22 +94,22 @@ public class Version10Patch extends Patch {
String statement = "INSERT INTO plan_users " +
"(id, uuid, registered, name)" +
" SELECT id, uuid, registered, name" +
" FROM " + tempTableName;
FROM + tempTableName;
execute(statement);
statement = "INSERT INTO plan_user_info " +
"(user_id, registered, opped, banned, server_id)" +
" SELECT id, registered, opped, banned, '" + serverID + "'" +
" FROM " + tempTableName;
FROM + tempTableName;
execute(statement);
statement = "INSERT INTO plan_nicknames " +
"(user_id, nickname, server_id)" +
" SELECT user_id, nickname, '" + serverID + "'" +
" FROM " + tempNickTableName;
FROM + tempNickTableName;
execute(statement);
statement = "INSERT INTO plan_kills " +
"(killer_id, victim_id, weapon, date, session_id)" +
" SELECT killer_id, victim_id, weapon, date, '0'" +
" FROM " + tempKillsTableName;
FROM + tempKillsTableName;
execute(statement);
}
@ -121,7 +123,7 @@ public class Version10Patch extends Patch {
String statement = "INSERT INTO plan_commandusages " +
"(command, times_used, server_id)" +
" SELECT command, times_used, '" + serverID + "'" +
" FROM " + tempTableName;
FROM + tempTableName;
execute(statement);
dropTable(tempTableName);
@ -138,7 +140,7 @@ public class Version10Patch extends Patch {
String statement = "INSERT INTO plan_tps " +
"(date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, server_id)" +
" SELECT date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, '" + serverID + "'" +
" FROM " + tempTableName;
FROM + tempTableName;
execute(statement);
dropTable(tempTableName);

View File

@ -33,6 +33,8 @@ import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
public class WorldsServerIDPatch extends Patch {
public WorldsServerIDPatch(SQLDB db) {
@ -51,7 +53,7 @@ public class WorldsServerIDPatch extends Patch {
}
private Boolean allValuesHaveServerID(String tableName, String columnName) {
String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=? LIMIT 1";
String sql = "SELECT *" + FROM + tableName + WHERE + columnName + "=? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
@ -87,11 +89,11 @@ public class WorldsServerIDPatch extends Patch {
String sessionServerUUIDColumn = SessionsTable.TABLE_NAME + "." + SessionsTable.SERVER_UUID;
String sql = "SELECT DISTINCT " +
WorldTable.NAME + " FROM " +
WorldTable.NAME + FROM +
WorldTable.TABLE_NAME +
" INNER JOIN " + WorldTimesTable.TABLE_NAME + " on " + worldIDColumn + "=" + WorldTable.TABLE_NAME + "." + WorldTable.ID +
" INNER JOIN " + SessionsTable.TABLE_NAME + " on " + worldSessionIDColumn + "=" + sessionIDColumn +
" WHERE " + sessionServerUUIDColumn + "=?";
WHERE + sessionServerUUIDColumn + "=?";
return query(new QueryStatement<Set<String>>(sql, 1000) {
@Override
@ -125,8 +127,8 @@ public class WorldsServerIDPatch extends Patch {
String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " +
WorldTimesTable.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.WORLD_ID + "=?" +
" AND " + "server_id=?";
WHERE + WorldTimesTable.WORLD_ID + "=?" +
AND + "server_id=?";
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {

View File

@ -22,6 +22,12 @@ public class Sql {
public static final String LONG = "bigint";
public static final String BOOL = "boolean";
public static final String SELECT = "SELECT ";
public static final String FROM = " FROM ";
public static final String WHERE = " WHERE ";
public static final String GROUP_BY = " GROUP BY ";
public static final String AND = " AND ";
private Sql() {
throw new IllegalStateException("Variable Class");
}

View File

@ -33,6 +33,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Table that represents plan_sessions.
* <p>
@ -66,11 +68,11 @@ public class SessionsTable extends Table {
+ SERVER_UUID
+ ") VALUES (?, ?, ?, ?, ?, ?, ?)";
public static final String SELECT_SESSION_ID_STATEMENT = "(SELECT " + TABLE_NAME + "." + ID + " FROM " + TABLE_NAME +
" WHERE " + TABLE_NAME + "." + USER_UUID + "=?" +
" AND " + TABLE_NAME + "." + SERVER_UUID + "=?" +
" AND " + SESSION_START + "=?" +
" AND " + SESSION_END + "=? LIMIT 1)";
public static final String SELECT_SESSION_ID_STATEMENT = "(SELECT " + TABLE_NAME + "." + ID + FROM + TABLE_NAME +
WHERE + TABLE_NAME + "." + USER_UUID + "=?" +
AND + TABLE_NAME + "." + SERVER_UUID + "=?" +
AND + SESSION_START + "=?" +
AND + SESSION_END + "=? LIMIT 1)";
public SessionsTable(SQLDB db) {
super(TABLE_NAME, db);
@ -98,12 +100,12 @@ public class SessionsTable extends Table {
* @return Milliseconds played after given epoch ms on the server. 0 if player or server not found.
*/
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) {
String sql = "SELECT" +
" (SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" +
" FROM " + tableName +
" WHERE " + SESSION_START + ">?" +
" AND " + USER_UUID + "=?" +
" AND " + SERVER_UUID + "=?";
String sql = SELECT +
"(SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" +
FROM + tableName +
WHERE + SESSION_START + ">?" +
AND + USER_UUID + "=?" +
AND + SERVER_UUID + "=?";
return query(new QueryStatement<Long>(sql) {
@Override
@ -131,11 +133,11 @@ public class SessionsTable extends Table {
* @return Milliseconds played after given epoch ms on the server. 0 if server not found.
*/
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) {
String sql = "SELECT" +
" (SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" +
" FROM " + tableName +
" WHERE " + SESSION_START + ">?" +
" AND " + SERVER_UUID + "=?";
String sql = SELECT +
"(SUM(" + SESSION_END + ") - SUM(" + SESSION_START + ")) as playtime" +
FROM + tableName +
WHERE + SESSION_START + ">?" +
AND + SERVER_UUID + "=?";
return query(new QueryStatement<Long>(sql) {
@Override
@ -163,12 +165,12 @@ public class SessionsTable extends Table {
* @return How many sessions player has. 0 if player or server not found.
*/
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) {
String sql = "SELECT" +
" COUNT(*) as logintimes" +
" FROM " + tableName +
" WHERE (" + SESSION_START + " >= ?)" +
" AND " + USER_UUID + "=?" +
" AND " + SERVER_UUID + "=?";
String sql = SELECT +
"COUNT(*) as login_times" +
FROM + tableName +
WHERE + SESSION_START + " >= ?" +
AND + USER_UUID + "=?" +
AND + SERVER_UUID + "=?";
return query(new QueryStatement<Integer>(sql) {
@Override
@ -181,7 +183,7 @@ public class SessionsTable extends Table {
@Override
public Integer processResults(ResultSet set) throws SQLException {
if (set.next()) {
return set.getInt("logintimes");
return set.getInt("login_times");
}
return 0;
}
@ -189,10 +191,10 @@ public class SessionsTable extends Table {
}
public Map<Integer, Integer> getIDServerIDRelation() {
String sql = "SELECT " +
String sql = SELECT +
ID + ", " +
"(SELECT plan_servers.id FROM plan_servers WHERE plan_servers.uuid=" + SERVER_UUID + ") as server_id" +
" FROM " + tableName;
FROM + tableName;
return query(new QueryAllStatement<Map<Integer, Integer>>(sql, 10000) {
@Override

View File

@ -30,7 +30,7 @@ public class DataCache {
@Inject
public DataCache() {
// Dagger requires an Inject constructor.
}
}

View File

@ -93,6 +93,7 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
@Override
public void kickAmount(Map<UUID, Integer> ofUsers) {
// Moved to BaseUser storage methods
}
@Override