First basic untested implementation
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2022-06-23 14:58:33 +02:00
Ursprung 3d280ac051
Commit 4f719e30ac
5 geänderte Dateien mit 138 neuen und 43 gelöschten Zeilen

Datei anzeigen

@ -19,21 +19,58 @@
package de.steamwar.sql; package de.steamwar.sql;
public class Field { import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class Field<T> {
private static final Map<Class<?>, String> sqlTypeMapping = new HashMap<>();
private static final Map<Class<?>, SqlTypeParser<?>> sqlTypeParser = new HashMap<>();
public static <T> void addTypeMapping(Class<T> clazz, String sqlType, SqlTypeParser<T> parser) {
sqlTypeMapping.put(clazz, sqlType);
sqlTypeParser.put(clazz, parser);
}
static {
addTypeMapping(String.class, "TEXT", (rs, field) -> rs.getString(field.identifier()));
addTypeMapping(boolean.class, "INTEGER(1)", (rs, field) -> rs.getBoolean(field.identifier()));
addTypeMapping(byte.class, "INTEGER(1)", (rs, field) -> rs.getByte(field.identifier()));
addTypeMapping(short.class, "INTEGER(2)", (rs, field) -> rs.getShort(field.identifier()));
addTypeMapping(int.class, "INTEGER(4)", (rs, field) -> rs.getInt(field.identifier()));
addTypeMapping(long.class, "INTEGER(8)", (rs, field) -> rs.getLong(field.identifier()));
addTypeMapping(float.class, "REAL", (rs, field) -> rs.getFloat(field.identifier()));
addTypeMapping(double.class, "REAL", (rs, field) -> rs.getDouble(field.identifier()));
}
private final String identifier; private final String identifier;
private final Class<?> type; private final Class<T> type;
public Field(String identifier, Class<?> type) { private final SqlTypeParser<T> parser;
private final String sqlType;
public Field(String identifier, Class<T> type) {
this.identifier = identifier; this.identifier = identifier;
this.type = type; this.type = type;
this.parser = (SqlTypeParser<T>) sqlTypeParser.get(type);
this.sqlType = sqlTypeMapping.get(type);
} }
public String identifier() { public String identifier() {
return identifier; return identifier;
} }
public Class<?> type() { public Class<T> type() {
return type; return type;
} }
public String sqlType() {
return sqlType;
}
public T parse(ResultSet rs) throws SQLException {
return parser.parse(rs, this);
}
} }

Datei anzeigen

@ -24,12 +24,16 @@ public class Row {
private final Table table; private final Table table;
private Object[] values; private Object[] values;
public Row(Table table, Object[] values) { public Row(Table table, Object... values) {
this.table = table; this.table = table;
this.values = values; this.values = values;
} }
void update(Field field, Object value) { private <T> T get(Field<T> field) {
return (T) values[table.getFieldId(field)];
}
void update(Field<?>[] fields, Object... values) {
table.update(values[table.keyId()], fields, values);
} }
} }

Datei anzeigen

@ -0,0 +1,27 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface SqlTypeParser<T> {
T parse(ResultSet rs, Field<T> field) throws SQLException;
}

Datei anzeigen

@ -47,7 +47,7 @@ public class Statement implements AutoCloseable {
throw new SecurityException("Could not load SQL connection", e); throw new SecurityException("Could not load SQL connection", e);
} }
String url = "jdbc:mysql://" + properties.getProperty("host") + ":" + properties.getProperty("port") + "/" + properties.getProperty("database") + "?autoReconnect=true&useServerPrepStmts=true"; String url = "jdbc:mysql://" + properties.getProperty("host") + ":" + properties.getProperty("port") + "/" + properties.getProperty("database") + "?useServerPrepStmts=true";
String user = properties.getProperty("user"); String user = properties.getProperty("user");
String password = properties.getProperty("password"); String password = properties.getProperty("password");

Datei anzeigen

