From e9a39d007d83a93e7dc50ab2706c9f16213bc900 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 15 Nov 2022 18:40:48 +0100 Subject: [PATCH] Current state --- src/de/steamwar/sql/NodeMember.java | 8 ++-- src/de/steamwar/sql/SchematicNode.java | 6 +-- src/de/steamwar/sql/UserConfig.java | 3 +- .../steamwar/sql/internal/SqlTypeMapper.java | 2 + src/de/steamwar/sql/internal/Statement.java | 45 ++++++++++--------- src/de/steamwar/sql/internal/Table.java | 4 +- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/de/steamwar/sql/NodeMember.java b/src/de/steamwar/sql/NodeMember.java index 32036ab..e704ad0 100644 --- a/src/de/steamwar/sql/NodeMember.java +++ b/src/de/steamwar/sql/NodeMember.java @@ -37,8 +37,8 @@ public class NodeMember { private static final Table table = new Table<>(NodeMember.class); private static final SelectStatement getNodeMember = table.select(Table.PRIMARY); - private static final SelectStatement getNodeMembers = table.selectFields("Node"); - private static final SelectStatement getSchematics = table.selectFields("Member"); + private static final SelectStatement getNodeMembers = table.selectFields("NodeId"); + private static final SelectStatement getSchematics = table.selectFields("UserId"); private static final Statement create = table.insertAll(); private static final Statement delete = table.delete(Table.PRIMARY); @@ -47,11 +47,11 @@ public class NodeMember { @Field(keys = {Table.PRIMARY}) private final int userId; - public int getNodeId() { + public int getNode() { return nodeId; } - public int getUserId() { + public int getMember() { return userId; } diff --git a/src/de/steamwar/sql/SchematicNode.java b/src/de/steamwar/sql/SchematicNode.java index be7f1b1..78fa24b 100644 --- a/src/de/steamwar/sql/SchematicNode.java +++ b/src/de/steamwar/sql/SchematicNode.java @@ -139,7 +139,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; - int nodeId = create.insertGetKey(owner, name, parent, type, item); + int nodeId = create.insertGetKey(owner, name, parent, item, type); return getSchematicNode(nodeId); } @@ -447,7 +447,7 @@ public class SchematicNode { private void updateDB() { this.lastUpdate = Timestamp.from(Instant.now()); - update.update(nodeId, nodeOwner, nodeName, parentNode, nodeItem, nodeType, lastUpdate, nodeRank, replaceColor, allowReplay, nodeFormat); + update.update(nodeId, nodeOwner, nodeName, parentNode, lastUpdate, nodeItem, nodeType, nodeRank, replaceColor, allowReplay, nodeFormat); this.brCache.clear(); TAB_CACHE.clear(); } @@ -480,7 +480,7 @@ public class SchematicNode { final Set nodeMembers = NodeMember.getSchematics(user.getId()); AtomicInteger i = new AtomicInteger(); i.set(currentNode.getId()); - while (currentNode.getParentNode() != null && nodeMembers.stream().noneMatch(nodeMember -> nodeMember.getNodeId() == i.get())) { + while (currentNode.getParentNode() != null && nodeMembers.stream().noneMatch(nodeMember -> nodeMember.getNode() == i.get())) { currentNode = currentNode.getParentNode(); i.set(currentNode.getId()); builder.insert(0, split) diff --git a/src/de/steamwar/sql/UserConfig.java b/src/de/steamwar/sql/UserConfig.java index 0b45f1f..f705dfd 100644 --- a/src/de/steamwar/sql/UserConfig.java +++ b/src/de/steamwar/sql/UserConfig.java @@ -47,7 +47,8 @@ public class UserConfig { } public static String getConfig(int player, String config) { - return select.select(player, config).value; + UserConfig value = select.select(player, config); + return value != null ? value.value : null; } public static void updatePlayerConfig(UUID uuid, String config, String value) { diff --git a/src/de/steamwar/sql/internal/SqlTypeMapper.java b/src/de/steamwar/sql/internal/SqlTypeMapper.java index ae3b7c8..34c6173 100644 --- a/src/de/steamwar/sql/internal/SqlTypeMapper.java +++ b/src/de/steamwar/sql/internal/SqlTypeMapper.java @@ -19,6 +19,7 @@ package de.steamwar.sql.internal; +import java.io.InputStream; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -63,6 +64,7 @@ public final class SqlTypeMapper { primitiveMapper(double.class, Double.class, "REAL", ResultSet::getDouble, PreparedStatement::setDouble); new SqlTypeMapper<>(String.class, "TEXT", ResultSet::getString, PreparedStatement::setString); new SqlTypeMapper<>(Timestamp.class, "TIMESTAMP", ResultSet::getTimestamp, PreparedStatement::setTimestamp); + new SqlTypeMapper<>(InputStream.class, "BLOB", ResultSet::getBinaryStream, PreparedStatement::setBinaryStream); } private static void primitiveMapper(Class primitive, Class wrapped, String sqlType, SQLReader reader, SQLWriter writer) { diff --git a/src/de/steamwar/sql/internal/Statement.java b/src/de/steamwar/sql/internal/Statement.java index 88aa5d6..6c44332 100644 --- a/src/de/steamwar/sql/internal/Statement.java +++ b/src/de/steamwar/sql/internal/Statement.java @@ -122,7 +122,6 @@ public class Statement implements AutoCloseable { } public Statement(String sql, boolean returnGeneratedKeys) { - System.out.println(sql); this.sql = sql; this.returnGeneratedKeys = returnGeneratedKeys; synchronized (statements) { @@ -159,34 +158,35 @@ public class Statement implements AutoCloseable { private T withConnection(SQLRunnable runnable, Object... objects) { Connection connection = aquireConnection(); - T result; try { - result = tryWithConnection(connection, runnable, objects); - } catch (SQLException e) { try { - if(connection.isClosed() || !connection.isValid(1)) { + return tryWithConnection(connection, runnable, objects); + } finally { + if(connectionInvalid(connection)) { closeConnection(connection); - return withConnection(runnable, objects); + } else { + synchronized (connections) { + connections.push(connection); + connections.notify(); + } } - } catch (SQLException ex) { - closeConnection(connection); - throw new SecurityException("Could not test connection validity", ex); } - - synchronized (connections) { - connections.push(connection); - connections.notify(); + } catch (SQLException e) { + if(connectionInvalid(connection)) { + return withConnection(runnable, objects); + } else { + throw new SecurityException("Failing sql statement", e); } - - throw new SecurityException("Failing sql statement", e); } + } - synchronized (connections) { - connections.push(connection); - connections.notify(); + private boolean connectionInvalid(Connection connection) { + try { + return connection.isClosed(); + } catch (SQLException e) { + logger.log(Level.INFO, "Could not check SQL connection status", e); // No database logging possible at this state + return true; } - - return result; } private T tryWithConnection(Connection connection, SQLRunnable runnable, Object... objects) throws SQLException { @@ -201,7 +201,10 @@ public class Statement implements AutoCloseable { for (int i = 0; i < objects.length; i++) { Object o = objects[i]; - SqlTypeMapper.getMapper(o.getClass()).write(st, i+1, o); + if(o != null) + SqlTypeMapper.getMapper(o.getClass()).write(st, i+1, o); + else + st.setNull(i+1, Types.NULL); } return runnable.run(st); diff --git a/src/de/steamwar/sql/internal/Table.java b/src/de/steamwar/sql/internal/Table.java index fb72709..cbad10a 100644 --- a/src/de/steamwar/sql/internal/Table.java +++ b/src/de/steamwar/sql/internal/Table.java @@ -75,7 +75,7 @@ public class Table { } public Statement updateFields(String[] fields, String... kfields) { - return new Statement("UPDATE " + name + " SET " + Arrays.stream(fields).map(f -> f + " = ?").collect(Collectors.joining(", ")) + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(", "))); + return new Statement("UPDATE " + name + " SET " + Arrays.stream(fields).map(f -> f + " = ?").collect(Collectors.joining(", ")) + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(" AND "))); } public Statement insert(String name) { @@ -100,7 +100,7 @@ public class Table { } public Statement deleteFields(String... kfields) { - return new Statement("DELETE FROM " + name + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(", "))); + return new Statement("DELETE FROM " + name + " WHERE " + Arrays.stream(kfields).map(f -> f + " = ?").collect(Collectors.joining(" AND "))); } void ensureExistanceInSqlite() {