Interface PluginDatabaseManager
Each plugin gets its own isolated database schema for data isolation. Plugins interact with the database through a JPA-like repository pattern - no raw SQL is allowed.
Example usage:
@Plugin(name = "MyPlugin", version = "1.0.0", author = "Author")
public class MyPlugin {
private PluginRepository<MyEntity> repository;
@OnEnable
public void onEnable(PluginContext context) {
PluginDatabaseManager db = context.getDatabaseManager();
// Define your table schema
TableSchema schema = TableSchema.builder("my_data")
.column("name", ColumnType.STRING, 255, false)
.column("count", ColumnType.INTEGER, false)
.column("active", ColumnType.BOOLEAN, false)
.column("data", ColumnType.TEXT, true) // nullable
.build();
// Create the table (idempotent - safe to call every startup)
db.createTable(schema);
// Get a repository for CRUD operations
repository = db.getRepository("my_data", MyEntity.class);
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordDatabase statistics. -
Method Summary
Modifier and TypeMethodDescriptionbooleancreateTable(TableSchema schema) Create a table for this plugin.booleanDrop a table.Get a simple key-value store for this plugin.Get the plugin ID this manager belongs to.<T> PluginRepository<T> getRepository(String tableName, Class<T> entityClass) Get a repository for CRUD operations on a table.Get the database schema name assigned to this plugin.intGet the current schema version for this plugin.getStats()Get database statistics for this plugin.List all tables owned by this plugin.booleanmigrate(int targetVersion, PluginMigration migration) Execute a migration if needed.voidsetSchemaVersion(int version) Set the schema version for this plugin.booleantableExists(String tableName) Check if a table exists.
-
Method Details
-
getSchemaName
String getSchemaName()Get the database schema name assigned to this plugin.All tables created by this plugin will be created in this schema. Format: "plugin_{pluginId}" (e.g., "plugin_myplugin")
- Returns:
- the plugin's database schema name
-
getPluginId
-
createTable
Create a table for this plugin.The table name will be automatically prefixed with the plugin's prefix. This operation is idempotent - calling it multiple times is safe.
- Parameters:
schema- the table schema definition- Returns:
- true if created, false if already exists
-
tableExists
Check if a table exists.- Parameters:
tableName- the table name (without prefix)- Returns:
- true if exists
-
dropTable
Drop a table.Warning: This permanently deletes all data in the table.
- Parameters:
tableName- the table name (without prefix)- Returns:
- true if dropped, false if didn't exist
-
getRepository
Get a repository for CRUD operations on a table.- Type Parameters:
T- the entity type- Parameters:
tableName- the table name (without prefix)entityClass- the entity class for mapping- Returns:
- a repository instance
-
getKeyValueStore
PluginKeyValueStore getKeyValueStore()Get a simple key-value store for this plugin.Useful for storing configuration or simple data without defining schemas.
- Returns:
- the key-value store
-
listTables
-
getSchemaVersion
int getSchemaVersion()Get the current schema version for this plugin.Used for migration management.
- Returns:
- current schema version, or 0 if not set
-
setSchemaVersion
void setSchemaVersion(int version) Set the schema version for this plugin.Call this after successfully applying migrations.
- Parameters:
version- the new schema version
-
migrate
Execute a migration if needed.The migration will only run if the current schema version is less than the target version. After successful migration, the schema version is updated.
- Parameters:
targetVersion- the version this migration upgrades tomigration- the migration to execute- Returns:
- true if migration was executed, false if already at or past target version
-
getStats
PluginDatabaseManager.DatabaseStats getStats()Get database statistics for this plugin.- Returns:
- database stats
-