Refactored GeoInfoTable#getGeoInfo into a query

This commit is contained in:
Rsl1122 2019-01-31 19:43:20 +02:00
parent 00c2c56717
commit 6478477eec
4 changed files with 45 additions and 31 deletions

View File

@ -16,13 +16,17 @@
*/
package com.djrapitops.plan.db.access.queries;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -37,6 +41,12 @@ public class PlayerFetchQueries {
/* static method class */
}
/**
* Query Player's name by player's UUID.
*
* @param playerUUID UUID of the player.
* @return Optional, Name if found.
*/
public static Query<Optional<String>> playerUserName(UUID playerUUID) {
String sql = "SELECT " + UsersTable.USER_NAME +
" FROM " + UsersTable.TABLE_NAME +
@ -56,4 +66,35 @@ public class PlayerFetchQueries {
}
};
}
/**
* Query Player's GeoInfo by player's UUID.
*
* @param playerUUID UUID of the player.
* @return List of {@link GeoInfo}, empty if none are found.
*/
public static Query<List<GeoInfo>> playerGeoInfo(UUID playerUUID) {
String sql = "SELECT DISTINCT * FROM " + GeoInfoTable.TABLE_NAME +
" WHERE " + GeoInfoTable.USER_UUID + "=?";
return new QueryStatement<List<GeoInfo>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
@Override
public List<GeoInfo> processResults(ResultSet set) throws SQLException {
List<GeoInfo> geoInfo = new ArrayList<>();
while (set.next()) {
String ip = set.getString(GeoInfoTable.IP);
String geolocation = set.getString(GeoInfoTable.GEOLOCATION);
String ipHash = set.getString(GeoInfoTable.IP_HASH);
long lastUsed = set.getLong(GeoInfoTable.LAST_USED);
geoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash));
}
return geoInfo;
}
};
}
}

View File

@ -30,6 +30,7 @@ import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.PlayerAggregateQueries;
import com.djrapitops.plan.db.access.queries.PlayerFetchQueries;
import java.util.HashMap;
import java.util.List;
@ -57,7 +58,7 @@ public class PlayerContainerQuery implements Query<PlayerContainer> {
container.putRawData(PlayerKeys.UUID, uuid);
container.putAll(db.getUsersTable().getUserInformation(uuid));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.getGeoInfoTable().getGeoInfo(uuid));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(PlayerFetchQueries.playerGeoInfo(uuid)));
container.putCachingSupplier(PlayerKeys.PING, () -> db.getPingTable().getPing(uuid));
container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.getNicknamesTable().getNicknameInformation(uuid));
container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> getPerServerData(db));

View File

@ -19,16 +19,12 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.queries.LargeFetchQueries;
import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.utilities.comparators.GeoInfoComparator;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -66,6 +62,7 @@ public class GeoInfoTable extends Table {
+ GEOLOCATION + ", "
+ LAST_USED
+ ") VALUES (?, ?, ?, ?, ?)";
public static final String UPDATE_STATEMENT = "UPDATE " + TABLE_NAME + " SET "
+ LAST_USED + "=?" +
" WHERE " + USER_UUID + "=?" +
@ -87,31 +84,6 @@ public class GeoInfoTable extends Table {
.toString();
}
public List<GeoInfo> getGeoInfo(UUID uuid) {
String sql = "SELECT DISTINCT * FROM " + tableName +
" WHERE " + USER_UUID + "=?";
return query(new QueryStatement<List<GeoInfo>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public List<GeoInfo> processResults(ResultSet set) throws SQLException {
List<GeoInfo> geoInfo = new ArrayList<>();
while (set.next()) {
String ip = set.getString(IP);
String geolocation = set.getString(GEOLOCATION);
String ipHash = set.getString(IP_HASH);
long lastUsed = set.getLong(LAST_USED);
geoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash));
}
return geoInfo;
}
});
}
public List<String> getNetworkGeolocations() {
List<String> geolocations = new ArrayList<>();

View File

@ -520,7 +520,7 @@ public abstract class CommonDBTest {
assertFalse(usersTable.isRegistered(playerUUID));
assertFalse(userInfoTable.isRegistered(playerUUID));
assertTrue(nicknamesTable.getNicknames(playerUUID).isEmpty());
assertTrue(geoInfoTable.getGeoInfo(playerUUID).isEmpty());
assertTrue(db.query(PlayerFetchQueries.playerGeoInfo(playerUUID)).isEmpty());
assertTrue(sessionsTable.getSessions(playerUUID).isEmpty());
}