Class MongoDBDatabase
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
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Commits the current transaction.void
Connects to a specific database using the provided connection string, username, and password.void
Create the database and tables required for the application to function.void
createIndex
(String collectionName, String fieldName, boolean unique) Creates an index in the specified collection on the given field.void
Deletes a record from the specified table based on the primary key value.void
Disconnects from the database.executeQuery
(String queryString) Executes a query on the MongoDB database.void
Inserts a new document into the specified table in the MongoDB database.void
Executes a database query with the given query string and clears the cache.void
Rolls back the current transaction.Retrieves a list of rows from a database table based on the given condition.Retrieves all records from the specified table in the MongoDB database.void
Starts a transaction.void
Updates a document in the specified MongoDB collection.
-
Constructor Details
-
MongoDBDatabase
public MongoDBDatabase()
-
-
Method Details
-
createDatabaseAndTables
public void createDatabaseAndTables()Create the database and tables required for the application to function. -
connect
Connects to a specific database using the provided connection string, username, and password.- Parameters:
connectionstring
- the connection string to connect to the databaseusername
- the username for authenticationpassword
- the password for authentication
-
disconnect
public void disconnect()Disconnects from the database. -
insert
Inserts a new document into the specified table in the MongoDB database.- Parameters:
tableName
- the name of the table to insert the document intodata
- 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 collectionprimaryKey
- the name of the primary key fieldprimaryKeyValue
- the value of the primary key for the document to updatenewData
- aMap
of field names and their updated values
-
delete
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 tableprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key to match
-
select
Retrieves a list of rows from a database table based on the given condition.- Parameters:
tableName
- the name of the table to select fromcondition
- 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
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
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
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
Creates an index in the specified collection on the given field.- Parameters:
collectionName
- the name of the collection where the index is to be createdfieldName
- the name of the field on which the index is to be createdunique
- 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.
-