Class Database
java.lang.Object
com.github.pinont.singularitylib.api.storage.Database
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA consumer that may throw.static interfaceA function that may throw. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static DatabasemySql(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).abstract ConnectionOpens (and caches) a raw connection.<T> CompletableFuture<T> query(Database.ThrowingFunction<Connection, T> mapper) Runs a query asynchronously; the function maps the result set to a value.Runs an async DB task with a raw connection, completing when done.static DatabaseCreates a SQLite-backed database stored at the given file.Executes an update/DDL statement asynchronously.
-
Field Details
-
plugin
protected final org.bukkit.plugin.Plugin plugin
-
-
Constructor Details
-
Database
protected Database(org.bukkit.plugin.Plugin plugin)
-
-
Method Details
-
openConnection
Opens (and caches) a raw connection.- Throws:
SQLException
-
getConnection
- Returns:
- a cached, open connection
- Throws:
SQLException
-
update
Executes an update/DDL statement asynchronously.- Parameters:
sql- the SQLparameters- optional positional parameters (?)- Returns:
- a future completing when the update is done
-
query
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:
closein interfaceAutoCloseable
-
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
-