SteamWar/SpigotCore
Archiviert
13
0

Fixes 'n' Statements
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-11-06 14:42:35 +01:00
Ursprung 0dd62f446b
Commit 1d9532d971
4 geänderte Dateien mit 118 neuen und 190 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<CheckedNode> getLastDeclinedOfNode(SchematicNode node){
return getLastDeclinedOfNode(node.getId());
}
public static List<CheckedNode> getLastDeclinedOfNode(int node){
return nodeHistory.select(rs -> {
List<CheckedNode> 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<CheckedNode> getLastDeclined(UUID uuid){
return getLastDelined(SteamwarUser.get(uuid).getId());
}
public static List<CheckedNode> getLastDelined(int schemOwner){
return checkHistory.select(rs -> {
List<CheckedNode> 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();
}
}

Datei anzeigen

@ -17,63 +17,114 @@
package de.steamwar.sql; package de.steamwar.sql;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Deprecated
public class CheckedSchematic { 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.node = node;
this.validator = validator;
this.startTime = startTime;
this.endTime = endTime;
this.declineReason = declineReason;
if(insertDB) {
insertDB();
}
} }
@Deprecated @Deprecated
public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ 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 @Deprecated
public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ 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<CheckedSchematic> getLastDeclined(UUID schemOwner){ public CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){
return getLastDelined(SteamwarUser.get(schemOwner).getId()); 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<CheckedSchematic> getLastDeclinedOfNode(SchematicNode node){
return getLastDeclinedOfNode(node.getId());
}
public static List<CheckedSchematic> getLastDeclinedOfNode(int node){
return nodeHistory.select(rs -> {
List<CheckedSchematic> 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<CheckedSchematic> getLastDeclined(UUID uuid){
return getLastDelined(SteamwarUser.get(uuid).getId());
} }
public static List<CheckedSchematic> getLastDelined(int schemOwner){ public static List<CheckedSchematic> getLastDelined(int schemOwner){
return CheckedNode.getLastDelined(schemOwner).stream().map(node1 -> new CheckedSchematic(node1)).collect(Collectors.toList()); return checkHistory.select(rs -> {
} List<CheckedSchematic> history = new ArrayList<>();
while(rs.next())
@Deprecated history.add(new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false));
public void remove() { return history;
node.remove(); }, schemOwner);
}
public String getSchemName() {
return node.getSchemName();
}
public int getSchemOwner() {
return node.getSchemOwner();
} }
public int getValidator() { public int getValidator() {
return node.getValidator(); return validator;
} }
public Timestamp getStartTime() { public Timestamp getStartTime() {
return node.getStartTime(); return startTime;
} }
public Timestamp getEndTime() { public Timestamp getEndTime() {
return node.getEndTime(); return endTime;
} }
public String getDeclineReason() { 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();
} }
} }

Datei anzeigen

@ -26,43 +26,40 @@ import java.util.Set;
public class NodeMember { 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) { public static NodeMember getNodeMember(int node, int member) {
ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); return getNodeMember.select(rs -> {
try { if(!rs.next())
if(!set.next())
return null; return null;
return new NodeMember(set); return new NodeMember(rs);
} catch (SQLException e) { }, node, member);
throw new SecurityException("Could not load NodeMember", e);
}
} }
public static Set<NodeMember> getNodeMembers(int node) { public static Set<NodeMember> getNodeMembers(int node) {
ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); return getNodeMembers.select(rs -> {
try {
Set<NodeMember> members = new HashSet<>(); Set<NodeMember> members = new HashSet<>();
while (set.next()) while (rs.next())
members.add(new NodeMember(set)); members.add(new NodeMember(rs));
return members; return members;
} catch (SQLException e) { }, node);
throw new SecurityException("Could not load NodeMember", e);
}
} }
public static Set<NodeMember> getSchematics(int member) { public static Set<NodeMember> getSchematics(int member) {
ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); return getSchematics.select(rs -> {
try {
Set<NodeMember> members = new HashSet<>(); Set<NodeMember> members = new HashSet<>();
while (set.next()) while (rs.next())
members.add(new NodeMember(set)); members.add(new NodeMember(rs));
return members; return members;
} catch (SQLException e) { }, member);
throw new SecurityException("Could not load NodeMember", e);
}
} }
public static NodeMember createNodeMember(int node, int 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); return getNodeMember(node, member);
} }
@ -83,6 +80,6 @@ public class NodeMember {
} }
public void delete() { public void delete() {
SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); deleteNodeMember.update(node, member);
} }
} }

Datei anzeigen

@ -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 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 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 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) { public static SchematicNode createSchematic(int owner, String name, Integer parent) {
return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), "");
@ -321,7 +325,7 @@ public class SchematicNode {
return parent; return parent;
} }
public void setParent(int parent) { public void setParent(Integer parent) {
this.parent = parent; this.parent = parent;
updateDB(); updateDB();
} }
@ -412,8 +416,7 @@ public class SchematicNode {
} }
private void updateDB() { private void updateDB() {
SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", updateDB.update(name, owner, parent == 0 ? null : parent, item, type, rank, id);
name, owner, parent == 0 ? null : parent, item, type, rank, id);
this.lastUpdate = Timestamp.from(Instant.now()); this.lastUpdate = Timestamp.from(Instant.now());
this.brCache.clear(); this.brCache.clear();
} }
@ -422,18 +425,24 @@ public class SchematicNode {
if (isDir()) { if (isDir()) {
getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); getSchematicNodeInNode(getId()).forEach(SchematicNode::delete);
} }
SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); deleteNode.update(id);
} }
public InputStream schemData() throws IOException { public InputStream schemData() throws IOException {
ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id);
try { try {
rs.next(); return selSchemData.select(rs -> {
Blob schemData = rs.getBlob("NodeData"); rs.next();
if(schemData == null) Blob schemData = rs.getBlob("NodeData");
throw new IOException("SchemData is null"); if(schemData == null) {
return new GZIPInputStream(schemData.getBinaryStream()); throw new SecurityException("SchemData is null");
} catch (SQLException e) { }
try {
return new GZIPInputStream(schemData.getBinaryStream());
} catch (IOException e) {
throw new SecurityException("SchemData is wrong", e);
}
}, id);
} catch (Exception e) {
throw new IOException(e); throw new IOException(e);
} }
} }
@ -469,7 +478,7 @@ public class SchematicNode {
} }
private void updateDatabase(InputStream blob, boolean newFormat) { 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; schemFormat = newFormat;
} }