From 92bea6255f6cae312674c7badffb7a8a44c4daee Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 29 Oct 2022 13:19:36 +0200 Subject: [PATCH] Fixes --- build.gradle | 2 +- src/de/steamwar/sql/SWException.java | 7 ++++++- src/de/steamwar/sql/internal/SqlTypeMapper.java | 4 ++-- src/de/steamwar/sql/internal/Statement.java | 6 ++++++ src/de/steamwar/sql/internal/Table.java | 6 +++--- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 48d33bd..d5f000a 100644 --- a/build.gradle +++ b/build.gradle @@ -82,7 +82,7 @@ dependencies { testImplementation 'junit:junit:4.13.2' testImplementation 'org.hamcrest:hamcrest:2.2' - implementation 'org.xerial:sqlite-jdbc:3.36.0' + compileOnly 'org.xerial:sqlite-jdbc:3.36.0' } task buildResources { diff --git a/src/de/steamwar/sql/SWException.java b/src/de/steamwar/sql/SWException.java index 3e8084b..4354867 100644 --- a/src/de/steamwar/sql/SWException.java +++ b/src/de/steamwar/sql/SWException.java @@ -25,6 +25,7 @@ import de.steamwar.sql.internal.Table; import lombok.AllArgsConstructor; import java.io.File; +import java.sql.Timestamp; @AllArgsConstructor public class SWException { @@ -37,8 +38,12 @@ public class SWException { private static final String SERVER_NAME = new File(CWD).getName(); private static final Table table = new Table<>(SWException.class, "Exception"); - private static final Statement insert = table.insertAll(); + private static final Statement insert = table.insertFields("server", "message", "stacktrace"); + @Field(keys = {Table.PRIMARY}) + private final int id; + @Field(def = "CURRENT_TIMESTAMP") + private final Timestamp time; @Field private final String server; @Field diff --git a/src/de/steamwar/sql/internal/SqlTypeMapper.java b/src/de/steamwar/sql/internal/SqlTypeMapper.java index d364bc4..5c41010 100644 --- a/src/de/steamwar/sql/internal/SqlTypeMapper.java +++ b/src/de/steamwar/sql/internal/SqlTypeMapper.java @@ -30,7 +30,7 @@ import java.util.Map; public final class SqlTypeMapper { private static final Map, SqlTypeMapper> mappers = new IdentityHashMap<>(); - public static SqlTypeMapper getMapper(Class clazz) { + public static SqlTypeMapper getMapper(Class clazz) { return (SqlTypeMapper) mappers.get(clazz); } @@ -47,7 +47,7 @@ public final class SqlTypeMapper { public static > void nameEnumMapper(Class type) { new SqlTypeMapper<>( type, - "VARCHAR(" + Arrays.stream(type.getEnumConstants()).map(e -> e.name().length()).max(Integer::compareTo) + ")", + "VARCHAR(" + Arrays.stream(type.getEnumConstants()).map(e -> e.name().length()).max(Integer::compareTo).get() + ")", (rs, identifier) -> Enum.valueOf(type, rs.getString(identifier)), (st, index, value) -> st.setString(index, value.name()) ); diff --git a/src/de/steamwar/sql/internal/Statement.java b/src/de/steamwar/sql/internal/Statement.java index c436fc2..152784f 100644 --- a/src/de/steamwar/sql/internal/Statement.java +++ b/src/de/steamwar/sql/internal/Statement.java @@ -115,6 +115,7 @@ public class Statement implements AutoCloseable { } public Statement(String sql, boolean returnGeneratedKeys) { + System.out.println(sql); this.sql = sql; this.returnGeneratedKeys = returnGeneratedKeys; synchronized (statements) { @@ -165,6 +166,11 @@ public class Statement implements AutoCloseable { throw new SecurityException("Could not test connection validity", ex); } + synchronized (connections) { + connections.push(connection); + connections.notify(); + } + throw new SecurityException("Failing sql statement", e); } diff --git a/src/de/steamwar/sql/internal/Table.java b/src/de/steamwar/sql/internal/Table.java index 937f54d..f9dd6f7 100644 --- a/src/de/steamwar/sql/internal/Table.java +++ b/src/de/steamwar/sql/internal/Table.java @@ -107,9 +107,9 @@ public class Table { List> primaryKey = keys.containsKey(PRIMARY) ? Arrays.asList(keys.get(PRIMARY)) : Collections.emptyList(); try (Statement statement = new Statement( "CREATE TABLE IF NOT EXISTS " + name + "(" + - Arrays.stream(fields).map(field -> field.identifier + " " + field.mapper.sqlType() + (field.field.nullable() ? " DEFAULT NULL" : " NOT NULL") + (field.field.nullable() || field.field.def().equals("") ? "" : " DEFAULT " + field.field.def()) + (primaryKey.contains(field) ? " PRIMARY KEY" : "") + (field.field.autoincrement() ? " AUTOINCREMENT" : "")).collect(Collectors.joining(", ")) + + Arrays.stream(fields).map(field -> field.identifier + " " + field.mapper.sqlType() + (field.field.nullable() ? " DEFAULT NULL" : " NOT NULL") + (field.field.nullable() || field.field.def().equals("") ? "" : " DEFAULT " + field.field.def()) + (primaryKey.contains(field) ? " PRIMARY KEY" : "")).collect(Collectors.joining(", ")) + keys.entrySet().stream().filter(entry -> !entry.getKey().equals(PRIMARY)).map(key -> ", UNIQUE (" + Arrays.stream(key.getValue()).map(field -> field.identifier).collect(Collectors.joining(", ")) + ")").collect(Collectors.joining(" ")) + - ") STRICT, WITHOUT ROWID")) { + ") WITHOUT ROWID")) { statement.update(); } } @@ -127,7 +127,7 @@ public class Table { private TableField(java.lang.reflect.Field field) { this.identifier = field.getName(); - this.mapper = (SqlTypeMapper) SqlTypeMapper.getMapper(field.getDeclaringClass()); + this.mapper = SqlTypeMapper.getMapper(field.getType()); this.field = field.getAnnotation(Field.class); }