Class DatabaseApi

java.lang.Object
nl.knaw.dans.lib.dataverse.DatabaseApi

public class DatabaseApi extends Object
Optional API to perform direct database operations on the Dataverse database. Requires databaseUrl, databaseUser, and databasePassword in DataverseClientConfig.
  • Field Details

    • MDKEY_PARAM_NAME_PREFIX

      protected static final String MDKEY_PARAM_NAME_PREFIX
      See Also:
    • httpClientWrapper

      protected final nl.knaw.dans.lib.dataverse.HttpClientWrapper httpClientWrapper
  • Method Details

    • query

      public QueryContext<ResultSet> query(@NonNull @NonNull String query)
      Creates a QueryContext for the provided SQL query. The QueryContext can be used to execute the query with different sets of parameters. Note that the connection used by the QueryContext is set to read-only mode, so this method is intended for executing read-only queries. It is generally not recommended to change the Dataverse database directly.
      Parameters:
      query - the SQL query to execute, which may contain parameter placeholders (e.g., "?") for use with prepared statements
      Returns:
      a QueryContext for executing the provided SQL query with different sets of parameters
    • query

      public <T> QueryContext<T> query(@NonNull @NonNull String query, @NonNull @NonNull Function<ResultSet,T> mapper)
      Creates a QueryContext for the provided SQL query, using the provided mapper function to map each ResultSet row to an object of type T. The QueryContext can be used to execute the query with different sets of parameters. Note that the connection used by the QueryContext is set to read-only mode, so this method is intended for executing read-only queries. It is generally not recommended to change the Dataverse database directly.
      Type Parameters:
      T - the type of objects to return in the list when executing the query with sets of parameters without providing a custom mapper
      Parameters:
      query - the SQL query to execute, which may contain parameter placeholders (e.g., "?") for use with prepared statements
      mapper - a function that maps a ResultSet row to an object of type T, which will be used as the default mapper for the QueryContext. This mapper will be used when executing the query with sets of parameters without providing a custom mapper.
      Returns:
      a QueryContext for executing the provided SQL query with different sets of parameters, using the provided mapper function to map ResultSet rows to objects of type T
    • truncateNotificationsForUser

      public int truncateNotificationsForUser(int userId, int numberOfRecordsToKeep) throws SQLException
      Truncate notifications for a single user, keeping the newest N records.
      Parameters:
      userId - the user_id in the Dataverse database (numeric)
      numberOfRecordsToKeep - number of latest notifications to keep (must be >= 0)
      Returns:
      number of deleted rows
      Throws:
      SQLException - when database access fails
    • truncateNotificationsForAllUsers

      public int truncateNotificationsForAllUsers(int numberOfRecordsToKeep) throws SQLException
      Truncate notifications for all users who currently have more than N notifications. Keeps the newest N records per user.
      Parameters:
      numberOfRecordsToKeep - number of latest notifications to keep (must be >= 0)
      Returns:
      total number of deleted rows across all users
      Throws:
      SQLException - when database access fails
    • buildPath

      protected Path buildPath(Path base, String... components)
    • getQueryParamsFromMetadataKeys

      protected Map<String,List<String>> getQueryParamsFromMetadataKeys(Map<String,String> metadataKeys)