Added builder, DataTracker and KeyValue #16

Manually merged
quentinlegot merged 40 commits from dev into master 2023-06-13 22:15:40 +02:00
3 changed files with 25 additions and 195 deletions
Showing only changes of commit 17cf359e83 - Show all commits

View File

@ -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<KeyValueConnection> {
private final RequiredParamBuilder<String> tableName;
private final RequiredCollectionParameterBuilder<AdditionalColumn, List<AdditionalColumn>> additionalColumns;
private final RequiredParamBuilder<SqlConnection> 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<KeyValueConnection> {
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<AdditionalColumn, Object> 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());
}
}

View File

@ -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<KeyValueBuilder.AdditionalColumn> additionColumns;
public KeyValueConnection(@NotNull SqlConnection connection, @NotNull String tableName, @NotNull List<KeyValueBuilder.AdditionalColumn> 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<Pair<KeyValueBuilder.AdditionalColumn, Object>> 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<Pair<KeyValueBuilder.AdditionalColumn, Object>> 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<Pair<KeyValueBuilder.AdditionalColumn, Object>> 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<KeyValueBuilder.AdditionalColumn> getAdditionColumns() {
return additionColumns;
}
}

View File

@ -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;
/**
* <p>Return the first value associated with the key and all the additional columns values given.</p>
* <p>In a perfect context, key and additional columns combinaison return a pseudo unique result.</p>
* <p>Return the first value associated with the unique key.</p>
*
* @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<Pair<KeyValueBuilder.AdditionalColumn, Object>> additionalColumns) throws SQLException;
@Nullable String getValue(String key) throws SQLException;
/**
* <p>Insert a new value in the table, associate key and additional columns with the value</p>
* <p>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</p>
* @param key String key which will be associated with the value, doesn't require to be unique
* <p>Insert a new value in the table, associated with key</p>
*
* @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<Pair<KeyValueBuilder.AdditionalColumn, Object>> 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
* <p>Update value column of the row associated with the key by {@code value}</p>
* <p>If {@code key} doesn't exist in table, will update no row without warning</p>
* @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;
/**
* 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<Pair<KeyValueBuilder.AdditionalColumn, Object>> additionalColumns) throws SQLException;
/**
* Give the declared additional columns given during initialisation of the class
* @return list of declared additional columns
*/
List<KeyValueBuilder.AdditionalColumn> 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) {
void updateValue(String key, String value) throws SQLException;
}
}