Change SQL queries a bit to be more efficient

This commit is contained in:
AlexTheCoder 2016-11-23 18:35:27 -05:00 committed by cnr
parent a2fb0bb491
commit 7b23c1bdd2
3 changed files with 33 additions and 9 deletions

View File

@ -3,6 +3,8 @@ package mineplex.core.communities;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
@ -21,6 +23,7 @@ import mineplex.core.communities.storage.CommunityRepository;
public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{
private final CommunityRepository _repo;
private final List<Community> _communityBrowser;
private final Map<Integer, Community> _loadedCommunities;
public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager)
@ -29,7 +32,8 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
_repo = new CommunityRepository(plugin);
_loadedCommunities = new HashMap<>();
_repo.loadCommunities(_loadedCommunities);
_communityBrowser = new LinkedList<>();
_repo.loadCommunities(_communityBrowser);
}
public Community getLoadedCommunity(String name)
@ -83,7 +87,9 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT communityId, communityRole FROM communityMembers WHERE accountId=" + accountId + ";";
String query = "SELECT c.*, cm.communityId, cm.communityRole, cs.settingId, cs.settingValue FROM communityMembers cm INNER JOIN communities c ON c.id=cm.communityId INNER JOIN communitySettings cs ON cs.communityId=c.id;";
return query;
//return "SELECT communityId, communityRole FROM communityMembers WHERE accountId=" + accountId + ";";//BEING REPLACED WITH AN INNER JOIN TO LOAD THE COMMUNITY AS WELL
}
@Override

View File

@ -5,7 +5,7 @@ import mineplex.core.common.util.Callback;
public enum CommunitySetting
{
CHAT_MESSAGE_COLOR(new Callback<Pair<String, Community>>()
CHAT_MESSAGE_COLOR(1, new Callback<Pair<String, Community>>()
{
public void run(Pair<String, Community> pair)
{
@ -17,7 +17,7 @@ public enum CommunitySetting
private Callback<Pair<String, Community>> _parser;
private CommunitySetting(Callback<Pair<String, Community>> parseValue)
private CommunitySetting(int id, Callback<Pair<String, Community>> parseValue)
{
}

View File

@ -1,9 +1,12 @@
package mineplex.core.communities.storage;
import java.util.Map;
import java.util.List;
import java.util.Random;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.collect.Lists;
import mineplex.core.communities.Community;
import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.database.DBPool;
@ -12,7 +15,7 @@ import mineplex.serverdata.database.column.ColumnVarChar;
public class CommunityRepository extends MinecraftRepository
{
private static final String GET_ALL_COMMUNITIES_FOR_BROWSER = "SELECT * FROM communities WHERE memberTotal > 4;";
private static final String GET_ALL_COMMUNITIES_FOR_BROWSER = "SELECT * FROM communities WHERE (SELECT COUNT(id) AS idTotal FROM communityMembers WHERE communityId=communities.id) > 5;";
private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;";
private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=?;";
private static final String GET_COMMUNITY_MEMBERS = "SELECT accountId, communityRole FROM communityMembers WHERE communityId=?;";
@ -52,14 +55,29 @@ public class CommunityRepository extends MinecraftRepository
return community;
}
public void loadCommunities(final Map<Integer, Community> communityMap)
public void loadCommunities(final List<Community> communityList)
{
executeQuery(GET_ALL_COMMUNITIES, resultSet ->
executeQuery(GET_ALL_COMMUNITIES_FOR_BROWSER, resultSet ->
{
List<Community> resultant = Lists.newArrayList();
while (resultSet.next())
{
Community c = new Community(resultSet.getInt("id"), "Name");
resultant.add(c);
}
Random rand = new Random();
List<Community> sortedResultant = Lists.newLinkedList();
while (!resultant.isEmpty())
{
if (rand.nextBoolean())
{
sortedResultant.add(resultant.remove(0));
}
}
communityList.clear();
communityList.addAll(sortedResultant);
});
}