Class Database

java.lang.Object
com.github.pinont.singularitylib.api.storage.Database
All Implemented Interfaces:
AutoCloseable

public abstract class Database extends Object implements AutoCloseable
Async-first database facade (Phase 2). Supports MySQL and SQLite.

Usage:

Database db = Database.sqlite(plugin, "data.db");           // or .mySql(plugin, cfg)
db.update("CREATE TABLE IF NOT EXISTS ...");
db.query(conn -> { ... return rs ...; }).thenAccept(...);

All query/update methods run on a shared background executor (never the main thread), and you schedule UI work back onto the server via the lib's Scheduler facade.

  • Field Details

    • plugin

      protected final org.bukkit.plugin.Plugin plugin
  • Constructor Details

    • Database

      protected Database(org.bukkit.plugin.Plugin plugin)
  • Method Details

    • openConnection

      public abstract Connection openConnection() throws SQLException
      Opens (and caches) a raw connection.
      Throws:
      SQLException
    • getConnection

      public Connection getConnection() throws SQLException
      Returns:
      a cached, open connection
      Throws:
      SQLException
    • update

      public CompletableFuture<Void> update(String sql, Object... parameters)
      Executes an update/DDL statement asynchronously.
      Parameters:
      sql - the SQL
      parameters - optional positional parameters (?)
      Returns:
      a future completing when the update is done
    • query

      public <T> CompletableFuture<T> query(Database.ThrowingFunction<Connection, T> mapper)
      Runs a query asynchronously; the function maps the result set to a value.
      Type Parameters:
      T - result type
      Parameters:
      mapper - receives the connection, returns a result
      Returns:
      a future with the mapped result
    • runAsync

      Runs an async DB task with a raw connection, completing when done.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • mySql

      public static Database mySql(org.bukkit.plugin.Plugin plugin, org.bukkit.configuration.ConfigurationSection cfg)
      Creates a MySQL-backed database from a config section (database.host/port/name/user/password).
    • sqlite

      public static Database sqlite(org.bukkit.plugin.Plugin plugin, String fileName)
      Creates a SQLite-backed database stored at the given file.