Class MySQLDatabase
java.lang.Object
me.github.simonplays15.betterbansystem.core.database.Database
me.github.simonplays15.betterbansystem.core.database.mysql.MySQLDatabase
- All Implemented Interfaces:
IDatabase
- Direct Known Subclasses:
CachedMySQLDatabase
MySQLDatabase is a class that extends the abstract class Database and provides
methods for managing an MySQL database.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Commits the current transaction by calling the 'commit()' method on the database connection.void
Establishes a connection to the database using the provided connection string, username, and password.void
Creates the database and tables required for the BetterBanSystem.void
createIndex
(String collectionName, String fieldName, boolean unique) Creates an index on a specified field in a collection.void
Deletes a record from the specified table based on the given primary key and primary key value.void
Disconnects from the database.executeQuery
(String queryString) Executes a query and returns the result as a list of maps.void
Inserts a new row into the specified table with the given data.void
Executes the given query string.void
Rolls back the current transaction and sets auto-commit mode to true.Executes a SELECT statement on the specified table with a given condition and returns the result as a list of maps.Retrieves all rows from the specified table in the database.void
Starts a transaction by disabling the auto-commit mode of the database connection.void
update
(String tableName, String primaryKey, Object primaryKeyValue, @NotNull Map<String, Object> newData) Updates a record in the specified table with the new data provided.
-
Constructor Details
-
MySQLDatabase
public MySQLDatabase()
-
-
Method Details
-
createDatabaseAndTables
public void createDatabaseAndTables()Creates the database and tables required for the BetterBanSystem. -
connect
Establishes a connection to the database using the provided connection string, username, and password.- Parameters:
connectionstring
- the connection string used to connect to the databaseusername
- the username for authenticationpassword
- the password for authentication
-
disconnect
public void disconnect()Disconnects from the database.This method closes the active connection to the database, if it is not already closed.
If an error occurs while closing the connection, an error message is logged using the global logger.
-
insert
Inserts a new row into the specified table with the given data.- Parameters:
tableName
- the name of the table to insert intodata
- a Map containing the column names and corresponding values for the new row- Throws:
NullPointerException
- if tableName or data is null
-
update
public void update(String tableName, String primaryKey, Object primaryKeyValue, @NotNull @NotNull Map<String, Object> newData) Updates a record in the specified table with the new data provided.- Parameters:
tableName
- the name of the table to updateprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key of the record to updatenewData
- a map of column names and their corresponding new values
-
delete
Deletes a record from the specified table based on the given primary key and primary key value.- Parameters:
tableName
- the name of the table from which to delete the recordprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key of the record to be deleted
-
select
Executes a SELECT statement on the specified table with a given condition and returns the result as a list of maps.- Parameters:
tableName
- the name of the table to select fromcondition
- the condition to apply in the WHERE clause- Returns:
- a list of maps representing the rows selected from the table, where each map contains column names as keys and corresponding values as values
-
selectAll
Retrieves all rows from the specified table in the database.- Parameters:
tableName
- the name of the table from which to retrieve the rows- Returns:
- a list of maps representing the rows in the table, where each map contains the column name as the key and the corresponding value of the column as the value
-
executeQuery
Executes a query and returns the result as a list of maps. Each map represents a row in the query result, where the keys are the column names and the values are the corresponding column values.- Parameters:
queryString
- the SQL query to be executed- Returns:
- a list of maps, where each map represents a row in the query result
-
query
Executes the given query string.- Parameters:
queryString
- the query string to execute
-
createIndex
Creates an index on a specified field in a collection.- Parameters:
collectionName
- The name of the collection.fieldName
- The name of the field to create the index on.unique
- True if the index should enforce uniqueness, false otherwise.
-
startTransaction
public void startTransaction()Starts a transaction by disabling the auto-commit mode of the database connection. Once the transaction is started, all database operations until the transaction is committed or rolled back will be treated as a single atomic unit of work. If an exception occurs while starting the transaction, an error message will be logged.- Throws:
SQLException
- if an error occurs while disabling the auto-commit mode of the database connection
-
commitTransaction
public void commitTransaction()Commits the current transaction by calling the 'commit()' method on the database connection. Sets auto-commit mode to true after the commit operation is successful. If an SQLException occurs during the commit operation, it is logged as an error. -
rollbackTransaction
public void rollbackTransaction()Rolls back the current transaction and sets auto-commit mode to true. If an SQLException is caught during the rollback, an error message will be logged.
-