All Implemented Interfaces:
IDatabase

public class CachedSQLiteDatabase extends SQLiteDatabase
This class extends the SQLiteDatabase class and provides caching capabilities for select queries. It maintains a cache map that stores the results of previous select queries based on the table name and condition. The cache is cleared whenever an insert, update, delete or query operation is performed. Additionally, the cache is also cleared when an index is created on a collection.
  • Constructor Details

    • CachedSQLiteDatabase

      public CachedSQLiteDatabase()
  • Method Details

    • select

      public List<Map<String,Object>> select (String tableName, String condition)
      Executes a SELECT query on the specified table with the given condition. Returns a list of maps, where each map represents a row of the result set with column names as keys and column values as values.
      Specified by:
      select in interface IDatabase
      Overrides:
      select in class SQLiteDatabase
      Parameters:
      tableName - the name of the table to query
      condition - the condition to apply in the WHERE clause of the query
      Returns:
      a list of maps representing the result set of the SELECT query
    • selectAll

      public List<Map<String,Object>> selectAll (String tableName)
      Retrieves all records from the specified table.
      Specified by:
      selectAll in interface IDatabase
      Overrides:
      selectAll in class SQLiteDatabase
      Parameters:
      tableName - the name of the table
      Returns:
      a list of maps representing each record in the table
    • insert

      public void insert (String tableName, @NotNull @NotNull Map<String,Object> data)
      Inserts data into the specified table.
      Specified by:
      insert in interface IDatabase
      Overrides:
      insert in class SQLiteDatabase
      Parameters:
      tableName - the name of the table
      data - a Map containing the column names and their corresponding values
    • update

      public void update (String tableName, String primaryKey, Object primaryKeyValue, @NotNull @NotNull Map<String,Object> newData)
      Updates a record in the specified table with new data based on the primary key.
      Specified by:
      update in interface IDatabase
      Overrides:
      update in class SQLiteDatabase
      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 containing the new column-value pairs to update
    • delete

      public void delete (String tableName, String primaryKey, Object primaryKeyValue)
      Deletes a record from the specified table based on the primary key.
      Specified by:
      delete in interface IDatabase
      Overrides:
      delete in class SQLiteDatabase
      Parameters:
      tableName - the name of the table where the record will be deleted
      primaryKey - the name of the primary key column
      primaryKeyValue - the value of the primary key for the record to be deleted
    • query

      public void query (String queryString)
      Executes a database query with the given query string.
      Specified by:
      query in interface IDatabase
      Overrides:
      query in class SQLiteDatabase
      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 given collection. This method overrides the createIndex method from the SQLiteDatabase class in order to provide caching capabilities for select queries. It maintains a cache map that stores the results of previous select queries based on the table name and condition. The cache is cleared whenever an insert, update, delete or query operation is performed. Additionally, the cache is also cleared when an index is created on a collection.
      Specified by:
      createIndex in interface IDatabase
      Overrides:
      createIndex in class SQLiteDatabase
      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 - Indicates whether the index should enforce uniqueness.