Package com.tccc.kos.commons.db
Class DatabaseFactory
java.lang.Object
com.tccc.kos.commons.db.DatabaseFactory
System component used to create a DataSource or Jdbi instance for a SQLite database.
This will handle creation of the database file and migration of the schema using the
supplied
DatabaseMigrator
. The result can be either a DataSoruce or Jdbi
instance to the resulting database.- Since:
- 1.0
- Version:
- 2022-10-21
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetDataSource
(DatabaseMigrator migrator, String name, File dir) Creates aDataSource
for the specified database file in the specified directory.Return the default directory.getJdbi
(DatabaseMigrator migrator, String name, File dir) Wrapper forgetDataSource
that returnsJdbi
instance instead.migrate
(DatabaseMigrator migrator, String name, File dir) This is a convenience method that simply callsgetDataSource()
and then returns the migrator.void
setDefaultDir
(File dir) Set the default directory
-
Constructor Details
-
DatabaseFactory
public DatabaseFactory()
-
-
Method Details
-
setDefaultDir
Set the default directory -
getDefaultDir
Return the default directory. -
migrate
This is a convenience method that simply callsgetDataSource()
and then returns the migrator. When a database only has a single DAO, then this is typically used by passing in a new instance of the DAO, migrating it, and then returning the DAO instance so it can be added to the context, all in one line of code.- Parameters:
migrator
- used to migrate the databasename
- the database's namedir
- the directory the database is located- Returns:
- the migrator that was passed in
-
getDataSource
Creates aDataSource
for the specified database file in the specified directory. The supplied migrator is used to migrate the database to the latest schema before the data source is returned. If migration needs to occur, it is performed with a temporary data source until everything is ready, and thensetDataSource()
is called on the migrator to indicate that things are stable and ready for use.When this method is called, the database is in one of the following states:
- It exists and its schema version is correct/current (most of the time)
- It exists but its schema version is older (needs updating)
- It exists but its schema version is newer (needs rolling-back)
- It does not exist
- Parameters:
migrator
- used to migrate the databasename
- the database's namedir
- the directory the database is located- Returns:
- the resulting datasource
-
getJdbi
Wrapper forgetDataSource
that returnsJdbi
instance instead. This will be the sameJdbi
instance used to callsetJdbi
on the migrator when migration is complete.- Parameters:
migrator
- used to migrate the databasename
- the database's namedir
- the directory the database is located- Returns:
- the resulting jdbi instance
-