java.lang.Object
is.codion.common.db.database.AbstractDatabase
- All Implemented Interfaces:
ConnectionFactory
,Database
A default abstract implementation of the Database interface.
-
Nested Class Summary
Nested classes/interfaces inherited from interface is.codion.common.db.database.Database
Database.Operation, Database.QueryCounter, Database.Statistics
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final String
FOR UPDATE
protected static final String
FOR UPDATE NOWAIT
Fields inherited from interface is.codion.common.db.database.Database
CONNECTION_VALIDITY_CHECK_TIMEOUT, COUNT_QUERIES, DATABASE_INIT_SCRIPTS, DATABASE_URL, LOGIN_TIMEOUT, PASSWORD_PROPERTY, SELECT_FOR_UPDATE_NOWAIT, TRANSACTION_ISOLATION, USER_PROPERTY
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractDatabase
(String url) Instantiates a new AbstractDatabase. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
closeConnectionPool
(String username) Closes and removes the pool associated with the given userfinal void
Closes and removes all available connection poolsfinal ConnectionPoolWrapper
connectionPool
(String username) final Collection<String>
final void
connectionProvider
(ConnectionProvider connectionProvider) Sets theConnectionProvider
instance used when creating connections.final boolean
connectionValid
(Connection connection) Validates the given connection.final boolean
containsConnectionPool
(String username) final Connection
createConnection
(User user) Creates a connection for the given user.final ConnectionPoolWrapper
createConnectionPool
(ConnectionPoolFactory connectionPoolFactory, User poolUser) Creates a connection pool for the given user in this database.protected static String
createLimitOffsetClause
(Integer limit, Integer offset) Creates a limit/offset clause of the formLIMIT {limit} OFFSET {offset}
.protected static String
createOffsetFetchNextClause
(Integer limit, Integer offset) Creates a offset/fetch next clause of the formOFFSET {offset} ROWS FETCH NEXT {limit} ROWS ONLY
.errorMessage
(SQLException exception, Database.Operation operation) Returns a user-friendly error message for the given exception, otherwise simply return the message fromexception
exception
(SQLException exception, Database.Operation operation) Maps the given SQLException to a DatabaseException.boolean
isAuthenticationException
(SQLException exception) Returns true if this exception represents a login credentials failureboolean
isReferentialIntegrityException
(SQLException exception) Returns true if this exception is a referential integrity exceptionboolean
isTimeoutException
(SQLException exception) Returns true if this exception is a timeout exceptionboolean
isUniqueConstraintException
(SQLException exception) Returns true if this exception is a unique key exceptionint
Returns the maximum number of prepared statement parameters, supported by this database.final Database.QueryCounter
protected static String
removeUrlPrefixOptionsAndParameters
(String url, String... prefixes) Removes the given prefixes along with any options and parameters from the given jdbc url.sequenceQuery
(String sequenceName) Returns a query string for selecting the next value from the given sequence.final Database.Statistics
Returns statistics collected viaDatabase.queryCounter()
.boolean
Returns true if this database requires that subqueries by aliased.final String
url()
Returns the jdbc database url for this connection factory.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface is.codion.common.db.database.Database
autoIncrementQuery, limitOffsetClause, name, selectForUpdateClause
-
Field Details
-
FOR_UPDATE
FOR UPDATE
- See Also:
-
FOR_UPDATE_NOWAIT
FOR UPDATE NOWAIT
- See Also:
-
-
Constructor Details
-
AbstractDatabase
Instantiates a new AbstractDatabase.- Parameters:
url
- the jdbc url
-
-
Method Details
-
url
Description copied from interface:ConnectionFactory
Returns the jdbc database url for this connection factory.- Specified by:
url
in interfaceConnectionFactory
- Returns:
- the jdbc database url for this connection factory.
-
createConnection
Description copied from interface:ConnectionFactory
Creates a connection for the given user.- Specified by:
createConnection
in interfaceConnectionFactory
- Parameters:
user
- the user for which to create a connection- Returns:
- a new JDBC connection
- Throws:
DatabaseException
- in case of a connection errorAuthenticationException
- in case of an authentication error
-
connectionValid
Description copied from interface:ConnectionFactory
Validates the given connection.- Specified by:
connectionValid
in interfaceConnectionFactory
- Parameters:
connection
- the connection to validate- Returns:
- true if the connection is valid
-
queryCounter
- Specified by:
queryCounter
in interfaceDatabase
- Returns:
- the
Database.QueryCounter
for collecting query statistics
-
statistics
Description copied from interface:Database
Returns statistics collected viaDatabase.queryCounter()
. Note that calling this method resets the counter.- Specified by:
statistics
in interfaceDatabase
- Returns:
- collected statistics.
-
createConnectionPool
public final ConnectionPoolWrapper createConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser) throws DatabaseException Description copied from interface:Database
Creates a connection pool for the given user in this database.- Specified by:
createConnectionPool
in interfaceDatabase
- Parameters:
connectionPoolFactory
- the ConnectionPoolFactory implementation to usepoolUser
- the user for which to create a connection pool- Returns:
- a new
ConnectionPoolWrapper
- Throws:
DatabaseException
- in case of a database exception
-
containsConnectionPool
- Specified by:
containsConnectionPool
in interfaceDatabase
- Parameters:
username
- the username- Returns:
- true if a connection pool exists for the given username
-
connectionPool
- Specified by:
connectionPool
in interfaceDatabase
- Parameters:
username
- the username- Returns:
- the connection pool for the given user, null if none exists
-
closeConnectionPool
Description copied from interface:Database
Closes and removes the pool associated with the given user- Specified by:
closeConnectionPool
in interfaceDatabase
- 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 interfaceDatabase
-
connectionPoolUsernames
- Specified by:
connectionPoolUsernames
in interfaceDatabase
- Returns:
- the usernames of all available connection pools
-
connectionProvider
Description copied from interface:Database
Sets theConnectionProvider
instance used when creating connections.- Specified by:
connectionProvider
in interfaceDatabase
- 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 interfaceDatabase
- 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 returnsInteger.MAX_VALUE
, as in, no limit.- Specified by:
maximumNumberOfParameters
in interfaceDatabase
- Returns:
- the maximum number of prepared statement parameters, supported by this database.
-
sequenceQuery
Description copied from interface:Database
Returns a query string for selecting the next value from the given sequence.- Specified by:
sequenceQuery
in interfaceDatabase
- Parameters:
sequenceName
- the name of the sequence- Returns:
- a query for selecting the next value from the given sequence
-
errorMessage
Description copied from interface:Database
Returns a user-friendly error message for the given exception, otherwise simply return the message fromexception
- Specified by:
errorMessage
in interfaceDatabase
- Parameters:
exception
- the underlying SQLExceptionoperation
- the operation resulting in the exception- Returns:
- the message assigned to the given exception
-
isAuthenticationException
Description copied from interface:Database
Returns true if this exception represents a login credentials failure- Specified by:
isAuthenticationException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception represents a login credentials failure
-
isReferentialIntegrityException
Description copied from interface:Database
Returns true if this exception is a referential integrity exception- Specified by:
isReferentialIntegrityException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception is a referential integrity exception
-
isUniqueConstraintException
Description copied from interface:Database
Returns true if this exception is a unique key exception- Specified by:
isUniqueConstraintException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception is a unique key exception
-
isTimeoutException
Description copied from interface:Database
Returns true if this exception is a timeout exception- Specified by:
isTimeoutException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception is a timeout exception
-
exception
Description copied from interface:Database
Maps the given SQLException to a DatabaseException. -
createLimitOffsetClause
Creates a limit/offset clause of the formLIMIT {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 nulloffset
- the offset, may be null- Returns:
- a limit/offset clause
-
createOffsetFetchNextClause
Creates a offset/fetch next clause of the formOFFSET {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 nulloffset
- the offset, may be null- Returns:
- a limit/offset clause
-
removeUrlPrefixOptionsAndParameters
Removes the given prefixes along with any options and parameters from the given jdbc url.- Parameters:
url
- the urlprefixes
- the prefixes to remove- Returns:
- the given url without prefixes, options and parameters
-