Package com.d3x.core.db
Class DatabaseConfig
- java.lang.Object
-
- com.d3x.core.db.DatabaseConfig
-
public class DatabaseConfig extends java.lang.ObjectA class that capture database configuration details- Author:
- Xavier Witdouck
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDatabaseConfig.DatabaseConfigBuilderstatic classDatabaseConfig.Deserializerstatic classDatabaseConfig.Serializer
-
Field Summary
Fields Modifier and Type Field Description private @NonNull com.d3x.core.util.Option<java.lang.Boolean>autoCommitTrue to mark connection as auto commitprivate @NonNull DatabaseDriverdriverThe driver definitionprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>fetchSizeThe default fetch size for statementsprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>initialPoolSizeThe initial size of the connection poolprivate static org.slf4j.Loggerlogprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>maxPoolIdleSizeThe max number of idle connections in poolprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>maxPoolSizeThe max size of connection poolprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>maxWaitTimeMillisThe max time to wait for an available connectionprivate @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret>passwordThe JDBC passwordprivate @NonNull java.util.Map<java.lang.String,java.lang.String>propertiesThe connection propertiesprivate @NonNull com.d3x.core.util.Option<java.lang.Integer>queryTimeOutSecondsThe query time out in secondsprivate @NonNull com.d3x.core.util.Option<java.lang.Boolean>readOnlyTrue if connections should be set to read onlystatic com.d3x.core.json.JsonSchemaschemaprivate @NonNull java.lang.StringurlThe JDBC connection urlprivate @NonNull com.d3x.core.util.Option<java.lang.String>userThe JDBC user name
-
Constructor Summary
Constructors Constructor Description DatabaseConfig(@NonNull DatabaseDriver driver, @NonNull java.lang.String url, @NonNull com.d3x.core.util.Option<java.lang.String> user, @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret> password, @NonNull com.d3x.core.util.Option<java.lang.Integer> initialPoolSize, @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolSize, @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolIdleSize, @NonNull com.d3x.core.util.Option<java.lang.Boolean> readOnly, @NonNull com.d3x.core.util.Option<java.lang.Boolean> autoCommit, @NonNull com.d3x.core.util.Option<java.lang.Integer> queryTimeOutSeconds, @NonNull com.d3x.core.util.Option<java.lang.Integer> maxWaitTimeMillis, @NonNull com.d3x.core.util.Option<java.lang.Integer> fetchSize, @NonNull java.util.Map<java.lang.String,java.lang.String> properties)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DatabaseConfig.DatabaseConfigBuilderbuilder()protected booleancanEqual(java.lang.Object other)booleanequals(java.lang.Object o)static DatabaseConfigfromJson(java.lang.String resource)Returns the config loaded from a json file on classpathstatic DatabaseConfigfromJson(java.net.URL url, com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto)Returns database config loaded from a JSON resource@NonNull com.d3x.core.util.Option<java.lang.Boolean>getAutoCommit()True to mark connection as auto commit@NonNull DatabaseDrivergetDriver()The driver definition@NonNull com.d3x.core.util.Option<java.lang.Integer>getFetchSize()The default fetch size for statements@NonNull com.d3x.core.util.Option<java.lang.Integer>getInitialPoolSize()The initial size of the connection pool@NonNull com.d3x.core.util.Option<java.lang.Integer>getMaxPoolIdleSize()The max number of idle connections in pool@NonNull com.d3x.core.util.Option<java.lang.Integer>getMaxPoolSize()The max size of connection pool@NonNull com.d3x.core.util.Option<java.lang.Integer>getMaxWaitTimeMillis()The max time to wait for an available connection@NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret>getPassword()The JDBC passwordjava.util.Map<java.lang.String,java.lang.String>getProperties()Returns the map of connection properties@NonNull com.d3x.core.util.Option<java.lang.Integer>getQueryTimeOutSeconds()The query time out in seconds@NonNull com.d3x.core.util.Option<java.lang.Boolean>getReadOnly()True if connections should be set to read only@NonNull java.lang.StringgetUrl()The JDBC connection url@NonNull com.d3x.core.util.Option<java.lang.String>getUser()The JDBC user namestatic com.google.gson.Gsongson(com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto)Returns a GSON adapter that can serialise DatabaseConfigstatic DatabaseConfigh2(java.io.File dbFile, java.lang.String user, java.lang.String password)Returns a new database config for an H2 databaseinthashCode()static DatabaseConfigmysql(java.lang.String url, java.lang.String username, com.d3x.core.util.Secret password)Returns a new config for MySQL with the details providedstatic DatabaseConfigof(DatabaseDriver driver, java.lang.String url, java.lang.String user, com.d3x.core.util.Secret password)Returns a new database config for the args providedstatic DatabaseConfigof(java.util.function.Consumer<DatabaseConfig.DatabaseConfigBuilder> consumer)Returns a new database config base on the consumer actionsDatabaseConfig.DatabaseConfigBuildertoBuilder()voidtoJson(com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto, java.io.OutputStream os)Writes this config as JSON to the output stream providedjava.lang.StringtoString()DatabaseConfigverify()Attempts to connect to the database given this config to assess if it is valid
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
schema
public static final com.d3x.core.json.JsonSchema schema
-
driver
@NonNull private @NonNull DatabaseDriver driver
The driver definition
-
url
@NonNull private @NonNull java.lang.String url
The JDBC connection url
-
user
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.String> user
The JDBC user name
-
password
@NonNull private @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret> password
The JDBC password
-
initialPoolSize
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> initialPoolSize
The initial size of the connection pool
-
maxPoolSize
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolSize
The max size of connection pool
-
maxPoolIdleSize
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolIdleSize
The max number of idle connections in pool
-
readOnly
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Boolean> readOnly
True if connections should be set to read only
-
autoCommit
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Boolean> autoCommit
True to mark connection as auto commit
-
queryTimeOutSeconds
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> queryTimeOutSeconds
The query time out in seconds
-
maxWaitTimeMillis
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxWaitTimeMillis
The max time to wait for an available connection
-
fetchSize
@NonNull private @NonNull com.d3x.core.util.Option<java.lang.Integer> fetchSize
The default fetch size for statements
-
properties
@NonNull private @NonNull java.util.Map<java.lang.String,java.lang.String> properties
The connection properties
-
-
Constructor Detail
-
DatabaseConfig
public DatabaseConfig(@NonNull @NonNull DatabaseDriver driver, @NonNull @NonNull java.lang.String url, @NonNull @NonNull com.d3x.core.util.Option<java.lang.String> user, @NonNull @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret> password, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> initialPoolSize, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolSize, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolIdleSize, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Boolean> readOnly, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Boolean> autoCommit, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> queryTimeOutSeconds, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> maxWaitTimeMillis, @NonNull @NonNull com.d3x.core.util.Option<java.lang.Integer> fetchSize, @NonNull @NonNull java.util.Map<java.lang.String,java.lang.String> properties)
-
-
Method Detail
-
getProperties
public java.util.Map<java.lang.String,java.lang.String> getProperties()
Returns the map of connection properties- Returns:
- the map of connection properties
-
h2
public static DatabaseConfig h2(java.io.File dbFile, java.lang.String user, java.lang.String password)
Returns a new database config for an H2 database- Parameters:
dbFile- the database fileuser- the usernamepassword- the password- Returns:
- the config
-
mysql
public static DatabaseConfig mysql(java.lang.String url, java.lang.String username, com.d3x.core.util.Secret password)
Returns a new config for MySQL with the details provided- Parameters:
url- the jdbc urlusername- the jdbc usernamepassword- the jdbc password- Returns:
- the newly created config
-
of
public static DatabaseConfig of(DatabaseDriver driver, java.lang.String url, java.lang.String user, com.d3x.core.util.Secret password)
Returns a new database config for the args provided- Parameters:
driver- the database driverurl- the database urluser- the usernamepassword- the password- Returns:
- the config
-
fromJson
public static DatabaseConfig fromJson(java.net.URL url, com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto) throws java.io.IOException
Returns database config loaded from a JSON resource- Parameters:
url- the URL to load config fromcrypto- the optional crypto if password is encrypted- Returns:
- the database config
- Throws:
java.io.IOException
-
fromJson
public static DatabaseConfig fromJson(java.lang.String resource) throws DatabaseException
Returns the config loaded from a json file on classpath- Parameters:
resource- the path to json resource- Returns:
- the database config
- Throws:
DatabaseException- if fails to initialize config
-
toJson
public void toJson(com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto, java.io.OutputStream os) throws java.io.IOExceptionWrites this config as JSON to the output stream provided- Parameters:
crypto- the optional crypto for password encryptionos- the output stream to write to- Throws:
java.io.IOException
-
of
public static DatabaseConfig of(java.util.function.Consumer<DatabaseConfig.DatabaseConfigBuilder> consumer)
Returns a new database config base on the consumer actions- Parameters:
consumer- the consumer to configure builder- Returns:
- the newly created config
-
gson
public static com.google.gson.Gson gson(com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto)
Returns a GSON adapter that can serialise DatabaseConfig- Parameters:
crypto- the optional crypto for password encryption- Returns:
- the GSON instance
-
verify
public DatabaseConfig verify()
Attempts to connect to the database given this config to assess if it is valid- Returns:
- this configuration object
- Throws:
DatabaseException- if fails to make a connection
-
builder
public static DatabaseConfig.DatabaseConfigBuilder builder()
-
toBuilder
public DatabaseConfig.DatabaseConfigBuilder toBuilder()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
canEqual
protected boolean canEqual(java.lang.Object other)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
getDriver
@NonNull public @NonNull DatabaseDriver getDriver()
The driver definition
-
getUrl
@NonNull public @NonNull java.lang.String getUrl()
The JDBC connection url
-
getUser
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.String> getUser()
The JDBC user name
-
getPassword
@NonNull public @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret> getPassword()
The JDBC password
-
getInitialPoolSize
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getInitialPoolSize()
The initial size of the connection pool
-
getMaxPoolSize
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getMaxPoolSize()
The max size of connection pool
-
getMaxPoolIdleSize
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getMaxPoolIdleSize()
The max number of idle connections in pool
-
getReadOnly
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Boolean> getReadOnly()
True if connections should be set to read only
-
getAutoCommit
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Boolean> getAutoCommit()
True to mark connection as auto commit
-
getQueryTimeOutSeconds
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getQueryTimeOutSeconds()
The query time out in seconds
-
getMaxWaitTimeMillis
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getMaxWaitTimeMillis()
The max time to wait for an available connection
-
getFetchSize
@NonNull public @NonNull com.d3x.core.util.Option<java.lang.Integer> getFetchSize()
The default fetch size for statements
-
-