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

public class MySQLDatabase extends Database
MySQLDatabase is a class that extends the abstract class Database and provides methods for managing an MySQL database.
  • Constructor Details

    • MySQLDatabase

      public MySQLDatabase()
  • Method Details

    • createDatabaseAndTables

      public void createDatabaseAndTables()
      Creates the database and tables required for the BetterBanSystem.
    • connect

      public void connect (String connectionstring, String username, String password)
      Establishes a connection to the database using the provided connection string, username, and password.
      Parameters:
      connectionstring - the connection string used to connect to the database
      username - the username for authentication
      password - 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

      public void insert (String tableName, @NotNull @NotNull Map<String,Object> data)
      Inserts a new row into the specified table with the given data.
      Parameters:
      tableName - the name of the table to insert into
      data - 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 update
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key of the record to update
      newData - a map of column names and their corresponding new values
    • delete

      public void delete (String tableName, String primaryKey, Object primaryKeyValue)
      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 record
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key of the record to be deleted
    • select

      public List<Map<String,Object>> select (String tableName, String condition)
      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 from
      condition - 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

      public List<Map<String,Object>> selectAll (String tableName)
      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

      public List<Map<String,Object>> executeQuery (String queryString)
      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

      public void query (String queryString)
      Executes the given query string.
      Parameters:
      queryString - the query string to execute
    • createIndex

      public void createIndex (String collectionName, String fieldName, boolean unique)
      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.