From cbc26862354d9803d548bec4c9942e7857b8e6c7 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 21 Nov 2021 08:12:22 +0100 Subject: [PATCH] Fix merge + remove direct sql --- .../src/de/steamwar/core/Core.java | 4 +- .../src/de/steamwar/core/ErrorHandler.java | 4 +- .../src/de/steamwar/sql/BauweltMember.java | 4 +- .../src/de/steamwar/sql/CheckedSchematic.java | 8 +- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 4 +- .../src/de/steamwar/sql/Event.java | 2 +- .../src/de/steamwar/sql/EventFight.java | 6 +- .../src/de/steamwar/sql/Fight.java | 8 +- .../src/de/steamwar/sql/FightPlayer.java | 2 +- .../src/de/steamwar/sql/NodeDownload.java | 2 +- .../src/de/steamwar/sql/NodeMember.java | 10 +- .../src/de/steamwar/sql/PersonalKit.java | 13 +- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 201 ------------------ .../src/de/steamwar/sql/SWException.java | 2 +- .../src/de/steamwar/sql/Schematic.java | 2 +- .../src/de/steamwar/sql/SchematicNode.java | 67 +++--- .../src/de/steamwar/sql/Statement.java | 159 ++++++++++++++ .../src/de/steamwar/sql/SteamwarUser.java | 6 +- SpigotCore_Main/src/de/steamwar/sql/Team.java | 4 +- .../src/de/steamwar/sql/TeamTeilnahme.java | 6 +- .../src/de/steamwar/sql/UserConfig.java | 6 +- 21 files changed, 236 insertions(+), 284 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/SQL.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Statement.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 4c28502..0dcb0c1 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -25,7 +25,7 @@ import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; import de.steamwar.core.events.WorldLoadEvent; -import de.steamwar.sql.SQL; +import de.steamwar.sql.Statement; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -85,7 +85,7 @@ public class Core extends JavaPlugin{ public void onDisable() { ChunkListener.protocol.close(); errorHandler.unregister(); - SQL.close(); + Statement.close(); } public static Core getInstance() { diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index 051efac..7853144 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -19,8 +19,8 @@ package de.steamwar.core; -import de.steamwar.sql.SQL; import de.steamwar.sql.SWException; +import de.steamwar.sql.Statement; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -83,7 +83,7 @@ public class ErrorHandler extends Handler { if(stacktrace.contains("POI data mismatch")) return; - if(!SQL.connectionStable()) + if(!Statement.connectionStable()) return; SWException.log(message, stacktrace); diff --git a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java index 31a7ed0..305aac5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -25,8 +25,8 @@ 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 static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); + private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?"); private final int bauweltID; private final int memberID; diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index d80b518..e130c70 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -26,10 +26,8 @@ import java.util.UUID; public class CheckedSchematic { - private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); - private static final SQL.Statement nodeHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - private static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?, ?)"); - private static final SQL.Statement setNodeNull = new SQL.Statement("UPDATE CheckedSchematic SET NodeId = NULL WHERE NodeId = ?"); + private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); + private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); private final Integer node; private final int validator; @@ -57,7 +55,7 @@ public class CheckedSchematic { Timestamp startTime = rs.getTimestamp("StartTime"); Timestamp endTime = rs.getTimestamp("EndTime"); String declineReason = rs.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); + lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason)); } return lastDeclined; }, node); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index f077199..6fa6504 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -22,8 +22,8 @@ package de.steamwar.sql; 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)"); + private static final Statement get = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); + private static final Statement set = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); public static int getElo(int userId, String gameMode){ return get.select(rs -> { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Event.java b/SpigotCore_Main/src/de/steamwar/sql/Event.java index a447f3c..12ab5dd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Event.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Event.java @@ -25,7 +25,7 @@ import java.sql.Timestamp; public class Event { - private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Event WHERE EventID = ?"); + private static final Statement get = new Statement("SELECT * FROM Event WHERE EventID = ?"); private final int eventID; private final String eventName; diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 0487855..684cd7a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -24,9 +24,9 @@ import java.sql.SQLException; public class EventFight { - 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 static final Statement get = new Statement("SELECT * FROM EventFight WHERE FightID = ?"); + private static final Statement setResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?"); + private static final Statement setFight = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?"); private final int eventID; private final int fightID; diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index 584d17e..3f9e2cd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -26,10 +26,10 @@ import java.util.function.Consumer; 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 = ?"); + private static final Statement create = new Statement("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + private static final Statement lastId = new Statement("SELECT LAST_INSERT_ID() AS FightID"); + private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?"); + private static final Statement setReplay = new 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); diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java index 69e6399..4814560 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -22,7 +22,7 @@ package de.steamwar.sql; public class FightPlayer { private FightPlayer(){} - private static final SQL.Statement create = new SQL.Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); + private static final Statement create = new Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); 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); diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java index acc7a11..b19348d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java @@ -27,7 +27,7 @@ import java.time.Instant; public class NodeDownload { - private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); private static final String BASE = "https://steamwar.de/download.php?schem="; diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index f2f1d90..cca8bd3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -26,11 +26,11 @@ import java.util.Set; public class NodeMember { - private static final SQL.Statement getNodeMember = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); - private static final SQL.Statement getNodeMembers = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); - private static final SQL.Statement getSchematics = new SQL.Statement("SELECT * FROM NodeMember WHERE UserId = ?"); - private static final SQL.Statement createNodeMember = new SQL.Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); - private static final SQL.Statement deleteNodeMember = new SQL.Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final Statement getNodeMember = new Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final Statement getNodeMembers = new Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); + private static final Statement getSchematics = new Statement("SELECT * FROM NodeMember WHERE UserId = ?"); + private static final Statement createNodeMember = new Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); + private static final Statement deleteNodeMember = new Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); public static NodeMember getNodeMember(int node, int member) { return getNodeMember.select(rs -> { diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index b4f0f41..7e9663a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -31,11 +31,11 @@ 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 static final Statement getKits = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?"); + private static final Statement getKit = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final Statement getKitInUse = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = 1"); + private static final Statement delete = new Statement("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final Statement update = new 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; @@ -74,8 +74,7 @@ public class PersonalKit { if(armor == null) { armor = new ItemStack[]{null, null, null, null}; } - SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(name)", - userID, gamemode, name, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor)); + update.update(userID, gamemode, name, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor), true); return get(userID, gamemode, name); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java deleted file mode 100644 index fc8c7a2..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ /dev/null @@ -1,201 +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 . -*/ - -package de.steamwar.sql; - -import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; -import de.steamwar.core.Core; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.sql.*; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; - - -public class SQL { - private SQL(){} - - private static Connection con; - private static final String URL; - private static final String USER; - private static final String PASSWORD; - - static { - File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(file); - - if(!file.exists()) - throw new SecurityException("SQL-ConfigFile not found!"); - - URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); - USER = config.getString("USER"); - PASSWORD = config.getString("PASSWORD"); - - connect(); - } - - private static void connect() { - try { - con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD); - } catch (SQLException e) { - throw new SecurityException("Could not start SQL connection", e); - } - } - - public static void close() { - for (Statement statement : Statement.statements) { - try { - statement.st.close(); - } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); - } - } - - try { - con.close(); - } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e); - } - } - - private static void reset() { - close(); - connect(); - - try { - for (Statement statement : Statement.statements) { - statement.init(); - } - } catch (SQLException ex) { - throw new SecurityException("Could not reprepare SQL Statements", ex); - } - } - - public static boolean connectionStable() { - try { - return !con.isClosed(); - } catch (SQLException e) { - return false; - } - } - - @Deprecated - static void update(String qry, Object... objects) { - try { - prepare(qry, objects).executeUpdate(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - static ResultSet select(String qry, Object... objects) { - try { - return prepare(qry, objects).executeQuery(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - static Blob blob() { - try { - return con.createBlob(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - private static PreparedStatement prepare(String qry, Object... objects) throws SQLException { - PreparedStatement st = con.prepareStatement(qry); - for(int i = 0; i < objects.length; i++){ - st.setObject(i+1, objects[i]); - } - return st; - } - - public static class Statement { - private static final List statements = new ArrayList<>(); - - private final String sql; - private PreparedStatement st; - - Statement(String sql) { - this.sql = sql; - statements.add(this); - try { - init(); - } catch (SQLException e) { - throw new SecurityException("Could not init statement", e); - } - } - - private synchronized void init() throws SQLException { - st = con.prepareStatement(sql); - } - - T select(ResultSetUser user, Object... objects) { - return prepare(() -> { - ResultSet rs = st.executeQuery(); - T result = user.use(rs); - rs.close(); - return result; - }, objects); - } - - void update(Object... objects) { - prepare(st::executeUpdate, objects); - } - - private synchronized T prepare(SQLRunnable runnable, Object... objects) { - try { - try { - setObjects(objects); - return runnable.run(); - } catch (CommunicationsException e) { - reset(); - setObjects(objects); - return runnable.run(); - } - } catch (SQLException e) { - throw new SecurityException("Could not execute SQL statement", e); - } - } - - private void setObjects(Object... objects) throws SQLException { - for (int i = 0; i < objects.length; i++) { - st.setObject(i + 1, objects[i]); - } - } - - interface ResultSetUser { - T use(ResultSet rs) throws SQLException; - } - - private interface SQLRunnable { - T run() throws SQLException; - } - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index e1b4cb3..99c34ac 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -25,7 +25,7 @@ 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 (?, ?, ?)"); + private static final Statement insert = new Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)"); public static void log(String message, String stacktrace){ message += "\n"; diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index efe4b66..4b82884 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -33,7 +33,7 @@ import java.util.UUID; @Deprecated public class Schematic { - private static final SQL.Statement getSchemsOfType = new SQL.Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); + private static final Statement getSchemsOfType = new Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); private final SchematicNode node; diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 70a4b8a..fa33be0 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -21,8 +21,6 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; -import de.steamwar.core.VersionedCallable; -import de.steamwar.core.VersionedRunnable; import de.steamwar.core.WorldEditWrapper; import org.bukkit.entity.Player; @@ -35,38 +33,37 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; public class SchematicNode { - private static final SQL.Statement createNode = new SQL.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)"); - private static final SQL.Statement getSchematicNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); - private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); - private static final SQL.Statement getDirsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); - private static final SQL.Statement getDirsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); - private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicNodeO = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicNodeId = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); - private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); - private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); - private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); - private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); - private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); - private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); - private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); - private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); - private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); - private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); - private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); - private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); - private static final SQL.Statement selSchemData = new SQL.Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); - private static final SQL.Statement deleteNode = new SQL.Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); + 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)"); + private static final Statement getSchematicNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); + private static final Statement getSchematicsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); + private static final Statement getDirsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); + private static final Statement getDirsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); + private static final Statement getSchematicDirectory_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicDirectory = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeO_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNodeO = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeId = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); + private static final Statement getAllSchemsOfTypeOwner = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); + private static final Statement getAllSchemsOfType = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); + private static final Statement getAccessibleByUser = new Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final Statement getAccessibleByUserByTypeInNode = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); + private static final Statement getAccessibleByUserByTypeInNode_Null = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); + private static final Statement getAccessibleByUserByType = new Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); + private static final Statement getAllSchematicsAccessibleByUser = new Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); + private static final Statement isSchematicAccessibleForUser = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); + private static final Statement getAllParentsOfNode = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); + private static final Statement countNodes = new Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + private static final Statement updateDB = new Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); + private static final Statement updateDatabase = new Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final Statement selSchemData = new Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); + private static final Statement deleteNode = new Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); @@ -118,7 +115,7 @@ public class SchematicNode { if (parent != null && parent == 0) { parent = null; } - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { SchematicNode node = new SchematicNode(rs); return node; @@ -135,7 +132,7 @@ public class SchematicNode { public static List getSchematicNodeInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) nodes.add(new SchematicNode(rs)); @@ -151,7 +148,7 @@ public class SchematicNode { public static List getSchematicDirectoryInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) nodes.add(new SchematicNode(rs)); @@ -167,7 +164,7 @@ public class SchematicNode { public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { SchematicNode node = new SchematicNode(rs); if(node.isDir()) @@ -186,7 +183,7 @@ public class SchematicNode { public static SchematicNode getSchematicNode(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { return new SchematicNode(rs); } @@ -208,7 +205,7 @@ public class SchematicNode { } public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) { nodes.add(new SchematicNode(rs)); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Statement.java b/SpigotCore_Main/src/de/steamwar/sql/Statement.java new file mode 100644 index 0000000..48719e6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Statement.java @@ -0,0 +1,159 @@ +/* + 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 . +*/ + +package de.steamwar.sql; + +import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; +import de.steamwar.core.Core; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +public class Statement { + + private static final String URL; + private static final String USER; + private static final String PASSWORD; + + private static Connection con; + + private static final List statements = new ArrayList<>(); + + static { + File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(file); + + if(!file.exists()) + throw new SecurityException("SQL-ConfigFile not found!"); + + URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); + USER = config.getString("USER"); + PASSWORD = config.getString("PASSWORD"); + + Statement.connect(); + } + + private static void connect() { + try { + con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD); + } catch (SQLException e) { + throw new SecurityException("Could not start SQL connection", e); + } + } + + public static void close() { + for (Statement statement : statements) { + try { + statement.st.close(); + } catch (SQLException e) { + Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); + } + } + + try { + con.close(); + } catch (SQLException e) { + Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e); + } + } + + private static void reset() { + close(); + connect(); + + try { + for (Statement statement : statements) { + statement.init(); + } + } catch (SQLException ex) { + throw new SecurityException("Could not reprepare SQL Statements", ex); + } + } + + public static boolean connectionStable() { + try { + return !con.isClosed(); + } catch (SQLException e) { + return false; + } + } + + private final String sql; + private PreparedStatement st; + + Statement(String sql) { + this.sql = sql; + try { + init(); + } catch (SQLException e) { + throw new SecurityException("Could not init statement", e); + } + statements.add(this); + } + + private synchronized void init() throws SQLException { + st = con.prepareStatement(sql); + } + + T select(ResultSetUser user, Object... objects) { + return prepare(() -> { + ResultSet rs = st.executeQuery(); + T result = user.use(rs); + rs.close(); + return result; + }, objects); + } + + void update(Object... objects) { + prepare(st::executeUpdate, objects); + } + + private synchronized T prepare(SQLRunnable runnable, Object... objects) { + try { + try { + setObjects(objects); + return runnable.run(); + } catch (CommunicationsException e) { + reset(); + setObjects(objects); + return runnable.run(); + } + } catch (SQLException e) { + throw new SecurityException("Could not execute SQL statement", e); + } + } + + private void setObjects(Object... objects) throws SQLException { + for (int i = 0; i < objects.length; i++) { + st.setObject(i + 1, objects[i]); + } + } + + interface ResultSetUser { + T use(ResultSet rs) throws SQLException; + } + + private interface SQLRunnable { + T run() throws SQLException; + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index f8f2bcb..a66a875 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -28,9 +28,9 @@ 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 Statement getId = new Statement("SELECT * FROM UserData WHERE id = ?"); + private static final Statement getUUID = new Statement("SELECT * FROM UserData WHERE UUID = ?"); + private static final Statement getName = new Statement("SELECT * FROM UserData WHERE lower(UserName) = ?"); private static final Map byUUID = new HashMap<>(); private static final Map byName = new HashMap<>(); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Team.java b/SpigotCore_Main/src/de/steamwar/sql/Team.java index 5f45e7c..c12e994 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Team.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Team.java @@ -26,8 +26,8 @@ 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 static final Statement get = new Statement("SELECT * FROM Team WHERE TeamID = ?"); + private static final Statement getMembers = new Statement("SELECT id FROM UserData WHERE Team = ?"); private final int teamId; private final String teamKuerzel; diff --git a/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java b/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java index cd350d0..4a3dd5e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java +++ b/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java @@ -26,9 +26,9 @@ import java.util.Set; public class TeamTeilnahme { private TeamTeilnahme(){} - private static final SQL.Statement byEventTeam = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ? AND EventID = ?"); - private static final SQL.Statement byEvent = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE EventID = ?"); - private static final SQL.Statement byTeam = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ?"); + private static final Statement byEventTeam = new Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ? AND EventID = ?"); + private static final Statement byEvent = new Statement("SELECT * FROM TeamTeilnahme WHERE EventID = ?"); + private static final Statement byTeam = new Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ?"); public static boolean nimmtTeil(int teamID, int eventID){ return byEventTeam.select(ResultSet::next, teamID, eventID); diff --git a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java index 62a6d08..a47932a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java +++ b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java @@ -24,9 +24,9 @@ import java.util.UUID; public class 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 = ?"); + private static final Statement get = new Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?"); + private static final Statement set = new Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)"); + private static final Statement delete = new Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?"); public static String getConfig(UUID player, String config) { return getConfig(SteamwarUser.get(player).getId(), config);