SteamWar/SpigotCore
Archiviert
13
0

New Statement system + ErrorHandler #119

Zusammengeführt
Lixfel hat 1 Commits von sqlSpigotCore nach master 2021-10-22 17:49:09 +02:00 zusammengeführt
15 geänderte Dateien mit 169 neuen und 223 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.core;
import de.steamwar.sql.SQL;
import de.steamwar.sql.SWException;
import java.io.ByteArrayOutputStream;
@ -80,6 +81,9 @@ public class ErrorHandler extends Handler {
if(stacktrace.contains("POI data mismatch"))
return;
if(!SQL.connectionStable())
return;
SWException.log(message, stacktrace);
}

Datei anzeigen

@ -19,14 +19,15 @@
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
public class BauweltMember{
private static final SQL.Statement getMember = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?");
private static final SQL.Statement getMembers = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?");
private final int bauweltID;
private final int memberID;
private boolean worldEdit;
@ -56,11 +57,13 @@ public class BauweltMember{
this(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId(), worldEdit, world, true);
}
@Deprecated
public void remove(){
SQL.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", bauweltID, memberID);
members.remove(this);
}
@Deprecated
private void updateDB(){
SQL.update("INSERT INTO BauweltMember (BauweltID, MemberID, WorldEdit, World) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE WorldEdit = VALUES(WorldEdit), World = VALUES(World)",
bauweltID, memberID, worldEdit, world);
@ -74,17 +77,11 @@ public class BauweltMember{
for(BauweltMember member : members)
if(member.memberID == memberID)
return member;
ResultSet member = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", ownerID, memberID);
try {
if(member == null || !member.next()){
return getMember.select(rs -> {
if(!rs.next())
return null;
}
boolean worldEdit = member.getBoolean("WorldEdit");
boolean testblock = member.getBoolean("World");
return new BauweltMember(ownerID, memberID, worldEdit, testblock, false);
} catch (SQLException e) {
throw new SecurityException("Did not get member", e);
}
return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false);
}, ownerID, memberID);
}
public static List<BauweltMember> getMembers(UUID bauweltID){
@ -92,19 +89,12 @@ public class BauweltMember{
}
public static List<BauweltMember> getMembers(int bauweltID){
try{
ResultSet memberlist = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ?", bauweltID);
List<BauweltMember> members = new LinkedList<>();
while(memberlist.next()){
int memberID = memberlist.getInt("MemberID");
boolean worldEdit = memberlist.getBoolean("WorldEdit");
boolean testblock = memberlist.getBoolean("World");
members.add(new BauweltMember(bauweltID, memberID, worldEdit, testblock, false));
}
return getMembers.select(rs -> {
List<BauweltMember> members = new ArrayList<>();
while(rs.next())
members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false));
return members;
}catch(SQLException e){
throw new SecurityException("Did not get members", e);
}
}, bauweltID);
}
public int getBauweltID() {
@ -119,6 +109,7 @@ public class BauweltMember{
return worldEdit;
}
@Deprecated
public void setWorldEdit(boolean worldEdit) {
this.worldEdit = worldEdit;
updateDB();
@ -128,6 +119,7 @@ public class BauweltMember{
return world;
}
@Deprecated
public void setWorld(boolean world) {
this.world = world;
updateDB();

Datei anzeigen

@ -19,19 +19,16 @@
package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
public class CheckedSchematic {
private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
private final String schemName;
private final int schemOwner;
private final int validator;
@ -50,14 +47,17 @@ public class CheckedSchematic {
insertDB();
}
@Deprecated
public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){
this(schemName, schemOwner, validator, startTime, endTime, declineReason, true);
}
@Deprecated
public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){
this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true);
}
@Deprecated
private void insertDB(){
SQL.update("INSERT INTO CheckedSchematic" +
" (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)",
@ -69,23 +69,15 @@ public class CheckedSchematic {
}
public static List<CheckedSchematic> getLastDelined(int schemOwner){
List<CheckedSchematic> lastDeclined = new LinkedList<>();
try{
ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", schemOwner);
while(lastRS.next()){
String schemName = lastRS.getString("SchemName");
int validator = lastRS.getInt("Validator");
Timestamp startTime = lastRS.getTimestamp("StartTime");
Timestamp endTime = lastRS.getTimestamp("EndTime");
String declineReason = lastRS.getString("DeclineReason");
lastDeclined.add(new CheckedSchematic(schemName, schemOwner, validator, startTime, endTime, declineReason, false));
}
}catch(SQLException e){
Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e);
}
return lastDeclined;
return checkHistory.select(rs -> {
List<CheckedSchematic> history = new ArrayList<>();
while(rs.next())
history.add(new CheckedSchematic(rs.getString("SchemName"), schemOwner, rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false));
return history;
}, schemOwner);
}
@Deprecated
public void remove() {
SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName);
}

Datei anzeigen

@ -28,6 +28,8 @@ import java.time.Instant;
public class DownloadSchematic {
private DownloadSchematic(){}
private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)");
private static final String BASE = "https://steamwar.de/download.php?schem=";
public static String getLink(Schematic schem){
@ -40,7 +42,7 @@ public class DownloadSchematic {
digest.reset();
digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes());
String hash = BaseEncoding.base16().encode(digest.digest());
SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getSchemID(), hash);
createLink.update(schem.getSchemID(), hash);
return BASE + hash;
}
}

