From 29bc98889a38e181e007519ae11245ef8b447895 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 11 Jun 2022 14:20:14 +0200 Subject: [PATCH] Prototyping new db interface Signed-off-by: Lixfel --- src/de/steamwar/sql/Field.java | 39 ++++++++++++++ src/de/steamwar/sql/Row.java | 35 ++++++++++++ src/de/steamwar/sql/Statement.java | 4 ++ src/de/steamwar/sql/Table.java | 87 ++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 src/de/steamwar/sql/Field.java create mode 100644 src/de/steamwar/sql/Row.java create mode 100644 src/de/steamwar/sql/Table.java diff --git a/src/de/steamwar/sql/Field.java b/src/de/steamwar/sql/Field.java new file mode 100644 index 0000000..8424665 --- /dev/null +++ b/src/de/steamwar/sql/Field.java @@ -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 . + */ + +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; + } +} diff --git a/src/de/steamwar/sql/Row.java b/src/de/steamwar/sql/Row.java new file mode 100644 index 0000000..c9ae439 --- /dev/null +++ b/src/de/steamwar/sql/Row.java @@ -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 . + */ + +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) { + + } +} diff --git a/src/de/steamwar/sql/Statement.java b/src/de/steamwar/sql/Statement.java index a1d55f0..a54a4ee 100644 --- a/src/de/steamwar/sql/Statement.java +++ b/src/de/steamwar/sql/Statement.java @@ -111,6 +111,10 @@ public class Statement implements AutoCloseable { withConnection(PreparedStatement::executeUpdate, objects); } + public String getSql() { + return sql; + } + private T withConnection(SQLRunnable runnable, Object... objects) { Connection connection = aquireConnection(); diff --git a/src/de/steamwar/sql/Table.java b/src/de/steamwar/sql/Table.java new file mode 100644 index 0000000..239b3f8 --- /dev/null +++ b/src/de/steamwar/sql/Table.java @@ -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 . + */ + +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 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 selectMulti(Field field, Object value) { + return select(rs -> { + List 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 select(Statement.ResultSetUser 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); + } +}