Class SQLStorage

java.lang.Object
me.tippie.tippieutils.v1_2_0.storage.SQLStorage

public class SQLStorage extends Object
Class that can be used as base for a SQL storage class.
Since:
1.2.0
  • 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

      protected void runResourceScript(@NotNull @NotNull String file) throws SQLException, IOException
      Run a sql script from the resources of this plugin.
      Parameters:
      file - The name of the file in the resources.
      Throws:
      SQLException - When a SQLException occurs 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 the SqlQuery annotation. Example of correct usage of this method inside a class that extends SQLStorage:
           @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 a PreparedStatement to a SQLStorage.
      Returns:
      A CompletableFuture that will be completed when function is finished