Archiviert
1
0

Move BungeeCore-SQL to use CommonDB Statement backend

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2023-01-15 22:25:04 +01:00
Ursprung 9af9e84723
Commit 990016aac7
27 geänderte Dateien mit 161 neuen und 201 gelöschten Zeilen

Datei anzeigen

@ -34,6 +34,7 @@ import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.SWTypeMapperCreator;
import de.steamwar.command.TabCompletionCache;
import de.steamwar.command.TypeMapper;
import de.steamwar.sql.internal.Statement;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.CommandSender;

Datei anzeigen

@ -2,11 +2,10 @@ package de.steamwar.bungeecore.commands;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.Message;
import de.steamwar.bungeecore.sql.Statement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.sql.SteamwarUser;
import de.steamwar.command.SWCommand;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.io.*;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.ArenaMode;
import java.sql.ResultSet;

Datei anzeigen

@ -1,5 +1,6 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.ArenaMode;
import java.sql.ResultSet;

Datei anzeigen

@ -1,5 +1,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.sql.ResultSet;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
public class Mod {
private static final Statement get = new Statement("SELECT * FROM Mods WHERE ModName = ? AND Platform = ?");

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.listeners.PollSystem;
import java.sql.ResultSet;

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
public class SWException {
private SWException(){}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import static de.steamwar.bungeecore.sql.UserElo.ELO_DEFAULT;
public class SchemElo {

Datei anzeigen

@ -19,19 +19,16 @@
package de.steamwar.bungeecore.sql;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import de.steamwar.sql.internal.Statement;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
public class SchematicNode {
private static final Statement createNode = new Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)");

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.Timestamp;
public class Session {

Datei anzeigen

@ -1,194 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.sql;
import de.steamwar.bungeecore.BungeeCore;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Statement implements AutoCloseable {
private static final Logger logger = BungeeCore.get().getLogger();
private static final List<Statement> statements = new ArrayList<>();
private static final Deque<Connection> connections = new ArrayDeque<>();
private static final String URL;
private static final String USER;
private static final String PASSWORD;
static {
File file = new File(System.getProperty("user.home"), "MySQL.yml");
Configuration config;
try {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
} catch (IOException e) {
ProxyServer.getInstance().stop();
throw new SecurityException("Could not load SQL connection", e);
}
URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE");
USER = config.getString("USER");
PASSWORD = config.getString("PASSWORD");
}
public static void closeAll() {
synchronized (connections) {
for(Connection connection : connections) {
try {
synchronized (statements) {
statements.forEach(statement -> statement.close(connection));
}
connection.close();
} catch (SQLException e) {
logger.log(Level.INFO, "Could not close SQL connection", e);
}
}
connections.clear();
}
}
private final String sql;
private final Map<Connection, PreparedStatement> cachedStatements = new HashMap<>();
public Statement(String sql) {
this.sql = sql;
synchronized (statements) {
statements.add(this);
}
}
public <T> T select(ResultSetUser<T> user, Object... objects) {
return withConnection(st -> {
ResultSet rs = st.executeQuery();
T result = user.use(rs);
rs.close();
return result;
}, objects);
}
public void update(Object... objects) {
withConnection(PreparedStatement::executeUpdate, objects);
}
private <T> T withConnection(SQLRunnable<T> runnable, Object... objects) {
Connection connection = aquireConnection();
T result;
try {
result = tryWithConnection(connection, runnable, objects);
} catch (SQLException e) {
closeConnection(connection);
connection = aquireConnection();
try {
result = tryWithConnection(connection, runnable, objects);
} catch (SQLException ex) {
closeConnection(connection);
throw new SecurityException("Could not execute statement", ex);
}
}
synchronized (connections) {
connections.addLast(connection);
}
return result;
}
private <T> T tryWithConnection(Connection connection, SQLRunnable<T> runnable, Object... objects) throws SQLException {
PreparedStatement st = cachedStatements.get(connection);
if(st == null) {
st = connection.prepareStatement(sql);
cachedStatements.put(connection, st);
}
for (int i = 0; i < objects.length; i++) {
st.setObject(i + 1, objects[i]);
}
return runnable.run(st);
}
@Override
public void close() {
cachedStatements.values().forEach(st -> closeStatement(st, false));
cachedStatements.clear();
synchronized (statements) {
statements.remove(this);
}
}
private void close(Connection connection) {
PreparedStatement st = cachedStatements.remove(connection);
if(st != null)
closeStatement(st, true);
}
private static Connection aquireConnection() {
synchronized (connections) {
if(!connections.isEmpty())
return connections.pop();
}
try {
return DriverManager.getConnection(URL + "?autoReconnect=true&useServerPrepStmts=true", USER, PASSWORD);
} catch (SQLException e) {
throw new SecurityException("Could not open connection", e);
}
}
private static void closeConnection(Connection connection) {
synchronized (statements) {
for (Statement statement : statements) {
statement.close(connection);
}
}
try {
connection.close();
} catch (SQLException e) {
logger.log(Level.INFO, "Could not close connection", e);
}
}
private static void closeStatement(PreparedStatement st, boolean silent) {
try {
st.close();
} catch (SQLException e) {
if(!silent)
logger.log(Level.INFO, "Could not close statement", e);
}
}
public interface ResultSetUser<T> {
T use(ResultSet rs) throws SQLException;
}
private interface SQLRunnable<T> {
T run(PreparedStatement st) throws SQLException;
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import com.google.gson.JsonParser;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.Message;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.ServerStarter;
import de.steamwar.bungeecore.SubserverSystem;

Datei anzeigen

@ -20,6 +20,8 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
public class UserConfig {
private UserConfig() {}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bungeecore.sql;
import de.steamwar.sql.internal.Statement;
import de.steamwar.bungeecore.ArenaMode;
import de.steamwar.bungeecore.Message;
import net.md_5.bungee.api.connection.ProxiedPlayer;

Datei anzeigen

@ -0,0 +1,37 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.sql.internal.SQLConfig;
import java.util.logging.Logger;
public class SQLConfigImpl implements SQLConfig {
@Override
public Logger getLogger() {
return BungeeCore.get().getLogger();
}
@Override
public int maxConnections() {
return 4;
}
}

Datei anzeigen

@ -0,0 +1,86 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import de.steamwar.bungeecore.BungeeCore;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SQLWrapperImpl implements SQLWrapper {
@Override
public void loadSchemTypes(List<SchematicType> tmpTypes, Map<String, SchematicType> tmpFromDB) {
File folder = new File(BungeeCore.get().getDataFolder().getParentFile(), "FightSystem");
if(folder.exists()) {
for(File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) {
Configuration config;
try {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
} catch (IOException e) {
throw new SecurityException("Could not load SchematicTypes", e);
}
if(!config.contains("Schematic"))
continue;
String type = config.getString("Schematic.Type");
assert type != null;
String shortcut = config.getString("Schematic.Shortcut");
if(tmpFromDB.containsKey(type.toLowerCase()))
continue;
SchematicType checktype = null;
String material = config.getString("Schematic.Material");
if(!config.getStringList("CheckQuestions").isEmpty()) {
checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material);
tmpTypes.add(checktype);
tmpFromDB.put(checktype.toDB(), checktype);
}
SchematicType current = new SchematicType(type, shortcut, !config.contains("Server") ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material);
tmpTypes.add(current);
tmpFromDB.put(type.toLowerCase(), current);
}
}
}
@Override
public void additionalExceptionMetadata(StringBuilder builder) {
builder.append("\nServers: ");
for(Map.Entry<String, ServerInfo> server : ProxyServer.getInstance().getServers().entrySet()) {
builder.append(server.getKey()).append("(");
for(ProxiedPlayer player : server.getValue().getPlayers()) {
builder.append(player.getName()).append(" ");
}
builder.append(")");
}
}
}