Prototyping new db interface
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2022-06-11 14:20:14 +02:00
Ursprung d6c29a25b9
Commit 29bc98889a
4 geänderte Dateien mit 165 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,39 @@
/*
* 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;
public class Field {
private final String identifier;
private final Class<?> type;
public Field(String identifier, Class<?> type) {
this.identifier = identifier;
this.type = type;
}
public String identifier() {
return identifier;
}
public Class<?> type() {
return type;
}
}

35
src/de/steamwar/sql/Row.java Normale Datei
Datei anzeigen

@ -0,0 +1,35 @@
/*
* 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;
public class Row {
private final Table table;
private Object[] values;
public Row(Table table, Object[] values) {
this.table = table;
this.values = values;
}
void update(Field field, Object value) {
}
}

Datei anzeigen

@ -111,6 +111,10 @@ public class Statement implements AutoCloseable {
withConnection(PreparedStatement::executeUpdate, objects);
}
public String getSql() {
return sql;
}
private <T> T withConnection(SQLRunnable<T> runnable, Object... objects) {
Connection connection = aquireConnection();

Datei anzeigen

@ -0,0 +1,87 @@
/*
* 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.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class Table {
private final String name;
private final Field key;
private final Field[] fields;
private final Map<Field, Statement> selectBy = new HashMap<>();
public Table(String name, Field key, Field... fields) {
this.name = name;
this.key = key;
this.fields = fields;
}
public Row selectSingle(Field field, Object value) {
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())
SQLConfig.impl.getLogger().log(Level.WARNING, "Statement returned more than one result " + selectBy.get(field).getSql() + " for value " + value);
return new Row(this, values);
}, field, value);
}
public List<Row> selectMulti(Field field, Object value) {
return select(rs -> {
List<Row> result = new ArrayList<>();
while(rs.next()) {
Object[] values = new Object[fields.length];
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;
}, field, value);
}
public void create() {
//TODO and alter table
}
public Field getKey() {
return key;
}
private <T> T select(Statement.ResultSetUser<T> u, Field field, Object value) {
Statement statement;
synchronized (selectBy) {
statement = selectBy.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);
}
}