From e52e9c5ccd8ef9b87ce06d4eeeaaa4044afa89b5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 21 Feb 2023 13:24:32 +0100 Subject: [PATCH] Seperate SchematicData --- src/de/steamwar/sql/NodeData.java | 69 ++++++++++++++++++++++++++ src/de/steamwar/sql/SchematicNode.java | 14 ++---- 2 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/de/steamwar/sql/NodeData.java diff --git a/src/de/steamwar/sql/NodeData.java b/src/de/steamwar/sql/NodeData.java new file mode 100644 index 0000000..6550458 --- /dev/null +++ b/src/de/steamwar/sql/NodeData.java @@ -0,0 +1,69 @@ +package de.steamwar.sql; + +import de.steamwar.sql.internal.*; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.sql.PreparedStatement; +import java.util.zip.GZIPInputStream; + +@AllArgsConstructor +public class NodeData { + + static { + new SqlTypeMapper<>(PipedInputStream.class, "BLOB", (rs, identifier) -> { throw new SecurityException("PipedInputStream is write only datatype"); }, PreparedStatement::setBinaryStream); + new SqlTypeMapper<>(ByteArrayInputStream.class, "BLOB", (rs, identifier) -> { throw new SecurityException("ByteArrayInputStream is write only datatype"); }, PreparedStatement::setBinaryStream); + } + + private static final Table table = new Table<>(NodeData.class); + + private static final Statement updateDatabase = new Statement("UPDATE NodeData SET SchemData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final Statement selSchemData = new Statement("SELECT SchemData FROM NodeData WHERE NodeId = ?"); + + private static final SelectStatement get = table.select(Table.PRIMARY); + + public static NodeData get(SchematicNode node) { + if(node.isDir()) + throw new IllegalArgumentException("Node is a directory"); + return get.select(node); + } + + @Getter + @Field(keys = {Table.PRIMARY}) + private final int nodeId; + + @Field + private boolean nodeFormat; + + public InputStream schemData() throws IOException { + try { + return selSchemData.select(rs -> { + rs.next(); + InputStream schemData = rs.getBinaryStream("SchemData"); + try { + if(rs.wasNull() || schemData.available() == 0) { + throw new SecurityException("SchemData is null"); + } + return new GZIPInputStream(schemData); + } catch (IOException e) { + throw new SecurityException("SchemData is wrong", e); + } + }, nodeId); + } catch (Exception e) { + throw new IOException(e); + } + } + + public void saveFromStream(InputStream blob, boolean newFormat) { + updateDatabase.update(blob, newFormat, nodeId); + nodeFormat = newFormat; + } + + public boolean getNodeFormat() { + return nodeFormat; + } +} diff --git a/src/de/steamwar/sql/SchematicNode.java b/src/de/steamwar/sql/SchematicNode.java index 0e64522..ec7b72f 100644 --- a/src/de/steamwar/sql/SchematicNode.java +++ b/src/de/steamwar/sql/SchematicNode.java @@ -44,7 +44,7 @@ public class SchematicNode { private static final Table table = new Table<>(SchematicNode.class); private static final Statement create = table.insertFields(true, "NodeOwner", "NodeName", "ParentNode", "NodeItem", "NodeType"); - private static final Statement update = table.update(Table.PRIMARY, "NodeName", "ParentNode", "NodeItem", "NodeType", "NodeRank", "ReplaceColor", "AllowReplay", "NodeFormat"); + private static final Statement update = table.update(Table.PRIMARY, "NodeName", "ParentNode", "NodeItem", "NodeType", "NodeRank", "ReplaceColor", "AllowReplay"); private static final Statement delete = table.delete(Table.PRIMARY); private static final SelectStatement byId = new SelectStatement<>(table, nodeSelector + "WHERE NodeId = ?"); @@ -89,9 +89,6 @@ public class SchematicNode { private boolean replaceColor; @Field(def = "1") private boolean allowReplay; - @Setter(AccessLevel.PACKAGE) - @Field(def = "1") - private boolean nodeFormat; private String brCache; @@ -106,8 +103,7 @@ public class SchematicNode { SchematicType nodeType, int nodeRank, boolean replaceColor, - boolean allowReplay, - boolean nodeFormat + boolean allowReplay ) { this.nodeId = nodeId; this.nodeOwner = nodeOwner; @@ -120,7 +116,6 @@ public class SchematicNode { this.nodeRank = nodeRank; this.replaceColor = replaceColor; this.allowReplay = allowReplay; - this.nodeFormat = nodeFormat; } public static List getAll(SteamwarUser user) { @@ -381,10 +376,11 @@ public class SchematicNode { return nodeType == null; } + @Deprecated public boolean getSchemFormat() { if(isDir()) throw new SecurityException("Node is Directory"); - return nodeFormat; + return NodeData.get(this).getNodeFormat(); } public int getRank() { @@ -462,7 +458,7 @@ public class SchematicNode { private void updateDB() { this.lastUpdate = Timestamp.from(Instant.now()); - update.update(nodeName, parentNode, nodeItem, nodeType, nodeRank, replaceColor, allowReplay, nodeFormat, nodeId); + update.update(nodeName, parentNode, nodeItem, nodeType, nodeRank, replaceColor, allowReplay, nodeId); TAB_CACHE.clear(); }