Class AbstractDatabase

java.lang.Object
is.codion.common.db.database.AbstractDatabase
All Implemented Interfaces:
ConnectionFactory, Database

public abstract class AbstractDatabase extends Object implements Database
A default abstract implementation of the Database interface.
  • Field Details

  • Constructor Details

    • AbstractDatabase

      protected AbstractDatabase(String url)
      Instantiates a new AbstractDatabase.
      Parameters:
      url - the jdbc url
  • Method Details

    • url

      public final String url()
      Description copied from interface: ConnectionFactory
      Returns the jdbc database url for this connection factory.
      Specified by:
      url in interface ConnectionFactory
      Returns:
      the jdbc database url for this connection factory.
    • createConnection

      public final Connection createConnection(User user)
      Description copied from interface: ConnectionFactory
      Creates a connection for the given user.
      Specified by:
      createConnection in interface ConnectionFactory
      Parameters:
      user - the user for which to create a connection
      Returns:
      a new JDBC connection
    • connectionValid

      public final boolean connectionValid(Connection connection)
      Description copied from interface: ConnectionFactory
      Validates the given connection.
      Specified by:
      connectionValid in interface ConnectionFactory
      Parameters:
      connection - the connection to validate
      Returns:
      true if the connection is valid
    • queryCounter

      public final Database.QueryCounter queryCounter()
      Specified by:
      queryCounter in interface Database
      Returns:
      the Database.QueryCounter for collecting query statistics
    • statistics

      public final Database.Statistics statistics()
      Description copied from interface: Database
      Returns statistics collected via Database.queryCounter(). Note that calling this method resets the counter.
      Specified by:
      statistics in interface Database
      Returns:
      collected statistics.
    • createConnectionPool

      public final ConnectionPoolWrapper createConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser)
      Description copied from interface: Database
      Creates a connection pool for the given user in this database.
      Specified by:
      createConnectionPool in interface Database
      Parameters:
      connectionPoolFactory - the ConnectionPoolFactory implementation to use
      poolUser - the user for which to create a connection pool
      Returns:
      a new ConnectionPoolWrapper
    • containsConnectionPool

      public final boolean containsConnectionPool(String username)
      Specified by:
      containsConnectionPool in interface Database
      Parameters:
      username - the username
      Returns:
      true if a connection pool exists for the given username
    • connectionPool

      public final ConnectionPoolWrapper connectionPool(String username)
      Specified by:
      connectionPool in interface Database
      Parameters:
      username - the username
      Returns:
      the connection pool for the given user, null if none exists
    • closeConnectionPool

      public final void closeConnectionPool(String username)
      Description copied from interface: Database
      Closes and removes the pool associated with the given user
      Specified by:
      closeConnectionPool in interface Database
      Parameters:
      username - the username of the pool that should be removed
    • closeConnectionPools

      public final void closeConnectionPools()
      Description copied from interface: Database
      Closes and removes all available connection pools
      Specified by:
      closeConnectionPools in interface Database
    • connectionPoolUsernames

      public final Collection<String> connectionPoolUsernames()
      Specified by:
      connectionPoolUsernames in interface Database
      Returns:
      the usernames of all available connection pools
    • connectionProvider

      public final void connectionProvider(ConnectionProvider connectionProvider)
      Description copied from interface: Database
      Sets the ConnectionProvider instance used when creating connections.
      Specified by:
      connectionProvider in interface Database
      Parameters:
      connectionProvider - the connection provider
    • subqueryRequiresAlias

      public boolean subqueryRequiresAlias()
      Description copied from interface: Database
      Returns true if this database requires that subqueries by aliased.
      Specified by:
      subqueryRequiresAlias in interface Database
      Returns:
      true if subqueries require an alias
    • maximumNumberOfParameters

      public int maximumNumberOfParameters()
      Description copied from interface: Database
      Returns the maximum number of prepared statement parameters, supported by this database. The default implementation simply returns Integer.MAX_VALUE, as in, no limit.
      Specified by:
      maximumNumberOfParameters in interface Database
      Returns:
      the maximum number of prepared statement parameters, supported by this database.
    • sequenceQuery

      public String sequenceQuery(String sequenceName)
      Description copied from interface: Database
      Returns a query string for selecting the next value from the given sequence.
      Specified by:
      sequenceQuery in interface Database
      Parameters:
      sequenceName - the name of the sequence
      Returns:
      a query for selecting the next value from the given sequence
    • errorMessage

      public String errorMessage(SQLException exception, Database.Operation operation)
      Description copied from interface: Database
      Returns a user-friendly error message for the given exception, otherwise simply return the message from exception
      Specified by:
      errorMessage in interface Database
      Parameters:
      exception - the underlying SQLException
      operation - the operation resulting in the exception
      Returns:
      the message assigned to the given exception
    • isAuthenticationException

      public boolean isAuthenticationException(SQLException exception)
      Description copied from interface: Database
      Returns true if this exception represents a login credentials failure
      Specified by:
      isAuthenticationException in interface Database
      Parameters:
      exception - the exception
      Returns:
      true if this exception represents a login credentials failure
    • isReferentialIntegrityException

      public boolean isReferentialIntegrityException(SQLException exception)
      Description copied from interface: Database
      Returns true if this exception is a referential integrity exception
      Specified by:
      isReferentialIntegrityException in interface Database
      Parameters:
      exception - the exception
      Returns:
      true if this exception is a referential integrity exception
    • isUniqueConstraintException

      public boolean isUniqueConstraintException(SQLException exception)
      Description copied from interface: Database
      Returns true if this exception is a unique key exception
      Specified by:
      isUniqueConstraintException in interface Database
      Parameters:
      exception - the exception
      Returns:
      true if this exception is a unique key exception
    • isTimeoutException

      public boolean isTimeoutException(SQLException exception)
      Description copied from interface: Database
      Returns true if this exception is a timeout exception
      Specified by:
      isTimeoutException in interface Database
      Parameters:
      exception - the exception
      Returns:
      true if this exception is a timeout exception
    • exception

      public DatabaseException exception(SQLException exception, Database.Operation operation)
      Description copied from interface: Database
      Maps the given SQLException to a DatabaseException.
      Specified by:
      exception in interface Database
      Parameters:
      exception - the SQL exception
      operation - the current operation
      Returns:
      a DatabaseException
      See Also:
    • createLimitOffsetClause

      protected static String createLimitOffsetClause(Integer limit, Integer offset)
      Creates a limit/offset clause of the form LIMIT {limit} OFFSET {offset}. Returns a partial clause if either value is null. If both values are null, an empty string is returned.
      Parameters:
      limit - the limit, may be null
      offset - the offset, may be null
      Returns:
      a limit/offset clause
    • createOffsetFetchNextClause

      protected static String createOffsetFetchNextClause(Integer limit, Integer offset)
      Creates a offset/fetch next clause of the form OFFSET {offset} ROWS FETCH NEXT {limit} ROWS ONLY. Returns a partial clause if either value is null. If both values are null, an empty string is returned.
      Parameters:
      limit - the limit, may be null
      offset - the offset, may be null
      Returns:
      a limit/offset clause
    • removeUrlPrefixOptionsAndParameters

      protected static String removeUrlPrefixOptionsAndParameters(String url, String... prefixes)
      Removes the given prefixes along with any options and parameters from the given jdbc url.
      Parameters:
      url - the url
      prefixes - the prefixes to remove
      Returns:
      the given url without prefixes, options and parameters