Class DatabaseConfig


  • public class DatabaseConfig
    extends java.lang.Object
    A class that capture database configuration details
    Author:
    Xavier Witdouck
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private @NonNull com.d3x.core.util.Option<java.lang.Boolean> autoCommit
      True to mark connection as auto commit
      private @NonNull DatabaseDriver driver
      The driver definition
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> fetchSize
      The default fetch size for statements
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> initialPoolSize
      The initial size of the connection pool
      private static org.slf4j.Logger log  
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolIdleSize
      The max number of idle connections in pool
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxPoolSize
      The max size of connection pool
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> maxWaitTimeMillis
      The max time to wait for an available connection
      private @NonNull com.d3x.core.util.Option<com.d3x.core.util.Secret> password
      The JDBC password
      private @NonNull java.util.Map<java.lang.String,​java.lang.String> properties
      The connection properties
      private @NonNull com.d3x.core.util.Option<java.lang.Integer> queryTimeOutSeconds
      The query time out in seconds
      private @NonNull com.d3x.core.util.Option<java.lang.Boolean> readOnly
      True if connections should be set to read only
      static com.d3x.core.json.JsonSchema schema  
      private @NonNull java.lang.String url
      The JDBC connection url
      private @NonNull com.d3x.core.util.Option<java.lang.String> user
      The 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.DatabaseConfigBuilder builder()  
      protected boolean canEqual​(java.lang.Object other)  
      boolean equals​(java.lang.Object o)  
      static DatabaseConfig fromJson​(java.lang.String resource)
      Returns the config loaded from a json file on classpath
      static DatabaseConfig fromJson​(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 DatabaseDriver getDriver()
      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 password
      java.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.String getUrl()
      The JDBC connection url
      @NonNull com.d3x.core.util.Option<java.lang.String> getUser()
      The JDBC user name
      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
      static DatabaseConfig h2​(java.io.File dbFile, java.lang.String user, java.lang.String password)
      Returns a new database config for an H2 database
      int hashCode()  
      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
      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
      static DatabaseConfig of​(java.util.function.Consumer<DatabaseConfig.DatabaseConfigBuilder> consumer)
      Returns a new database config base on the consumer actions
      DatabaseConfig.DatabaseConfigBuilder toBuilder()  
      void toJson​(com.d3x.core.util.Option<com.d3x.core.util.Crypto> crypto, java.io.OutputStream os)
      Writes this config as JSON to the output stream provided
      java.lang.String toString()  
      DatabaseConfig verify()
      Attempts to connect to the database given this config to assess if it is valid
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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 file
        user - the username
        password - 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 url
        username - the jdbc username
        password - 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 driver
        url - the database url
        user - the username
        password - 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 from
        crypto - 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.IOException
        Writes this config as JSON to the output stream provided
        Parameters:
        crypto - the optional crypto for password encryption
        os - 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
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • canEqual

        protected boolean canEqual​(java.lang.Object other)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.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