Class Datastore2

java.lang.Object
com.gmt2001.datastore2.Datastore2
Direct Known Subclasses:
H2Store2, MariaDBStore2, MySQLStore2, SQLiteStore2

public abstract class Datastore2 extends Object
Manages a JDBC-compatible, SQL-based, data storage method
Author:
gmt2001
See Also:
  • Field Details

  • Constructor Details

    • Datastore2

      public Datastore2()
  • Method Details

    • instance

      public static Datastore2 instance()
      Provides an instance of Datastore2

      If the instance has not been initialized, calls init()

      Returns:
      an instance of Datastore2
    • timestamp

      public static String timestamp()
      Returns a timestamp suitable for database backup names in the format yyyy-MM-dd.hh-mm-ss
      Returns:
      the timestamp
    • init

      public static void init()
      Initializes a new instance of Datastore2, based on the property datastore in botlogin.txt

      If a datastore driver is not specified or is blank, defaults to H2Store2

      If this function is called when Datastore2 is already initialized, it is a no-op

      Builtin datastore drivers are not case-sensitive. Custom datastore drivers are case-sensitive

      If loading a custom datastore driver, the following requirements must be met:

    • init

      protected void init(ConnectionPoolDataSource dataSource, SQLDialect sqlDialect)
      Instance Initializer. Sets a max connections of 30 and a timeout of 20

      Valid dialects:

      NOTE: SQL will be generated according to the latest supported version of the selected database dialect

      Parameters:
      dataSource - a ConnectionPoolDataSource which can be used with MiniConnectionPoolManager
      sqlDialect - the dialect to use with objects created from the DSLContext
    • init

      protected void init(ConnectionPoolDataSource dataSource, int maxConnections, int timeout, SQLDialect sqlDialect)
      Instance Initializer

      Valid dialects:

      NOTE: SQL will be generated according to the latest supported version of the selected database dialect

      Parameters:
      dataSource - a ConnectionPoolDataSource which can be used with MiniConnectionPoolManager
      maxConnections - the maximum number of Connection objects to hold in the pool
      timeout - the number of seconds until a call to MiniConnectionPoolManager.getConnection() fails waiting for a Connection to become available
      sqlDialect - the dialect to use with objects created from the DSLContext
    • checkVersion

      protected void checkVersion(SQLDialect dialect, Connection connection, String className) throws IllegalStateException, SQLException
      Checks if JOOQ supports the software version of the connected database
      Parameters:
      dialect - the SQLDialect to check againse
      connection - a valid Connection to the database
      className - the name of the Datastore2 subclass that is requesting the check
      Throws:
      IllegalStateException - if the detected software version of the database is not supported by JOOQ
      SQLException - if a database access error occurs
    • prepareConnection

      protected void prepareConnection(Connection connection) throws SQLException
      Allows the driver to perform operations to prepare the connection for use, such as selecting the schema
      Parameters:
      connection - the connection to prepare
      Throws:
      SQLException - if a database access error occurs
    • meta

      public Meta meta()
      Allows the driver to perform operations to prepare a meta object for use, such as selecting the schema
      Returns:
      A Meta object for the current database
    • getConnection

      Retrieves a Connection from the connection pool

      If the maximum number of connections are already in use, the method waits until a connection becomes available or the timeout has elapsed

      When the application is finished using the connection, it must call Connection.close() on it in order to return it to the connection pool

      Consider using try-with-resources instead to safely auto-close the connection

      Transactions are not committed automatically when closing a Connection that has auto-commit disabled

      Returns:
      a new Connection object
      Throws:
      SQLException - if a database access error occurs
      MiniConnectionPoolManager.TimeoutException - when no connection becomes available within the timeout
    • testConnection

      public boolean testConnection() throws SQLException
      Tests if the database is accessible

      How this is achieved is driver-defined, but is usually something similar to executing SELECT 1;

      Returns:
      true if a valid connection can be established to the database before the timeout expires
      Throws:
      SQLException - if a database access error occurs
    • dslContext

      public DSLContext dslContext()
      Returns the DSLContext which can be used to start a fluent statement
      Returns:
      the DSLContext
      See Also:
    • invalidateTableCache

      public void invalidateTableCache()
      Invalidates the cache of known tables

      This should be called every time a table is created, renamed, or deleted

    • tablesAsync

      public Mono<List<Table<?>>> tablesAsync()
      Returns a list of cached known tables in a Mono

      If the cache has not been invalidated, returns a Mono which returns the previous cached result

      If the cache has been invalidated with invalidateTableCache(), refreshes the cache

      Returns:
      a Mono which will provive a list of Table
    • tables

      public List<Table<?>> tables()
      Returns a list of cached known tables, blocking until available

      If the cache has not been invalidated, returns the previous cached result

      If the cache has been invalidated with invalidateTableCache(), refreshes the cache

      Returns:
      a list of Table
    • findTable

      public Optional<Table<?>> findTable(String tableName)
      Attempts to find the named table
      Parameters:
      tableName - the table name
      Returns:
      an Optional which contains the matching Table, if found
    • findTableRequired

      public Table<?> findTableRequired(String tableName) throws TableDoesNotExistException
      Attempts to find the named table
      Parameters:
      tableName - the table name
      Returns:
      the matching Table
      Throws:
      TableDoesNotExistException - if the table can not be found or the cache is stale
    • longTextDataType

      public abstract DataType<String> longTextDataType()
      Returns the DataType representing the LONGTEXT equivalent SQL data type for the driver
      Returns:
      the DataType
    • supportsBackup

      public boolean supportsBackup()
      Indicates if this driver supports making backups without an external tool
      Returns:
      true if supported
    • backupFileName

      public String backupFileName()
      Returns the default backup filename, which is usually the database name with timestamp() appended
      Returns:
      the filename
    • backup

      public void backup()
      Performs a backup of the database to the dbbackup folder

      The default backup filename is used from backupFileName()

    • backup

      public void backup(String fileName)
      Performs a backup of the database to the dbbackup folder
      Parameters:
      fileName - the name of the backup file
    • restoreBackup

      public void restoreBackup(String fileName) throws FileNotFoundException
      Restores a database from the specified backup file, which must be in the dbbackup folder
      Parameters:
      fileName - the name of the backup file
      Throws:
      FileNotFoundException - if the backup file can not be found
    • doMaintenance

      public void doMaintenance()
      Performs periodic database maintenance
    • dispose

      public void dispose()
      Disposes of resources as necessary

      Once this is called, the connection pool is invalid and the program must be restarted to futher access the database

    • driverDispose

      protected void driverDispose()
      Allows the driver to perform additional disposal actions beyond what the base Datastore2 class performs

      This method is called before the connection pool is disposed