diff --git a/pom.xml b/pom.xml index 164ca433..c4c1823f 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ CoreAPI - 1.287.2 + 1.288.0 1.61.2 diff --git a/src/main/java/minevalley/core/api/Core.java b/src/main/java/minevalley/core/api/Core.java index 9ad74893..2b8d7526 100644 --- a/src/main/java/minevalley/core/api/Core.java +++ b/src/main/java/minevalley/core/api/Core.java @@ -4,10 +4,7 @@ import minevalley.core.api.armorstand.FakeArmorStand; import minevalley.core.api.corporations.Group; import minevalley.core.api.corporations.companies.*; -import minevalley.core.api.database.DatabaseEntry; -import minevalley.core.api.database.DatabaseEntryCollection; -import minevalley.core.api.database.DatabaseTable; -import minevalley.core.api.database.Value; +import minevalley.core.api.database.StatementBuilder; import minevalley.core.api.discord.EmbeddedMessage; import minevalley.core.api.discord.Webhook; import minevalley.core.api.economy.BankAccount; @@ -59,12 +56,14 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; +import org.intellij.lang.annotations.Language; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.sql.SQLException; import java.text.SimpleDateFormat; import java.time.DayOfWeek; import java.util.*; @@ -178,6 +177,48 @@ public static BukkitTask runAsyncTaskPeriodically(long delay, long period, @Nonn return server.runAsyncTaskPeriodically(delay, period, task); } + /** + * Create a {@link StatementBuilder} based on the given SQL code. + * + * @param sql SQL code to prepare + * @return a new {@link StatementBuilder} instance + * @throws SQLException if a database access error occurs + */ + @Nonnull + @Contract("_ -> new") + public static StatementBuilder prepareSQL(@Nonnull @Language("SQL") String sql) throws SQLException { + return server.prepareSQL(sql, false); + } + + /** + * Create a {@link StatementBuilder} based on the given SQL code. + * + * @param sql SQL code to prepare + * @param retrieveGeneratedKeys whether to retrieve generated keys + * @return a new {@link StatementBuilder} instance + * @throws SQLException if a database access error occurs + */ + @Nonnull + @Contract("_, _ -> new") + public static StatementBuilder prepareSQL(@Nonnull @Language("SQL") String sql, boolean retrieveGeneratedKeys) throws SQLException { + return server.prepareSQL(sql, retrieveGeneratedKeys); + } + + /** + * Generate a random id based on the given length that was not used in the given column and table. + * + * @param table table to check + * @param column column to check + * @param amountOfChars amount of chars the id should have + * @return a unique id + * @throws IllegalArgumentException if the table is null, column is null or the amount of chars is less than 1 + * @throws SQLException if a database access error occurs + */ + @Contract("_, _, _ -> _") + public static int generateUniqueId(@Nonnull String table, @Nonnull String column, int amountOfChars) throws IllegalArgumentException, SQLException { + return server.generateUniqueId(table, column, amountOfChars); + } + /** * Registers an event listener. * @@ -318,91 +359,6 @@ public static void sendDebug(@Nonnull DebugType type, @Nonnull String message) { server.sendDebug(type, removeColorCodes(message)); } - /** - * Gets the specific database-entry from the specified table with the specified value in the column. - * If there are more than one entry, that math the given description, this gets the first one. - * If you want to get multiple entries, use database-collection, or database-table! - * - * @param tableName name of the table as string - * @param searchValue value according to which the entries are filtered in a specific column - * @return the first database-entry that matches the given description - */ - public static DatabaseEntry getDatabaseEntry(String tableName, Value searchValue) { - return server.getDatabaseEntry(tableName, searchValue); - } - - /** - * Gets the specific database-entry from the specified table with the specified value in the column. - * If there are more than one entry, that math the given description, this gets the first one. - * If you want to get multiple entries, use database-collection, or database-table! - * - * @param tableName name of the table as string - * @param searchValues value according to which the entries are filtered in a specific column - * @return the first database-entry that matches the given description - */ - public static DatabaseEntry getDatabaseEntryAnd(String tableName, Value... searchValues) { - return server.getDatabaseEntryAnd(tableName, searchValues); - } - - /** - * Gets the specific database-entry from the specified table with the specified value in the column. - * If there are more than one entry, that math the given description, this gets the first one. - * If you want to get multiple entries, use database-collection, or database-table! - * - * @param tableName name of the table as string - * @param searchValues value according to which the entries are filtered in a specific column - * @return the first database-entry that matches the given description - */ - public static DatabaseEntry getDatabaseEntryOr(String tableName, Value... searchValues) { - return server.getDatabaseEntryOr(tableName, searchValues); - } - - /** - * Gets a database-collection from the specified table with the specified value in the column. - * This gets all the entries that match the description. If you're searching for one single entry, use database-entry! - * - * @param tableName name of the table as string - * @param searchValue value according to which the entries are filtered in a specific column - * @return a collection of all database-entries in this table, that matches the given description - */ - public static DatabaseEntryCollection getDatabaseEntryCollection(String tableName, Value searchValue) { - return server.getDatabaseEntryCollection(tableName, searchValue); - } - - /** - * Gets a database-collection from the specified table with the specified value in the column. - * This gets all the entries that match the description. If you're searching for one single entry, use database-entry! - * - * @param tableName name of the table as string - * @param searchValues value according to which the entries are filtered in a specific column - * @return a collection of all database-entries in this table, that matches the given description - */ - public static DatabaseEntryCollection getDatabaseEntryCollectionAnd(String tableName, Value... searchValues) { - return server.getDatabaseEntryCollectionAnd(tableName, searchValues); - } - - /** - * Gets a database-collection from the specified table with the specified value in the column. - * This gets all the entries that match the description. If you're searching for one single entry, use database-entry! - * - * @param tableName name of the table as string - * @param searchValues value according to which the entries are filtered in a specific column - * @return a collection of all database-entries in this table, that matches the given description - */ - public static DatabaseEntryCollection getDatabaseEntryCollectionOr(String tableName, Value... searchValues) { - return server.getDatabaseEntryCollectionOr(tableName, searchValues); - } - - /** - * Gets the database-table with the specific name. - * - * @param tableName name of the database-table - * @return database-table with specific name - */ - public static DatabaseTable getDatabaseTable(String tableName) { - return server.getDatabaseTable(tableName); - } - /** * Sets the setting with the given key. *

diff --git a/src/main/java/minevalley/core/api/CoreServer.java b/src/main/java/minevalley/core/api/CoreServer.java index 44e6ff16..4b6eb23a 100644 --- a/src/main/java/minevalley/core/api/CoreServer.java +++ b/src/main/java/minevalley/core/api/CoreServer.java @@ -4,10 +4,7 @@ import minevalley.core.api.armorstand.FakeArmorStand; import minevalley.core.api.corporations.Group; import minevalley.core.api.corporations.companies.*; -import minevalley.core.api.database.DatabaseEntry; -import minevalley.core.api.database.DatabaseEntryCollection; -import minevalley.core.api.database.DatabaseTable; -import minevalley.core.api.database.Value; +import minevalley.core.api.database.StatementBuilder; import minevalley.core.api.discord.EmbeddedMessage; import minevalley.core.api.discord.Webhook; import minevalley.core.api.economy.BankAccount; @@ -55,11 +52,13 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; +import org.intellij.lang.annotations.Language; import org.jetbrains.annotations.ApiStatus; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.sql.SQLException; import java.time.DayOfWeek; import java.util.List; import java.util.UUID; @@ -84,6 +83,11 @@ public interface CoreServer { @Nonnull BukkitTask runAsyncTaskPeriodically(long delay, long period, @Nonnull Runnable runnable) throws IllegalArgumentException; + @Nonnull + StatementBuilder prepareSQL(@Nonnull @Language("SQL") String sql, boolean retrieveGeneratedKeys) throws SQLException; + + int generateUniqueId(@Nonnull String table, @Nonnull String column, int amountOfChars) throws IllegalArgumentException, SQLException; + void registerListener(@Nonnull Class cls, @Nonnull EventListener listener) throws IllegalArgumentException; void unregisterListener(@Nonnull Class cls, @Nonnull EventListener listener) throws IllegalArgumentException; @@ -111,20 +115,6 @@ public interface CoreServer { void sendDebug(@Nonnull DebugType type, @Nonnull String message); - DatabaseEntry getDatabaseEntry(String tableName, Value searchValue); - - DatabaseEntry getDatabaseEntryAnd(String tableName, Value... searchValues); - - DatabaseEntry getDatabaseEntryOr(String tableName, Value... searchValues); - - DatabaseEntryCollection getDatabaseEntryCollection(String tableName, Value searchValue); - - DatabaseEntryCollection getDatabaseEntryCollectionAnd(String tableName, Value... searchValues); - - DatabaseEntryCollection getDatabaseEntryCollectionOr(String tableName, Value... searchValues); - - DatabaseTable getDatabaseTable(String tableName); - void setSetting(@Nonnull String key, @Nonnull String value) throws IllegalArgumentException; String getSetting(String key); diff --git a/src/main/java/minevalley/core/api/database/ColumnType.java b/src/main/java/minevalley/core/api/database/ColumnType.java new file mode 100644 index 00000000..5ec19c1f --- /dev/null +++ b/src/main/java/minevalley/core/api/database/ColumnType.java @@ -0,0 +1,207 @@ +package minevalley.core.api.database; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public enum ColumnType { + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code BIT}. + */ + BIT(-7), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code TINYINT}. + */ + TINYINT(-6), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code SMALLINT}. + */ + SMALLINT(5), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code INTEGER}. + */ + INTEGER(4), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code BIGINT}. + */ + BIGINT(-5), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code FLOAT}. + */ + FLOAT(6), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code REAL}. + */ + REAL(7), + + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code DOUBLE}. + */ + DOUBLE(8), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code NUMERIC}. + */ + NUMERIC(2), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code DECIMAL}. + */ + DECIMAL(3), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code CHAR}. + */ + CHAR(1), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code VARCHAR}. + */ + VARCHAR(12), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code LONGVARCHAR}. + */ + LONGVARCHAR(-1), + + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code DATE}. + */ + DATE(91), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code TIME}. + */ + TIME(92), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code TIMESTAMP}. + */ + TIMESTAMP(93), + + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code BINARY}. + */ + BINARY(-2), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code VARBINARY}. + */ + VARBINARY(-3), + + /** + *

The constant in the Java programming language, sometimes referred + * to as a type code, that identifies the generic SQL type + * {@code LONGVARBINARY}. + */ + LONGVARBINARY(-4), + + /** + *

The constant in the Java programming language + * that identifies the generic SQL value + * {@code NULL}. + */ + NULL(0), + + /** + * The constant in the Java programming language that indicates + * that the SQL type is database-specific and + * gets mapped to a Java object that can be accessed via + * the methods {@code getObject} and {@code setObject}. + */ + OTHER(1111), + + /** + * The constant in the Java programming language, sometimes referred to + * as a type code, that identifies the generic SQL type + * {@code DISTINCT}. + * + * @since 1.2 + */ + DISTINCT(2001), + + + /** + * The constant in the Java programming language, sometimes referred to + * as a type code, that identifies the generic SQL type {@code BOOLEAN}. + * + * @since 1.4 + */ + BOOLEAN(16), + + /** + * The constant in the Java programming language, sometimes referred to + * as a type code, that identifies the generic SQL type {@code REF CURSOR}. + * + * @since 1.8 + */ + REF_CURSOR(2012), + + /** + * The constant in the Java programming language, sometimes referred to + * as a type code, that identifies the generic SQL type + * {@code TIME WITH TIMEZONE}. + * + * @since 1.8 + */ + TIME_WITH_TIMEZONE(2013), + + /** + * The constant in the Java programming language, sometimes referred to + * as a type code, that identifies the generic SQL type + * {@code TIMESTAMP WITH TIMEZONE}. + * + * @since 1.8 + */ + TIMESTAMP_WITH_TIMEZONE(2014); + + private final int type; +} diff --git a/src/main/java/minevalley/core/api/database/DatabaseEntry.java b/src/main/java/minevalley/core/api/database/DatabaseEntry.java deleted file mode 100644 index f93a8a09..00000000 --- a/src/main/java/minevalley/core/api/database/DatabaseEntry.java +++ /dev/null @@ -1,134 +0,0 @@ -package minevalley.core.api.database; - -import minevalley.core.api.Registrant; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.inventory.ItemStack; - -import java.sql.Array; - -public interface DatabaseEntry { - - /** - * Removes the specific database entry from the table. - */ - void remove(); - - /** - * Checks if the database-entry exists. - * - * @return true if the selected database entry exists in the table - */ - boolean exists(); - - /** - * Changes a value of the selected entry. - * - * @param value value object with the column and the new value - */ - void changeValue(Value value); - - /** - * Gets the string at the given column. - * - * @param column name of the column - * @return string at given column from the selected database entry - */ - String getString(String column); - - /** - * Gets the integer at the given column. - * - * @param column name of the column - * @return integer at given column from the selected database entry - */ - int getInteger(String column); - - /** - * Gets the boolean at the given column. - * - * @param column name of the column - * @return boolean at given column from the selected database entry - */ - boolean getBoolean(String column); - - /** - * Gets the double at the given column. - * - * @param column name of the column - * @return double at given column from the selected database entry - */ - double getDouble(String column); - - /** - * Gets the float at the given column. - * - * @param column name of the column - * @return float at given column from the selected database entry - */ - float getFloat(String column); - - /** - * Gets the long at the given column. - * - * @param column name of the column - * @return long at given column from the selected database entry - */ - long getLong(String column); - - /** - * Gets the byte at the given column. - * - * @param column name of the column - * @return byte at given column from the selected database entry - */ - byte getByte(String column); - - /** - * Gets the array at the given column. - * - * @param column name of the column - * @return array at given column from the selected database entry - */ - Array getArray(String column); - - /** - * Gets the location at the given column (with pitch & yaw). - * - * @param column name of the column - * @return location at given column from the selected database entry - */ - Location getLocation(String column); - - /** - * Gets the block at the given column. - * - * @param column name of the column - * @return block at given column from the selected database entry - */ - Block getBlock(String column); - - /** - * Gets the registrant at the given column. - * - * @param column name of the column - * @return registrant at given column from the selected database entry - */ - Registrant getRegistrant(String column); - - /** - * Gets the itemstack at the given column. - * - * @param column name of the column - * @return itemstack at given column from the selected database entry - */ - ItemStack getItemStack(String column); - - /** - * Gets the itemstack[] at the given column. - * - * @param column name of the column - * @return itemstack[] at given column from the selected database entry - */ - ItemStack[] getItemStacks(String column); -} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/database/DatabaseEntryCollection.java b/src/main/java/minevalley/core/api/database/DatabaseEntryCollection.java deleted file mode 100644 index 327c7196..00000000 --- a/src/main/java/minevalley/core/api/database/DatabaseEntryCollection.java +++ /dev/null @@ -1,18 +0,0 @@ -package minevalley.core.api.database; - -import java.util.List; - -public interface DatabaseEntryCollection { - - /** - * Gets a list of all entries in this collection. - * - * @return list of database entries - */ - List getEntries(); - - /** - * Removes every single entry of this collection from the database. - */ - void remove(); -} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/database/DatabaseTable.java b/src/main/java/minevalley/core/api/database/DatabaseTable.java deleted file mode 100644 index 8b54e8a4..00000000 --- a/src/main/java/minevalley/core/api/database/DatabaseTable.java +++ /dev/null @@ -1,25 +0,0 @@ -package minevalley.core.api.database; - -import java.util.List; - -public interface DatabaseTable { - - /** - * Gets all entries in the table - * - * @return list of all database-entries - */ - List getEntries(); - - /** - * Removes all entries from this table - */ - void clear(); - - /** - * Adds a new entry with the given values into the table - * - * @param entries values and their target column - */ - void addEntry(Value... entries); -} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/database/StatementBuilder.java b/src/main/java/minevalley/core/api/database/StatementBuilder.java new file mode 100644 index 00000000..cd455401 --- /dev/null +++ b/src/main/java/minevalley/core/api/database/StatementBuilder.java @@ -0,0 +1,419 @@ +package minevalley.core.api.database; + +import minevalley.core.api.Core; +import org.intellij.lang.annotations.Language; +import org.jetbrains.annotations.Contract; + +import javax.annotation.Nonnull; +import java.math.BigDecimal; +import java.sql.*; +import java.util.concurrent.CompletableFuture; + +/** + * This interface covers some but not nearly all features of the {@link PreparedStatement} interface as provided by the JDBC API. + * It's designed to provide some improvements such as an asynchronous execution and a builder pattern for setting parameters to reduce boilerplate code. + * Such as the {@link PreparedStatement} interface, the results of the query can be retrieved using the {@link ResultSet} interface. + *

You can call {@link StatementBuilder#executeUpdate()} or {@link StatementBuilder#executeQuery()} multiple times with or without changing parameters in between. + *

Use {@link StatementBuilder#unwrap()} to get the underlying {@link PreparedStatement} object if you need to use a method that is not covered by this interface. + *

The following code snippet demonstrates a possible way to use this interface: + *

+ * {@code
+ * Core.prepareSQL("UPDATE team SET note = ? WHERE unique_id = ?")
+ * .setString(1, "Coolest boss ever")
+ * .setString(2, "e0ae458e-214c-409e-ad6d-4091a6719bb0")
+ * .executeUpdate();
+ * }
+ * 
+ * + * @see Core#prepareSQL(String) + * @see PreparedStatement + * @see ResultSet + */ +@SuppressWarnings("unused") +public interface StatementBuilder extends AutoCloseable { + + /** + * Gets the SQL code that will be executed when calling one of the {@code execute} methods. + * + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + */ + @Nonnull + @Contract(pure = true) + String getSQL() throws IllegalStateException; + + /** + * Sets the SQL code that will be executed when calling one of the {@code execute} methods. + * + * @param sql the SQL code to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code sql} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + */ + @Nonnull + @Contract("_ -> this") + StatementBuilder setSQL(@Nonnull @Language("SQL") String sql) throws IllegalArgumentException, IllegalStateException; + + /** + * Sets the parameter at the given index to SQL {@code NULL}. + *

+ * Note: Even though JDBC handles untyped {@code NULL} values pretty well, it is mandatory to provide the {@link ColumnType} of the parameter. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param type the {@link ColumnType} of the parameter + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setNull(int parameterIndex, @Nonnull ColumnType type) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code boolean} value. + *

Representation: {@code boolean} values are converted to SQL {@code BIT} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param bool the {@code boolean} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setBoolean(int parameterIndex, boolean bool) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code byte} value. + *

Representation: {@code byte} values are converted to SQL {@code TINYINT} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param b the {@code byte} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setByte(int parameterIndex, byte b) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code short} value. + *

Representation: {@code short} values are converted to SQL {@code SMALLINT} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param s the {@code short} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setShort(int parameterIndex, short s) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code int} value. + *

Representation: {@code int} values are converted to SQL {@code INTEGER} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param i the {@code int} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setInt(int parameterIndex, int i) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code long} value. + *

Representation: {@code long} values are converted to SQL {@code BIGINT} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param l the {@code long} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setLong(int parameterIndex, long l) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code float} value. + *

Representation: {@code float} values are converted to SQL {@code FLOAT} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param f the {@code float} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setFloat(int parameterIndex, float f) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code double} value. + *

Representation: {@code double} values are converted to SQL {@code DOUBLE} values

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param d the {@code double} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setDouble(int parameterIndex, double d) throws IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link BigDecimal} value. + *

Representation: {@link BigDecimal} values are converted to SQL {@code DECIMAL} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param bd the {@link BigDecimal} value to set. + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code bd} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setBigDecimal(int parameterIndex, @Nonnull BigDecimal bd) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link String} value. + *

Representation: {@link String} values are converted to SQL {@code VARCHAR} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param s the {@link String} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code s} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setString(int parameterIndex, @Nonnull String s) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@code byte} array. + *

Representation: {@code byte} arrays are converted to SQL {@code VARBINARY} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param bytes the {@code byte} array to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code bytes} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setBytes(int parameterIndex, @Nonnull byte[] bytes) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link Date} value. + *

Representation: {@link Date} values are converted to SQL {@code DATE} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param date the {@link Date} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code date} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setDate(int parameterIndex, @Nonnull Date date) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link Time} value. + *

Representation: {@link Time} values are converted to SQL {@code TIME} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param time the {@link Time} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code time} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setTime(int parameterIndex, @Nonnull Time time) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link Timestamp} value. + *

Representation: {@link Timestamp} values are converted to SQL {@code TIMESTAMP} values

+ *

+ * Note: This method is not meant to handle {@code NULL} values. Use {@link StatementBuilder#setNull(int, ColumnType)} instead. + * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param timestamp the {@link Timestamp} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code timestamp} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setTimestamp(int parameterIndex, @Nonnull Timestamp timestamp) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link Object} value. + *

+ * Note: + *

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param obj the {@link Object} value to set + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code obj} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _ -> this") + StatementBuilder setObject(int parameterIndex, @Nonnull Object obj) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Sets the parameter at the given index to the given {@link Object} value. + *

+ * Note: + *

+ * + * @param parameterIndex the index of the parameter to set, beginning with 1 + * @param obj the {@link Object} value to set + * @param type the {@link ColumnType} of the parameter + * @return this {@code StatementBuilder} object + * @throws IllegalArgumentException if {@code obj} is {@code null} + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or if {@code parameterIndex} does not correspond to a parameter marker in the SQL statement + */ + @Nonnull + @Contract("_, _, _ -> this") + StatementBuilder setObject(int parameterIndex, @Nonnull Object obj, @Nonnull ColumnType type) throws IllegalArgumentException, IllegalStateException, SQLException; + + /** + * Clears all parameters that have been set so far. + * + * @return this {@code StatementBuilder} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs + */ + @Nonnull + @Contract("-> this") + StatementBuilder clearParameters() throws IllegalStateException, SQLException; + + /** + * Executes the SQL query that has been built so far. + *

+ * Note: This method should be used for queries that do not return a result set. + * + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs + */ + @Contract(pure = true) + void executeUpdate() throws IllegalStateException, SQLException; + + /** + * Executes the SQL query that has been built so far asynchronously. + *

+ * Note: This method should be used for queries that do not return a result set. + * + * @return a {@link CompletableFuture} that will be completed once the query has been executed + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + */ + @Nonnull + @Contract(pure = true) + CompletableFuture executeUpdateAsync() throws IllegalStateException; + + /** + * Executes the SQL query that has been built so far and retrieves the generated key. + *

+ * Note: This method should be used for queries that do not return a result set. + * + * @return the generated key + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} or this statement builder is not supposed to retrieve generated keys + * @throws SQLException if a database access error occurs + */ + @Contract(pure = true) + int executeUpdateAndRetrieveKey() throws IllegalStateException, SQLException; + + /** + * Executes the SQL query that has been built so far and retrieves the generated key asynchronously. + *

+ * Note: This method should be used for queries that do not return a result set. + * + * @return a {@link CompletableFuture} that will be completed once the query has been executed and the key has been retrieved + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} or this statement builder is not supposed to retrieve generated keys + */ + @Nonnull + @Contract(pure = true) + CompletableFuture executeUpdateAndRetrieveKeyAsync() throws IllegalStateException; + + /** + * Executes the SQL query that has been built so far and returns the result set. + *

+ * Note: This method should be used for queries that return a result set. + * + * @return the result set + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + * @throws SQLException if a database access error occurs or the query does not return a result set + */ + @Nonnull + @Contract(pure = true) + ResultSet executeQuery() throws IllegalStateException, SQLException; + + /** + * Executes the SQL query that has been built so far and returns the result set asynchronously. + *

+ * Note: This method should be used for queries that return a result set. + * + * @return a {@link CompletableFuture} that will be completed once the query has been executed and the result set has been retrieved + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + */ + @Nonnull + @Contract(pure = true) + CompletableFuture executeQueryAsync() throws IllegalStateException; + + /** + * Returns a copy of the underlying {@link PreparedStatement} object. + *

+ * Note: + *

+ * + * @return a copy of the underlying {@link PreparedStatement} object + * @throws IllegalStateException if this method is called on a closed {@code StatementBuilder} + */ + @Nonnull + @Contract(pure = true) + PreparedStatement unwrap() throws IllegalStateException; + + /** + * Closes the {@code StatementBuilder} and releases any resources it holds. + * + * @throws SQLException if a database access error occurs + */ + @Override + void close() throws SQLException; +} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/database/Value.java b/src/main/java/minevalley/core/api/database/Value.java deleted file mode 100644 index 880c2315..00000000 --- a/src/main/java/minevalley/core/api/database/Value.java +++ /dev/null @@ -1,12 +0,0 @@ -package minevalley.core.api.database; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public final class Value { - - private final String column; - private final Object value; -} \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/events/StatisticCreateEvent.java b/src/main/java/minevalley/core/api/events/StatisticCreateEvent.java index 734c7a79..d63ac323 100644 --- a/src/main/java/minevalley/core/api/events/StatisticCreateEvent.java +++ b/src/main/java/minevalley/core/api/events/StatisticCreateEvent.java @@ -1,26 +1,20 @@ package minevalley.core.api.events; import lombok.RequiredArgsConstructor; -import minevalley.core.api.database.DatabaseEntry; -import minevalley.core.api.database.Value; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @RequiredArgsConstructor public class StatisticCreateEvent extends Event { - public static final HandlerList HANDLER_LIST = new HandlerList(); + // TODO 29.12.2024: recently removed DatabaseEntry object due to rework of database util might not work anymore (StatisticCreateEvent) - private final DatabaseEntry entry; + public static final HandlerList HANDLER_LIST = new HandlerList(); public static HandlerList getHandlerList() { return HANDLER_LIST; } - public void setStatistic(String key, double value) { - entry.changeValue(new Value(key, value)); - } - @Override public HandlerList getHandlers() { return HANDLER_LIST; diff --git a/src/main/java/minevalley/core/api/users/OnTimeHandler.java b/src/main/java/minevalley/core/api/users/OnTimeHandler.java index 3fd9351d..3d0d9e63 100644 --- a/src/main/java/minevalley/core/api/users/OnTimeHandler.java +++ b/src/main/java/minevalley/core/api/users/OnTimeHandler.java @@ -1,7 +1,6 @@ package minevalley.core.api.users; import lombok.Setter; -import minevalley.core.api.database.Value; import java.time.LocalDate; import java.util.Map;