Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Ursprung
4f719e30ac
Commit
c5bed0acfa
@ -29,6 +29,7 @@ public class Field<T> {
|
|||||||
private static final Map<Class<?>, String> sqlTypeMapping = new HashMap<>();
|
private static final Map<Class<?>, String> sqlTypeMapping = new HashMap<>();
|
||||||
private static final Map<Class<?>, SqlTypeParser<?>> sqlTypeParser = new HashMap<>();
|
private static final Map<Class<?>, SqlTypeParser<?>> sqlTypeParser = new HashMap<>();
|
||||||
|
|
||||||
|
//TODO andere Richtung Objekt -> SQL
|
||||||
public static <T> void addTypeMapping(Class<T> clazz, String sqlType, SqlTypeParser<T> parser) {
|
public static <T> void addTypeMapping(Class<T> clazz, String sqlType, SqlTypeParser<T> parser) {
|
||||||
sqlTypeMapping.put(clazz, sqlType);
|
sqlTypeMapping.put(clazz, sqlType);
|
||||||
sqlTypeParser.put(clazz, parser);
|
sqlTypeParser.put(clazz, parser);
|
||||||
|
@ -34,6 +34,6 @@ public class Row {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void update(Field<?>[] fields, Object... values) {
|
void update(Field<?>[] fields, Object... values) {
|
||||||
table.update(values[table.keyId()], fields, values);
|
table.update(values[table.keyIds()], fields, values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,41 +27,41 @@ import java.util.stream.Collectors;
|
|||||||
public class Table {
|
public class Table {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final Field<?> key;
|
private final Set<Field<?>> keys;
|
||||||
private final Field<?>[] fields;
|
private final Field<?>[] fields;
|
||||||
private final Map<Field<?>, Integer> fieldIds = new HashMap<>();
|
private final Map<Field<?>, Integer> fieldIds = new HashMap<>();
|
||||||
private final int keyId;
|
private final List<Integer> keyIds;
|
||||||
|
|
||||||
private final Map<Field<?>, Statement> cachedSelect = new HashMap<>();
|
private final Map<Field<?>[], Statement> cachedSelect = new HashMap<>();
|
||||||
private final Map<Field<?>[], Statement> cachedInsert = new HashMap<>();
|
private final Map<Field<?>[], Statement> cachedInsert = new HashMap<>();
|
||||||
private final Map<Field<?>[], Statement> cachedUpdate = new HashMap<>();
|
private final Map<Field<?>[], Statement> cachedUpdate = new HashMap<>();
|
||||||
|
|
||||||
public Table(String name, Field<?> key, Field<?>... fields) {
|
public Table(String name, Field<?>[] keys, Field<?>... fields) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.key = key;
|
this.keys = Arrays.stream(keys).collect(Collectors.toSet());
|
||||||
this.fields = fields;
|
this.fields = fields;
|
||||||
for(int i = 0; i < fields.length; i++) {
|
for(int i = 0; i < fields.length; i++) {
|
||||||
fieldIds.put(fields[i], i);
|
fieldIds.put(fields[i], i);
|
||||||
}
|
}
|
||||||
keyId = fieldIds.get(key);
|
keyIds = Arrays.stream(keys).map(fieldIds::get).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row selectSingle(Field<?> field, Object value) {
|
public Row selectSingle(Field<?>[] fields, Object... values) {
|
||||||
return select(rs -> {
|
return select(rs -> {
|
||||||
if(rs.next())
|
if(rs.next())
|
||||||
return read(rs);
|
return read(rs);
|
||||||
return null;
|
return null;
|
||||||
}, field, value);
|
}, fields, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Row> selectMulti(Field<?> field, Object value) {
|
public List<Row> selectMulti(Field<?>[] fields, Object... values) {
|
||||||
return select(rs -> {
|
return select(rs -> {
|
||||||
List<Row> result = new ArrayList<>();
|
List<Row> result = new ArrayList<>();
|
||||||
while(rs.next())
|
while(rs.next())
|
||||||
result.add(read(rs));
|
result.add(read(rs));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}, field, value);
|
}, fields, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insert(Field<?>[] fields, Object... values) {
|
public void insert(Field<?>[] fields, Object... values) {
|
||||||
@ -72,35 +72,35 @@ public class Table {
|
|||||||
statement.update(values);
|
statement.update(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Object keyvalue, Field<?>[] fields, Object... values) {
|
public void update(Object[] keyvalues, Field<?>[] fields, Object... values) {
|
||||||
Statement statement;
|
Statement statement;
|
||||||
synchronized (cachedUpdate) {
|
synchronized (cachedUpdate) {
|
||||||
statement = cachedUpdate.computeIfAbsent(fields, fs -> new Statement("UPDATE " + name + " SET " + Arrays.stream(fs).map(f -> f.identifier() + " = ?").collect(Collectors.joining(", ")) + " WHERE " + key.identifier() + " = ?"));
|
statement = cachedUpdate.computeIfAbsent(fields, fs -> new Statement("UPDATE " + name + " SET " + Arrays.stream(fs).map(f -> f.identifier() + " = ?").collect(Collectors.joining(", ")) + " WHERE " + keys.stream().map(f -> f.identifier() + " = ?").collect(Collectors.joining(", "))));
|
||||||
}
|
}
|
||||||
statement.update(values, keyvalue);
|
statement.update(values, keyvalues);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
//TODO syntax mysql/sqlite
|
//TODO syntax mysql/sqlite
|
||||||
try (Statement statement = new Statement("CREATE TABLE IF NOT EXISTS " + name + "(" + Arrays.stream(fields).map(field -> field.identifier() + " " + field.sqlType() + (field == key ? " PRIMARY KEY" : "")).collect(Collectors.joining(", ")) + ") STRICT")) {
|
try (Statement statement = new Statement("CREATE TABLE IF NOT EXISTS " + name + "(" + Arrays.stream(fields).map(field -> field.identifier() + " " + field.sqlType() + (keys.contains(field) ? " PRIMARY KEY" : "")).collect(Collectors.joining(", ")) + ") STRICT")) {
|
||||||
statement.update();
|
statement.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int keyId() {
|
public List<Integer> keyIds() {
|
||||||
return keyId;
|
return keyIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFieldId(Field<?> field) {
|
public int getFieldId(Field<?> field) {
|
||||||
return fieldIds.get(field);
|
return fieldIds.get(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T select(Statement.ResultSetUser<T> u, Field<?> field, Object value) {
|
private <T> T select(Statement.ResultSetUser<T> u, Field<?>[] fields, Object... values) {
|
||||||
Statement statement;
|
Statement statement;
|
||||||
synchronized (cachedSelect) {
|
synchronized (cachedSelect) {
|
||||||
statement = cachedSelect.computeIfAbsent(field, f -> new Statement("SELECT " + Arrays.stream(fields).map(Field::identifier).collect(Collectors.joining(", ")) + " FROM " + name + " WHERE " + field.identifier() + " = ?"));
|
statement = cachedSelect.computeIfAbsent(fields, fs -> new Statement("SELECT " + Arrays.stream(fields).map(Field::identifier).collect(Collectors.joining(", ")) + " FROM " + name + " WHERE " + Arrays.stream(fs).map(f -> f.identifier() + " = ?").collect(Collectors.joining(", "))));
|
||||||
}
|
}
|
||||||
return statement.select(u, value);
|
return statement.select(u, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Row read(ResultSet rs) throws SQLException {
|
private Row read(ResultSet rs) throws SQLException {
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren