Class Datastore2
- Direct Known Subclasses:
H2Store2
,MariaDBStore2
,MySQLStore2
,SQLiteStore2
- Author:
- gmt2001
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
backup()
Performs a backup of the database to thedbbackup
foldervoid
Performs a backup of the database to thedbbackup
folderReturns the default backup filename, which is usually the database name withtimestamp()
appendedprotected void
checkVersion
(SQLDialect dialect, Connection connection, String className) Checks if JOOQ supports the software version of the connected databasevoid
dispose()
Disposes of resources as necessaryvoid
Performs periodic database maintenanceprotected void
Allows the driver to perform additional disposal actions beyond what the base Datastore2 class performsReturns theDSLContext
which can be used to start a fluent statementAttempts to find the named tableTable<?>
findTableRequired
(String tableName) Attempts to find the named tableRetrieves aConnection
from the connection poolstatic void
init()
Initializes a new instance of Datastore2, based on the propertydatastore
in botlogin.txtprotected void
init
(ConnectionPoolDataSource dataSource, int maxConnections, int timeout, SQLDialect sqlDialect) Instance Initializerprotected void
init
(ConnectionPoolDataSource dataSource, SQLDialect sqlDialect) Instance Initializer.static Datastore2
instance()
Provides an instance ofDatastore2
void
Invalidates the cache of known tablesReturns theDataType
representing theLONGTEXT
equivalent SQL data type for the drivermeta()
Allows the driver to perform operations to prepare a meta object for use, such as selecting the schemaprotected void
prepareConnection
(Connection connection) Allows the driver to perform operations to prepare the connection for use, such as selecting the schemavoid
restoreBackup
(String fileName) Restores a database from the specified backup file, which must be in thedbbackup
folderboolean
Indicates if this driver supports making backups without an external tooltables()
Returns a list of cached known tables, blocking until availableReturns a list of cached known tables in aMono
boolean
Tests if the database is accessiblestatic String
Returns a timestamp suitable for database backup names in the formatyyyy-MM-dd.hh-mm-ss
-
Field Details
-
PREFIX
Table name prefix for all tables created as POJOs- See Also:
-
-
Constructor Details
-
Datastore2
public Datastore2()
-
-
Method Details
-
instance
Provides an instance ofDatastore2
If the instance has not been initialized, calls
init()
- Returns:
- an instance of
Datastore2
-
timestamp
Returns a timestamp suitable for database backup names in the formatyyyy-MM-dd.hh-mm-ss
- Returns:
- the timestamp
-
init
public static void init()Initializes a new instance of Datastore2, based on the propertydatastore
in botlogin.txtIf 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:
- Must extend
Datastore2
- Must call
init(ConnectionPoolDataSource, SQLDialect)
orinit(ConnectionPoolDataSource, int, int, SQLDialect)
, passing in a validConnectionPoolDataSource
, in the constructor - May optionally start a timer in the constructor to periodically call
doMaintenance()
- Must be in a JAR file located in the
./datastores
folder - The name of the JAR file must match the output of
Class.getSimpleName()
(Classname) of the type, including case - The
.jar
file extension on the JAR file must be lower-case - The value of the
datastore
property in botlogin.txt must match the output ofClass.getName()
(Fully qualified classname) of the type, including case
- Must extend
-
init
Instance Initializer. Sets a max connections of 30 and a timeout of 20Valid dialects:
SQLDialect.DERBY
SQLDialect.FIREBIRD
SQLDialect.H2
SQLDialect.HSQLDB
SQLDialect.MARIADB
SQLDialect.MYSQL
SQLDialect.POSTGRES
SQLDialect.SQLITE
SQLDialect.YUGABYTEDB
NOTE: SQL will be generated according to the latest supported version of the selected database dialect
- Parameters:
dataSource
- aConnectionPoolDataSource
which can be used withMiniConnectionPoolManager
sqlDialect
- the dialect to use with objects created from theDSLContext
-
init
protected void init(ConnectionPoolDataSource dataSource, int maxConnections, int timeout, SQLDialect sqlDialect) Instance InitializerValid dialects:
SQLDialect.DERBY
SQLDialect.FIREBIRD
SQLDialect.H2
SQLDialect.HSQLDB
SQLDialect.MARIADB
SQLDialect.MYSQL
SQLDialect.POSTGRES
SQLDialect.SQLITE
SQLDialect.YUGABYTEDB
NOTE: SQL will be generated according to the latest supported version of the selected database dialect
- Parameters:
dataSource
- aConnectionPoolDataSource
which can be used withMiniConnectionPoolManager
maxConnections
- the maximum number ofConnection
objects to hold in the pooltimeout
- the number of seconds until a call toMiniConnectionPoolManager.getConnection()
fails waiting for aConnection
to become availablesqlDialect
- the dialect to use with objects created from theDSLContext
-
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
- theSQLDialect
to check againseconnection
- a validConnection
to the databaseclassName
- 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 JOOQSQLException
- if a database access error occurs
-
prepareConnection
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
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 aConnection
from the connection poolIf 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 poolConsider 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 occursMiniConnectionPoolManager.TimeoutException
- when no connection becomes available within the timeout
-
testConnection
Tests if the database is accessibleHow 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
Returns theDSLContext
which can be used to start a fluent statement- Returns:
- the
DSLContext
- See Also:
-
invalidateTableCache
public void invalidateTableCache()Invalidates the cache of known tablesThis should be called every time a table is created, renamed, or deleted
-
tablesAsync
Returns a list of cached known tables in aMono
If the cache has not been invalidated, returns a
Mono
which returns the previous cached resultIf the cache has been invalidated with
invalidateTableCache()
, refreshes the cache -
tables
Returns a list of cached known tables, blocking until availableIf 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
Attempts to find the named table -
findTableRequired
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
Returns theDataType
representing theLONGTEXT
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
Returns the default backup filename, which is usually the database name withtimestamp()
appended- Returns:
- the filename
-
backup
public void backup()Performs a backup of the database to thedbbackup
folderThe default backup filename is used from
backupFileName()
-
backup
Performs a backup of the database to thedbbackup
folder- Parameters:
fileName
- the name of the backup file
-
restoreBackup
Restores a database from the specified backup file, which must be in thedbbackup
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 necessaryOnce 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 performsThis method is called before the connection pool is disposed
-