CommonDB #46

Zusammengeführt
Lixfel hat 8 Commits von commonDB2 nach master 2023-02-23 08:05:56 +01:00 zusammengeführt
Nur Änderungen aus Commit 3e1c4e9059 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.sql;
import de.steamwar.sql.internal.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -42,38 +43,97 @@ public class Team {
private static final Table<Team> table = new Table<>(Team.class);
private static final SelectStatement<Team> byId = table.select(Table.PRIMARY);
private static final SelectStatement<Team> byName = new SelectStatement<>(table, "SELECT * FROM Team WHERE (lower(TeamName) = ? OR lower(TeamKuerzel) = ?) AND NOT TeamDeleted");
private static final SelectStatement<Team> all = table.selectFields("TeamDeleted");
private static final Statement insert = table.insertFields("TeamKuerzel", "TeamName");
private static final Statement update = table.update(Table.PRIMARY, "TeamKuerzel", "TeamName", "TeamColor", "Address", "Port");
private static final Statement delete = table.update(Table.PRIMARY, "TeamDeleted");
private static final Statement getSize = new Statement("SELECT COUNT(id) FROM UserData WHERE Team = ?");
@Field(keys = {Table.PRIMARY})
@Field(keys = {Table.PRIMARY}, autoincrement = true)
@Getter
private final int teamId;
@Field
@Getter
private final String teamKuerzel;
private String teamKuerzel;
@Field
@Getter
private final String teamName;
private String teamName;
@Field(def = "'8'")
@Getter
private final String teamColor;
private String teamColor;
@Field(nullable = true)
@Getter
private String address;
@Field(def = "'25565'")
@Getter
private int port;
@Field(def = "0")
private boolean teamDeleted;
public static void create(String kuerzel, String name){
insert.update(kuerzel, name);
}
public static Team get(int id) {
return teamCache.computeIfAbsent(id, byId::select);
//TODOs: BungeeCore flush caches
//TODOs: Team public removal test
//TODOs: ArraySelect test
}
public static Team get(String name){
// No cache lookup due to low frequency use
return byName.select(name, name);
}
public static List<Team> getAll(){
clear();
List<Team> teams = all.listSelect(false);
teams.forEach(team -> teamCache.put(team.getTeamId(), team));
return teams;
}
public List<Integer> getMembers(){
return SteamwarUser.getTeam(teamId).stream().map(SteamwarUser::getId).collect(Collectors.toList());
}
public int size(){
return getSize.select(rs -> {
rs.next();
return rs.getInt("COUNT(id)");
}, teamId);
}
public void disband(SteamwarUser user){
user.setLeader(false);
delete.update(true, teamId);
teamCache.remove(teamId);
TeamTeilnahme.deleteFuture(teamId);
}
public void setTeamKuerzel(String teamKuerzel) {
this.teamKuerzel = teamKuerzel;
updateDB();
}
public void setTeamName(String teamName) {
this.teamName = teamName;
updateDB();
}
public void setTeamColor(String teamColor) {
this.teamColor = teamColor;
updateDB();
}
public void setAddress(String address) {
this.address = address;
updateDB();
}
public void setPort(int port) {
this.port = port;
updateDB();
}
private void updateDB(){
update.update(teamKuerzel, teamName, teamColor, address, port, teamId);
}
}