Enum Class 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.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum Constants -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Connection
The active database connection.private DatabaseEngine
The database engine used for the connection. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate String
buildConnectionString
(String databaseName, SqlOperation sqlOperation) Builds the connection string based on the selectedDatabaseEngine
and theSqlOperation
.void
close()
Closes the active database connection if it exists.void
connect
(String databaseName, SqlOperation sqlOperation) Establishes a connection to the specified database.private void
ensureDirectoryExists
(String path) Ensures that the specified directory exists.Retrieves the active database connection.private boolean
Checks if there is an active database connection.private void
Validates whether a connection is active.static GenericDatabaseConfiguration
Returns the enum constant of this class with the specified name.static GenericDatabaseConfiguration[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
INSTANCE
The singleton instance of the database configuration.
-
-
Field Details
-
databaseEngine
The database engine used for the connection. -
connection
The active database connection.
-
-
Constructor Details
-
GenericDatabaseConfiguration
private GenericDatabaseConfiguration()
-
-
Method Details
-
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
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 nameNullPointerException
- 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 interfaceDatabaseConfiguration
- Parameters:
databaseName
- The name of the database.sqlOperation
- The type ofSqlOperation
to perform.- Throws:
DatabaseConnectionException
- If the connection cannot be established.
-
close
Closes the active database connection if it exists.If the connection is active, it is closed, and resources are released.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDatabaseConfiguration
- Throws:
DatabaseConnectionException
- If an error occurs while closing the connection.
-
getConnection
Retrieves the active database connection.If the connection is not established or has been closed, an exception is thrown.
- Specified by:
getConnection
in interfaceDatabaseConfiguration
- Returns:
- The active
Connection
instance. - Throws:
DatabaseConnectionException
- If no active connection is available.
-
validateConnection
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
Builds the connection string based on the selectedDatabaseEngine
and theSqlOperation
.- 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
- TheSqlOperation
to determine if database creation is needed.- Returns:
- A connection string for the specified database engine.
- For Derby: The database is stored in the project's
-
ensureDirectoryExists
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.
-