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.QueryCounter, Database.Statistics -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringFOR UPDATEprotected static final StringFOR UPDATE NOWAITFields inherited from interface is.codion.common.db.database.Database
CLIENT_INFO, CONNECTION_VALIDITY_CHECK_TIMEOUT, COUNT_QUERIES, INIT_SCRIPTS, LEGACY_JDBC, LOGIN_TIMEOUT, PASSWORD, SELECT_FOR_UPDATE_NOWAIT, TRANSACTION_ISOLATION, URL, URL_SCOPED_INSTANCE, USER -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractDatabase(String url) Instantiates a new AbstractDatabase. -
Method Summary
Modifier and TypeMethodDescriptionReturns 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, seeerrorDetail(SQLException, ErrorType).protected static voidclientInfoProperty(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, seeDatabase.clientInfo(Connection, ClientInfo).final voidclose()Closes this database, releasing any local resources, like connection pools or in memory storage.final voidcloseConnectionPool(String username) Closes and removes the pool associated with the given user.final voidCloses and removes all available connection poolsprotected voidCloses the database, releasing any in memory storage or other resources.final ConnectionPoolWrapperconnectionPool(String username) The username is case-insensitive.final Collection<String> Note that the returned usernames are lower case.final voidconnectionProvider(ConnectionProvider connectionProvider) Sets theConnectionProviderinstance to use when creating connections.final booleanconnectionValid(Connection connection) Validates the given connection.final booleancontainsConnectionPool(String username) The username is case-insensitive.final ConnectionCreates a connection.final ConnectioncreateConnection(User user) Creates a connection for the given user.final ConnectionPoolWrappercreateConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser) Creates a connection pool for the given user in this database.protected static StringcreateLimitOffsetClause(@Nullable Integer limit, @Nullable Integer offset) Creates a limit/offset clause of the formLIMIT {limit} OFFSET {offset}.protected static StringcreateLimitOffsetClause(@Nullable Integer limit, @Nullable Integer offset, @Nullable String noLimit) Creates a limit/offset clause of the formLIMIT {limit} OFFSET {offset}, for databases which do not accept an offset without a limit.protected static StringcreateOffsetFetchNextClause(@Nullable Integer limit, @Nullable Integer offset) Creates an offset/fetch next clause of the formOFFSET {offset} ROWS FETCH NEXT {limit} ROWS ONLY.protected static StringdatabaseOrHost(String url) Returns the database from a url of the form//host:port/database, stripped of prefix, options and parameters, seeremoveUrlPrefixOptionsAndParameters(String, String...), that is, what follows the last slash.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.errorType(SQLException exception) Returns the type of error the given exception represents, null if it is not recognized.final DatabaseExceptionexception(SQLException exception, Operation operation) Returns an exception based on theErrorTypeof the given exception, seeerrorType(SQLException), carrying the error type along with the detail, if any, seeerrorDetail(SQLException, ErrorType), its message the one associated with the error type, in the language of the one reading it, seeDatabaseException.getMessage(), or the exception message in case the error type is not recognized, seemessage(SQLException).GetValue<?> getter(int sqlType) intReturns the maximum number of prepared statement parameters supported by this database,Integer.MAX_VALUEindicating no limit.message(SQLException exception) Returns the message to present for an exception of an unrecognized type, by default the exception message.final Database.QueryCounterprotected static StringremoveUrlPrefixOptionsAndParameters(String url, String... prefixes) Removes the given prefixes along with any options and parameters from the given jdbc url.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.sequenceQuery(String sequenceName) Returns a query string for selecting the next value from the given sequence.SetValue<?> setter(int sqlType) final Database.StatisticsReturns statistics collected viaDatabase.queryCounter().booleanReturns true if this database requires that subqueries by aliased.final Stringurl()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, waitMethods inherited from interface is.codion.common.db.database.Database
autoIncrementQuery, clientInfo, 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:ConnectionFactoryReturns the jdbc database url for this connection factory.- Specified by:
urlin interfaceConnectionFactory- Returns:
- the jdbc database url for this connection factory.
-
createConnection
Description copied from interface:ConnectionFactoryCreates a connection.- Specified by:
createConnectionin interfaceConnectionFactory- Returns:
- a new JDBC connection
-
createConnection
Description copied from interface:ConnectionFactoryCreates a connection for the given user.- Specified by:
createConnectionin interfaceConnectionFactory- Parameters:
user- the user for which to create a connection- Returns:
- a new JDBC connection
-
connectionValid
Description copied from interface:ConnectionFactoryValidates the given connection.- Specified by:
connectionValidin interfaceConnectionFactory- Parameters:
connection- the connection to validate- Returns:
- true if the connection is valid
-
queryCounter
- Specified by:
queryCounterin interfaceDatabase- Returns:
- the
Database.QueryCounterfor collecting query statistics
-
statistics
Description copied from interface:DatabaseReturns statistics collected viaDatabase.queryCounter(). Note that calling this method resets the counter.- Specified by:
statisticsin interfaceDatabase- Returns:
- collected statistics.
-
createConnectionPool
public final ConnectionPoolWrapper createConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser) Description copied from interface:DatabaseCreates a connection pool for the given user in this database. The pool is keyed on the username, case-insensitively.- Specified by:
createConnectionPoolin interfaceDatabase- Parameters:
connectionPoolFactory- the ConnectionPoolFactory implementation to usepoolUser- the user for which to create a connection pool- Returns:
- a new
ConnectionPoolWrapper
-
containsConnectionPool
Description copied from interface:DatabaseThe username is case-insensitive.- Specified by:
containsConnectionPoolin interfaceDatabase- Parameters:
username- the username- Returns:
- true if a connection pool exists for the given username
-
connectionPool
Description copied from interface:DatabaseThe username is case-insensitive.- Specified by:
connectionPoolin interfaceDatabase- Parameters:
username- the username- Returns:
- the connection pool for the given user
-
closeConnectionPool
Description copied from interface:DatabaseCloses and removes the pool associated with the given user. The username is case-insensitive.- Specified by:
closeConnectionPoolin interfaceDatabase- Parameters:
username- the username of the pool that should be removed
-
closeConnectionPools
public final void closeConnectionPools()Description copied from interface:DatabaseCloses and removes all available connection pools- Specified by:
closeConnectionPoolsin interfaceDatabase
-
connectionPoolUsernames
Description copied from interface:DatabaseNote that the returned usernames are lower case.- Specified by:
connectionPoolUsernamesin interfaceDatabase- Returns:
- the usernames of all available connection pools
-
connectionProvider
Description copied from interface:DatabaseSets theConnectionProviderinstance to use when creating connections.- Specified by:
connectionProviderin interfaceDatabase- Parameters:
connectionProvider- the connection provider
-
getter
Description copied from interface:Database -
setter
Description copied from interface:Database -
selectForUpdateTableHint
Description copied from interface:DatabaseReturns 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:
selectForUpdateTableHintin interfaceDatabase- Returns:
- a select for update table hint
- See Also:
-
subqueryRequiresAlias
public boolean subqueryRequiresAlias()Description copied from interface:DatabaseReturns true if this database requires that subqueries by aliased.- Specified by:
subqueryRequiresAliasin interfaceDatabase- Returns:
- true if subqueries require an alias
-
maximumParameters
public int maximumParameters()Description copied from interface:DatabaseReturns the maximum number of prepared statement parameters supported by this database,Integer.MAX_VALUEindicating no limit.- Specified by:
maximumParametersin interfaceDatabase- Returns:
- the maximum number of prepared statement parameters, supported by this database.
-
sequenceQuery
Description copied from interface:DatabaseReturns a query string for selecting the next value from the given sequence.- Specified by:
sequenceQueryin interfaceDatabase- Parameters:
sequenceName- the name of the sequence- Returns:
- a query for selecting the next value from the given sequence
-
exception
Returns an exception based on theErrorTypeof the given exception, seeerrorType(SQLException), carrying the error type along with the detail, if any, seeerrorDetail(SQLException, ErrorType), its message the one associated with the error type, in the language of the one reading it, seeDatabaseException.getMessage(), or the exception message in case the error type is not recognized, seemessage(SQLException).- Specified by:
exceptionin interfaceDatabase- Parameters:
exception- the SQL exceptionoperation- the current operation- Returns:
- a
DatabaseException - See Also:
-
close
public final void close()Description copied from interface:DatabaseCloses this database, releasing any local resources, like connection pools or in memory storage. -
errorType
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 onSQLTimeoutException. 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
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 exceptionerrorType- the error type- Returns:
- the detail, null if none is available
-
message
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
Closes the database, releasing any in memory storage or other resources.- Throws:
SQLException- in case of an exception
-
clientInfoProperty
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 connectionproperty- the client info property namevalue- the value, an empty String to clear it, null values being rejected by some drivers
-
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
-
createLimitOffsetClause
protected static String createLimitOffsetClause(@Nullable Integer limit, @Nullable Integer offset, @Nullable String noLimit) Creates a limit/offset clause of the formLIMIT {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 nulloffset- the offset, may be nullnoLimit- 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 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
-
databaseOrHost
Returns the database from a url of the form//host:port/database, stripped of prefix, options and parameters, seeremoveUrlPrefixOptionsAndParameters(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
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, seeerrorDetail(SQLException, ErrorType).- Parameters:
text- the textprefix- the prefixsuffix- the suffix- Returns:
- the text between the first occurrence of the prefix and the suffix following it, null if either is not found
-