Refactored UsersTable#getPlayerNames to a query

This commit is contained in:
Rsl1122 2019-02-15 21:03:09 +02:00
parent e398a53425
commit bb52f29bd4
4 changed files with 24 additions and 22 deletions

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.api.data.ServerContainer;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
import com.djrapitops.plan.db.access.queries.objects.BaseUserQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.utilities.uuid.UUIDUtility;
import com.djrapitops.plugin.logging.L;
@ -65,7 +66,7 @@ public abstract class CommonAPI implements PlanAPI {
@Override
public Map<UUID, String> getKnownPlayerNames() {
try {
return fetchFromPlanDB().getPlayerNames();
return queryDB(BaseUserQueries.fetchPlayerNames());
} catch (DBOpException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
return new HashMap<>();

View File

@ -163,4 +163,22 @@ public class BaseUserQueries {
}
};
}
public static Query<Map<UUID, String>> fetchPlayerNames() {
String sql = Select.from(UsersTable.TABLE_NAME, UsersTable.USER_UUID, UsersTable.USER_NAME).toString();
return new QueryAllStatement<Map<UUID, String>>(sql, 20000) {
@Override
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString((UsersTable.USER_UUID)));
String name = set.getString((UsersTable.USER_NAME));
names.put(uuid, name);
}
return names;
}
};
}
}

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert;
@ -29,7 +28,9 @@ import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Table that is in charge of storing common player data for all servers.
@ -179,22 +180,4 @@ public class UsersTable extends Table {
}
});
}
public Map<UUID, String> getPlayerNames() {
String sql = Select.from(tableName, USER_UUID, USER_NAME).toString();
return query(new QueryAllStatement<Map<UUID, String>>(sql, 20000) {
@Override
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(USER_UUID));
String name = set.getString(USER_NAME);
names.put(uuid, name);
}
return names;
}
});
}
}

View File

@ -120,7 +120,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public Map<UUID, String> getPlayerNames() {
return usersTable.getPlayerNames();
return db.query(BaseUserQueries.fetchPlayerNames());
}
@Override