Skip to content

Commit

Permalink
chore: remove usage of multiple DB connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Thatsmusic99 committed Jul 6, 2024
1 parent 2fea90a commit 6e7fcd4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public CompletableFuture<Integer> getTotalChallengesComplete(UUID uuid, boolean
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement("SELECT SUM(count) FROM headsplus_challenges " +
"WHERE user_id = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));

ResultSet set = statement.executeQuery();
if (!set.next()) return -1;
Expand All @@ -92,7 +92,7 @@ public CompletableFuture<Void> completeChallenge(UUID uuid, String challenge, bo
return createConnection(connection -> {
PreparedStatement checkStatement = connection.prepareStatement("SELECT count FROM headsplus_challenges " +
"WHERE user_id = ? AND challenge = ?");
checkStatement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
checkStatement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
checkStatement.setString(2, challenge);

ResultSet set = checkStatement.executeQuery();
Expand All @@ -105,7 +105,7 @@ public CompletableFuture<Void> completeChallenge(UUID uuid, String challenge, bo
"last_completion_time = ? WHERE user_id = ? AND challenge = ?");
}
updateStatement.setLong(1, System.currentTimeMillis());
updateStatement.setInt(2, PlayerSQLManager.get().getUserID(uuid));
updateStatement.setInt(2, PlayerSQLManager.get().getUserID(uuid, connection));
updateStatement.setString(3, challenge);
set.close();
updateStatement.executeUpdate();
Expand All @@ -118,7 +118,7 @@ public CompletableFuture<List<String>> getCompleteChallenges(UUID uuid) {
List<String> challenges = new ArrayList<>();
PreparedStatement checkStatement = connection.prepareStatement("SELECT challenge FROM " +
"headsplus_challenges WHERE user_id = ?");
checkStatement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
checkStatement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));

ResultSet set = checkStatement.executeQuery();
while (set.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class FavouriteHeadsSQLManager extends SQLManager {

Expand Down Expand Up @@ -79,7 +78,7 @@ public CompletableFuture<Void> addHead(UUID uuid, String head) {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO headsplus_fav_heads (user_id, head) VALUES (?, ?)");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, head);

statement.executeUpdate();
Expand All @@ -91,7 +90,7 @@ public CompletableFuture<Void> removeHead(UUID uuid, String head) {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM headsplus_fav_heads WHERE user_id = ? AND head = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, head);

statement.executeUpdate();
Expand All @@ -103,7 +102,7 @@ public CompletableFuture<List<String>> getFavouriteHeads(UUID uuid) {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT head FROM headsplus_fav_heads WHERE user_id = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
ResultSet set = statement.executeQuery();
List<String> heads = new ArrayList<>();
while (set.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public CompletableFuture<List<String>> getPinnedChallenges(UUID uuid) {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT challenge FROM headsplus_pinned_challenges WHERE user_id = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
ResultSet set = statement.executeQuery();
List<String> challenges = new ArrayList<>();
while (set.next()) {
Expand All @@ -92,7 +92,7 @@ public CompletableFuture<Void> addChallenge(UUID uuid, String challenge) {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO headsplus_pinned_challenges (user_id, challenge) VALUES (?, ?)");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, challenge);

statement.executeUpdate();
Expand All @@ -105,7 +105,7 @@ public CompletableFuture<Void> removeChallenge(UUID uuid, String challenge) {
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM headsplus_pinned_challenges " +
"WHERE headsplus_players.id = ? AND challenge = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, challenge);

statement.executeUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,17 @@ public CompletableFuture<Long> getXP(String username, boolean async) {
}

protected int getUserID(UUID uuid) throws ExecutionException, InterruptedException {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement("SELECT id FROM headsplus_players WHERE uuid = " +
"?");
statement.setString(1, uuid.toString());
return createConnection(connection -> getUserID(uuid, connection), false,
"get user ID for " + uuid.toString()).get();
}

ResultSet set = statement.executeQuery();
if (!set.next()) return -1;
return set.getInt("id");
}, false, "get user ID for " + uuid.toString()).get();
protected int getUserID(UUID uuid, Connection connection) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT id FROM headsplus_players WHERE uuid = ?");
statement.setString(1, uuid.toString());

ResultSet set = statement.executeQuery();
if (!set.next()) return -1;
return set.getInt("id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void transferOldData(Connection connection) throws SQLException, Executio
if (mobObj.equals("total")) continue;
ConfigSection defaultSection = ConfigMobs.get().getConfigSection(mobObj + ".default");
String head = "";
if (defaultSection != null && defaultSection.getKeys(false).size() != 0) {
if (defaultSection != null && !defaultSection.getKeys(false).isEmpty()) {
head = defaultSection.getKeys(false).get(0);
}
int total = Integer.parseInt(String.valueOf(huntingObj.get(mobObj)));

statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, "HUNTING");
statement.setString(3, head);
statement.setString(4, "entity=" + mobObj);
Expand All @@ -100,12 +100,12 @@ public void transferOldData(Connection connection) throws SQLException, Executio
if (mobObj.equals("total")) continue;
ConfigSection defaultSection = ConfigMobs.get().getConfigSection(mobObj + ".default");
String head = "";
if (defaultSection != null && defaultSection.getKeys(false).size() != 0) {
if (defaultSection != null && !defaultSection.getKeys(false).isEmpty()) {
head = defaultSection.getKeys(false).get(0);
}
int total = Integer.parseInt(String.valueOf(craftingObj.get(mobObj)));

statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, "CRAFTING");
statement.setString(3, head);
statement.setString(4, "mob=" + mobObj);
Expand All @@ -129,7 +129,7 @@ public CompletableFuture<Integer> getStat(UUID uuid, CollectionType type, boolea
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count), username FROM headsplus_stats, headsplus_players " +
"WHERE user_id = ? AND id = user_id AND collection_type = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, type.name());

ResultSet set = statement.executeQuery();
Expand All @@ -143,7 +143,7 @@ public CompletableFuture<Integer> getStat(UUID uuid, CollectionType type, String
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count), username FROM headsplus_stats, headsplus_players " +
"WHERE user_id = ? AND id = user_id AND collection_type = ? AND head = ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, type.name());
statement.setString(3, head);

Expand All @@ -158,7 +158,7 @@ public CompletableFuture<Integer> getStatMeta(UUID uuid, CollectionType type, St
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count), username FROM headsplus_stats, headsplus_players " +
"WHERE user_id = ? AND id = user_id AND collection_type = ? AND metadata LIKE ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, type.name());
statement.setString(3, "%" + metadata + "%");

Expand All @@ -173,7 +173,7 @@ public CompletableFuture<Integer> getStat(UUID uuid, CollectionType type, String
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count), username FROM headsplus_stats, headsplus_players " +
"WHERE user_id = ? AND id = user_id AND collection_type = ? AND head = ? AND metadata LIKE ?");
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
statement.setInt(1, PlayerSQLManager.get().getUserID(uuid, connection));
statement.setString(2, type.name());
statement.setString(3, head);
statement.setString(4, "%" + metadata + "%");
Expand Down Expand Up @@ -300,7 +300,7 @@ public void addToTotal(UUID uuid, CollectionType type, String head, String metad
createConnection(connection -> {
PreparedStatement checkStatement = connection.prepareStatement("SELECT count FROM headsplus_stats WHERE " +
"user_id = ? AND collection_type = ? AND head = ? AND metadata = ?");
int id = PlayerSQLManager.get().getUserID(uuid);
int id = PlayerSQLManager.get().getUserID(uuid, connection);
checkStatement.setInt(1, id);
checkStatement.setString(2, type.name());
checkStatement.setString(3, head);
Expand Down

0 comments on commit 6e7fcd4

Please sign in to comment.