Enum Class GenericDatabaseConfiguration

java.lang.Object
java.lang.Enum<GenericDatabaseConfiguration>
dimstyl.orm.configuration.GenericDatabaseConfiguration
All Implemented Interfaces:
DatabaseConfiguration, Serializable, AutoCloseable, Comparable<GenericDatabaseConfiguration>, Constable

enum GenericDatabaseConfiguration extends Enum<GenericDatabaseConfiguration> implements DatabaseConfiguration
Singleton implementation of DatabaseConfiguration for managing database connections.

This enum provides methods for establishing, retrieving, and closing database connections while supporting multiple database engines such as Derby, SQLite, and H2.

  • Enum Constant Details

  • Field Details

    • databaseEngine

      private DatabaseEngine databaseEngine
      The database engine used for the connection.
    • connection

      private Connection connection
      The active database connection.
  • Constructor Details

    • GenericDatabaseConfiguration

      private GenericDatabaseConfiguration()
  • Method Details

    • values

      public static GenericDatabaseConfiguration[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static GenericDatabaseConfiguration valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • connect

      public void connect(String databaseName, SqlOperation sqlOperation) throws DatabaseConnectionException
      Establishes a connection to the specified database.

      If the database engine is SQLite, it ensures that the required directory exists. The connection string is built based on the selected DatabaseEngine and the SQL operation.

      Specified by:
      connect in interface DatabaseConfiguration
      Parameters:
      databaseName - The name of the database.
      sqlOperation - The type of SqlOperation to perform.
      Throws:
      DatabaseConnectionException - If the connection cannot be established.
    • close

      public void close() throws DatabaseConnectionException
      Closes the active database connection if it exists.

      If the connection is active, it is closed, and resources are released.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface DatabaseConfiguration
      Throws:
      DatabaseConnectionException - If an error occurs while closing the connection.
    • getConnection

      public Connection getConnection() throws DatabaseConnectionException
      Retrieves the active database connection.

      If the connection is not established or has been closed, an exception is thrown.

      Specified by:
      getConnection in interface DatabaseConfiguration
      Returns:
      The active Connection instance.
      Throws:
      DatabaseConnectionException - If no active connection is available.
    • validateConnection

      private void validateConnection() throws DatabaseConnectionException
      Validates whether a connection is active.

      If the connection is not established or has been closed, an exception is thrown.

      Throws:
      DatabaseConnectionException - If the connection is not available.
    • isConnected

      private boolean isConnected()
      Checks if there is an active database connection.
      Returns:
      true if a valid connection exists, false otherwise.
    • buildConnectionString

      private String buildConnectionString(String databaseName, SqlOperation sqlOperation)
      Builds the connection string based on the selected DatabaseEngine and the SqlOperation.

      • For Derby: The database is stored in the project's /db/derby directory.
      • For SQLite: The database is stored in the project's /db/sqlite directory.
      • For H2: The database is stored in the project's /db/h2 directory.

      Parameters:
      databaseName - The name of the database.
      sqlOperation - The SqlOperation to determine if database creation is needed.
      Returns:
      A connection string for the specified database engine.
    • ensureDirectoryExists

      private void ensureDirectoryExists(String path) throws IOException
      Ensures that the specified directory exists.

      If the directory does not exist, it is created.

      Parameters:
      path - The directory path to check or create.
      Throws:
      IOException - If an error occurs while creating the directory.