diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index 962bdba..3cdadcf 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -21,6 +21,7 @@ package de.steamwar.sql; import de.steamwar.core.Core; +import java.io.IOException; import java.io.InputStream; import java.sql.Timestamp; import java.util.List; @@ -80,4 +81,24 @@ public interface Provider { String getConfig(int player, String config); void updatePlayerConfig(int id, String config, String value); void removePlayerConfig(int id, String config); + + void createSchematicNode(int owner, String name, Integer parent, String type, String item); + SchematicNode getSchematicNode(int owner, String name, Integer parent); + List getSchematicNodeInNode(Integer parent); + List getSchematicDirectoryInNode(Integer parent); + SchematicNode getSchematicDirectory(String name, Integer parent); + SchematicNode getSchematicNode(String name, Integer parent); + SchematicNode getSchematicNode(int id); + List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent); + List getAllAccessibleSchematicsOfType(int user, String schemType); + List getAllSchematicsOfType(int owner, String schemType); + List getAllSchematicsOfType(String schemType); + List getSchematicsAccessibleByUser(int user, Integer parent); + List getAllSchematicsAccessibleByUser(int user); + List getAllParentsOfNode(int node); + Integer countNodes(); + void updateSchematicNode(SchematicNode node); + void deleteSchematicNode(SchematicNode node); + InputStream getSchematicData(SchematicNode node) throws IOException; + void saveSchematicNode(SchematicNode node, InputStream blob, boolean newFormat); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index da12849..0fdb551 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -19,12 +19,15 @@ package de.steamwar.sql; +import java.io.IOException; import java.io.InputStream; +import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.*; import java.util.function.Consumer; +import java.util.zip.GZIPInputStream; public class SQLProvider implements Provider { @@ -354,4 +357,249 @@ public class SQLProvider implements Provider { public void removePlayerConfig(int id, String config) { deleteConfig.update(id, config); } + + 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 ORDER BY NodeName"); + private static final Statement getSchematicsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? ORDER BY NodeName"); + 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 ORDER BY NodeName"); + 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 ORDER BY NodeName"); + 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 ORDER BY NodeName"); + private static final Statement getSchematicDirectory = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ? ORDER BY NodeName"); + 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 = ? ORDER BY NodeName"); + 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 = ? ORDER BY NodeName"); + private static final Statement getAllSchemsOfType = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ? ORDER BY NodeName"); + 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 = ? ORDER BY NodeName"); + 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 ORDER BY NodeName"); + 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 = ? ORDER BY NodeName"); + 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 ORDER BY NodeName"); + 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 ORDER BY NodeName"); + 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 = ?"); + + private static final Statement.ResultSetUser> toSchematicList = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) { + nodes.add(nodeFromResultSet(rs)); + } + return nodes; + }; + private static final Statement.ResultSetUser toSchematicNode = rs -> { + if (rs.next()) { + return nodeFromResultSet(rs); + } + return null; + }; + + private static SchematicNode nodeFromResultSet(ResultSet set) throws SQLException { + Integer parent = set.getInt("ParentNode"); + if(set.wasNull()) { + parent = null; + } + String type = set.getString("NodeType"); + boolean isDir = true; + Integer rank = null; + boolean schemFormat = false; + if (type != null) { + isDir = false; + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("NodeFormat"); + } + return new SchematicNode( + set.getInt("NodeId"), + set.getInt("NodeOwner"), + set.getString("NodeName"), + parent, + set.getString("NodeItem"), + type, + isDir, + rank, + set.getTimestamp("LastUpdate"), + schemFormat + ); + } + + @Override + public void createSchematicNode(int owner, String name, Integer parent, String type, String item) { + createNode.update(owner, name, parent, type, item); + } + + @Override + public SchematicNode getSchematicNode(int owner, String name, Integer parent) { + + if(parent == null) { + return getSchematicNode_Null.select(toSchematicNode, owner, name); + } else { + return getSchematicNode.select(toSchematicNode, owner, name, parent); + } + } + + @Override + public List getSchematicNodeInNode(Integer parent) { + if(parent == null) { + return getSchematicsInNode_Null.select(toSchematicList); + }else { + return getSchematicsInNode.select(toSchematicList, parent); + } + } + + @Override + public List getSchematicDirectoryInNode(Integer parent) { + if(parent == null) { + return getDirsInNode_Null.select(toSchematicList); + }else { + return getDirsInNode.select(toSchematicList, parent); + } + } + + @Override + public SchematicNode getSchematicDirectory(String name, Integer parent) { + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = nodeFromResultSet(rs); + if(node.isDir()) + return node; + } + return null; + }; + + if(parent == null) { + return getSchematicDirectory_Null.select(user, name); + }else { + return getSchematicDirectory.select(user, name, parent); + } + } + + @Override + public SchematicNode getSchematicNode(String name, Integer parent) { + if(parent == null) { + return getSchematicNodeO_Null.select(toSchematicNode, name); + }else { + return getSchematicNodeO.select(toSchematicNode, name, parent); + } + } + + @Override + public SchematicNode getSchematicNode(int id) { + return getSchematicNodeId.select(rs -> { + if (!rs.next()) + return null; + return nodeFromResultSet(rs); + }, id); + } + + @Override + public List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { + if(parent == null || parent == 0) { + return getAccessibleByUserByTypeInNode_Null.select(toSchematicList, owner, owner, schemType); + } else { + return getAccessibleByUserByTypeInNode.select(toSchematicList, owner, owner, schemType, parent); + } + } + + @Override + public List getAllAccessibleSchematicsOfType(int user, String schemType) { + return getAccessibleByUserByType.select(toSchematicList, user, user, schemType); + } + + @Override + public List getAllSchematicsOfType(int owner, String schemType) { + return getAllSchemsOfTypeOwner.select(toSchematicList, owner, schemType); + } + + @Override + public List getAllSchematicsOfType(String schemType) { + return getAllSchemsOfType.select(toSchematicList, schemType); + } + + @Override + public List getSchematicsAccessibleByUser(int user, Integer parent) { + if (parent != null && parent != 0) { + if(Boolean.TRUE.equals(isSchematicAccessibleForUser.select(rs -> { + rs.next(); + return rs.getInt("Accessible") > 0; + }, parent, user, user))) { + return getSchematicNodeInNode(parent); + } + } else { + return getAccessibleByUser.select(rs -> { + List nodes = new ArrayList<>(); + while(rs.next()) + nodes.add(nodeFromResultSet(rs)); + return nodes; + }, user, user, user, user); + } + return Collections.emptyList(); + } + + @Override + public List getAllSchematicsAccessibleByUser(int user) { + return getAllSchematicsAccessibleByUser.select(toSchematicList, user, user); + } + + @Override + public List getAllParentsOfNode(int node) { + return getAllParentsOfNode.select(toSchematicList, node); + } + + @Override + public Integer countNodes() { + return countNodes.select(rs -> { + if (rs.next()) { + return rs.getInt("count"); + } + return 0; + }); + } + + @Override + public void updateSchematicNode(SchematicNode node) { + updateDB.update( + node.getName(), + node.getOwner(), + node.getParent(), + node.getItem(), + node.getType(), + node.getRank(), + node.getId() + ); + } + + @Override + public void deleteSchematicNode(SchematicNode node) { + deleteNode.update(node.getId()); + } + + @Override + public InputStream getSchematicData(SchematicNode node) throws IOException { + try { + 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); + } + }, node.getId()); + } catch (Exception e) { + throw new IOException(e); + } + } + + @Override + public void saveSchematicNode(SchematicNode node, InputStream blob, boolean newFormat) { + updateDatabase.update(blob, newFormat, node.getId()); + } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 32c3d51..34439ea 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -39,33 +39,6 @@ import java.util.zip.GZIPInputStream; public class SchematicNode { - private static final Statement createNode = new Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); - 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 ORDER BY NodeName"); - private static final Statement getSchematicsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? ORDER BY NodeName"); - 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 ORDER BY NodeName"); - 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 ORDER BY NodeName"); - 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 ORDER BY NodeName"); - private static final Statement getSchematicDirectory = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ? ORDER BY NodeName"); - 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 = ? ORDER BY NodeName"); - 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 = ? ORDER BY NodeName"); - private static final Statement getAllSchemsOfType = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ? ORDER BY NodeName"); - 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 = ? ORDER BY NodeName"); - 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 ORDER BY NodeName"); - 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 = ? ORDER BY NodeName"); - 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 ORDER BY NodeName"); - 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 ORDER BY NodeName"); - 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(), ""); } @@ -77,7 +50,7 @@ public class SchematicNode { public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { if (parent != null && parent == 0) parent = null; - createNode.update(name, owner, parent, type, item); + Provider.impl.createSchematicNode(owner, name, parent, type, item); return getSchematicNode(owner, name, parent); } @@ -85,23 +58,29 @@ public class SchematicNode { return getSchematicNode(owner, name, parent.getId()); } - private SchematicNode(ResultSet set) throws SQLException { - id = set.getInt("NodeId"); - owner = set.getInt("NodeOwner"); - name = set.getString("NodeName"); - parent = set.getInt("ParentNode"); - if(set.wasNull()) { - parent = null; - } - item = set.getString("NodeItem"); - type = set.getString("NodeType"); - lastUpdate = set.getTimestamp("LastUpdate"); - if (type != null) { - isDir = false; - rank = set.getInt("NodeRank"); - schemFormat = set.getBoolean("NodeFormat"); - } else { - isDir = true; + public SchematicNode( + int id, + int owner, + String name, + Integer parent, + String item, + String type, + boolean isDir, + int rank, + Timestamp lastUpdate, + boolean schemFormat + ) { + this.id = id; + this.owner = owner; + this.name = name; + this.parent = parent; + this.item = item; + this.type = type; + this.lastUpdate = lastUpdate; + this.isDir = isDir; + if (!isDir) { + this.schemFormat = schemFormat; + this.rank = rank; } } @@ -117,136 +96,60 @@ public class SchematicNode { if (parent != null && parent == 0) { parent = null; } - Statement.ResultSetUser user = rs -> { - if (rs.next()) { - return new SchematicNode(rs); - } - return null; - }; - if(parent == null) { - return getSchematicNode_Null.select(user, owner, name); - } else { - return getSchematicNode.select(user, owner, name, parent); - } + return Provider.impl.getSchematicNode(owner, name, parent); } public static List getSchematicNodeInNode(Integer parent) { - if(parent != null && parent == 0) + if(parent != null && parent == 0) { parent = null; - Statement.ResultSetUser> user = rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) { - nodes.add(new SchematicNode(rs)); - } - return nodes; - }; - if(parent == null) { - return getSchematicsInNode_Null.select(user); - }else { - return getSchematicsInNode.select(user, parent); } + return Provider.impl.getSchematicNodeInNode(parent); } public static List getSchematicDirectoryInNode(Integer parent) { - if(parent != null && parent == 0) + if(parent != null && parent == 0) { parent = null; - Statement.ResultSetUser> user = rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) - nodes.add(new SchematicNode(rs)); - return nodes; - }; - if(parent == null) { - return getDirsInNode_Null.select(user); - }else { - return getDirsInNode.select(user, parent); } + return Provider.impl.getSchematicDirectoryInNode(parent); } public static SchematicNode getSchematicDirectory(String name, Integer parent) { - if(parent != null && parent == 0) + if(parent != null && parent == 0) { parent = null; - Statement.ResultSetUser user = rs -> { - while (rs.next()) { - SchematicNode node = new SchematicNode(rs); - if(node.isDir()) - return node; - } - return null; - }; - - if(parent == null) { - return getSchematicDirectory_Null.select(user, name); - }else { - return getSchematicDirectory.select(user, name, parent); } + return Provider.impl.getSchematicDirectory(name, parent); } public static SchematicNode getSchematicNode(String name, Integer parent) { - if(parent != null && parent == 0) + if(parent != null && parent == 0) { parent = null; - Statement.ResultSetUser user = rs -> { - while (rs.next()) { - return new SchematicNode(rs); - } - return null; - }; - if(parent == null) { - return getSchematicNodeO_Null.select(user, name); - }else { - return getSchematicNodeO.select(user, name, parent); } + return Provider.impl.getSchematicNode(name, parent); } public static SchematicNode getSchematicNode(int id) { - return getSchematicNodeId.select(rs -> { - if (!rs.next()) - return null; - return new SchematicNode(rs); - }, id); + return Provider.impl.getSchematicNode(id); } public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { - Statement.ResultSetUser> user = rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) { - nodes.add(new SchematicNode(rs)); - } - return nodes; - }; - if(parent == null || parent == 0) { - return getAccessibleByUserByTypeInNode_Null.select(user, owner, owner, schemType); - } else { - return getAccessibleByUserByTypeInNode.select(user, owner, owner, schemType, parent); - } + return Provider.impl.getAccessibleSchematicsOfTypeInParent(owner, schemType, parent); } public static List getAllAccessibleSchematicsOfType(int user, String schemType) { - return getAccessibleByUserByType.select(rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) { - nodes.add(new SchematicNode(rs)); - } - return nodes; - }, user, user, schemType); + return Provider.impl.getAllAccessibleSchematicsOfType(user, schemType); } public static List getAllSchematicsOfType(int owner, String schemType) { - return getAllSchemsOfTypeOwner.select(rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) - nodes.add(new SchematicNode(rs)); - return nodes; - }, owner, schemType); + return Provider.impl.getAllSchematicsOfType(owner, schemType); } + @Deprecated public static List getAllSchematicsOfType(String schemType) { - return getAllSchemsOfType.select(rs -> { - List nodes = new ArrayList<>(); - while (rs.next()) - nodes.add(new SchematicNode(rs)); - return nodes; - }, schemType); + return Provider.impl.getAllSchematicsOfType(schemType); + } + + public static List getAllSchematicsOfType(SchematicType schemType) { + return Provider.impl.getAllSchematicsOfType(schemType.toDB()); } public static List deepGet(Integer parent, Predicate filter) { @@ -264,32 +167,11 @@ public class SchematicNode { } public static List getSchematicsAccessibleByUser(int user, Integer parent) { - if (parent != null && parent != 0) { - if(Boolean.TRUE.equals(isSchematicAccessibleForUser.select(rs -> { - rs.next(); - return rs.getInt("Accessible") > 0; - }, parent, user, user))) { - return getSchematicNodeInNode(parent); - } - } else { - return getAccessibleByUser.select(rs -> { - List nodes = new ArrayList<>(); - while(rs.next()) - nodes.add(new SchematicNode(rs)); - return nodes; - }, user, user, user, user); - } - return Collections.emptyList(); + return Provider.impl.getSchematicsAccessibleByUser(user, parent); } public static List getAllSchematicsAccessibleByUser(int user) { - return getAllSchematicsAccessibleByUser.select(rs -> { - List nodes = new ArrayList<>(); - while(rs.next()) { - nodes.add(new SchematicNode(rs)); - } - return nodes; - }, user, user); + return Provider.impl.getAllSchematicsAccessibleByUser(user); } public static List getAllParentsOfNode(SchematicNode node) { @@ -297,13 +179,7 @@ public class SchematicNode { } public static List getAllParentsOfNode(int node) { - return getAllParentsOfNode.select(rs -> { - List nodes = new ArrayList<>(); - while(rs.next()) { - nodes.add(new SchematicNode(rs)); - } - return nodes; - }, node); + return Provider.impl.getAllParentsOfNode(node); } public static SchematicNode getNodeFromPath(SteamwarUser user, String s) { @@ -367,12 +243,7 @@ public class SchematicNode { } public static Integer countNodes() { - return countNodes.select(rs -> { - if (rs.next()) { - return rs.getInt("count"); - } - return 0; - }); + return Provider.impl.countNodes(); } public int getId() { @@ -501,32 +372,17 @@ public class SchematicNode { } private void updateDB() { - updateDB.update(name, owner, parent, item, type, rank, id); + Provider.impl.updateSchematicNode(this); this.lastUpdate = Timestamp.from(Instant.now()); this.brCache.clear(); } public void delete() { - deleteNode.update(id); + Provider.impl.deleteSchematicNode(this); } public InputStream schemData() throws IOException { - try { - 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); - } + return Provider.impl.getSchematicData(this); } public Clipboard load() throws IOException, NoClipboardException { @@ -560,7 +416,7 @@ public class SchematicNode { } private void updateDatabase(InputStream blob, boolean newFormat) { - updateDatabase.update(blob, newFormat, id); + Provider.impl.saveSchematicNode(this, blob, newFormat); schemFormat = newFormat; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index c005c3d..8e334ae 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -23,6 +23,7 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import java.io.IOException; import java.io.InputStream; import java.sql.Timestamp; import java.time.Instant; @@ -191,5 +192,90 @@ public class StandaloneProvider implements Provider { configs.computeIfAbsent(id, player -> new HashMap<>()).remove(config); } + @Override + public void createSchematicNode(int owner, String name, Integer parent, String type, String item) {} + @Override + public SchematicNode getSchematicNode(int owner, String name, Integer parent) { + return null; + } + + @Override + public List getSchematicNodeInNode(Integer parent) { + return Collections.emptyList(); + } + + @Override + public List getSchematicDirectoryInNode(Integer parent) { + return Collections.emptyList(); + } + + @Override + public SchematicNode getSchematicDirectory(String name, Integer parent) { + return null; + } + + @Override + public SchematicNode getSchematicNode(String name, Integer parent) { + return null; + } + + @Override + public SchematicNode getSchematicNode(int id) { + return null; + } + + @Override + public List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { + return Collections.emptyList(); + } + + @Override + public List getAllAccessibleSchematicsOfType(int user, String schemType) { + return Collections.emptyList(); + } + + @Override + public List getAllSchematicsOfType(int owner, String schemType) { + return Collections.emptyList(); + } + + @Override + public List getAllSchematicsOfType(String schemType) { + return Collections.emptyList(); + } + + @Override + public List getSchematicsAccessibleByUser(int user, Integer parent) { + return Collections.emptyList(); + } + + @Override + public List getAllSchematicsAccessibleByUser(int user) { + return Collections.emptyList(); + } + + @Override + public List getAllParentsOfNode(int node) { + return Collections.emptyList(); + } + + @Override + public Integer countNodes() { + return 0; + } + + @Override + public void updateSchematicNode(SchematicNode node) {} + + @Override + public void deleteSchematicNode(SchematicNode node) {} + + @Override + public InputStream getSchematicData(SchematicNode node) throws IOException { + return null; + } + + @Override + public void saveSchematicNode(SchematicNode node, InputStream blob, boolean newFormat) {} }