Interface PluginDatabaseManager


public interface PluginDatabaseManager
Database manager for plugin data persistence.

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);
    }
}

  • 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

      String getPluginId()
      Get the plugin ID this manager belongs to.
      Returns:
      the plugin ID
    • createTable

      boolean createTable(TableSchema schema)
      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

      boolean tableExists(String tableName)
      Check if a table exists.
      Parameters:
      tableName - the table name (without prefix)
      Returns:
      true if exists
    • dropTable

      boolean dropTable(String tableName)
      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

      <T> PluginRepository<T> getRepository(String tableName, Class<T> entityClass)
      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

      List<String> listTables()
      List all tables owned by this plugin.
      Returns:
      list of table names (without prefix)
    • 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

      boolean migrate(int targetVersion, PluginMigration migration)
      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 to
      migration - the migration to execute
      Returns:
      true if migration was executed, false if already at or past target version
    • getStats

      Get database statistics for this plugin.
      Returns:
      database stats