Interface IDatabase
- All Known Implementing Classes:
CachedMongoDBDatabase
,CachedMySQLDatabase
,CachedSQLiteDatabase
,Database
,MongoDBDatabase
,MySQLDatabase
,SQLiteDatabase
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Commits the current transaction.void
Establishes a connection to the specified database using the provided connection string, username, and password.void
Creates a database and associated tables.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 primary key value.void
Disconnects from the database.executeQuery
(String queryString) Executes a SQL query and returns the result as a List of Maps.void
Inserts data into the specified table and clears the corresponding cache entries.void
Executes a database query with the given query string and clears the cache.void
Rolls back the current transaction.Executes a SELECT query on a specified table with a provided condition.Retrieves all records from the specified table.void
Starts a new transaction.void
Updates a record in the specified table with the given primary key and new data.
-
Method Details
-
createDatabaseAndTables
void createDatabaseAndTables()Creates a database and associated tables. -
connect
Establishes a connection to the specified database using the provided connection string, username, and password.- Parameters:
connectionstring
- A string containing the connection details for the database.username
- The username for the database connection.password
- The password for the database connection.
-
disconnect
void disconnect()Disconnects from the database. This method closes the connection to the database and releases any resources associated with it. After calling this method, further database operations cannot be performed until a new connection is established. -
insert
Inserts data into the specified table and clears the corresponding cache entries.- Parameters:
tableName
- the name of the tabledata
- a map containing the column names and their corresponding values to be inserted
-
update
void update(String tableName, String primaryKey, Object primaryKeyValue, Map<String, Object> newData) Updates a record in the specified table with the given primary key and new data.- Parameters:
tableName
- the name of the table to updateprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key for the record to updatenewData
- a map of column names to new values for the record
-
delete
Deletes a record from the specified table based on the primary key value.- Parameters:
tableName
- the name of the table from which to delete the recordprimaryKey
- the name of the primary key column in the tableprimaryKeyValue
- the value of the primary key to identify the record to be deleted
-
select
Executes a SELECT query on a specified table with a provided condition.- Parameters:
tableName
- the name of the table to select fromcondition
- the condition to apply in the WHERE clause- Returns:
- a List of Map objects where each Map represents a row in the result set, with column names as keys and column values as values
-
selectAll
Retrieves all records from the specified table.- Parameters:
tableName
- the name of the table from which to retrieve the records- Returns:
- a list of maps, where each map represents a record from the table. The keys in the map correspond to the column names, and the values are the values of the corresponding columns in the record.
-
executeQuery
Executes a SQL query and returns the result as a List of Maps.- Parameters:
queryString
- the SQL query to be executed- Returns:
- a List of Maps containing the query result. Each Map represents a row of the result, with the column names as keys and the corresponding values as values.
-
query
Executes a database query with the given query string and clears the cache.- 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 on which to create the index.fieldName
- The name of the field on which to create the index.unique
- True if the index should be unique, false otherwise.
-
startTransaction
void startTransaction()Starts a new transaction.A transaction is a sequence of actions that are executed as a single unit of work. In a transaction, multiple database operations can be performed, such as inserting, updating, and deleting data. Changes made during a transaction are temporary and isolated from other transactions until the transaction is committed. If a transaction is not committed, the changes will be rolled back and discarded.
Note that transactions are supported by the underlying database, and some databases may not support transactions. If the database does not support transactions, calling this method will have no effect.
Example usage:
database.startTransaction(); try { // Perform database operations database.insert("customers", customerData); database.update("products", "id", productId, updatedProductData); database.delete("orders", "id", orderId); // Commit the transaction database.commitTransaction(); } catch (Exception e) { // Handle exception // Roll back the transaction database.rollbackTransaction(); }
-
commitTransaction
void commitTransaction()Commits the current transaction. -
rollbackTransaction
void rollbackTransaction()Rolls back the current transaction. This method is used to undo changes made within a transaction and restore the database to its previous state. If a rollback operation is successful, all changes made within the transaction are discarded and the database is returned to its state prior to starting the transaction . If the rollback operation fails, an exception may be thrown, indicating that the transaction was not successfully rolled back. This method should only be called after starting a transaction using thestartTransaction()
method.Example usage:
IDatabase database = new Database(); ... database.startTransaction(); try { // Perform database operations within the transaction ... database.rollbackTransaction(); } catch (Exception ex) { // Handle exception }
-