Class SQLStorage
java.lang.Object
me.tippie.tippieutils.v1_2_0.storage.SQLStorage
Class that can be used as base for a SQL storage class.
- Since:
- 1.2.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionSQLStorage(org.bukkit.plugin.Plugin plugin, Driver driver, SQLStorage.SQLType type, File file)Initialise the SQLStorage base for an embedded database. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> CompletableFuture<T>prepareStatement(@NotNull Function<PreparedStatement,T> function)Get a prepared statement from the query in theSqlQueryannotation.protected voidrunResourceScript(@NotNull String file)Run a sql script from the resources of this plugin.
-
Constructor Details
-
SQLStorage
public SQLStorage(org.bukkit.plugin.Plugin plugin, Driver driver, SQLStorage.SQLType type, File file)Initialise the SQLStorage base for an embedded database.- Parameters:
plugin- The plugin that is using this storage.driver- The SQL driver to use.type- The type of database to use.file- The file where the database should be embedded.
-
-
Method Details
-
runResourceScript
Run a sql script from the resources of this plugin.- Parameters:
file- The name of the file in the resources.- Throws:
SQLException- When aSQLExceptionoccurs whilst running the script.IOException- When a IOException occurs whilst reading the script.
-
prepareStatement
protected <T> CompletableFuture<T> prepareStatement(@NotNull @NotNull Function<PreparedStatement,T> function)Get a prepared statement from the query in theSqlQueryannotation. Example of correct usage of this method inside a class that extendsSQLStorage:@SqlQuery("SELECT * FROM table WHERE id = ?") // In this case the 'test' column will have an int value. public CompletableFuture<Optional<Integer>> getRow(int id) { return prepareStatement((stmt) -> { try { stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); if (rs.next()) { return Optional.of(rs.getInt("TEST")); } else { return Optional.empty(); } } catch (SQLException e) { throw new RuntimeException(e); } }); }- Type Parameters:
T- The return value of the consumer- Parameters:
function- A function from aPreparedStatementto aSQLStorage.- Returns:
- A
CompletableFuturethat will be completed when function is finished
-