commonDb #21

Zusammengeführt
Lixfel hat 21 Commits von commonDb nach master 2022-11-22 11:34:05 +01:00 zusammengeführt
5 geänderte Dateien mit 18 neuen und 7 gelöschten Zeilen
Nur Änderungen aus Commit 92bea6255f werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -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 {

Datei anzeigen

@ -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<SWException> 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

Datei anzeigen

@ -30,7 +30,7 @@ import java.util.Map;
public final class SqlTypeMapper<T> {
private static final Map<Class<?>, SqlTypeMapper<?>> mappers = new IdentityHashMap<>();
public static <T> SqlTypeMapper<T> getMapper(Class<T> clazz) {
public static <T> SqlTypeMapper<T> getMapper(Class<?> clazz) {
return (SqlTypeMapper<T>) mappers.get(clazz);
}
@ -47,7 +47,7 @@ public final class SqlTypeMapper<T> {
public static <T extends Enum<T>> void nameEnumMapper(Class<T> 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())
);

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -107,9 +107,9 @@ public class Table<T> {
List<TableField<?>> 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<T> {
private TableField(java.lang.reflect.Field field) {
this.identifier = field.getName();
this.mapper = (SqlTypeMapper<T>) SqlTypeMapper.getMapper(field.getDeclaringClass());
this.mapper = SqlTypeMapper.getMapper(field.getType());
this.field = field.getAnnotation(Field.class);
}