Class DataManager
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Enum representing the types of databases supported. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate com.zaxxer.hikari.HikariDataSource
The data source used for database connections.private final Graves
The main plugin instance associated with Graves.private DataManager.Type
The type of the component or event. -
Constructor Summary
ConstructorsConstructorDescriptionDataManager
(Graves plugin) Initializes the DataManager with the specified plugin instance and sets up the database connection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBlockData
(BlockData blockData) Adds block data to the database.private void
addColumnIfNotExists
(String tableName, String columnName, String columnDefinition) Adds a column to a table if it does not exist.void
addEntityData
(EntityData entityData) Adds entity data to the database.void
Adds a grave to the database.void
addHologramData
(HologramData hologramData) Adds hologram data to the database.private void
addMySQLProperties
(com.zaxxer.hikari.HikariConfig config, boolean useSSL, boolean allowPublicKeyRetrieval, boolean verifyServerCertificate) Adds MySQL or MariaDB-specific properties to the HikariConfig.private void
addPostgreSQLProperties
(com.zaxxer.hikari.HikariConfig config, boolean ssl) Adds PostgreSQL-specific properties to the HikariConfig.private void
Adjusts the grave table for the target database if necessary for MSSQL.private void
alterColumnIfExists
(String tableName, String columnName, String columnDefinition) Alters an existing column’s definition if the column already exists in the given table.private void
Checks if the Database is locked and attempts to unlock the database.private void
void
Closes the database connection.private void
closeConnection
(Connection connection) Closes a database connection.private void
closeResultSet
(ResultSet resultSet) Closes a ResultSet.private void
closeStatement
(Statement statement) Closes a statement.private void
configureH2
(com.zaxxer.hikari.HikariConfig config) Configures the H2 data source.private void
configureMSSQL
(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for Microsoft SQL Server (MSSQL).private void
configureMySQLOrMariaDB
(com.zaxxer.hikari.HikariConfig config, DataManager.Type type) Configures the HikariConfig for MySQL or MariaDB.private void
configurePostgreSQL
(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for PostgreSQL.private void
configureSQLite
(com.zaxxer.hikari.HikariConfig config) Configures the SQLite data source.private void
Creates the entity data map table if it does not exist.Returns the table name for the specified entity data type.private void
executeBatch
(Statement statement) Executes a batch of SQL statements.private ResultSet
executeQuery
(String sql) Executes a query SQL statement.private ResultSet
executeQuery
(String sql, Object[] params) Executes a query SQL statement with parameters.private void
executeUpdate
(String sql, Object[] parameters) Executes an update SQL statement with parameters.getChunkData
(org.bukkit.Location location) Retrieves chunk data for a specified location.getColumnList
(String tableName) Retrieves a list of columns for a specified table.private Connection
Retrieves a connection from the data source.Retrieves the version of the database connection type.Retrieves the versions of supported databases.private String
Retrieves the configured storage prefix for GravesX from the plugin configuration.getType()
private void
Handles unlocking for Microsoft SQL Server databases using COMMIT.private void
Handles unlocking for MySQL and MariaDB databases.private void
Handles unlocking for PostgreSQL/H2 databases using COMMIT or Rollback.private void
Handles unlocking for SQLite databases using COMMIT or Rollback.boolean
hasChunkData
(org.bukkit.Location location) Checks if chunk data exists for a specified location.boolean
hasGraveAtLocation
(org.bukkit.Location location) private boolean
Checks if the database connection is active.private boolean
isIntegrationEnabled
(String integration) Checks if the integration is enabled.private boolean
isVersionGreaterThan
(String version) private void
Keeps the database connection alive by periodically executing a query.private void
load()
Loads data from the database asynchronously.void
Loads the block map from the database.private void
loadEntityDataMap
(String table, EntityData.Type type) Loads entity data from the database.private void
loadEntityMap
(String table, EntityData.Type type) Loads an entity map from the database.void
Loads the grave map from the database.void
Loads the hologram map from the database.private void
Loads database tables.void
loadType
(DataManager.Type type) Loads the database type and sets up the data source.private String
mapSQLiteTypeToH2
(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding H2 type.private String
mapSQLiteTypeToMSSQL
(String sqliteType, String columnName) Maps a SQLite column type (including length specifiers) to the corresponding MSSQL type.private String
mapSQLiteTypeToMySQL
(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding MySQL/MariaDB type.private String
mapSQLiteTypeToPostgreSQL
(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding PostgreSQL type.private String
mapSQLiteTypeToTargetDB
(String sqliteType, String columnName) Maps a SQLite data type to the appropriate type for the configured target database (MySQL/MariaDB, PostgreSQL, H2, or MSSQL).private void
migrate()
Migrates data from the plugin’s SQLite schema into the configured target database.private void
Migrates root data to a sub-data directory.void
reload()
Reloads the data manager with the current type.void
reload
(DataManager.Type type) Reloads the data manager with the specified type.void
removeBlockData
(org.bukkit.Location location) Removes block data from the database.void
removeChunkData
(ChunkData chunkData) Removes chunk data.void
removeEntityData
(EntityData entityData) Removes entity data from the database.void
removeEntityData
(List<EntityData> entityDataList) Removes a list of entity data from the database.void
removeGrave
(Grave grave) Removes a grave from the database.void
removeGrave
(UUID uuid) void
removeHologramData
(Grave grave) Removes hologram entries from the database based on the grave UUID.void
removeHologramData
(List<EntityData> entityDataList) Deprecated, for removal: This API element is subject to removal in a future version.resultSetToGrave
(ResultSet resultSet) Converts a ResultSet to a Grave object.void
Sets up the block table in the database.private void
setupEntityTable
(String name) Sets up an entity table in the database.private void
Sets up entity tables.void
Sets up the grave table in the database.void
Sets up the hologram table in the database.boolean
tableExists
(String tableName) Checks if a table exists in the database.private boolean
Tests the MySQL connection.void
updateGrave
(Grave grave, String column, int integer) Updates a grave in the database.void
updateGrave
(Grave grave, String column, String string) Updates a grave in the database.
-
Field Details
-
plugin
The main plugin instance associated with Graves.This
Graves
instance represents the core plugin that this Graves is part of. It provides access to the plugin's functionality, configuration, and other services. -
type
The type of the component or event.This
DataManager.Type
represents the specific type or category of this component or event. -
dataSource
private com.zaxxer.hikari.HikariDataSource dataSourceThe data source used for database connections.This
HikariDataSource
provides the connection pool for interacting with the database.
-
-
Constructor Details
-
DataManager
Initializes the DataManager with the specified plugin instance and sets up the database connection.- Parameters:
plugin
- the Graves plugin instance.
-
-
Method Details
-
getType
-
load
private void load()Loads data from the database asynchronously. -
isIntegrationEnabled
Checks if the integration is enabled.- Parameters:
integration
- The name of the integration.- Returns:
- true if enabled, false otherwise.
-
loadTables
Loads database tables.- Throws:
SQLException
- if an SQL error occurs.
-
setupEntityTables
Sets up entity tables.- Throws:
SQLException
- if an SQL error occurs.
-
reload
public void reload()Reloads the data manager with the current type. -
reload
Reloads the data manager with the specified type.- Parameters:
type
- the type of database.
-
loadType
Loads the database type and sets up the data source.- Parameters:
type
- the type of database.
-
checkMariaDBasMySQL
private void checkMariaDBasMySQL() -
isVersionGreaterThan
-
configureSQLite
private void configureSQLite(com.zaxxer.hikari.HikariConfig config) Configures the SQLite data source.- Parameters:
config
- the HikariConfig to configure.
-
configurePostgreSQL
private void configurePostgreSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for PostgreSQL.- Parameters:
config
- the HikariConfig to configure.
-
addPostgreSQLProperties
private void addPostgreSQLProperties(com.zaxxer.hikari.HikariConfig config, boolean ssl) Adds PostgreSQL-specific properties to the HikariConfig.- Parameters:
config
- the HikariConfig to which properties will be added.ssl
- whether SSL is enabled.
-
configureH2
private void configureH2(com.zaxxer.hikari.HikariConfig config) Configures the H2 data source.- Parameters:
config
- the HikariConfig to configure.
-
configureMySQLOrMariaDB
Configures the HikariConfig for MySQL or MariaDB.- Parameters:
config
- the HikariConfig to configure.type
- the type of database (MYSQL or MARIADB).
-
addMySQLProperties
private void addMySQLProperties(com.zaxxer.hikari.HikariConfig config, boolean useSSL, boolean allowPublicKeyRetrieval, boolean verifyServerCertificate) Adds MySQL or MariaDB-specific properties to the HikariConfig.- Parameters:
config
- the HikariConfig to which properties will be added.useSSL
- whether SSL is enabled.allowPublicKeyRetrieval
- whether to allow public key retrieval.verifyServerCertificate
- whether to verify the server certificate.
-
configureMSSQL
private void configureMSSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for Microsoft SQL Server (MSSQL).- Parameters:
config
- the HikariConfig to configure.
-
migrateRootDataSubData
private void migrateRootDataSubData()Migrates root data to a sub-data directory. -
hasChunkData
public boolean hasChunkData(org.bukkit.Location location) Checks if chunk data exists for a specified location.- Parameters:
location
- the location to check.- Returns:
- true if chunk data exists, false otherwise.
-
getChunkData
Retrieves chunk data for a specified location.- Parameters:
location
- the location to retrieve chunk data for.- Returns:
- the chunk data.
-
removeChunkData
Removes chunk data.- Parameters:
chunkData
- the chunk data to remove.
-
getColumnList
Retrieves a list of columns for a specified table.- Parameters:
tableName
- the table name.- Returns:
- the list of columns.
-
tableExists
Checks if a table exists in the database.- Parameters:
tableName
- the table name.- Returns:
- true if the table exists, false otherwise.
-
addColumnIfNotExists
private void addColumnIfNotExists(String tableName, String columnName, String columnDefinition) throws SQLException Adds a column to a table if it does not exist.- Parameters:
tableName
- the table name.columnName
- the column name.columnDefinition
- the column definition.- Throws:
SQLException
- if an SQL error occurs.
-
alterColumnIfExists
private void alterColumnIfExists(String tableName, String columnName, String columnDefinition) throws SQLException Alters an existing column’s definition if the column already exists in the given table.- Parameters:
tableName
- the name of the table to modifycolumnName
- the name of the column to altercolumnDefinition
- the SQL definition to apply (type, length, etc.)- Throws:
SQLException
- if an SQL error occurs during the ALTER TABLE operation
-
setupGraveTable
Sets up the grave table in the database.- Throws:
SQLException
- if an SQL error occurs.
-
setupBlockTable
Sets up the block table in the database.- Throws:
SQLException
- if an SQL error occurs.
-
setupHologramTable
Sets up the hologram table in the database.- Throws:
SQLException
- if an SQL error occurs.
-
setupEntityTable
Sets up an entity table in the database.- Parameters:
name
- the name of the table.- Throws:
SQLException
- if an SQL error occurs.
-
loadGraveMap
public void loadGraveMap()Loads the grave map from the database. -
loadBlockMap
public void loadBlockMap()Loads the block map from the database. -
loadEntityMap
Loads an entity map from the database.- Parameters:
table
- the table name.type
- the type of entity data.
-
loadHologramMap
public void loadHologramMap()Loads the hologram map from the database. -
createEntityDataMapTable
Creates the entity data map table if it does not exist.- Parameters:
name
- the name of the table.
-
loadEntityDataMap
Loads entity data from the database.- Parameters:
table
- the table name.type
- the type of entity data.
-
addBlockData
Adds block data to the database.- Parameters:
blockData
- the block data to add.
-
removeBlockData
public void removeBlockData(org.bukkit.Location location) Removes block data from the database.- Parameters:
location
- the location of the block data to remove.
-
addHologramData
Adds hologram data to the database.- Parameters:
hologramData
- the hologram data to add.
-
removeHologramData
Removes hologram entries from the database based on the grave UUID. This is a fallback if entity data is not loaded in memory.- Parameters:
grave
- the grave to remove hologram data.
-
removeHologramData
Deprecated, for removal: This API element is subject to removal in a future version.Useto properly remove grave hologram data
Removes hologram data from the database.- Parameters:
entityDataList
- the list of entity data to remove.
-
addEntityData
Adds entity data to the database.- Parameters:
entityData
- the entity data to add.
-
removeEntityData
Removes entity data from the database.- Parameters:
entityData
- the entity data to remove.
-
removeEntityData
Removes a list of entity data from the database.- Parameters:
entityDataList
- the list of entity data to remove.
-
hasGraveAtLocation
public boolean hasGraveAtLocation(org.bukkit.Location location) -
entityDataTypeTable
Returns the table name for the specified entity data type.- Parameters:
type
- the entity data type.- Returns:
- the table name.
-
addGrave
Adds a grave to the database.- Parameters:
grave
- the grave to add.
-
removeGrave
Removes a grave from the database.- Parameters:
grave
- the grave to remove.
-
removeGrave
-
updateGrave
Updates a grave in the database.- Parameters:
grave
- the grave to update.column
- the column to update.integer
- the new integer value for the column.
-
updateGrave
Updates a grave in the database.- Parameters:
grave
- the grave to update.column
- the column to update.string
- the new value for the column.
-
resultSetToGrave
Converts a ResultSet to a Grave object.- Parameters:
resultSet
- the ResultSet to convert.- Returns:
- the Grave object, or null if an error occurs.
-
isConnected
private boolean isConnected()Checks if the database connection is active.- Returns:
- true if the connection is active, false otherwise.
-
getConnection
Retrieves a connection from the data source.- Returns:
- the database connection.
-
getDatabaseVersions
Retrieves the versions of supported databases.- Returns:
- a map of database types and their versions as integers.
- Throws:
SQLException
- if a database access error occurs.
-
getDatabaseVersion
Retrieves the version of the database connection type.- Returns:
- the database version as a string.
- Throws:
SQLException
- if a database access error occurs.
-
closeConnection
public void closeConnection()Closes the database connection. -
executeBatch
Executes a batch of SQL statements.- Parameters:
statement
- the statement containing the batch.
-
executeUpdate
Executes an update SQL statement with parameters.- Parameters:
sql
- the SQL statement.parameters
- the parameters for the SQL statement.- Throws:
SQLException
- if a database access error occurs.
-
executeQuery
Executes a query SQL statement.- Parameters:
sql
- the SQL statement.- Returns:
- the ResultSet of the query.
- Throws:
SQLException
-
executeQuery
Executes a query SQL statement with parameters.- Parameters:
sql
- the SQL statement with placeholders (e.g., ? for parameters).params
- the parameters to be set in the prepared statement.- Returns:
- the ResultSet of the query.
- Throws:
SQLException
- if an SQL error occurs.
-
closeConnection
Closes a database connection.- Parameters:
connection
- the connection to close.
-
closeStatement
Closes a statement.- Parameters:
statement
- the statement to close.
-
closeResultSet
Closes a ResultSet.- Parameters:
resultSet
- the ResultSet to close.
-
testDatabaseConnection
private boolean testDatabaseConnection()Tests the MySQL connection.- Returns:
- true if the connection is successful, false otherwise.
-
migrate
private void migrate()Migrates data from the plugin’s SQLite schema into the configured target database.- Reads each table from the SQLite file in
plugin/data/data.db
. - Recreates it in the target database with mapped column types. - Copies all rows. - Renames the SQLite file to data.old.db on success. -
mapSQLiteTypeToTargetDB
Maps a SQLite data type to the appropriate type for the configured target database (MySQL/MariaDB, PostgreSQL, H2, or MSSQL).- Parameters:
sqliteType
- the SQLite type declaration (e.g. "VARCHAR(255)", "INT")columnName
- the column name, used for special-case mappings- Returns:
- the target database type declaration, or null if no mapping is available
-
mapSQLiteTypeToMySQL
Maps a SQLite column type (including optional length/precision) to the corresponding MySQL/MariaDB type.- Parameters:
sqliteType
- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName
- the column name, used for special‐casing certain fields- Returns:
- the MySQL/MariaDB type declaration, or null if no mapping is available
-
mapSQLiteTypeToPostgreSQL
Maps a SQLite column type (including optional length/precision) to the corresponding PostgreSQL type.- Parameters:
sqliteType
- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName
- the column name, used for special casing certain fields- Returns:
- the PostgreSQL type declaration, or null if no mapping is available
-
mapSQLiteTypeToH2
Maps a SQLite column type (including optional length/precision) to the corresponding H2 type.- Parameters:
sqliteType
- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName
- the column name, used for special casing certain fields- Returns:
- the H2 type declaration, or null if no mapping is available
-
mapSQLiteTypeToMSSQL
Maps a SQLite column type (including length specifiers) to the corresponding MSSQL type.- Parameters:
sqliteType
- the SQLite type declaration (e.g. "VARCHAR(255)", "INT", "NUMERIC(10,2)")columnName
- the column name, used for special casing certain fields- Returns:
- the MSSQL type declaration, or null if no mapping is available
-
adjustGraveTableForTargetDB
Adjusts the grave table for the target database if necessary for MSSQL.- Throws:
SQLException
-
keepConnectionAlive
private void keepConnectionAlive()Keeps the database connection alive by periodically executing a query. -
checkAndUnlockDatabase
private void checkAndUnlockDatabase()Checks if the Database is locked and attempts to unlock the database. -
handleUnlockSQLite
private void handleUnlockSQLite()Handles unlocking for SQLite databases using COMMIT or Rollback. -
handleUnlockMySQL
private void handleUnlockMySQL()Handles unlocking for MySQL and MariaDB databases. -
handleUnlockPostgreSQLandH2
private void handleUnlockPostgreSQLandH2()Handles unlocking for PostgreSQL/H2 databases using COMMIT or Rollback. -
handleUnlockMSSQL
private void handleUnlockMSSQL()Handles unlocking for Microsoft SQL Server databases using COMMIT. -
getStoragePrefix
Retrieves the configured storage prefix for GravesX from the plugin configuration.If the prefix is not set or is empty/whitespace, this method returns an empty string. If a valid prefix is provided, it appends an underscore to the end (e.g.,
"gravesx_"
).While this is intended to help namespace GravesX database tables, note that shared databases (e.g., running multiple plugin instances in the same DB) are not supported.
- Returns:
- the formatted table prefix with an underscore, or an empty string if no prefix is set.
-
to properly remove grave hologram data
Removes hologram data from the database.