java.lang.Object
me.github.simonplays15.betterbansystem.core.database.Database
me.github.simonplays15.betterbansystem.core.database.sqlite.SQLiteDatabase
All Implemented Interfaces:
IDatabase
Direct Known Subclasses:
CachedSQLiteDatabase

public class SQLiteDatabase extends Database
The SQLiteDatabase class represents a database connection and provides methods for interacting with the database. It extends the Database class and overrides several methods for executing SQL queries and transactions.
  • Constructor Details

    • SQLiteDatabase

      public SQLiteDatabase()
  • Method Details

    • connect

      public void connect (String connectionstring, String username, String password)
      Connects to the database using the given connection string, username, and password.
      Parameters:
      connectionstring - the connection string to connect to the database
      username - the username to authenticate with the database
      password - the password to authenticate with the database
    • createDatabaseAndTables

      public void createDatabaseAndTables()
      Creates the necessary database tables if they don't already exist.
    • disconnect

      public void disconnect()
      Disconnects from the database by closing the connection. If the connection is already closed or null, no action is performed. Any exception that occurs during the process will be logged to 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 provided data.
      Parameters:
      tableName - the name of the table to insert into
      data - a map containing the column names as keys and the values to insert as values
      Throws:
      NullPointerException - if tableName is null or if data is null or contains null keys
      SQLException - if an error occurs while executing the SQL statement
    • update

      public void update (String tableName, String primaryKey, Object primaryKeyValue, @NotNull @NotNull Map<String,Object> newData)
      Updates a record in the specified table.
      Parameters:
      tableName - the name of the table to update the record in
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key for the record to update
      newData - a map representing the new data to update the record with
      Throws:
      NullPointerException - if tableName, primaryKey, primaryKeyValue, or newData is null
      SQLException - if a database access error occurs
    • delete

      public void delete (String tableName, String primaryKey, Object primaryKeyValue)
      Deletes a record from the specified table using the given primary key and value.
      Parameters:
      tableName - the name of the table to delete from
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key for the record to delete
    • select

      public List<Map<String,Object>> select (String tableName, String condition)
      Executes a SELECT SQL statement on the specified table with the given condition.
      Parameters:
      tableName - the name of the table to select from
      condition - the condition for selecting rows from the table
      Returns:
      a List of Maps representing the selected rows, where each map contains the column names as keys and the corresponding column values as values
    • selectAll

      public List<Map<String,Object>> selectAll (String tableName)
      Retrieves all records from the specified table.
      Parameters:
      tableName - the name of the table to select records from.
      Returns:
      a list of maps representing the records retrieved from the table. Each map contains the column names as keys and the corresponding column values as values.
    • executeQuery

      public List<Map<String,Object>> executeQuery (String queryString)
      Executes the given SQL query and returns a list of maps containing the query results.
      Parameters:
      queryString - the SQL query to execute
      Returns:
      a list of maps where each map represents a row from the query result, with column names as keys
    • query

      public void query (String queryString)
      Executes a database query with the given query string.
      Parameters:
      queryString - The SQL 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 - indicates whether the index should be unique or not
    • startTransaction

      public void startTransaction()
      Starts a transaction by setting the auto commit value to false. If an SQLException occurs, it is logged.
    • commitTransaction

      public void commitTransaction()
      Commits the current transaction.

      This method commits the changes made during the current transaction. It calls the Connection.commit() method to commit the changes and then sets the auto-commit mode back to true using Connection.setAutoCommit(boolean).

      If an error occurs during the commit process, an error message will be logged using the GlobalLogger class.

      See Also:
    • rollbackTransaction

      public void rollbackTransaction()
      Rollbacks the current transaction and restores the auto-commit behavior of the database connection. If an SQLException occurs during the rollback, it is logged as an error.