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
public void onDisable(){
ErrorLogger.stop();
Statement.close();
Statement.closeAll();
try {
SteamwarDiscordBot.instance().getJda().shutdownNow();
SteamwarDiscordBot.instance().getJda().awaitStatus(JDA.Status.SHUTDOWN);

Datei anzeigen

@ -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<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 = new ArrayList<>();
try {
List<Fight> fights = getPage.select(rs -> {
List<Fight> 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<FightPlayer> fightPlayers = FightPlayer.batchGet(fights.stream().map(f -> f.fightID).collect(Collectors.toSet()));
for(Fight fight : fights) {

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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<Statement> 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();

Datei anzeigen

@ -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<Integer> 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) {