Datei anzeigen

@ -19,24 +19,21 @@
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Elo {
private Elo(){}
private static final SQL.Statement get = new SQL.Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?");
private static final SQL.Statement set = new SQL.Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)");
public static int getElo(int userId, String gameMode){
ResultSet rs = SQL.select("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?", userId, gameMode);
try{
if(!rs.next())
return 1000;
return rs.getInt("Elo");
}catch(SQLException e){
throw new SecurityException("Could not get elo", e);
}
return get.select(rs -> {
if(rs.next())
return rs.getInt("id");
return 1000;
}, userId, gameMode);
}
public static void setElo(int userId, String gameMode, int elo){
SQL.update("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)", userId, gameMode, elo);
set.update(userId, gameMode, elo);
}
}

Datei anzeigen

@ -19,15 +19,14 @@
package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.logging.Level;
public class Event {
private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Event WHERE EventID = ?");
private final int eventID;
private final String eventName;
private final Timestamp start;
@ -47,16 +46,10 @@ public class Event {
}
public static Event get(int eventID){
ResultSet rs = SQL.select("SELECT * FROM Event WHERE EventID = ?", eventID);
try{
if(!rs.next())
throw new IllegalArgumentException();
return get.select(rs -> {
rs.next();
return new Event(rs);
}catch (SQLException e){
Bukkit.getLogger().log(Level.SEVERE, "Failed to load Event", e);
throw new SecurityException();
}
}, eventID);
}
public int getEventID() {

Datei anzeigen

@ -19,19 +19,21 @@
package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
public class EventFight {
private int eventID;
private int fightID;
private int teamBlue;
private int teamRed;
private int kampfleiter;
private int ergebnis;
private static final SQL.Statement get = new SQL.Statement("SELECT * FROM EventFight WHERE FightID = ?");
private static final SQL.Statement setResult = new SQL.Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?");
private static final SQL.Statement setFight = new SQL.Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?");
private final int eventID;
private final int fightID;
private final int teamBlue;
private final int teamRed;
private final int kampfleiter;
private final int ergebnis;
private EventFight(ResultSet rs) throws SQLException{
this.eventID = rs.getInt("EventID");
@ -43,23 +45,19 @@ public class EventFight {
}
public static EventFight get(int fightID){
ResultSet rs = SQL.select("SELECT * FROM EventFight WHERE FightID = " + fightID);
try{
return get.select(rs -> {
rs.next();
return new EventFight(rs);
}catch (SQLException e){
Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight", e);
}
return null;
}, fightID);
}
public void setErgebnis(int winner){
SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, fightID);
setResult.update(winner, fightID);
}
public void setFight(int fight){
//Fight.FightID, not EventFight.FightID
SQL.update("UPDATE EventFight SET Fight = ? WHERE FightID = ?", fight, fightID);
setFight.update(fight, fightID);
}
public int getTeamBlue() {

Datei anzeigen

@ -21,42 +21,35 @@ package de.steamwar.sql;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
public class Fight {
private Fight(){}
private static final SQL.Statement create = new SQL.Statement("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
private static final SQL.Statement lastId = new SQL.Statement("SELECT LAST_INSERT_ID() AS FightID");
private static final SQL.Statement getReplay = new SQL.Statement("SELECT Replay FROM Fight WHERE FightID = ?");
private static final SQL.Statement setReplay = new SQL.Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?");
public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){
return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition);
}
public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){
SQL.update("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition);
ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID");
try{
if(!rs.next())
throw new SecurityException("No last insert id");
create.update(gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition);
return lastId.select(rs -> {
rs.next();
return rs.getInt("FightID");
} catch (SQLException e) {
throw new SecurityException(e);
}
});
}
public static InputStream getReplay(int fightID) {
ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID);
try {
return getReplay.select(rs -> {
rs.next();
Blob replay = rs.getBlob("Replay");
if(replay == null)
throw new SecurityException("Replay null");
return replay.getBinaryStream();
} catch (SQLException e) {
throw new SecurityException(e);
}
}, fightID);
}
public static void setReplay(int fightID, byte[] data) {
@ -66,6 +59,6 @@ public class Fight {
} catch (SQLException e) {
throw new SecurityException(e);
}
SQL.update("UPDATE Fight SET Replay = ? WHERE FightID = ?", blob, fightID);
setReplay.update(blob, fightID);
}
}

Datei anzeigen

@ -22,14 +22,9 @@ package de.steamwar.sql;
public class FightPlayer {
private FightPlayer(){}
public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){
SQL.update("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)",
fightID, userID, blue ? 1 : 2, kit, kills, isOut);
}
private static final SQL.Statement create = new SQL.Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)");
@Deprecated
public static void create(int fightID, int userID, String kit, int kills, boolean isOut){
SQL.update("INSERT INTO FightPlayer (FightID, UserID, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?)",
fightID, userID, kit, kills, isOut);
public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){
create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut);
}
}

