Class Database

java.lang.Object
de.murmelmeister.murmelapi.utils.Database

public final class Database extends Object
Database class to manage the database. (Thread-safe)
  • Field Details

    • DATA_SOURCE

      private static final com.zaxxer.hikari.HikariDataSource DATA_SOURCE
    • LOCK

      private static final ReadWriteLock LOCK
    • READ_LOCK

      private static final Lock READ_LOCK
    • WRITE_LOCK

      private static final Lock WRITE_LOCK
  • Constructor Details

    • Database

      public Database()
  • Method Details

    • connect

      public static void connect(String url, String user, String password)
      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

      public static void connectEnv(String url, String user, String password)
      Connects to an environment-based configuration using the provided parameters.
      Parameters:
      url - The environment variable key for the URL to connect to
      user - The environment variable key for the username to use for the connection
      password - 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 driver
      hostname - The name of the environment variable containing the database host name
      port - The name of the environment variable containing the database port number
      database - The name of the environment variable containing the database name
      username - The name of the environment variable containing the database username
      password - 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

      public static void update(String sql, Object... objects)
      Executes an update operation on the database using the provided SQL statement and parameters.
      Parameters:
      sql - The SQL statement to be executed
      objects - The parameters to be set in the SQL statement
    • callUpdate

      public static void callUpdate(String name, Object... objects)
      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 executed
      objects - A variable number of objects to be passed as parameters to the query
    • createTable

      public static void createTable(String tableName, String value)
      Creates a new table in the database if it does not already exist.
      Parameters:
      tableName - The name of the table to be created
      value - 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 empty
      label - The column label of the result to retrieve
      type - The type of the result to be returned
      sql - The SQL query string to execute
      objects - 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

      public static <T> List<T> queryList(String label, Class<T> type, String sql, Object... objects)
      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 results
      type - The class type of the items to be returned
      sql - The SQL query to be executed
      objects - 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 found
      label - The label of the column to retrieve the value from
      type - The class type of the expected result
      name - The name of the query or stored procedure to be executed
      objects - 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 objects
      type - The class type of the objects to retrieve
      name - The name of the callable query to execute
      objects - The parameters to be applied to the callable query
      Returns:
      a synchronized list of result objects fetched from the specified column
    • exists

      public static boolean exists(String sql, Object... objects)
      Checks if any records exist in the database for the provided SQL query and parameters.
      Parameters:
      sql - The SQL query to execute
      objects - The parameters to set in the SQL query
      Returns:
      true if records exist, false otherwise
    • callExists

      public static boolean callExists(String name, Object... objects)
      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 existence
      objects - A variable number of objects that are considered in the call lookup process
      Returns:
      true if records exist, false otherwise
    • getProcedureQuery

      public static String getProcedureQuery(String name, String input, String query, Object... objects)
      Returns the SQL query for creating a stored procedure.
      Parameters:
      name - the name of the stored procedure
      input - the input parameters of the stored procedure in the format "parameter1 type1, parameter2 type2, ..."
      query - the body of the stored procedure
      objects - the objects to be formatted into the query string
      Returns:
      the SQL query string for creating the stored procedure
    • getProcedureQueryWithoutObjects

      public static String getProcedureQueryWithoutObjects(String name, String input, String query)
      Generates a SQL procedure creation query as a single string, without including any objects.
      Parameters:
      name - the name of the procedure
      input - the input parameters for the procedure
      query - the SQL query to be executed within the procedure
      Returns:
      the complete SQL procedure creation statement as a string
    • getQueryWithCall

      private static String getQueryWithCall(String name, Object... objects)
      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.