From 1d9532d971555933ae99fd818a86070cdc449bac Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 6 Nov 2021 14:42:35 +0100 Subject: [PATCH] Fixes 'n' Statements Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/CheckedNode.java | 129 ------------------ .../src/de/steamwar/sql/CheckedSchematic.java | 103 ++++++++++---- .../src/de/steamwar/sql/NodeMember.java | 43 +++--- .../src/de/steamwar/sql/SchematicNode.java | 33 +++-- 4 files changed, 118 insertions(+), 190 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java deleted file mode 100644 index f52137a..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java +++ /dev/null @@ -1,129 +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 java.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - - -public class CheckedNode { - - private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedNode WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - private static final SQL.Statement nodeHistory = new SQL.Statement("SELECT * FROM CheckedNode WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - - private final Integer node; - private final int validator; - private final Timestamp startTime; - private final Timestamp endTime; - private final String declineReason; - - private CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ - this.node = node; - this.validator = validator; - this.startTime = startTime; - this.endTime = endTime; - this.declineReason = declineReason; - if(insertDB) { - insertDB(); - } - } - - public CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(node, validator, startTime, endTime, declineReason, true); - } - - public CheckedNode(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); - } - - private void insertDB() { - SchematicNode sNode = SchematicNode.getSchematicNode(node); - String nodeName = sNode.getName(); - int nodeOwner = sNode.getOwner(); - SQL.update("INSERT INTO CheckedSchematic" + - " (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", - node, nodeName, nodeOwner, validator, startTime, endTime, declineReason); - } - - public static List getLastDeclinedOfNode(SchematicNode node){ - return getLastDeclinedOfNode(node.getId()); - } - - public static List getLastDeclinedOfNode(int node){ - return nodeHistory.select(rs -> { - List lastDeclined = new ArrayList<>(); - while(rs.next()){ - int validator = rs.getInt("Validator"); - Timestamp startTime = rs.getTimestamp("StartTime"); - Timestamp endTime = rs.getTimestamp("EndTime"); - String declineReason = rs.getString("DeclineReason"); - lastDeclined.add(new CheckedNode(node, validator, startTime, endTime, declineReason, false)); - } - return lastDeclined; - }, node); - } - - public static List getLastDeclined(UUID uuid){ - return getLastDelined(SteamwarUser.get(uuid).getId()); - } - - public static List getLastDelined(int schemOwner){ - return checkHistory.select(rs -> { - List history = new ArrayList<>(); - while(rs.next()) - history.add(new CheckedNode(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); - return history; - }, schemOwner); - } - - public void remove() { - SQL.update("DELETE FROM CheckedSchematic WHERE NodeId", node); - } - - public int getValidator() { - return validator; - } - - public Timestamp getStartTime() { - return startTime; - } - - public Timestamp getEndTime() { - return endTime; - } - - public String getDeclineReason() { - return declineReason; - } - - public int getNode() { - return node; - } - - public String getSchemName() { - return SchematicNode.getSchematicNode(node).getName(); - } - - public int getSchemOwner() { - return SchematicNode.getSchematicNode(node).getId(); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index b3ff430..808ff1f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -17,63 +17,114 @@ package de.steamwar.sql; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; -@Deprecated public class CheckedSchematic { - private final CheckedNode node; + 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 CheckedSchematic(CheckedNode node){ + private final Integer node; + private final int validator; + private final Timestamp startTime; + private final Timestamp endTime; + private final String declineReason; + + private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ this.node = node; + this.validator = validator; + this.startTime = startTime; + this.endTime = endTime; + this.declineReason = declineReason; + if(insertDB) { + insertDB(); + } } @Deprecated public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + this(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator, startTime, endTime, declineReason, true); } @Deprecated public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + this(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); } - public static List getLastDeclined(UUID schemOwner){ - return getLastDelined(SteamwarUser.get(schemOwner).getId()); + public CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, validator, startTime, endTime, declineReason, true); + } + + public CheckedSchematic(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); + } + + private void insertDB() { + SchematicNode sNode = SchematicNode.getSchematicNode(node); + String nodeName = sNode.getName(); + int nodeOwner = sNode.getOwner(); + insert.update(node, nodeName, nodeOwner, validator, startTime, endTime, declineReason); + } + + public static List getLastDeclinedOfNode(SchematicNode node){ + return getLastDeclinedOfNode(node.getId()); + } + + public static List getLastDeclinedOfNode(int node){ + return nodeHistory.select(rs -> { + List lastDeclined = new ArrayList<>(); + while(rs.next()){ + int validator = rs.getInt("Validator"); + 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)); + } + return lastDeclined; + }, node); + } + + public static List getLastDeclined(UUID uuid){ + return getLastDelined(SteamwarUser.get(uuid).getId()); } public static List getLastDelined(int schemOwner){ - return CheckedNode.getLastDelined(schemOwner).stream().map(node1 -> new CheckedSchematic(node1)).collect(Collectors.toList()); - } - - @Deprecated - public void remove() { - node.remove(); - } - - public String getSchemName() { - return node.getSchemName(); - } - - public int getSchemOwner() { - return node.getSchemOwner(); + return checkHistory.select(rs -> { + List history = new ArrayList<>(); + while(rs.next()) + history.add(new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); + return history; + }, schemOwner); } public int getValidator() { - return node.getValidator(); + return validator; } public Timestamp getStartTime() { - return node.getStartTime(); + return startTime; } public Timestamp getEndTime() { - return node.getEndTime(); + return endTime; } public String getDeclineReason() { - return node.getDeclineReason(); + return declineReason; + } + + public int getNode() { + return node; + } + + public String getSchemName() { + return SchematicNode.getSchematicNode(node).getName(); + } + + public int getSchemOwner() { + return SchematicNode.getSchematicNode(node).getId(); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index ad265e7..f2f1d90 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -26,43 +26,40 @@ 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 = ?"); + public static NodeMember getNodeMember(int node, int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); - try { - if(!set.next()) + return getNodeMember.select(rs -> { + if(!rs.next()) return null; - return new NodeMember(set); - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + return new NodeMember(rs); + }, node, member); } public static Set getNodeMembers(int node) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); - try { + return getNodeMembers.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, node); } public static Set getSchematics(int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); - try { + return getSchematics.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, member); } public static NodeMember createNodeMember(int node, int member) { - SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + createNodeMember.update(node, member); return getNodeMember(node, member); } @@ -83,6 +80,6 @@ public class NodeMember { } public void delete() { - SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + deleteNodeMember.update(node, member); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 5089fa1..3b6ee03 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -54,6 +54,10 @@ public class SchematicNode { 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.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); 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 = ?"); public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); @@ -321,7 +325,7 @@ public class SchematicNode { return parent; } - public void setParent(int parent) { + public void setParent(Integer parent) { this.parent = parent; updateDB(); } @@ -412,8 +416,7 @@ public class SchematicNode { } private void updateDB() { - SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", - name, owner, parent == 0 ? null : parent, item, type, rank, id); + updateDB.update(name, owner, parent == 0 ? null : parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); this.brCache.clear(); } @@ -422,18 +425,24 @@ public class SchematicNode { if (isDir()) { getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); } - SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); + deleteNode.update(id); } public InputStream schemData() throws IOException { - ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { - rs.next(); - Blob schemData = rs.getBlob("NodeData"); - if(schemData == null) - throw new IOException("SchemData is null"); - return new GZIPInputStream(schemData.getBinaryStream()); - } catch (SQLException e) { + return selSchemData.select(rs -> { + rs.next(); + Blob schemData = rs.getBlob("NodeData"); + if(schemData == null) { + throw new SecurityException("SchemData is null"); + } + try { + return new GZIPInputStream(schemData.getBinaryStream()); + } catch (IOException e) { + throw new SecurityException("SchemData is wrong", e); + } + }, id); + } catch (Exception e) { throw new IOException(e); } } @@ -469,7 +478,7 @@ public class SchematicNode { } private void updateDatabase(InputStream blob, boolean newFormat) { - SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); + updateDatabase.update(blob, newFormat, id); schemFormat = newFormat; }