From 17cf359e832dbc91d2dcaec458f4c340338309c9 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 8 Jun 2023 18:41:05 +0200 Subject: [PATCH] KeyValue has been simplified --- .../database/keyvalue/KeyValueBuilder.java | 37 +----- .../database/keyvalue/KeyValueConnection.java | 119 ++---------------- .../database/keyvalue/KeyValueTable.java | 64 +++------- 3 files changed, 25 insertions(+), 195 deletions(-) diff --git a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueBuilder.java b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueBuilder.java index c802d67..78394da 100644 --- a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueBuilder.java +++ b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueBuilder.java @@ -1,29 +1,21 @@ package fr.altarik.toolbox.database.keyvalue; import fr.altarik.toolbox.core.builder.IBuilder; -import fr.altarik.toolbox.core.builder.RequiredCollectionParameterBuilder; import fr.altarik.toolbox.core.builder.RequiredParamBuilder; import fr.altarik.toolbox.database.SqlConnection; -import net.minecraft.util.Pair; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.sql.JDBCType; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; + public class KeyValueBuilder implements IBuilder { private final RequiredParamBuilder tableName; - private final RequiredCollectionParameterBuilder> additionalColumns; private final RequiredParamBuilder connection; private KeyValueBuilder() { this.tableName = new RequiredParamBuilder<>(); this.connection = new RequiredParamBuilder<>(); - this.additionalColumns = new RequiredCollectionParameterBuilder<>(new ArrayList<>(), true); } public static KeyValueBuilder builder() { @@ -40,33 +32,8 @@ public class KeyValueBuilder implements IBuilder { return this; } - public KeyValueBuilder addColumn(AdditionalColumn additionalColumn) { - this.additionalColumns.add(additionalColumn); - return this; - } - public KeyValueConnection build() throws SQLException { - return new KeyValueConnection(connection.get(), tableName.get(), additionalColumns.get()); - } - - public record AdditionalColumn(@NotNull String columnName, @NotNull JDBCType type, boolean notNull, @Nullable AdditionalColumnReference reference) { - public AdditionalColumn { - Objects.requireNonNull(columnName); - Objects.requireNonNull(type); - } - - public Pair toPair(Object value) { - return new Pair<>(this, value); - } - } - - public record AdditionalColumnReference(@NotNull String referenceTable, @NotNull String... referenceColumns) { - public AdditionalColumnReference { - Objects.requireNonNull(referenceTable); - if(Objects.requireNonNull(referenceColumns).length == 0) - throw new IllegalArgumentException("Reference Columns should not be empty"); - } - + return new KeyValueConnection(connection.get(), tableName.get()); } } diff --git a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueConnection.java b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueConnection.java index 2fabedb..0f16d3d 100644 --- a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueConnection.java +++ b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueConnection.java @@ -1,7 +1,6 @@ package fr.altarik.toolbox.database.keyvalue; import fr.altarik.toolbox.database.SqlConnection; -import net.minecraft.util.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -9,151 +8,51 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.List; public class KeyValueConnection implements KeyValueTable { private final SqlConnection connection; private final String tableName; - private final List additionColumns; - public KeyValueConnection(@NotNull SqlConnection connection, @NotNull String tableName, @NotNull List additionalColumns) throws SQLException { + public KeyValueConnection(@NotNull SqlConnection connection, @NotNull String tableName) throws SQLException { this.connection = connection; this.tableName = tableName; - this.additionColumns = additionalColumns; connection.checkConnection(); try(Statement statement = connection.getConnection().createStatement()) { - StringBuilder sql = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(tableName).append("(id SERIAL,"); - for(KeyValueBuilder.AdditionalColumn additionalColumn : additionalColumns) { - sql.append(additionalColumn.columnName()).append(" ").append(additionalColumn.type().getName()); - if(additionalColumn.notNull()) { - sql.append(" NOT NULL "); - } - if(additionalColumn.reference() != null) { - sql.append("REFERENCES ").append(additionalColumn.reference().referenceTable()).append("("); - for(int i = 0; i < additionalColumn.reference().referenceColumns().length; ++i) { - sql.append(additionalColumn.reference().referenceColumns()[i]); - if(i != additionalColumn.reference().referenceColumns().length - 1) { - sql.append(", "); - } - } - sql.append(")"); - } - sql.append(","); - sql.append("key VARCHAR(50) NOT NULL,").append("value TEXT NOT NULL,").append("PRIMARY KEY(id)"); - } - statement.executeUpdate(sql.toString()); - - + statement.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + "(key VARCHAR(50) NOT NULL, value TEXT NOT NULL, PRIMARY KEY(key));"); } } @Override - public @Nullable String getValueById(int id) throws SQLException { + public @Nullable String getValue(String key) throws SQLException { connection.checkConnection(); - try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement("SELECT value FROM " + tableName + " WHERE id=?")) { - preparedStatement.setInt(1, id); - ResultSet resultSet = preparedStatement.executeQuery(); - if(resultSet.next()) { - return resultSet.getString(1); - } - return null; - } - } - - @Override - public @Nullable Result getValueByAdditionalColumnAndKey(String key, List> additionalColumns) throws SQLException { - connection.checkConnection(); - StringBuilder sql = new StringBuilder("SELECT id, value FROM ").append(tableName).append(" WHERE key=? AND "); - for(int i = 0; i < additionalColumns.size(); ++i) { - sql.append(additionalColumns.get(i).getLeft().columnName()).append("=?"); - if(i != additionalColumns.size() - 1) { - sql.append(" AND "); - } - } - sql.append(";"); - try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement(sql.toString())) { + try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement("SELECT value FROM " + tableName + " WHERE key=?;")) { preparedStatement.setString(1, key); - for(int i = 0; i < additionalColumns.size(); ++i) { - preparedStatement.setObject(i + 1, additionalColumns.get(i).getRight()); - } ResultSet resultSet = preparedStatement.executeQuery(); if(resultSet.next()) - return new Result(resultSet.getString(1), resultSet.getString(2)); + return resultSet.getString(1); return null; } } @Override - public long insertValue(String key, String value, List> additionalColumns) throws SQLException { + public void insertValue(String key, String value) throws SQLException { connection.checkConnection(); - StringBuilder sql = new StringBuilder("INSERT INTO " + tableName + "(key, value"); - for(int i = 0; i < additionalColumns.size(); ++i) { - if(i != additionalColumns.size() - 1) { - sql.append(", "); - } - sql.append(additionalColumns.get(i).getLeft().columnName()); - - } - sql.append(") VALUES (?, ?"); - for(int i = 0; i < additionalColumns.size(); ++i) { - if(i != additionalColumns.size() - 1) { - sql.append(", "); - } - sql.append("?"); - } - sql.append(");"); - try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS)){ + try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement("INSERT INTO " + tableName + "(key, value) VALUES (?, ?);")){ preparedStatement.setString(1, key); preparedStatement.setString(2, value); - for(int i = 0; i < additionalColumns.size(); ++i) { - preparedStatement.setObject(i + 3, additionalColumns.get(i).getRight()); - } preparedStatement.executeUpdate(); - try(ResultSet resultSet = preparedStatement.getGeneratedKeys()) { - if(resultSet.next()) { - return resultSet.getLong(1); - } - return -1L; - } } } @Override - public void updateValueById(String key, String value) throws SQLException { + public void updateValue(String key, String value) throws SQLException { connection.checkConnection(); - try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement("UPDATE " + tableName + " SET value=? WHERE key=?")) { + try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement("UPDATE " + tableName + " SET value=? WHERE key=?;")) { preparedStatement.setString(1, value); preparedStatement.setString(2, key); preparedStatement.executeUpdate(); } - - } - - @Override - public void updateValue(String key, String value, List> additionalColumns) throws SQLException { - connection.checkConnection(); - StringBuilder sql = new StringBuilder("UPDATE " + tableName + " SET value=? WHERE key=?"); - for(int i = 0; i < additionalColumns.size(); ++i) { - if(i != additionalColumns.size() - 1) { - sql.append(" AND "); - } - sql.append(additionalColumns.get(i).getLeft().columnName()).append("=?"); - } - sql.append(";"); - try(PreparedStatement preparedStatement = connection.getConnection().prepareStatement(sql.toString())) { - preparedStatement.setString(1, value); - preparedStatement.setString(2, key); - for(int i = 0; i < additionalColumns.size(); ++i) { - preparedStatement.setObject(3 + i, additionalColumns.get(i).getRight()); - } - preparedStatement.executeUpdate(); - } - } - - @Override - public List getAdditionColumns() { - return additionColumns; } } diff --git a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueTable.java b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueTable.java index 67079f9..6aa53c7 100644 --- a/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueTable.java +++ b/Database/src/main/java/fr/altarik/toolbox/database/keyvalue/KeyValueTable.java @@ -1,72 +1,36 @@ package fr.altarik.toolbox.database.keyvalue; -import net.minecraft.util.Pair; import org.jetbrains.annotations.Nullable; import java.sql.SQLException; -import java.util.List; public interface KeyValueTable { /** - * Return the value which correspond to the given id - * @param id primary key, unique id of the key value table - * @return the correspond value - * @throws SQLException when connection is lost - */ - @Nullable String getValueById(int id) throws SQLException; - - /** - *

Return the first value associated with the key and all the additional columns values given.

- *

In a perfect context, key and additional columns combinaison return a pseudo unique result.

+ *

Return the first value associated with the unique key.

* - * @param key String key of associated to the value, doesn't require to be unique - * @param additionalColumns Object keys of associated to the value, doesn't require to be unique - * @return first (id, value) pair associated with the key and additonal columns combinaison - * @throws SQLException if connection is lost, or if additional columns name or value format is incorrect + * @param key String key of associated to the value + * @return value associated with the key + * @throws SQLException if connection is lost */ - @Nullable Result getValueByAdditionalColumnAndKey(String key, List> additionalColumns) throws SQLException; + @Nullable String getValue(String key) throws SQLException; /** - *

Insert a new value in the table, associate key and additional columns with the value

- *

Key and additional columns doesn't need to be unique, the combinaison of them have the goal to be unique or it isn't a hard requirement

- * @param key String key which will be associated with the value, doesn't require to be unique + *

Insert a new value in the table, associated with key

+ * + * @param key String key which will be associated with the value, is unique * @param value String value which will be stored in the database - * @param additionalColumns Additional columns which will be associated with the value, doesn't require to be unique - * @return the id (unique id) of the newly inserted value - * @throws SQLException if connection is lost, or if additional columns name or value format is incorrect + * @throws SQLException if connection is lost or if {@code key} is not unique (already exist in database) */ - long insertValue(String key, String value, List> additionalColumns) throws SQLException; + void insertValue(String key, String value) throws SQLException; /** - * Update a row associated with the unique key {@code id} with the new value given in parameter - * @param key unique key associated with the value + *

Update value column of the row associated with the key by {@code value}

+ *

If {@code key} doesn't exist in table, will update no row without warning

+ * @param key String key which will is associated with the value, is unique * @param value new value * @throws SQLException if connection is lost */ - void updateValueById(String key, String value) throws SQLException; + void updateValue(String key, String value) throws SQLException; - /** - * Update all rows associated with the key and additional columns combinaisons with the new value given in parameter - * @param key String key which will is associated with the value, doesn't require to be unique - * @param value new value - * @param additionalColumns Additional columns which are associated with the value, doesn't require to be unique - * @throws SQLException if connection is lost, or if additional columns name or value format is incorrect - */ - void updateValue(String key, String value, List> additionalColumns) throws SQLException; - - /** - * Give the declared additional columns given during initialisation of the class - * @return list of declared additional columns - */ - List getAdditionColumns(); - - /** - * Result unique id and value pair stored in the table when using {@link KeyValueTable#getValueByAdditionalColumnAndKey(String, List)} - * @param id unique id the row - * @param value value store in the same row as {@code id} - */ - record Result(String id, String value) { - - } }