Archiviert
1
0

Rework to new SQL api

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2021-09-11 09:26:14 +02:00
Ursprung ef471c9ff9
Commit 8daedc4e74
5 geänderte Dateien mit 38 neuen und 28 gelöschten Zeilen

Datei anzeigen

@ -160,7 +160,7 @@ public class BungeeCore extends Plugin {
@Override @Override
public void onDisable(){ public void onDisable(){
ErrorLogger.stop(); ErrorLogger.stop();
Statement.close(); Statement.closeAll();
try { try {
SteamwarDiscordBot.instance().getJda().shutdownNow(); SteamwarDiscordBot.instance().getJda().shutdownNow();
SteamwarDiscordBot.instance().getJda().awaitStatus(JDA.Status.SHUTDOWN); SteamwarDiscordBot.instance().getJda().awaitStatus(JDA.Status.SHUTDOWN);

Datei anzeigen

@ -11,6 +11,8 @@ import java.util.stream.Collectors;
public class Fight { 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 int fightID;
private final String gameMode; private final String gameMode;
private final String server; private final String server;
@ -54,15 +56,13 @@ public class Fight {
} }
public static List<Fight> getPage(int page, int elementsPerPage) { public static List<Fight> 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<Fight> fights = getPage.select(rs -> {
List<Fight> fights = new ArrayList<>(); List<Fight> f = new ArrayList<>();
try {
while(rs.next()){ while(rs.next()){
fights.add(new Fight(rs)); f.add(new Fight(rs));
} }
}catch (SQLException e) { return f;
throw new SecurityException("Could not load Fights", e); }, page * elementsPerPage, elementsPerPage);
}
List<FightPlayer> fightPlayers = FightPlayer.batchGet(fights.stream().map(f -> f.fightID).collect(Collectors.toSet())); List<FightPlayer> fightPlayers = FightPlayer.batchGet(fights.stream().map(f -> f.fightID).collect(Collectors.toSet()));
for(Fight fight : fights) { for(Fight fight : fights) {

Datei anzeigen

@ -30,14 +30,13 @@ public class FightPlayer {
if(fightIds.isEmpty()) if(fightIds.isEmpty())
return fightPlayers; return fightPlayers;
ResultSet rs = SQL.select("SELECT * FROM FightPlayer WHERE FightID IN (" + fightIds.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); Statement batch = new Statement("SELECT * FROM FightPlayer WHERE FightID IN (" + fightIds.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")");
try { batch.select(rs -> {
while(rs.next()){ while(rs.next())
fightPlayers.add(new FightPlayer(rs)); fightPlayers.add(new FightPlayer(rs));
} return null;
}catch (SQLException e) { });
throw new SecurityException("Could not load FightPlayers", e); batch.close();
}
return fightPlayers; return fightPlayers;
} }

Datei anzeigen

@ -27,7 +27,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
public class Statement { public class Statement implements AutoCloseable {
private static final List<Statement> statements = new ArrayList<>(); private static final List<Statement> statements = new ArrayList<>();
private static Connection con; private static Connection con;
@ -49,7 +49,7 @@ public class Statement {
private static void reset(SQLException e) { private static void reset(SQLException e) {
BungeeCore.get().getLogger().log(Level.WARNING, "SQL Exception thrown", e); BungeeCore.get().getLogger().log(Level.WARNING, "SQL Exception thrown", e);
close(); closeAll();
connect(url, user, password); connect(url, user, password);
try { try {
for (Statement statement : statements) { 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) { for (Statement statement : statements) {
try { try {
statement.st.close(); statement.st.close();

Datei anzeigen

@ -37,11 +37,9 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.Scanner;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors;
public class SteamwarUser { public class SteamwarUser {
@ -179,13 +177,16 @@ public class SteamwarUser {
public static void batchCache(Set<Integer> ids) { public static void batchCache(Set<Integer> ids) {
ids.removeIf(usersById::containsKey); ids.removeIf(usersById::containsKey);
if(ids.isEmpty())
return;
if(!ids.isEmpty()) { Statement batch = new Statement("SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")");
ResultSet rs = SQL.select("SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")"); batch.select(rs -> {
while(dbInit(rs) != null) { while(rs.next())
// nothing to do new SteamwarUser(rs);
} return null;
} });
batch.close();
} }
public static SteamwarUser get(Long discordId) { public static SteamwarUser get(Long discordId) {