java.lang.Object
me.github.simonplays15.betterbansystem.core.database.Database
me.github.simonplays15.betterbansystem.core.database.mongodb.MongoDBDatabase
All Implemented Interfaces:
IDatabase
Direct Known Subclasses:
CachedMongoDBDatabase

public class MongoDBDatabase extends Database
MongoDBDatabase is a concrete class that extends the abstract class Database. It provides the implementation of the methods to interact with a MongoDB database. This class uses the MongoDB driver to connect to and perform operations on the database.
  • Constructor Details

    • MongoDBDatabase

      public MongoDBDatabase()
  • Method Details

    • createDatabaseAndTables

      public void createDatabaseAndTables()
      Create the database and tables required for the application to function.
    • connect

      public void connect (@NotNull @NotNull String connectionstring, String username, String password)
      Connects to a specific database using the provided connection string, username, and password.
      Parameters:
      connectionstring - the connection string to connect to the database
      username - the username for authentication
      password - the password for authentication
    • disconnect

      public void disconnect()
      Disconnects from the database.
    • insert

      public void insert (String tableName, Map<String,Object> data)
      Inserts a new document into the specified table in the MongoDB database.
      Parameters:
      tableName - the name of the table to insert the document into
      data - a map containing the data to be inserted, with the field names as keys and the field values as values
    • update

      public void update (String tableName, String primaryKey, Object primaryKeyValue, Map<String,Object> newData)
      Updates a document in the specified MongoDB collection.
      Parameters:
      tableName - the name of the collection
      primaryKey - the name of the primary key field
      primaryKeyValue - the value of the primary key for the document to update
      newData - a Map of field names and their updated values
    • delete

      public void delete (String tableName, String primaryKey, Object primaryKeyValue)
      Deletes a record from the specified table based on the primary key value. This method overrides the delete method in the super class and additionally clears the cache of query results that are associated with the deleted table.
      Parameters:
      tableName - the name of the table
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key to match
    • select

      public List<Map<String,Object>> select (String tableName, String condition)
      Retrieves a list of rows from a database table based on the given condition.
      Parameters:
      tableName - the name of the table to select from
      condition - the condition to use for selecting rows
      Returns:
      a list of maps representing the selected rows, where each map contains column names as keys and column values as values
    • selectAll

      public List<Map<String,Object>> selectAll (String tableName)
      Retrieves all records from the specified table in the MongoDB database.
      Parameters:
      tableName - the name of the table to select from
      Returns:
      a list of maps representing the selected records
    • executeQuery

      public List<Map<String,Object>> executeQuery (String queryString)
      Executes a query on the MongoDB database.
      Parameters:
      queryString - The query string to be executed.
      Returns:
      A list of maps representing the query result. Each map contains field-value pairs of the documents returned by the query.
    • query

      public void query (String queryString)
      Description copied from interface: IDatabase
      Executes a database query with the given query string and clears the cache.
      Parameters:
      queryString - the query string to execute
    • createIndex

      public void createIndex (String collectionName, String fieldName, boolean unique)
      Creates an index in the specified collection on the given field.
      Parameters:
      collectionName - the name of the collection where the index is to be created
      fieldName - the name of the field on which the index is to be created
      unique - true if the index should enforce a unique constraint, false otherwise
    • startTransaction

      public void startTransaction()
      Starts a transaction.
    • commitTransaction

      public void commitTransaction()
      Commits the current transaction.
    • rollbackTransaction

      public void rollbackTransaction()
      Rolls back the current transaction.