Datei anzeigen

@ -31,6 +31,12 @@ import java.util.Objects;
public class PersonalKit {
private static final SQL.Statement getKits = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?");
private static final SQL.Statement getKit = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?");
private static final SQL.Statement getKitInUse = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = 1");
private static final SQL.Statement delete = new SQL.Statement("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?");
private static final SQL.Statement update = new SQL.Statement("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor, InUse) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(Name), InUse = VALUES(InUse)");
private final int userID;
private String name;
private final String gamemode;
@ -48,26 +54,20 @@ public class PersonalKit {
}
public static List<PersonalKit> get(int userID, String gamemode){
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode);
try {
return getKits.select(rs -> {
List<PersonalKit> list = new ArrayList<>();
while (rs.next())
list.add(new PersonalKit(rs));
return list;
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
}, userID, gamemode);
}
public static PersonalKit get(int userID, String gamemode, String name) {
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
try {
if(!rs.next())
return null;
return new PersonalKit(rs);
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
return getKit.select(rs -> {
if(rs.next())
return new PersonalKit(rs);
return null;
}, userID, gamemode, name);
}
public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){
@ -80,24 +80,17 @@ public class PersonalKit {
}
public static PersonalKit getKitInUse(int userID, String gamemode) {
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = ?", userID, gamemode, true);
try {
List<PersonalKit> list = new ArrayList<>();
return getKitInUse.select(rs -> {
if(!rs.next())
return null;
PersonalKit kit = new PersonalKit(rs);
while (rs.next())
list.add(new PersonalKit(rs));
if(list.size() > 1) {
list.forEach(kit -> {
if(list.indexOf(kit) >= 1) kit.setUse(false);
});
list.removeIf(kit -> !kit.isInUse());
}
if(list.isEmpty()) return null;
return list.get(0);
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
new PersonalKit(rs).setUse(false); //TODO: Violation of integrity, should not be necessary?
return kit;
}, userID, gamemode);
}
@Deprecated
public static boolean nameInUse(int userID, String gamemode, String name) {
ResultSet set = SQL.select("SELECT COUNT(*) AS Count FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
try {
@ -165,7 +158,7 @@ public class PersonalKit {
}
public void delete() {
SQL.update("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
delete.update(userID, gamemode, name);
}
private static String saveInvConfig(String name, ItemStack[] inv) {
@ -176,7 +169,6 @@ public class PersonalKit {
}
private void updateDB() {
SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor, InUse) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(Name), InUse = VALUES(InUse)",
userID, gamemode, name, inventory, armor, inUse);
update.update(userID, gamemode, name, inventory, armor, inUse);
}
}

Datei anzeigen

@ -53,7 +53,7 @@ public class SQL {
private static void connect() {
try {
con = DriverManager.getConnection(url + "?autoreconnect=true", user, password);
con = DriverManager.getConnection(url + "?autoReconnect=true&useServerPrepStmts=true", user, password);
} catch (SQLException e) {
throw new SecurityException("Could not start SQL connection", e);
}
@ -71,14 +71,14 @@ public class SQL {
try {
con.close();
} catch (SQLException e) {
Core.getInstance().getLogger().log(Level.WARNING, "Could not close SQL-Connection", e);
Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e);
}
}
private static void reset(SQLException e) {
Core.getInstance().getLogger().log(Level.WARNING, "SQL Exception thrown", e);
private static void reset() {
close();
connect();
try {
for (Statement statement : Statement.statements) {
statement.init();
@ -87,17 +87,21 @@ public class SQL {
throw new SecurityException("Could not reprepare SQL Statements", ex);
}
}
public static boolean connectionStable() {
try {
return !con.isClosed();
} catch (SQLException e) {
return false;
}
}
static void update(String qry, Object... objects) {
try {
prepare(qry, objects).executeUpdate();
} catch (SQLException e) {
reset(e);
try {
prepare(qry, objects).executeUpdate();
} catch (SQLException ex) {
throw new SecurityException("Could not perform update", ex);
}
reset();
throw new SecurityException("Could not execute SQL statement", e);
}
}
@ -105,12 +109,8 @@ public class SQL {
try {
return prepare(qry, objects).executeQuery();
} catch (SQLException e) {
reset(e);
try {
return prepare(qry, objects).executeQuery();
} catch (SQLException ex) {
throw new SecurityException("Could not perform select", ex);
}
reset();
throw new SecurityException("Could not execute SQL statement", e);
}
}
@ -118,12 +118,8 @@ public class SQL {
try {
return con.createBlob();
} catch (SQLException e) {
reset(e);
try {
return con.createBlob();
} catch (SQLException ex) {
throw new SecurityException("Could not create blob", ex);
}
reset();
throw new SecurityException("Could not execute SQL statement", e);
}
}
@ -147,7 +143,7 @@ public class SQL {
try {
init();
} catch (SQLException e) {
reset(e);
throw new SecurityException("Could not init statement", e);
}
}
@ -173,13 +169,8 @@ public class SQL {
setObjects(objects);
return runnable.run();
} catch (SQLException e) {
reset(e);
try {
setObjects(objects);
return runnable.run();
} catch (SQLException ex) {
throw new SecurityException("Could not execute SQL statement", ex);
}
reset();
throw new SecurityException("Could not execute SQL statement", e);
}
}

Datei anzeigen

@ -25,6 +25,8 @@ import org.bukkit.entity.Player;
public class SWException {
private SWException(){}
private static final SQL.Statement insert = new SQL.Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)");
public static void log(String message, String stacktrace){
message += "\n";
for(Player player : Bukkit.getOnlinePlayers())
@ -36,6 +38,6 @@ public class SWException {
else
server = Bukkit.getWorlds().get(0).getName();
SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", server, message, stacktrace);
insert.update(server, message, stacktrace);
}
}

Datei anzeigen

@ -28,6 +28,10 @@ import java.util.*;
public class SteamwarUser {
private static final SQL.Statement getId = new SQL.Statement("SELECT * FROM UserData WHERE id = ?");
private static final SQL.Statement getUUID = new SQL.Statement("SELECT * FROM UserData WHERE UUID = ?");
private static final SQL.Statement getName = new SQL.Statement("SELECT * FROM UserData WHERE lower(UserName) = ?");
private static final Map<UUID, SteamwarUser> byUUID = new HashMap<>();
private static final Map<String, SteamwarUser> byName = new HashMap<>();
private static final Map<Integer, SteamwarUser> byId = new HashMap<>();
@ -88,35 +92,34 @@ public class SteamwarUser {
return bedrock;
}
private static SteamwarUser fromDB(String statement, Object identifier){
ResultSet rs = SQL.select(statement, identifier);
try {
if(rs.next())
return new SteamwarUser(rs);
} catch (SQLException e) {
throw new SecurityException("SQL Statement failed to get User", e);
}
return null;
}
public static SteamwarUser get(String userName){
SteamwarUser user = byName.get(userName.toLowerCase());
if(user == null)
user = fromDB("SELECT * FROM UserData WHERE lower(UserName) = ?", userName.toLowerCase());
user = getName.select(rs -> {
if(rs.next())
return new SteamwarUser(rs);
return null;
}, userName.toLowerCase());
return user;
}
public static SteamwarUser get(UUID uuid){
SteamwarUser user = byUUID.get(uuid);
if(user == null)
user = fromDB("SELECT * FROM UserData WHERE UUID = ?", uuid.toString());
user = getUUID.select(rs -> {
rs.next();
return new SteamwarUser(rs);
}, uuid.toString());
return user;
}
public static SteamwarUser get(int id) {
SteamwarUser user = byId.get(id);
if(user == null)
user = fromDB("SELECT * FROM UserData WHERE id = ?", id);
user = getId.select(rs -> {
rs.next();
return new SteamwarUser(rs);
}, id);
return user;
}
}

Datei anzeigen

@ -21,10 +21,14 @@ package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
public class Team {
private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Team WHERE TeamID = ?");
private static final SQL.Statement getMembers = new SQL.Statement("SELECT id FROM UserData WHERE Team = ?");
private final int teamId;
private final String teamKuerzel;
private final String teamName;
@ -46,14 +50,10 @@ public class Team {
public static Team get(int id){
if(id == 0)
return pub;
ResultSet rs = SQL.select("SELECT * FROM Team WHERE TeamID = ?", id);
try {
if(!rs.next())
return null;
return get.select(rs -> {
rs.next();
return new Team(rs);
} catch (SQLException e) {
throw new SecurityException("Could not load team", e);
}
}, id);
}
public int getTeamId() {
@ -73,14 +73,11 @@ public class Team {
}
public List<Integer> getMembers(){
try{
ResultSet memberlist = SQL.select("SELECT id FROM UserData WHERE Team = ?", teamId);
List<Integer> members = new LinkedList<>();
while(memberlist.next())
members.add(memberlist.getInt("id"));
return getMembers.select(rs -> {
List<Integer> members = new ArrayList<>();
while(rs.next())
members.add(rs.getInt("id"));
return members;
}catch(SQLException e){
throw new SecurityException("Could not load Teammembers", e);
}
}, teamId);
}
}

Datei anzeigen

@ -19,30 +19,25 @@
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class UserConfig {
private UserConfig() {}
private UserConfig() {
}
private static final SQL.Statement get = new SQL.Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?");
private static final SQL.Statement set = new SQL.Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)");
private static final SQL.Statement delete = new SQL.Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?");
public static String getConfig(UUID player, String config) {
return getConfig(SteamwarUser.get(player).getId(), config);
}
public static String getConfig(int player, String config) {
ResultSet configResult = SQL.select("SELECT * FROM UserConfig WHERE User = ? AND Config = ?", player, config);
try {
if (!configResult.next()) {
return null;
}
return configResult.getString("Value");
} catch (SQLException e) {
throw new SecurityException(e.getMessage(), e);
}
return get.select(rs -> {
if(rs.next())
return rs.getString("Value");
return null;
}, player, config);
}
public static void updatePlayerConfig(UUID uuid, String config, String value) {
@ -54,7 +49,7 @@ public class UserConfig {
removePlayerConfig(id, config);
return;
}
SQL.update("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)", id, config, value);
set.update(id, config, value);
}
public static void removePlayerConfig(UUID uuid, String config) {
@ -62,6 +57,6 @@ public class UserConfig {
}
public static void removePlayerConfig(int id, String config) {
SQL.update("DELETE FROM UserConfig WHERE User = ? AND Config = ?", id, config);
delete.update(id, config);
}
}