Refactored UsersTable#getUserInformation to a query

This commit is contained in:
Rsl1122 2019-02-15 11:11:54 +02:00
parent ee74fc328e
commit e398a53425
3 changed files with 33 additions and 49 deletions

View File

@ -16,7 +16,9 @@
*/ */
package com.djrapitops.plan.db.access.queries.containers; package com.djrapitops.plan.db.access.queries.containers;
import com.djrapitops.plan.data.container.BaseUser;
import com.djrapitops.plan.data.container.Session; import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.Key;
import com.djrapitops.plan.data.store.containers.PlayerContainer; import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.data.store.keys.PlayerKeys; import com.djrapitops.plan.data.store.keys.PlayerKeys;
import com.djrapitops.plan.data.store.keys.SessionKeys; import com.djrapitops.plan.data.store.keys.SessionKeys;
@ -25,10 +27,7 @@ import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.objects.GeoInfoQueries; import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.db.access.queries.objects.NicknameQueries;
import com.djrapitops.plan.db.access.queries.objects.PingQueries;
import com.djrapitops.plan.db.access.queries.objects.WorldTimesQueries;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -54,7 +53,12 @@ public class PlayerContainerQuery implements Query<PlayerContainer> {
PlayerContainer container = new PlayerContainer(); PlayerContainer container = new PlayerContainer();
container.putRawData(PlayerKeys.UUID, uuid); container.putRawData(PlayerKeys.UUID, uuid);
container.putAll(db.getUsersTable().getUserInformation(uuid)); Key<BaseUser> baseUserKey = new Key<>(BaseUser.class, "BASE_USER");
container.putSupplier(baseUserKey, () -> db.query(BaseUserQueries.fetchCommonUserInformationOfPlayer(uuid)).orElse(null));
container.putSupplier(PlayerKeys.REGISTERED, () -> container.getValue(baseUserKey).map(BaseUser::getRegistered).orElse(null));
container.putSupplier(PlayerKeys.NAME, () -> container.getValue(baseUserKey).map(BaseUser::getName).orElse(null));
container.putSupplier(PlayerKeys.KICK_COUNT, () -> container.getValue(baseUserKey).map(BaseUser::getTimesKicked).orElse(null));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(GeoInfoQueries.fetchPlayerGeoInformation(uuid))); container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(GeoInfoQueries.fetchPlayerGeoInformation(uuid)));
container.putCachingSupplier(PlayerKeys.PING, () -> db.query(PingQueries.fetchPingDataOfPlayer(uuid))); container.putCachingSupplier(PlayerKeys.PING, () -> db.query(PingQueries.fetchPingDataOfPlayer(uuid)));
container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.query(NicknameQueries.fetchNicknameDataOfPlayer(uuid))); container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.query(NicknameQueries.fetchNicknameDataOfPlayer(uuid)));

View File

@ -83,6 +83,30 @@ public class BaseUserQueries {
}; };
} }
public static Query<Optional<BaseUser>> fetchCommonUserInformationOfPlayer(UUID playerUUID) {
String sql = Select.all(UsersTable.TABLE_NAME).where(UsersTable.USER_UUID + "=?").toString();
return new QueryStatement<Optional<BaseUser>>(sql, 20000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
@Override
public Optional<BaseUser> processResults(ResultSet set) throws SQLException {
if (set.next()) {
UUID playerUUID = UUID.fromString(set.getString(UsersTable.USER_UUID));
String name = set.getString(UsersTable.USER_NAME);
long registered = set.getLong(UsersTable.REGISTERED);
int kicked = set.getInt(UsersTable.TIMES_KICKED);
return Optional.of(new BaseUser(playerUUID, name, registered, kicked));
}
return Optional.empty();
}
};
}
public static Query<Collection<BaseUser>> fetchServerBaseUsers(UUID serverUUID) { public static Query<Collection<BaseUser>> fetchServerBaseUsers(UUID serverUUID) {
String sql = "SELECT " + String sql = "SELECT " +
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " + UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " +

View File

@ -16,10 +16,6 @@
*/ */
package com.djrapitops.plan.db.sql.tables; package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.data.store.Key;
import com.djrapitops.plan.data.store.containers.DataContainer;
import com.djrapitops.plan.data.store.containers.SupplierDataContainer;
import com.djrapitops.plan.data.store.keys.PlayerKeys;
import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement; import com.djrapitops.plan.db.access.ExecStatement;
@ -201,44 +197,4 @@ public class UsersTable extends Table {
} }
}); });
} }
public DataContainer getUserInformation(UUID uuid) {
Key<DataContainer> key = new Key<>(DataContainer.class, "plan_users_data");
DataContainer returnValue = new SupplierDataContainer();
returnValue.putSupplier(key, () -> getUserInformationDataContainer(uuid));
returnValue.putRawData(PlayerKeys.UUID, uuid);
returnValue.putSupplier(PlayerKeys.REGISTERED, () -> returnValue.getUnsafe(key).getValue(PlayerKeys.REGISTERED).orElse(null));
returnValue.putSupplier(PlayerKeys.NAME, () -> returnValue.getUnsafe(key).getValue(PlayerKeys.NAME).orElse(null));
returnValue.putSupplier(PlayerKeys.KICK_COUNT, () -> returnValue.getUnsafe(key).getValue(PlayerKeys.KICK_COUNT).orElse(null));
return returnValue;
}
private DataContainer getUserInformationDataContainer(UUID uuid) {
String sql = "SELECT * FROM " + tableName + " WHERE " + USER_UUID + "=?";
return query(new QueryStatement<DataContainer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public DataContainer processResults(ResultSet set) throws SQLException {
DataContainer container = new SupplierDataContainer();
if (set.next()) {
long registered = set.getLong(REGISTERED);
String name = set.getString(USER_NAME);
int timesKicked = set.getInt(TIMES_KICKED);
container.putRawData(PlayerKeys.REGISTERED, registered);
container.putRawData(PlayerKeys.NAME, name);
container.putRawData(PlayerKeys.KICK_COUNT, timesKicked);
}
return container;
}
});
}
} }