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()
      Description copied from interface: ConnectionFactory
      Creates a connection.
      Specified by:
      createConnection in interface ConnectionFactory
      Returns:
      a new JDBC connection
    • 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. The pool is keyed on the username, case-insensitively.
      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)
      Description copied from interface: Database
      The username is case-insensitive.
      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)
      Description copied from interface: Database
      The username is case-insensitive.
      Specified by:
      connectionPool in interface Database
      Parameters:
      username - the username
      Returns:
      the connection pool for the given user
    • closeConnectionPool

      public final void closeConnectionPool(String username)
      Description copied from interface: Database
      Closes and removes the pool associated with the given user. The username is case-insensitive.
      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()
      Description copied from interface: Database
      Note that the returned usernames are lower case.
      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 to use when creating connections.
      Specified by:
      connectionProvider in interface Database
      Parameters:
      connectionProvider - the connection provider
    • getter

      public GetValue<?> getter(int sqlType)
      Description copied from interface: Database
      Returns the GetValue for reading a value of the given SQL type from a ResultSet.
      Specified by:
      getter in interface Database
      Parameters:
      sqlType - the SQL type
      Returns:
      the GetValue for the given SQL type
      See Also:
    • setter

      public SetValue<?> setter(int sqlType)
      Description copied from interface: Database
      Returns the SetValue for setting a value of the given SQL type on a PreparedStatement.
      Specified by:
      setter in interface Database
      Parameters:
      sqlType - the SQL type
      Returns:
      the SetValue for the given SQL type
    • selectForUpdateTableHint

      public String selectForUpdateTableHint()
      Description copied from interface: Database
      Returns a table hint for locking the selected rows, appended to the table name in the from clause of a select for update query, for databases which lock rows via a table hint instead of a select for update clause, an empty string if not applicable. Note that the hint is not applied when selecting from a custom from clause, there being no single table to apply it to.
      Specified by:
      selectForUpdateTableHint in interface Database
      Returns:
      a select for update table hint
      See Also:
    • 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
    • maximumParameters

      public int maximumParameters()
      Description copied from interface: Database
      Returns the maximum number of prepared statement parameters supported by this database, Integer.MAX_VALUE indicating no limit.
      Specified by:
      maximumParameters 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
    • exception

      public final DatabaseException exception(SQLException exception, Operation operation)
      Returns an exception based on the ErrorType of the given exception, see errorType(SQLException), carrying the error type along with the detail, if any, see errorDetail(SQLException, ErrorType), its message the one associated with the error type, in the language of the one reading it, see DatabaseException.getMessage(), or the exception message in case the error type is not recognized, see message(SQLException).
      Specified by:
      exception in interface Database
      Parameters:
      exception - the SQL exception
      operation - the current operation
      Returns:
      a DatabaseException
      See Also:
    • close

      public final void close()
      Description copied from interface: Database
      Closes this database, releasing any local resources, like connection pools or in memory storage.
      Specified by:
      close in interface Database
      See Also:
    • errorType

      protected @Nullable ErrorType errorType(SQLException exception)
      Returns the type of error the given exception represents, null if it is not recognized. This default implementation is based on the SQL states databases agree on, and on SQLTimeoutException. Databases reporting their own error codes override, falling back on this one for the ones not recognized. Note that this is called while an exception is being handled, an exception thrown here is logged and ignored.
      Parameters:
      exception - the exception
      Returns:
      the error type, null if not recognized
    • errorDetail

      protected @Nullable String errorDetail(SQLException exception, ErrorType errorType)
      Returns the detail to append to the message associated with the given error type, the name of the column missing a value for example, null if none is available, which is the default. Note that this is called while an exception is being handled, an exception thrown here is logged and ignored.
      Parameters:
      exception - the exception
      errorType - the error type
      Returns:
      the detail, null if none is available
    • message

      protected @Nullable String message(SQLException exception)
      Returns the message to present for an exception of an unrecognized type, by default the exception message. Override to remove what the driver or database adds to it, the statement or links to documentation for example. Note that this is called while an exception is being handled, an exception thrown here is logged and ignored.
      Parameters:
      exception - the exception
      Returns:
      the message
    • closeDatabase

      protected void closeDatabase() throws SQLException
      Closes the database, releasing any in memory storage or other resources.
      Throws:
      SQLException - in case of an exception
    • clientInfoProperty

      protected static void clientInfoProperty(Connection connection, String property, String value)

      Sets a single client info property, swallowing the exception a driver throws for a property it does not recognize - a stamp is a nicety, not a precondition for using the connection, see Database.clientInfo(Connection, ClientInfo). For subclasses implementing that.

      Logged at debug rather than warn: a driver which refuses one property refuses it on every check out, and a warning per database call is worse than the missing stamp.

      Parameters:
      connection - the connection
      property - the client info property name
      value - the value, an empty String to clear it, null values being rejected by some drivers
    • createLimitOffsetClause

      protected static String createLimitOffsetClause(@Nullable Integer limit, @Nullable 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
    • createLimitOffsetClause

      protected static String createLimitOffsetClause(@Nullable Integer limit, @Nullable Integer offset, @Nullable String noLimit)
      Creates a limit/offset clause of the form LIMIT {limit} OFFSET {offset}, for databases which do not accept an offset without a limit. If both values are null, an empty string is returned.
      Parameters:
      limit - the limit, may be null
      offset - the offset, may be null
      noLimit - the limit to use in case only the offset is specified, null for none
      Returns:
      a limit/offset clause
    • createOffsetFetchNextClause

      protected static String createOffsetFetchNextClause(@Nullable Integer limit, @Nullable Integer offset)
      Creates an 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
    • databaseOrHost

      protected static String databaseOrHost(String url)
      Returns the database from a url of the form //host:port/database, stripped of prefix, options and parameters, see removeUrlPrefixOptionsAndParameters(String, String...), that is, what follows the last slash. In case no database is specified, //host:port/ or //host:port, the host and port are returned.
      Parameters:
      url - the url, without prefix, options and parameters
      Returns:
      the database, or the host in case no database is specified
    • between

      protected static @Nullable String between(String text, String prefix, String suffix)
      Returns the text found between the given prefix and the suffix following it, for picking the name of a constraint or column from an exception message, see errorDetail(SQLException, ErrorType).
      Parameters:
      text - the text
      prefix - the prefix
      suffix - the suffix
      Returns:
      the text between the first occurrence of the prefix and the suffix following it, null if either is not found