From 8daedc4e7466d0bd5baada03f4ab1f2e271120e1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 11 Sep 2021 09:26:14 +0200 Subject: [PATCH] Rework to new SQL api Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/BungeeCore.java | 2 +- src/de/steamwar/bungeecore/sql/Fight.java | 14 ++++++------- .../steamwar/bungeecore/sql/FightPlayer.java | 13 ++++++------ src/de/steamwar/bungeecore/sql/Statement.java | 16 +++++++++++--- .../steamwar/bungeecore/sql/SteamwarUser.java | 21 ++++++++++--------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 8b5c3f6..26313b9 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -160,7 +160,7 @@ public class BungeeCore extends Plugin { @Override public void onDisable(){ ErrorLogger.stop(); - Statement.close(); + Statement.closeAll(); try { SteamwarDiscordBot.instance().getJda().shutdownNow(); SteamwarDiscordBot.instance().getJda().awaitStatus(JDA.Status.SHUTDOWN); diff --git a/src/de/steamwar/bungeecore/sql/Fight.java b/src/de/steamwar/bungeecore/sql/Fight.java index 08c24e6..50e16a8 100644 --- a/src/de/steamwar/bungeecore/sql/Fight.java +++ b/src/de/steamwar/bungeecore/sql/Fight.java @@ -11,6 +11,8 @@ import java.util.stream.Collectors; public class Fight { + private static final Statement getPage = new Statement("SELECT FightID, GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition FROM Fight WHERE Replay is not NULL ORDER BY FightID DESC LIMIT ?, ?"); + private final int fightID; private final String gameMode; private final String server; @@ -54,15 +56,13 @@ public class Fight { } public static List getPage(int page, int elementsPerPage) { - ResultSet rs = SQL.select("SELECT FightID, GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition FROM Fight WHERE Replay is not NULL ORDER BY FightID DESC LIMIT ?, ?", page * elementsPerPage, elementsPerPage); - List fights = new ArrayList<>(); - try { + List fights = getPage.select(rs -> { + List f = new ArrayList<>(); while(rs.next()){ - fights.add(new Fight(rs)); + f.add(new Fight(rs)); } - }catch (SQLException e) { - throw new SecurityException("Could not load Fights", e); - } + return f; + }, page * elementsPerPage, elementsPerPage); List fightPlayers = FightPlayer.batchGet(fights.stream().map(f -> f.fightID).collect(Collectors.toSet())); for(Fight fight : fights) { diff --git a/src/de/steamwar/bungeecore/sql/FightPlayer.java b/src/de/steamwar/bungeecore/sql/FightPlayer.java index 760bf81..e2f118d 100644 --- a/src/de/steamwar/bungeecore/sql/FightPlayer.java +++ b/src/de/steamwar/bungeecore/sql/FightPlayer.java @@ -30,14 +30,13 @@ public class FightPlayer { if(fightIds.isEmpty()) return fightPlayers; - ResultSet rs = SQL.select("SELECT * FROM FightPlayer WHERE FightID IN (" + fightIds.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); - try { - while(rs.next()){ + Statement batch = new Statement("SELECT * FROM FightPlayer WHERE FightID IN (" + fightIds.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); + batch.select(rs -> { + while(rs.next()) fightPlayers.add(new FightPlayer(rs)); - } - }catch (SQLException e) { - throw new SecurityException("Could not load FightPlayers", e); - } + return null; + }); + batch.close(); return fightPlayers; } diff --git a/src/de/steamwar/bungeecore/sql/Statement.java b/src/de/steamwar/bungeecore/sql/Statement.java index eac7650..22c900b 100644 --- a/src/de/steamwar/bungeecore/sql/Statement.java +++ b/src/de/steamwar/bungeecore/sql/Statement.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; -public class Statement { +public class Statement implements AutoCloseable { private static final List statements = new ArrayList<>(); private static Connection con; @@ -49,7 +49,7 @@ public class Statement { private static void reset(SQLException e) { BungeeCore.get().getLogger().log(Level.WARNING, "SQL Exception thrown", e); - close(); + closeAll(); connect(url, user, password); try { for (Statement statement : statements) { @@ -60,7 +60,17 @@ public class Statement { } } - public static void close() { + @Override + public void close() { + try { + st.close(); + } catch (SQLException e) { + BungeeCore.get().getLogger().log(Level.INFO, "Could not close statement", e); + } + statements.remove(this); + } + + public static void closeAll() { for (Statement statement : statements) { try { statement.st.close(); diff --git a/src/de/steamwar/bungeecore/sql/SteamwarUser.java b/src/de/steamwar/bungeecore/sql/SteamwarUser.java index 7de4359..5cef03f 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -37,11 +37,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; -import java.util.Scanner; -import java.util.UUID; +import java.util.*; import java.util.logging.Level; +import java.util.stream.Collectors; public class SteamwarUser { @@ -179,13 +177,16 @@ public class SteamwarUser { public static void batchCache(Set ids) { ids.removeIf(usersById::containsKey); + if(ids.isEmpty()) + return; - if(!ids.isEmpty()) { - ResultSet rs = SQL.select("SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); - while(dbInit(rs) != null) { - // nothing to do - } - } + Statement batch = new Statement("SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); + batch.select(rs -> { + while(rs.next()) + new SteamwarUser(rs); + return null; + }); + batch.close(); } public static SteamwarUser get(Long discordId) {