@ -19,69 +19,96 @@
package de.steamwar.sql; package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors; 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 Field<?> key;
private final Field[] fields; private final Field<?>[] fields;
private final Map<Field<?>, Integer> fieldIds = new HashMap<>();
private final int keyId;
private final Map<Field, Statement> selectBy = new HashMap<>(); private final Map<Field<?>, Statement> cachedSelect = new HashMap<>();
private final Map<Field<?>[], Statement> cachedInsert = new HashMap<>();
private final Map<Field<?>[], Statement> cachedUpdate = new HashMap<>();
public Table(String name, Field key, Field... fields) { public Table(String name, Field<?> key, Field<?>... fields) {
this.name = name; this.name = name;
this.key = key; this.key = key;
this.fields = fields; this.fields = fields;
for(int i = 0; i < fields.length; i++) {
fieldIds.put(fields[i], i);
}
keyId = fieldIds.get(key);
} }
public Row selectSingle(Field field, Object value) { public Row selectSingle(Field<?> field, Object value) {
return select(rs -> { return select(rs -> {
if(!rs.next())
return null;
Object[] values = new Object[fields.length];
for(int i = 0; i < fields.length; i++) {
values[i] = fields[i].type().cast(rs.getObject(i));
}
if(rs.next()) if(rs.next())
SQLConfig.impl.getLogger().log(Level.WARNING, "Statement returned more than one result " + selectBy.get(field).getSql() + " for value " + value); return read(rs);
return null;
return new Row(this, values);
}, field, value); }, field, value);
} }
public List<Row> selectMulti(Field field, Object value) { public List<Row> selectMulti(Field<?> field, Object value) {
return select(rs -> { return select(rs -> {
List<Row> result = new ArrayList<>(); List<Row> result = new ArrayList<>();
while(rs.next()) { while(rs.next())
Object[] values = new Object[fields.length]; result.add(read(rs));
for(int i = 0; i < fields.length; i++) {
values[i] = fields[i].type().cast(rs.getObject(i));
}
result.add(new Row(this, values));
}
return result; return result;
}, field, value); }, field, value);
} }
public void create() { public void insert(Field<?>[] fields, Object... values) {
//TODO and alter table
}
public Field getKey() {
return key;
}
private <T> T select(Statement.ResultSetUser<T> u, Field field, Object value) {
Statement statement; Statement statement;
synchronized (selectBy) { synchronized (cachedInsert) {
statement = selectBy.computeIfAbsent(field, f -> new Statement("SELECT " + Arrays.stream(fields).map(Field::identifier).collect(Collectors.joining(", ")) + " FROM " + name + " WHERE " + field.identifier() + " = ?")); statement = cachedInsert.computeIfAbsent(fields, fs -> new Statement("INSERT INTO " + name + " (" + Arrays.stream(fs).map(Field::identifier).collect(Collectors.joining(", ")) + ") VALUES (" + Arrays.stream(fs).map(f -> "?").collect(Collectors.joining(", ")) + ")"));
}
statement.update(values);
}
public void update(Object keyvalue, Field<?>[] fields, Object... values) {
Statement statement;
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.update(values, keyvalue);
}
public void create() {
//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")) {
statement.update();
}
}
public int keyId() {
return keyId;
}
public int getFieldId(Field<?> field) {
return fieldIds.get(field);
}
private <T> T select(Statement.ResultSetUser<T> u, Field<?> field, Object value) {
Statement statement;
synchronized (cachedSelect) {
statement = cachedSelect.computeIfAbsent(field, f -> new Statement("SELECT " + Arrays.stream(fields).map(Field::identifier).collect(Collectors.joining(", ")) + " FROM " + name + " WHERE " + field.identifier() + " = ?"));
} }
return statement.select(u, value); return statement.select(u, value);
} }
private Row read(ResultSet rs) throws SQLException {
Object[] values = new Object[fields.length];
for(int i = 0; i < fields.length; i++) {
values[i] = fields[i].parse(rs);
}
return new Row(this, values);
}
} }