SteamWar/SpigotCore
Archiviert
13
0

New Statement System
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-11-05 16:31:15 +01:00
Ursprung 1865342a00
Commit 474212a340

Datei anzeigen

@ -40,6 +40,21 @@ import java.util.zip.GZIPInputStream;
public class SchematicNode { 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 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.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");
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(), "");
} }
@ -51,8 +66,7 @@ public class SchematicNode {
public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) {
if (parent == 0) if (parent == 0)
parent = null; parent = null;
SQL.update("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)", createNode.update(name, owner, parent, type, item);
name, owner, parent, type, item);
return getSchematicNode(owner, name, parent); return getSchematicNode(owner, name, parent);
} }
@ -88,100 +102,80 @@ public class SchematicNode {
} }
public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { public static SchematicNode getSchematicNode(int owner, String name, Integer parent) {
if (parent != null && parent == 0) if (parent != null && parent == 0) {
parent = null; parent = null;
ResultSet set;
if (parent == null) {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name);
} else {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent);
} }
try { SQL.Statement.ResultSetUser<SchematicNode> user = rs -> {
while (set.next()) { while (rs.next()) {
SchematicNode node = new SchematicNode(set); SchematicNode node = new SchematicNode(rs);
return node; return node;
} }
List<SchematicNode> nodes = getSchematicNodeInNode(parent);
for (SchematicNode node:nodes) {
if (node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null)
return node;
}
return null; return null;
}catch (SQLException e) { };
throw new SecurityException("Failed to load Schemnodes", e); if(parent == null) {
return getSchematicNode_Null.select(user, owner, name);
} else {
return getSchematicNode.select(user, owner, name, parent);
} }
} }
public static List<SchematicNode> getSchematicNodeInNode(Integer parent) { public static List<SchematicNode> getSchematicNodeInNode(Integer parent) {
if(parent != null && parent == 0) if(parent != null && parent == 0)
parent = null; parent = null;
ResultSet set; SQL.Statement.ResultSetUser<List<SchematicNode>> user = rs -> {
if(parent == null) {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL");
}else {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?", parent);
}
try {
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while (set.next()) while (rs.next())
nodes.add(new SchematicNode(set)); nodes.add(new SchematicNode(rs));
return nodes; return nodes;
}catch (SQLException e) { };
throw new SecurityException("Failed to load Schemnodes", e); if(parent == null) {
return getSchematicsInNode_Null.select(user);
}else {
return getSchematicsInNode.select(user, parent);
} }
} }
public static SchematicNode getSchematicDirectory(String name, Integer parent) { public static SchematicNode getSchematicDirectory(String name, Integer parent) {
if(parent != null && parent == 0) if(parent != null && parent == 0)
parent = null; parent = null;
ResultSet set; SQL.Statement.ResultSetUser<SchematicNode> user = rs -> {
if(parent == null) { while (rs.next()) {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); SchematicNode node = new SchematicNode(rs);
}else {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent);
}
try {
while (set.next()) {
SchematicNode node = new SchematicNode(set);
if(node.isDir()) if(node.isDir())
return node; return node;
} }
return null; return null;
}catch (SQLException e) { };
throw new SecurityException("Failed to load Schemnodes", e);
if(parent == null) {
return getSchematicDirectory_Null.select(user, name);
}else {
return getSchematicDirectory.select(user, name, parent);
} }
} }
public static SchematicNode getSchematicInParent(String name, Integer parent) { public static SchematicNode getSchematicNode(String name, Integer parent) {
if(parent != null && parent == 0) if(parent != null && parent == 0)
parent = null; parent = null;
ResultSet set; SQL.Statement.ResultSetUser<SchematicNode> user = rs -> {
if(parent == null) { while (rs.next()) {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name); return new SchematicNode(rs);
}else {
set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent);
}
try {
while (set.next()) {
SchematicNode node = new SchematicNode(set);
if(!node.isDir())
return node;
} }
return null; return null;
}catch (SQLException e) { };
throw new SecurityException("Failed to load Schemnodes", e); if(parent == null) {
return getSchematicNodeO_Null.select(user, name);
}else {
return getSchematicNodeO.select(user, name, parent);
} }
} }
public static SchematicNode getSchematicNode(int id) { public static SchematicNode getSchematicNode(int id) {
ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?", id); return getSchematicNodeId.select(rs -> {
try { if (!rs.next())
if (!set.next())
return null; return null;
return new SchematicNode(set); return new SchematicNode(rs);
} catch (SQLException e) { });
throw new SecurityException("Failed to load Schemnodes", e);
}
} }
public static List<SchematicNode> getSchematicsOfType(int owner, String schemType, Integer parent) { public static List<SchematicNode> getSchematicsOfType(int owner, String schemType, Integer parent) {
@ -199,27 +193,22 @@ public class SchematicNode {
} }
public static List<SchematicNode> getAllSchematicsOfType(int owner, String schemType) { public static List<SchematicNode> getAllSchematicsOfType(int owner, String schemType) {
ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); return getAllSchemsOfTypeOwner.select(rs -> {
try {
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while (set.next()) while (rs.next())
nodes.add(new SchematicNode(set)); nodes.add(new SchematicNode(rs));
return nodes; return nodes;
} catch (SQLException e) { }, owner, schemType);
throw new SecurityException("Failed to load Schemnodes", e);
}
} }
public static List<SchematicNode> getAllSchematicsOfType(String schemType) { public static List<SchematicNode> getAllSchematicsOfType(String schemType) {
ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?", schemType); return getAllSchemsOfType.select(rs -> {
try {
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while (set.next()) while (rs.next())
nodes.add(new SchematicNode(set)); nodes.add(new SchematicNode(rs));
return nodes; return nodes;
} catch (SQLException e) { }, schemType);
throw new SecurityException("Failed to load Schemnodes", e);
}
} }
public static List<SchematicNode> deepGet(Integer parent, Predicate<SchematicNode> filter) { public static List<SchematicNode> deepGet(Integer parent, Predicate<SchematicNode> filter) {
@ -252,29 +241,29 @@ public class SchematicNode {
if(isAdded) if(isAdded)
return getSchematicNodeInNode(parent); return getSchematicNodeInNode(parent);
} else { } else {
ResultSet set = SQL.select("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", user, user); return getAccessibleByUser.select(rs -> {
try{
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while(set.next()) while(rs.next())
nodes.add(new SchematicNode(set)); nodes.add(new SchematicNode(rs));
return nodes; return nodes;
}catch(SQLException e){ }, user, user);
throw new SecurityException("Failed listing schematics", e);
}
} }
return Collections.emptyList(); return Collections.emptyList();
} }
public static List<SchematicNode> getAllSchematicsAccessibleByUser(int user) { public static List<SchematicNode> getAllSchematicsAccessibleByUser(int user) {
ResultSet set = SQL.select("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 nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); return getAccessibleByUser.select(rs -> {
try{
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while(set.next()) while(rs.next()) {
nodes.add(new SchematicNode(set)); SchematicNode node = new SchematicNode(rs);
if(node.isDir()) {
nodes.addAll(deepGet(node.getId(), node1 -> true));
} else {
nodes.add(node);
}
}
return nodes; return nodes;
}catch(SQLException e){ }, user, user);
throw new SecurityException("Failed listing schematics", e);
}
} }
private final int id; private final int id;
@ -303,14 +292,12 @@ public class SchematicNode {
} }
public static Integer countNodes() { public static Integer countNodes() {
ResultSet set = SQL.select("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); return countNodes.select(rs -> {
try { if (rs.next()) {
if (set.next()) return rs.getInt("count");
return set.getInt("count"); }
return 0; return 0;
} catch (SQLException e) { });
throw new SecurityException("Failed listing schematics", e);
}
} }
public int getId() { public int getId() {