From 78cdab77f1df685c1e7abc1b71cd924b7181798a Mon Sep 17 00:00:00 2001 From: hsgamer Date: Wed, 11 Sep 2024 09:46:05 +0700 Subject: [PATCH] remove StatementBuilder rework later --- .../client/sql/ResultSetConsumer.java | 15 -- .../client/sql/ResultSetConverter.java | 22 -- .../hscore/database/client/sql/SqlClient.java | 28 --- .../database/client/sql/SqlExecutor.java | 23 -- .../database/client/sql/StatementBuilder.java | 212 ------------------ 5 files changed, 300 deletions(-) delete mode 100644 database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConsumer.java delete mode 100644 database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConverter.java delete mode 100644 database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlExecutor.java delete mode 100644 database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/StatementBuilder.java diff --git a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConsumer.java b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConsumer.java deleted file mode 100644 index 821a144d62..0000000000 --- a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConsumer.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.hsgamer.hscore.database.client.sql; - -import java.sql.ResultSet; - -/** - * The interface to consume the {@link ResultSet} - */ -public interface ResultSetConsumer { - /** - * Consume the result set - * - * @param resultSet The result set - */ - void consume(ResultSet resultSet); -} diff --git a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConverter.java b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConverter.java deleted file mode 100644 index 2f02535038..0000000000 --- a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/ResultSetConverter.java +++ /dev/null @@ -1,22 +0,0 @@ -package me.hsgamer.hscore.database.client.sql; - -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * The interface to convert the {@link ResultSet} to the final object - * - * @param the type of the object - */ -public interface ResultSetConverter { - /** - * Convert the result set to the final object - * - * @param resultSet The result set - * - * @return The final object - * - * @throws SQLException If an SQL error occurs - */ - T convert(ResultSet resultSet) throws SQLException; -} diff --git a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlClient.java b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlClient.java index 6270f1c149..c24c4a4929 100644 --- a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlClient.java +++ b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlClient.java @@ -1,12 +1,9 @@ package me.hsgamer.hscore.database.client.sql; import me.hsgamer.hscore.database.Client; -import me.hsgamer.hscore.logger.common.LogLevel; -import me.hsgamer.hscore.logger.provider.LoggerProvider; import java.sql.Connection; import java.sql.SQLException; -import java.util.Optional; /** * The interface for SQL client @@ -23,29 +20,4 @@ public interface SqlClient extends Client { * @throws SQLException if there is an SQL error */ Connection getConnection() throws SQLException; - - /** - * Create a new statement builder for this client - * - * @return the statement builder - * - * @throws SQLException if there is an SQL error - */ - default StatementBuilder createStatementBuilder() throws SQLException { - return StatementBuilder.create(this.getConnection()); - } - - /** - * Create a new statement builder for this client but ignores exceptions - * - * @return the statement builder - */ - default Optional createStatementBuilderSafe() { - try { - return Optional.of(this.createStatementBuilder()); - } catch (Exception e) { - LoggerProvider.getLogger(this.getClass()).log(LogLevel.WARN, e); - return Optional.empty(); - } - } } diff --git a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlExecutor.java b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlExecutor.java deleted file mode 100644 index 98ffd03d7c..0000000000 --- a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/SqlExecutor.java +++ /dev/null @@ -1,23 +0,0 @@ -package me.hsgamer.hscore.database.client.sql; - -import java.sql.PreparedStatement; -import java.sql.SQLException; - -/** - * The executor to apply the {@link PreparedStatement} - * - * @param The type of the result - */ -@FunctionalInterface -public interface SqlExecutor { - /** - * Apply the prepared statement to get the result - * - * @param preparedStatement The prepared statement - * - * @return The result - * - * @throws SQLException If an SQL error occurs - */ - T apply(PreparedStatement preparedStatement) throws SQLException; -} diff --git a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/StatementBuilder.java b/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/StatementBuilder.java deleted file mode 100644 index 72205f6228..0000000000 --- a/database/database-client-sql/src/main/java/me/hsgamer/hscore/database/client/sql/StatementBuilder.java +++ /dev/null @@ -1,212 +0,0 @@ -package me.hsgamer.hscore.database.client.sql; - -import me.hsgamer.hscore.logger.common.LogLevel; -import me.hsgamer.hscore.logger.common.Logger; -import me.hsgamer.hscore.logger.provider.LoggerProvider; -import org.intellij.lang.annotations.Language; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicReference; - -/** - * The {@link PreparedStatement} builder - */ -public class StatementBuilder { - private static final Logger LOGGER = LoggerProvider.getLogger(StatementBuilder.class); - private final Connection connection; - private final AtomicReference statement = new AtomicReference<>(""); - private final List values = new ArrayList<>(); - - private StatementBuilder(Connection connection) { - this.connection = connection; - } - - /** - * Create a new builder for the connection - * - * @param connection the connection - * - * @return the builder - */ - public static StatementBuilder create(Connection connection) { - return new StatementBuilder(connection); - } - - /** - * Add values to the statement - * - * @param values the values - * - * @return the builder for chaining - */ - public StatementBuilder addValues(Object... values) { - Collections.addAll(this.values, values); - return this; - } - - /** - * Add values to the statement - * - * @param values the values - * - * @return the builder for chaining - */ - public StatementBuilder addValues(List values) { - this.values.addAll(values); - return this; - } - - /** - * Execute the statement - * - * @param sqlExecutor the executor - * @param the type of the result - * - * @return the result - * - * @throws SQLException if there is an SQL error - */ - public T execute(SqlExecutor sqlExecutor) throws SQLException { - try (PreparedStatement preparedStatement = connection.prepareStatement(statement.get())) { - for (int i = 0; i < values.size(); i++) { - preparedStatement.setObject(i + 1, values.get(i)); - } - return sqlExecutor.apply(preparedStatement); - } - } - - /** - * Query from the connection and convert to the final object - * - * @param the type of the final object - * @param converter the converter - * - * @return the final object - * - * @throws SQLException if there is an SQL error - */ - public T query(ResultSetConverter converter) throws SQLException { - return this.execute(preparedStatement -> { - try (ResultSet resultSet = preparedStatement.executeQuery()) { - return converter.convert(resultSet); - } - }); - } - - /** - * Query from the connection and consume the result set - * - * @param consumer the consumer - * - * @throws SQLException if there is an SQL error - */ - public void consume(ResultSetConsumer consumer) throws SQLException { - this.execute(preparedStatement -> { - try (ResultSet resultSet = preparedStatement.executeQuery()) { - consumer.consume(resultSet); - } - return null; - }); - } - - /** - * Update the database - * - * @return the row count or 0 for nothing - * - * @throws SQLException if there is an SQL error - */ - public int update() throws SQLException { - return this.execute(PreparedStatement::executeUpdate); - } - - /** - * Query from the connection and convert to the final object, but ignores exceptions - * - * @param the type of the final object - * @param converter the converter - * - * @return the final object - */ - public Optional querySafe(ResultSetConverter converter) { - try { - return Optional.of(this.query(converter)); - } catch (Exception e) { - LOGGER.log(LogLevel.WARN, e); - return Optional.empty(); - } - } - - /** - * Query from the connection and consume the result set, but ignores exceptions - * - * @param consumer the consumer - */ - public void consumeSafe(ResultSetConsumer consumer) { - try { - this.consume(consumer); - } catch (Exception e) { - LOGGER.log(LogLevel.WARN, e); - } - } - - /** - * Update the database but ignores exceptions - * - * @return the row count or 0 for nothing - */ - public int updateSafe() { - try { - return this.update(); - } catch (Exception e) { - LOGGER.log(LogLevel.WARN, e); - return 0; - } - } - - /** - * Get the connection - * - * @return the connection - */ - public Connection getConnection() { - return connection; - } - - /** - * Get the statement - * - * @return the statement - */ - public String getStatement() { - return statement.get(); - } - - /** - * Set the statement to execute - * - * @param statement the statement - * - * @return the builder for chaining - */ - public StatementBuilder setStatement(@Language("SQL") String statement) { - this.statement.set(statement); - return this; - } - - /** - * Get the values - * - * @return the values - */ - public List getValues() { - return Collections.unmodifiableList(values); - } -}