Package de.murmelmeister.murmelapi.utils
Class Database
java.lang.Object
de.murmelmeister.murmelapi.utils.Database
Database class to manage the database.
(Thread-safe)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final com.zaxxer.hikari.HikariDataSourceprivate static final ReadWriteLockprivate static final Lockprivate static final Lock -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancallExists(String name, Object... objects) Checks if a call with the specified name exists in the database, considering the provided objects.static <T> TExecutes a database stored procedure query and retrieves a value from the result set based on the provided label and type.static <T> List<T> callQueryList(String label, Class<T> type, String name, Object... objects) Executes a database stored procedure query and retrieves a list of result objects.static voidcallUpdate(String name, Object... objects) Executes an update operation in the database using a callable query constructed from the provided name and objects.static voidConnects to the database using the provided URL, username and password.static voidconnect(String driver, String hostname, String port, String database, String username, String password) Connects to the database using the provided driver, host, port, database, username, and password.static voidconnectEnv(String url, String user, String password) Connects to an environment-based configuration using the provided parameters.static voidconnectEnv(String driver, String hostname, String port, String database, String username, String password) Establishes a connection to an environment-specific database using the provided parameters.static voidcreateTable(String tableName, String value) Creates a new table in the database if it does not already exist.static voidDisconnects from the database and close the connection pool.static booleanChecks if any records exist in the database for the provided SQL query and parameters.private static CallableStatementgetCallableStatement(Connection connection, String name, Object... objects) Creates a CallableStatement for a stored procedure call with the given name and parameters.private static PreparedStatementgetPreparedStatement(Connection connection, String query, Object... objects) Creates a PreparedStatement for the given SQL query and sets the provided parameters.static StringgetProcedureQuery(String name, String input, String query, Object... objects) Returns the SQL query for creating a stored procedure.static StringgetProcedureQueryWithoutObjects(String name, String input, String query) Generates a SQL procedure creation query as a single string, without including any objects.private static StringgetQueryWithCall(String name, Object... objects) Constructs a SQL query for a stored procedure call.static <T> TExecutes a SQL query and returns a result of type T.static <T> List<T> Executes the provided SQL query and returns a list of results extracted from the specified column label.private static voidsetParameters(PreparedStatement statement, Object... objects) Sets the parameters for a PreparedStatement.static voidExecutes an update operation on the database using the provided SQL statement and parameters.
-
Field Details
-
DATA_SOURCE
private static final com.zaxxer.hikari.HikariDataSource DATA_SOURCE -
LOCK
-
READ_LOCK
-
WRITE_LOCK
-
-
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.
-
connectEnv
Connects to an environment-based configuration using the provided parameters.- Parameters:
url- The environment variable key for the URL to connect touser- The environment variable key for the username to use for the connectionpassword- The environment variable key for the password to use for the connection
-
connect
public static void connect(String driver, String hostname, String port, String database, String username, String password) Connects to the database using the provided driver, host, port, database, username, and password. Note: It is not checked whether it is really connected!- Parameters:
driver- The JDBC driver for the database.hostname- The hostname of the database.port- The port of the database.database- The name of the database.username- The username for the database.password- The password for the database.
-
connectEnv
public static void connectEnv(String driver, String hostname, String port, String database, String username, String password) Establishes a connection to an environment-specific database using the provided parameters.- Parameters:
driver- The name of the environment variable containing the database driverhostname- The name of the environment variable containing the database host nameport- The name of the environment variable containing the database port numberdatabase- The name of the environment variable containing the database nameusername- The name of the environment variable containing the database usernamepassword- The name of the environment variable containing the database password
-
disconnect
public static void disconnect()Disconnects from the database and close the connection pool. -
update
Executes an update operation on the database using the provided SQL statement and parameters.- Parameters:
sql- The SQL statement to be executedobjects- The parameters to be set in the SQL statement
-
callUpdate
Executes an update operation in the database using a callable query constructed from the provided name and objects.- Parameters:
name- The name identifying the callable query to be executedobjects- A variable number of objects to be passed as parameters to the query
-
createTable
Creates a new table in the database if it does not already exist.- Parameters:
tableName- The name of the table to be createdvalue- The column definitions and other SQL specifications for the table
-
query
public static <T> T query(T defaultValue, String label, Class<T> type, String sql, Object... objects) Executes a SQL query and returns a result of type T.- Parameters:
defaultValue- The default value to return if the query result is emptylabel- The column label of the result to retrievetype- The type of the result to be returnedsql- The SQL query string to executeobjects- The parameters for the SQL query- Returns:
- the result of the query of type T, or the default value if the query result is empty
-
queryList
Executes the provided SQL query and returns a list of results extracted from the specified column label.- Type Parameters:
T- The type of the list items to be returned- Parameters:
label- The label of the column from which to extract the resultstype- The class type of the items to be returnedsql- The SQL query to be executedobjects- The parameters to be set in the SQL query- Returns:
- a list of results extracted from the specified column label
- Throws:
RuntimeException- if there is a database access error
-
callQuery
public static <T> T callQuery(T defaultValue, String label, Class<T> type, String name, Object... objects) Executes a database stored procedure query and retrieves a value from the result set based on the provided label and type.- Parameters:
defaultValue- The default value to return if no result is foundlabel- The label of the column to retrieve the value fromtype- The class type of the expected resultname- The name of the query or stored procedure to be executedobjects- Additional objects to be included in the query- Returns:
- the value retrieved from the result set based on the provided label and type, or the default value if no result is found
- Throws:
RuntimeException- if a database query error occurs
-
callQueryList
public static <T> List<T> callQueryList(String label, Class<T> type, String name, Object... objects) Executes a database stored procedure query and retrieves a list of result objects.- Parameters:
label- The label of the column from which to retrieve the result objectstype- The class type of the objects to retrievename- The name of the callable query to executeobjects- The parameters to be applied to the callable query- Returns:
- a synchronized list of result objects fetched from the specified column
-
exists
Checks if any records exist in the database for the provided SQL query and parameters.- Parameters:
sql- The SQL query to executeobjects- The parameters to set in the SQL query- Returns:
trueif records exist,falseotherwise
-
callExists
Checks if a call with the specified name exists in the database, considering the provided objects.- Parameters:
name- The name of the call to check for existenceobjects- A variable number of objects that are considered in the call lookup process- Returns:
trueif records exist,falseotherwise
-
getProcedureQuery
Returns the SQL query for creating a stored procedure.- Parameters:
name- the name of the stored procedureinput- the input parameters of the stored procedure in the format "parameter1 type1, parameter2 type2, ..."query- the body of the stored procedureobjects- the objects to be formatted into the query string- Returns:
- the SQL query string for creating the stored procedure
-
getProcedureQueryWithoutObjects
Generates a SQL procedure creation query as a single string, without including any objects.- Parameters:
name- the name of the procedureinput- the input parameters for the procedurequery- the SQL query to be executed within the procedure- Returns:
- the complete SQL procedure creation statement as a string
-
getQueryWithCall
Constructs a SQL query for a stored procedure call.- Parameters:
name- the name of the stored procedure to call.objects- the parameters to pass to the stored procedure.- Returns:
- a string representing the constructed SQL query with the provided parameters.
-
getCallableStatement
private static CallableStatement getCallableStatement(Connection connection, String name, Object... objects) throws SQLException Creates a CallableStatement for a stored procedure call with the given name and parameters.- Parameters:
connection- The database connection to be used for creating the CallableStatement.name- The name of the stored procedure to be called.objects- The parameters to be passed to the stored procedure.- Returns:
- The created CallableStatement with parameters set.
- Throws:
SQLException- If a database access error occurs or this method is called on a closed connection.
-
getPreparedStatement
private static PreparedStatement getPreparedStatement(Connection connection, String query, Object... objects) throws SQLException Creates a PreparedStatement for the given SQL query and sets the provided parameters.- Parameters:
connection- The database connection to be used for creating the PreparedStatement.query- The SQL query string for which the PreparedStatement is to be created.objects- The parameters to be set in the PreparedStatement.- Returns:
- The created PreparedStatement with the parameters set.
- Throws:
SQLException- If a database access error occurs or this method is called on a closed connection.
-
setParameters
private static void setParameters(PreparedStatement statement, Object... objects) throws SQLException Sets the parameters for a PreparedStatement.- Parameters:
statement- The PreparedStatement to which the parameters are to be set.objects- The parameters to set in the PreparedStatement.- Throws:
SQLException- If an SQL exception occurs while setting the parameters.
-