Class Database
java.lang.Object
de.murmelmeister.murmelapi.database.Database
Database class to manage the database.
(Thread-safe)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcallUpdate(String name, Object... objects) Executes a database update call using a CallableStatement with the given name and parameters.voidConnects to the database using the provided URL, username and password.voidconnect(String driver, String hostname, String port, String database, String username, String password) Establishes a connection to a database using the specified parameters.voidcreateTable(String tableName, String value) Creates a new database table if it does not already exist.voidCloses the database connection safely by attempting to close the underlying data source if it is not already closed.booleanChecks whether a record exists in the database for a given stored procedure name and the provided parameters.Generates a unique identifier of typeUUIDthat does not conflict with existing entries associated with the specified name.static StringgetProcedureQuery(String name, String input, String query) Generates a SQL query string for creating a stored procedure in the database.<T> TExecutes a database query using a prepared statement and retrieves a result of the specified type and label.<T> List<T> Executes a database query using a callable statement and populates the provided list with the results.Executes a database query using a callable statement and populates the provided map with the results.voidExecutes an update on the database using the provided SQL query and parameters.
-
Constructor Details
-
Database
public Database()
-
-
Method Details
-
connect
Connects to the database using the provided URL, username and password. Note: It is not checked whether it is really connected!- Parameters:
url- The JDBC URL for the database.user- The username for the database.password- The password for the database.
-
connect
public void connect(String driver, String hostname, String port, String database, String username, String password) Establishes a connection to a database using the specified parameters.- Parameters:
driver- The database driver to be used (e.g., "mysql", "postgresql").hostname- The hostname or IP address of the database server.port- The port number on which the database server is listening.database- The name of the database to connect to.username- The username to use for authentication.password- The password to use for authentication.
-
disconnect
public void disconnect()Closes the database connection safely by attempting to close the underlying data source if it is not already closed. Ensures that the operation is thread-safe by acquiring a write lock before performing the closure. If an exception occurs during the closure process, a runtime exception is thrown with the error details. The lock is always released after the operation, regardless of its success. -
update
Executes an update on the database using the provided SQL query and parameters. This method acquires a write lock to ensure thread safety while performing the operation.- Parameters:
sql- The SQL query to be executed. It may contain placeholders for parameters.objects- The arguments to be used as parameters in the SQL query. These will replace the placeholders in the provided query.- Throws:
DatabaseException- if an error occurs during database updating, wrapping the SQLException.
-
callUpdate
Executes a database update call using a CallableStatement with the given name and parameters. The method acquires a write lock before performing the operation to ensure thread-safety.- Parameters:
name- The name of the database procedure or function to be called.objects- The parameters to be passed to the CallableStatement. These can be any number of objects to match the required procedure or function signature.
-
createTable
Creates a new database table if it does not already exist.- Parameters:
tableName- The name of the table to be createdvalue- The column definitions for the table
-
query
Executes a database query using a prepared statement and retrieves a result of the specified type and label.- Type Parameters:
T- The type of the result to be retrieved from the database query.- Parameters:
defaultValue- The default value to return in case the query does not produce a result.label- The label of the column in the result set to extract the value from.type- The class type of the expected result.name- The name of the stored procedure or query to execute.objects- The parameters to be set in the prepared statement for the query.- Returns:
- The value retrieved from the database query result set, or the default value if no result is found.
- Throws:
DatabaseException- If there is an error while executing the database query or processing the result.
-
queryList
public <T> List<T> queryList(List<T> defaultList, String label, Class<T> type, String name, Object... objects) Executes a database query using a callable statement and populates the provided list with the results. The method operates under a read lock to ensure thread safety during data retrieval. The query is executed using the given stored procedure name and parameters.- Type Parameters:
T- The type of elements to be retrieved and added to the list.- Parameters:
defaultList- A list to populate with the query results. The elements are cast to the specified type.label- The label or column name from the query result set to retrieve values from.type- The class type of the elements to be added to the list.name- The name of the stored procedure to be executed.objects- A variable number of parameters to be passed to the stored procedure.- Returns:
- The list provided as the input, populated with elements extracted from the query result set.
- Throws:
DatabaseException- If a database access error occurs while querying or processing the results.
-
queryMap
public <V> Map<String,V> queryMap(Map<String, V> defaultMap, Class<V> valueType, String name, Object... objects) Executes a database query using a callable statement and populates the provided map with the results. The method operates under a read lock to ensure thread safety during data retrieval. The query is executed using the given stored procedure name and parameters.- Type Parameters:
V- The type of values to be stored in the map.- Parameters:
defaultMap- A map to populate with the query results. The keys are column labels from the query result set, and the values are cast to the specified type.valueType- The class type of the values to be stored in the map.name- The name of the stored procedure to be executed.objects- A variable number of parameters to be passed to the stored procedure.- Returns:
- The map populated with key-value pairs extracted from the query result set.
- Throws:
DatabaseException- If a database access error occurs while querying or processing the results.
-
exists
Checks whether a record exists in the database for a given stored procedure name and the provided parameters.- Parameters:
name- The name of the stored procedure to be executed for checking existence.objects- A variable number of objects representing the parameters to be passed to the stored procedure.- Returns:
trueif a record exists in the database for the specified procedure and parameters,falseotherwise.
-
generateUniqueIdentifier
Generates a unique identifier of typeUUIDthat does not conflict with existing entries associated with the specified name. The method ensures uniqueness by repeatedly generating random UUIDs and checking if they already exist in the database through theexistsmethod.- Parameters:
name- The name used to identify the context in which the unique identifier is generated. Typically refers to a database table or similar scope for uniqueness checks.- Returns:
- A universally unique identifier (UUID) that is guaranteed to be unique in the specified context.
-
getProcedureQuery
Generates a SQL query string for creating a stored procedure in the database.- Parameters:
name- The name of the stored procedure.input- The input parameters for the stored procedure, defined as a comma-separated list of parameter names and types (e.g., "param1 INT, param2 VARCHAR(100)").query- The SQL statement(s) to be executed within the body of the stored procedure.- Returns:
- A string containing the complete SQL query for creating the stored procedure, including the name, input parameters, and body.
-